langchain.agents.structured_chat.base.create_structured_chat_agent

langchain.agents.structured_chat.base.create_structured_chat_agent(llm: ~langchain_core.language_models.base.BaseLanguageModel, tools: ~typing.Sequence[~langchain_core.tools.BaseTool], prompt: ~langchain_core.prompts.chat.ChatPromptTemplate, tools_renderer: ~typing.Callable[[~typing.List[~langchain_core.tools.BaseTool]], str] = <function render_text_description_and_args>, *, stop_sequence: ~typing.Union[bool, ~typing.List[str]] = True) Runnable[source]

创建一个旨在支持多输入工具的智能体。

参数
  • llm (BaseLanguageModel) – 作为智能体使用的LLM。

  • tools (Sequence[BaseTool]) – 该智能体可访问的工具。

  • prompt (ChatPromptTemplate) – 要使用的提示。有关更多信息,请参阅下面的提示部分。

  • stop_sequence (Union[bool, List[str]]) –

    bool或字符串列表。如果为True,则添加“观察:”作为停止标记以避免幻觉。如果为False,则不添加停止标记。如果为字符串列表,则使用提供的列表作为停止标记。

    默认为True。如果您使用的LLM不支持停止序列,则可以将其设置为False。

  • tools_renderer (Callable[[List[BaseTool]], str]) – 这控制工具如何转换为字符串然后输入LLM。默认为render_text_description

返回

表示智能体的可运行序列。它接受与传递的提示相同的所有输入变量。作为输出,它返回AgentAction或AgentFinish。

返回类型

Runnable

示例

from langchain import hub
from langchain_community.chat_models import ChatOpenAI
from langchain.agents import AgentExecutor, create_structured_chat_agent

prompt = hub.pull("hwchase17/structured-chat-agent")
model = ChatOpenAI()
tools = ...

agent = create_structured_chat_agent(model, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)

agent_executor.invoke({"input": "hi"})

# Using with chat history
from langchain_core.messages import AIMessage, HumanMessage
agent_executor.invoke(
    {
        "input": "what's my name?",
        "chat_history": [
            HumanMessage(content="hi! my name is bob"),
            AIMessage(content="Hello Bob! How can I assist you today?"),
        ],
    }
)

提示

提示必须具有输入键
  • tools:包含每个工具的描述和参数。

  • tool_names:包含所有工具名称。

  • agent_scratchpad:包含以前的智能体操作和工具输出作为一个字符串。

以下是一个例子

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

system = '''Respond to the human as helpfully and accurately as possible. You have access to the following tools:

{tools}

Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).

Valid "action" values: "Final Answer" or {tool_names}

Provide only ONE action per $JSON_BLOB, as shown:

```
{{
  "action": $TOOL_NAME,
  "action_input": $INPUT
}}
```

Follow this format:

Question: input question to answer
Thought: consider previous and subsequent steps
Action:
```
$JSON_BLOB
```
Observation: action result
... (repeat Thought/Action/Observation N times)
Thought: I know what to respond
Action:
```
{{
  "action": "Final Answer",
  "action_input": "Final response to human"
}}

Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation'''

human = '''{input}

{agent_scratchpad}

(reminder to respond in a JSON blob no matter what)'''

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", system),
        MessagesPlaceholder("chat_history", optional=True),
        ("human", human),
    ]
)

使用create_structured_chat_agent的示例