03 react custom
Custom ReAct (Reason+Act)
- AgentExecutorで実行開始、終了条件に達するまでLoopを回し、Loop内でAgent.planを呼ぶ。
- Agent.planではLLMにどのActionを取るべきか聞き帰ってきたOutputをParseして、AgentFinishの場合は終わり、AgentActionの場合は、InputとともにToolを実行し、その結果を返す。
Action
(Toolの中から何を使うべきかの司令),Thought
(LLMがどのActionを取るべきか返してきたもの),Observation
(Toolの結果),Finish
(最終的な答え) は、PromptやExample内で使われるだけでなく、OutputParserやAgent内のfinish_tool_name
,observation_prefix
,llm_prefix
で定義される (日本語にすべて変えることも可能 #19)
1. Run
- 緑: LLMからのアウトプット
- 黄色、ピンク: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:
name (str)
, is required and must be unique within a set of tools provided to an agentdescription (str)
, is optional but recommended, as it is used by an agent to determine tool usereturn_direct (bool)
, defaults to Falseargs_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:
-
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")
````
作り方2: 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)
- Using decorator
Others
-
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
- 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.
- 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)
- 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
- conversational-react-description:
2.2.2. Agent Classes
- BaseSingleActionAgent: tbd
- LLMSingleActionAgent: BaseSingleActionAgentの拡張
- Agent: BaseSingleActionAgentの拡張
- 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
- CSV Agent: Pandasを使ってinteractiveにCSVとやり取りできる (ref: https://python.langchain.com/docs/templates/csv-agent)
- Vectorstore agent