langchain.agents.xml.base
.create_xml_agent¶
- langchain.agents.xml.base.create_xml_agent(llm: ~langchain_core.language_models.base.BaseLanguageModel, tools: ~typing.Sequence[~langchain_core.tools.BaseTool], prompt: ~langchain_core.prompts.base.BasePromptTemplate, tools_renderer: ~typing.Callable[[~typing.List[~langchain_core.tools.BaseTool]], str] = <function render_text_description>, *, stop_sequence: ~typing.Union[bool, ~typing.List[str]] = True) Runnable [source]¶
创建一个使用XML格式化其逻辑的代理。
- 参数:
llm:LLM,用作代理。 tools:此代理可以访问的工具。 prompt:要使用的提示,必须具有输入键
tools:包含每个工具的描述。 agent_scratchpad:包含先前代理操作和工具输出。
tools_renderer:控制工具如何转换为字符串,然后传递给LLM。默认为`render_text_description`。 stop_sequence:布尔值或字符串列表。
如果为True,则添加一个停止标记”</tool_input>”以避免产生幻觉。 如果为False,则不添加停止标记。 如果为字符串列表,则使用提供的列表作为停止标记。
默认为True。如果您使用的LLM不支持停止序列,则可以将其设置为False。
- 返回:
代表代理的可运行序列。它接受与传递的提示相同的所有输入变量。它返回一个AgentAction或AgentFinish。
示例:
from langchain import hub from langchain_community.chat_models import ChatAnthropic from langchain.agents import AgentExecutor, create_xml_agent prompt = hub.pull("hwchase17/xml-agent-convo") model = ChatAnthropic(model="claude-3-haiku-20240307") tools = ... agent = create_xml_agent(model, tools, prompt) agent_executor = AgentExecutor(agent=agent, tools=tools) agent_executor.invoke({"input": "hi"}) # 与聊天历史一起使用 from langchain_core.messages import AIMessage, HumanMessage agent_executor.invoke( { "input": "what's my name?", # 请注意,chat_history是一个字符串 # 因为此提示针对LLMs,而不是聊天模型 "chat_history": "Human: My name is Bob
- AI: Hello Bob!”,
}
)
提示:
- 提示必须具有以下输入键:
tools:包含每个工具的描述。
agent_scratchpad:包含先前代理操作和工具输出的XML字符串。
这里有一个示例:
from langchain_core.prompts import PromptTemplate template = '''You are a helpful assistant. Help the user answer any questions. You have access to the following tools: {tools} In order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags. You will then get back a response in the form <observation></observation> For example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond: <tool>search</tool><tool_input>weather in SF</tool_input> <observation>64 degrees</observation> When you are done, respond with a final answer between <final_answer></final_answer>. For example: <final_answer>The weather in SF is 64 degrees</final_answer> Begin! Previous Conversation: {chat_history} Question: {input} {agent_scratchpad}''' prompt = PromptTemplate.from_template(template)
- Parameters
llm (BaseLanguageModel) –
tools (Sequence[BaseTool]) –
prompt (BasePromptTemplate) –
tools_renderer (Callable[[List[BaseTool]], str]) –
stop_sequence (Union[bool, List[str]]) –
- Return type