create_json_chat_agent#
- langchain.agents.json_chat.base.create_json_chat_agent(llm: ~langchain_core.language_models.base.BaseLanguageModel, tools: ~typing.Sequence[~langchain_core.tools.base.BaseTool], prompt: ~langchain_core.prompts.chat.ChatPromptTemplate, stop_sequence: bool | ~typing.List[str] = True, tools_renderer: ~typing.Callable[[list[~langchain_core.tools.base.BaseTool]], str] = <function render_text_description>, template_tool_response: str = "TOOL RESPONSE: \n---------------------\n{observation}\n\nUSER'S INPUT\n--------------------\n\nOkay, so what is the response to my last comment? If using information obtained from the tools you must mention it explicitly without mentioning the tool names - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else - even if you just want to respond to the user. Do NOT respond with anything except a JSON snippet no matter what!") Runnable [source]#
创建一个使用JSON来格式化其逻辑的代理,专为聊天模型构建。
- Parameters:
llm (BaseLanguageModel) – 用作代理的LLM。
tools (Sequence[BaseTool]) – 此代理可以访问的工具。
prompt (ChatPromptTemplate) – 要使用的提示。有关更多信息,请参见下面的提示部分。
stop_sequence (bool | List[str]) –
布尔值或字符串列表。 如果为True,则添加一个“Observation:”的停止标记以避免幻觉。 如果为False,则不添加停止标记。 如果是字符串列表,则使用提供的列表作为停止标记。
默认值为True。如果您使用的LLM不支持停止序列,您可能需要将其设置为False。
tools_renderer (Callable[[list[BaseTool]], str]) – 这控制了工具如何转换为字符串,然后传递给LLM。默认是render_text_description。
template_tool_response (str) – 使用工具响应(观察)的模板提示,以使LLM生成下一步要采取的行动。默认值为TEMPLATE_TOOL_RESPONSE。
- Returns:
一个表示代理的可运行序列。它接收与传入提示相同的所有输入变量作为输入。它返回一个AgentAction或AgentFinish作为输出。
- Raises:
ValueError – 如果提示缺少必需的变量。
ValueError – 如果 template_tool_response 缺少必需的变量 'observation'。
- Return type:
示例
from langchain import hub from langchain_community.chat_models import ChatOpenAI from langchain.agents import AgentExecutor, create_json_chat_agent prompt = hub.pull("hwchase17/react-chat-json") model = ChatOpenAI() tools = ... agent = create_json_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?"), ], } )
提示:
- The prompt must have input keys:
tools: 包含每个工具的描述和参数。
tool_names: 包含所有工具名称。
agent_scratchpad: 必须是一个 MessagesPlaceholder。包含之前的代理操作和工具输出作为消息。
这是一个例子:
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder system = '''Assistant is a large language model trained by OpenAI. Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand. Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics. Overall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.''' human = '''TOOLS ------ Assistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are: {tools} RESPONSE FORMAT INSTRUCTIONS ---------------------------- When responding to me, please output a response in one of two formats: **Option 1:** Use this if you want the human to use a tool. Markdown code snippet formatted in the following schema: ```json {{ "action": string, \ The action to take. Must be one of {tool_names} "action_input": string \ The input to the action }} ``` **Option #2:** Use this if you want to respond directly to the human. Markdown code snippet formatted in the following schema: ```json {{ "action": "Final Answer", "action_input": string \ You should put what you want to return to use here }} ``` USER'S INPUT -------------------- Here is the user's input (remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else): {input}''' prompt = ChatPromptTemplate.from_messages( [ ("system", system), MessagesPlaceholder("chat_history", optional=True), ("human", human), MessagesPlaceholder("agent_scratchpad"), ] )
使用 create_json_chat_agent 的示例