Skip to content

03 react custom

Custom ReAct (Reason+Act)

  1. AgentExecutorで実行開始、終了条件に達するまでLoopを回し、Loop内でAgent.planを呼ぶ。
  2. Agent.planではLLMにどのActionを取るべきか聞き帰ってきたOutputをParseして、AgentFinishの場合は終わり、AgentActionの場合は、InputとともにToolを実行し、その結果を返す。
  3. Action (Toolの中から何を使うべきかの司令), Thought (LLMがどのActionを取るべきか返してきたもの), Observation (Toolの結果), Finish (最終的な答え) は、PromptやExample内で使われるだけでなく、OutputParserやAgent内のfinish_tool_name, observation_prefix, llm_prefixで定義される (日本語にすべて変えることも可能 #19)

1. Run

poetry run python src/langchain/react_custom.py

  1. 緑: LLMからのアウトプット
  2. 黄色、ピンク:Toolの結果

2. Components

2.1. Tools

ツールリストもある: https://python.langchain.com/en/latest/modules/agents/tools/getting_started.html

Custom Tools:

Tool consists of several components:

  1. name (str), is required and must be unique within a set of tools provided to an agent
  2. description (str), is optional but recommended, as it is used by an agent to determine tool use
  3. return_direct (bool), defaults to False
  4. args_schema (Pydantic BaseModel), is optional but recommended, can be used to provide more information (e.g., few-shot examples) or validation for expected parameters.

作り方1: Tool:

  1. Tool dataclass

    Tool.from_function(
        func=search.run,
        name = "Search",
        description="useful for when you need to answer questions about current events"
        # coroutine= ... <- you can specify an async method if desired as well
    ),
    
  2. Subclassing the BaseTool class

    ```py class CustomSearchTool(BaseTool): name = "custom_search" description = "useful for when you need to answer questions about current events"

    def _run(self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None) -> str:
        """Use the tool."""
        return search.run(query)
    
    async def _arun(self, query: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None) -> str:
        """Use the tool asynchronously."""
        raise NotImplementedError("custom_search does not support async")
    

    ````

  3. Using @tool decorator

    from langchain.tools import tool
    
    @tool("search", return_direct=True)
    def search_api(query: str) -> str:
        """Searches the API for the query."""
        return "Results"
    
    search_api
    

作り方2: Structured Tool:

  1. Structured Tool

    def post_message(url: str, body: dict, parameters: Optional[dict] = None) -> str:
        """Sends a POST request to the given url with the given body and parameters."""
        result = requests.post(url, json=body, params=parameters)
        return f"Status: {result.status_code} - {result.text}"
    
    tool = StructuredTool.from_function(post_message)
    
  2. Subclassing BaseTool

  3. Using decorator

Others

  1. Priorities of tools

    DescriptionにUse this more than the normal search if the question is about Music, like 'who is the singer of yesterday?' or 'what is the most popular song in 2022?'のように書く。Defaultではnormal toolが優先される 1. return_direct=True: Toolの結果がそのまま返される (LLMなしで?)

2.2. Agents

2.2.1. Agent Types

  1. zero-shot-react-description: This agent uses the ReAct framework to determine which tool to use based solely on the tool’s description. Any number of tools can be provided. This agent requires that a description is provided for each tool.
  2. react-docstore: This agent uses the ReAct framework to interact with a docstore. Two tools must be provided: a Search tool and a Lookup tool (REACT: SYNERGIZING REASONING AND ACTING IN LANGUAGE MODELS)
  3. self-ask-with-search: utilizes a single tool that should be named Intermediate Answer. (MEASURING AND NARROWING THE COMPOSITIONALITY GAP IN LANGUAGE MODELS)
    • the term compositionality gap to describe the fraction of compositional questions that the model answers incorrectly out of all the compositional questions for which the model answers the sub-questions correctly
  4. conversational-react-description:

2.2.2. Agent Classes

  1. BaseSingleActionAgent: tbd
  2. LLMSingleActionAgent: BaseSingleActionAgentの拡張
  3. Agent: BaseSingleActionAgentの拡張
    1. use output parsers in agents#2987からOutputParserが導入された

Agentの以下の関数でfinish, observation, prefixの名前を指定する。

    @property
    def finish_tool_name(self) -> str:
        return "Finish"

    @property
    def observation_prefix(self) -> str:
        return "Observation: "

    @property
    def llm_prefix(self) -> str:
        return "Thought: "

2.3. Toolkikt

  1. CSV Agent: Pandasを使ってinteractiveにCSVとやり取りできる (ref: https://python.langchain.com/docs/templates/csv-agent)
  2. Vectorstore agent

2.4. Agent Executors

2.5. Plan and Execute

Ref

  1. Custom Agent
  2. ReAct with OpenAI GPT and LangChain
  3. Custom Agent (Blog from LangChain)
  4. 推論し、行動するChatGPT(OpenAI API) Agentを作る – langchain ReAct Custom Agent基礎 –
  5. Change ReAct words to Japanese
  6. Custom ReAct解剖