agent-toolkit

tool

https://python.langchain.com/v0.2/docs/integrations/tools/

toolkit

https://python.langchain.com/v0.2/docs/integrations/toolkits/

agent executor๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ agent๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š”๋ฐ ๋‹ค์Œ ๋ฌธ์„œ๋ฅผ ๋ณด์ž.

https://python.langchain.com/v0.2/docs/how_to/agent_executor/#jupyter-notebook

์ด ์„น์…˜์—์„œ๋Š” ๋ ˆ๊ฑฐ์‹œ LangChain AgentExecutor๋ฅผ ์‚ฌ์šฉํ•œ ๊ตฌ์ถ•์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ์‹œ์ž‘ํ•˜๋Š” ๋ฐ๋Š” ๊ดœ์ฐฎ์ง€๋งŒ ํŠน์ • ์ง€์ ์ด ์ง€๋‚˜๋ฉด ์ด ๊ธฐ๋Šฅ์ด ์ œ๊ณตํ•˜์ง€ ์•Š๋Š” ์œ ์—ฐ์„ฑ๊ณผ ์ œ์–ด ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ณ ๊ธ‰ ์—์ด์ „ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด LangGraph ์—์ด์ „ํŠธ ๋˜๋Š” ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ๊ฐ€์ด๋“œ๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

๋ ˆ๊ฑฐ์‹œ๋กœ ์ฒ˜๋ฆฌ๋˜๊ณ  ์ž‡๋‹ค. langgraph agent๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ๋งž๋Š”๊ฑฐ๊ฐ™์€๋ฐ ๊ทธ๋ž˜์„œ ๊ทธ๋Ÿฐ์ง€ ์ด๊ฑด ๋Œ€์ถฉ ๋ด๋‘๊ณ  ๋ฐ”๋กœ langgraph๋กœ ๊ฐ€์ž.

Python REPL

%pip install langchain_experimental
from langchain_core.tools import Tool
from langchain_experimental.utilities import PythonREPL
python_repl = PythonREPL()
python_repl.run("print(1+1)")
# You can create the tool to pass to an agent
repl_tool = Tool(
    name="python_repl",
    description="ํŒŒ์ด์ฌ ์…ธ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ๋ช…๋ น์„ ์‹คํ–‰ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ์€ ์œ ํšจํ•œ ํŒŒ์ด์ฌ ๋ช…๋ น์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ’์˜ ์ถœ๋ ฅ์„ ํ™•์ธํ•˜๋ ค๋ฉด `print(...)`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.",
    func=python_repl.run,
)

repl_tool.invoke("print(5+5)")

์ฃผ์‹ ํ‹ฐ์ปค ์ฐพ๊ธฐ agent

from dotenv import load_dotenv
load_dotenv()
from typing import Any, Type
from langchain_openai import ChatOpenAI
from langchain.tools import BaseTool
from pydantic import BaseModel, Field
from langchain.agents import initialize_agent, AgentType

llm = ChatOpenAI(temperature=0.1)

from langchain.utilities import DuckDuckGoSearchAPIWrapper

# schema
class StockMarketSymbolSearchToolArgsSchema(BaseModel):
    query: str = Field(description="๊ฒ€์ƒ‰ํ•  ์ฟผ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ฟผ๋ฆฌ ์˜ˆ์‹œ: Apple ํšŒ์‚ฌ์˜ ์ฃผ์‹ ์‹œ์žฅ ์‹ฌ๋ณผ")

from langchain.utilities import DuckDuckGoSearchAPIWrapper
class StockMarketSymbolSearchTool(BaseTool):
    name = "StockMarketSymbolSearchTool"
    description = """
    ์ด ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํšŒ์‚ฌ์˜ ์ฃผ์‹ ์‹œ์žฅ ์‹ฌ๋ณผ์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    ์ฟผ๋ฆฌ๋ฅผ ์ธ์ˆ˜๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.
    """
    args_schema: Type[
        StockMarketSymbolSearchToolArgsSchema
    ] = StockMarketSymbolSearchToolArgsSchema

    def _run(self, query):
        ddg = DuckDuckGoSearchAPIWrapper()
        return ddg.run(query)
agent = initialize_agent(
    llm=llm,
    verbose=True,
    agent=AgentType.OPENAI_FUNCTIONS,
    handle_parsing_errors=True,
    tools=[
        StockMarketSymbolSearchTool(),
    ],
)

prompt = "Cloudflare์˜ ํ‹ฐ์ปค๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ๋ ค์ฃผ์„ธ์š”. ํ•œ๊ตญ์–ด๋กœ ๋‹ต๋ณ€ํ•ด์ค˜"

agent.invoke(prompt)

sql agent

https://github.com/lerocha/chinook-database/blob/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite

chinook_sqlite๋ฅผ ์‚ฌ์šฉํ•จ

!wget 'https://github.com/lerocha/chinook-database/releases/download/v1.4.2/Chinook_Sqlite.sql'

from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
    temperature=0.1,
)
from langchain_community.utilities.sql_database import SQLDatabase

db = SQLDatabase.from_uri("sqlite:///db/Chinook.sqlite")
print(db.dialect) #sqlite

print(db.get_usable_table_names())
result = db.run("select * from artist limit 5;")
# db.run("select * from artists limit 5;")
print(type(result))
result
from langchain_community.agent_toolkits import SQLDatabaseToolkit
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
from langchain.agents import create_sql_agent, AgentType

agent_executor = create_sql_agent(
    llm=llm,
    toolkit=toolkit,
    agent_type="openai-tools",
    verbose=False,
)

agent_executor.invoke(
    "employees๋Š” ๋ช‡๋ช…์ด์•ผ?"
)
agent_executor.invoke(
    "artist ๋ช‡๋ช…์ด์•ผ?"
)
agent_executor.invoke(
    "Alanis Morissette ๋…ธ๋ž˜๋งŒ ๋ฝ‘์•„์ค˜?"
)
agent_executor.invoke({"input": "Describe the schema of the playlist table"})
agent_executor.invoke(
    {
        "input": "๊ตญ๊ฐ€๋ณ„ ์ด ๋งค์ถœ์„ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๋งŽ์ด ์ง€์ถœํ•œ ๊ตญ๊ฐ€?"
    }
)

langgraph

์š”์ฆ˜์€ langchain agent๋ณด๋‹ค langgraph๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€๊ฑฐ๊ฐ™์€ ๊ฐœ์ธ์  ๋Š๋‚Œ์ด๋ผ. agent์ชฝ์€ ๋งŽ์ด ๋ณด์ง€๋Š” ์•Š๊ณ  langgraph๋กœ ๋ฐ”๋กœ ๋„˜์–ด๊ฐ€๋ฒ„๋ฆด๊นŒ ํ•ฉ๋‹ˆ๋‹ค.

Agent

์ธ๊ณต์ง€๋Šฅ Agent๋Š” ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ๋ฐ›์€ ํ›„ ์–ด๋–ค ๊ธฐ๋Šฅ์„ ์–ด๋–ค ์ˆœ์„œ๋กœ ์‹คํ–‰ํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. Agent์˜ ์ฃผ์š” ํŠน์ง•๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ๋ชฉํ‘œ(์ •์ฑ…) ๊ธฐ๋ฐ˜ ํ–‰๋™: ์ฃผ์–ด์ง„ ๋ชฉํ‘œ๋ฅผ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ํ–‰๋™ํ•ฉ๋‹ˆ๋‹ค.

  • ์ž์œจ์„ฑ: ๋ชฉํ‘œ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ž๋™์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

  • ๊ฐ์ง€: ์ฃผ๋ณ€ ํ™˜๊ฒฝ์—์„œ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•ฉ๋‹ˆ๋‹ค.

Tools๋Š” Agent๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ

alt text
  1. Input: ์‚ฌ์šฉ์ž๊ฐ€ Agent์—๊ฒŒ ์ž‘์—…์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.

  2. Thought: Agent๊ฐ€ ์ž‘์—…์„ ์™„์ˆ˜ํ•˜๊ธฐ ์œ„ํ•ด ๋ฌด์—‡์„ ํ• ์ง€ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

  3. Action/Action Input: ์‚ฌ์šฉํ•  ๋„๊ตฌ๋ฅผ ๊ฒฐ์ •ํ•˜๊ณ , ๋„๊ตฌ์˜ ์ž…๋ ฅ(ํ•จ์ˆ˜์˜ ์ž…๋ ฅ๊ฐ’)์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

  4. Observation: ๋„๊ตฌ์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๊ด€์ฐฐํ•ฉ๋‹ˆ๋‹ค.

  5. ๊ด€์ฐฐ ๊ฒฐ๊ณผ ์ž‘์—…์„ ์™„๋ฃŒ(Finish)ํ–ˆ๋‹ค๋Š” ํŒ๋‹จ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ 2~4๋ฒˆ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

Last updated