from langchain import hubfrom langchain.agents import AgentExecutor, toolfrom langchain.agents.output_parsers import XMLAgentOutputParserfrom langchain_openai import ChatOpenAIimport subprocessmodel = ChatOpenAI( model="gpt-3.5-turbo",)@tooldef search(query: str) -> str: """Search things about current events.""" return "32 degrees"@tool
def get_docker_info(docker_name: str) -> str:
"""Get information about a docker pod container info."""
result = subprocess.run(['docker', 'inspect', str(docker_name)], capture_output=True, text=True)
return result.stdout
tool_list = [search, get_docker_info]# Get the prompt to use - you can modify this!prompt = hub.pull("hwchase17/xml-agent-convo")# Logic for going from intermediate steps to a string to pass into model# This is pretty tied to the promptdef convert_intermediate_steps(intermediate_steps): log = "" for action, observation in intermediate_steps: log += ( f"<tool>{action.tool}</tool><tool_input>{action.tool_input}" f"</tool_input><observation>{observation}</observation>" ) return log# Logic for converting tools to string to go in promptdef convert_tools(tools): return "\n".join([f"{tool.name}: {tool.description}" for tool in tools])agent = ( { "input": lambda x: x["input"], "agent_scratchpad": lambda x: convert_intermediate_steps( x["intermediate_steps"] ), } | prompt.partial(tools=convert_tools(tool_list)) | model.bind(stop=["</tool_input>", "</final_answer>"]) | XMLAgentOutputParser())agent_executor = AgentExecutor(agent=agent, tools=tool_list)message1 = agent_executor.invoke({"input": "whats the weather in New york?"})print(f"message1: {message1}")message2 = agent_executor.invoke({"input": "what is docker pod which name 'lobe-chat-wzk' info? I want to know it 'Image' url"})print(f"message2: {message2}")
复制代码
执行代码
➜ python3 test10.py
message1: {'input': 'whats the weather in New york?', 'output': 'The weather in New York is 32 degrees'}
message2: {'input': "what is docker pod which name 'lobe-chat-wzk' info? I want to know it 'Image' url", 'output': 'The Image URL for the docker pod named \'lobe-chat-wzk\' is "lobehub/lobe-chat"'}