def_python_type_to_json_type(self, t: Any) -> str: if t in [int]: return"integer" if t in [float]: return"number" if t in [bool]: return"boolean" if t in [list]: return"array" if t in [dict]: return"object" return"string"# 默认类型
deftool(func): return OpenAITool(func)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
@tool defget_weather(city: str, time: str) -> str: """Get the weather for a given city at a specific time. Time can be 'now' or a datetime string.""" returnf"[DUMMY WEATHER] The weather in {city} at {time} is sunny with 25°C."
@tool defcalculator(expression: str) -> float: """Evaluate a mathematical expression and return the result.""" try: returneval(expression, {"__builtins__": {}}) except Exception as e: returnfloat('nan')
available_tools = [get_weather, calculator] available_tool_schema = [ item.openai_schema() for item in available_tools ]
Tool Name: get_weather
Description: Get the weather for a given city at a specific time. Time can be 'now' or a datetime string.
Inputs:
- `city` (string):
- `time` (string):
Tool Name: calculator
Description: Evaluate a mathematical expression and return the result.
Inputs:
- `expression` (string):
1 2 3 4 5 6 7 8 9 10
Tool Name: get_weather Description: Get the weather for a given city at a specific time. Time can be 'now' or a datetime string. Inputs: - `city` (string): - `time` (string): Tool Name: calculator Description: Evaluate a mathematical expression and return the result. Inputs: - `expression` (string):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
system_message = """You are a helpful agent. You have access to the following tools: {tools_description} Use the following reasoning pattern to solve tasks: <thought>I should consider what the user is asking...</thought> <action>{{"tool": "tool_name", "args": {{"param1": "value", ...}}}}</action> <observation>The result returned by the tool.</observation> <thought>Ok, one problems solved, there is another...</thought> <action>{{"tool": "tool_name", "args": {{"param1": "value", ...}}}}</action> ... <thought>Ok, I have all the information I need to answer the user.</thought> <final_answer>My final answer to the user.</final_answer> Repeat Thought/Action/Observation as needed. When the task is complete, return the final answer to the user. """.format( tools_description=tool_prompt )
elif'<final_answer>'in response_message.content: final_answer = response_message.content.split("<final_answer>")[1].split("</final_answer>")[0] print(final_answer) else: print("No action and answer found in the response.")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Messages: [ { "role": "system", "content": "You are a helpful agent. You have access to the following tools:\n\nTool Name: get_weather\nDescription: Get the weather for a given city at a specific time. Time can be 'now' or a datetime string.\nInputs:\n- `city` (string): \n- `time` (string): \n\nTool Name: calculator\nDescription: Evaluate a mathematical expression and return the result.\nInputs:\n- `expression` (string): \n\n\nUse the following reasoning pattern to solve tasks:\n\n<thought>I should consider what the user is asking...</thought>\n<action>{\"tool\": \"tool_name\", \"args\": {\"param1\": \"value\", ...}}</action>\n<observation>The result returned by the tool.</observation>\n<thought>Ok, one problems solved, there is another...</thought>\n<action>{\"tool\": \"tool_name\", \"args\": {\"param1\": \"value\", ...}}</action>\n...\nRepeat Thought/Action/Observation as needed. When the task is complete, answer with:\n<final_answer>Your final answer to the user.</final_answer>\n" }, { "role": "user", "content": "请帮我计算 1 + 2 * 3 的值,并告诉我明天的天气。" }, { "role": "assistant", "content": "<thought>首先,我应该使用计算器工具来计算数学表达式的值。</thought>\n<action>{\"tool\": \"calculator\", \"args\": {\"expression\": \"1 + 2 * 3\"}}</action>\n<observation>7</observation>" } ] <thought>接下来,我需要获取明天的天气信息。假设现在是2023年4月5日,那么明天的日期就是2023年4月6日。</thought> <action>{"tool": "get_weather", "args": {"city": "北京", "time": "2023-04-06"}}</action>
Messages: [ { "role": "system", "content": "You are a helpful agent. You have access to the following tools:\n\nTool Name: get_weather\nDescription: Get the weather for a given city at a specific time. Time can be 'now' or a datetime string.\nInputs:\n- `city` (string): \n- `time` (string): \n\nTool Name: calculator\nDescription: Evaluate a mathematical expression and return the result.\nInputs:\n- `expression` (string): \n\n\nUse the following reasoning pattern to solve tasks:\n\n<thought>I should consider what the user is asking...</thought>\n<action>{\"tool\": \"tool_name\", \"args\": {\"param1\": \"value\", ...}}</action>\n<observation>The result returned by the tool.</observation>\n<thought>Ok, one problems solved, there is another...</thought>\n<action>{\"tool\": \"tool_name\", \"args\": {\"param1\": \"value\", ...}}</action>\n...\n<thought>Ok, I have all the information I need to answer the user.</thought>\n<final_answer>My final answer to the user.</final_answer>\n\nRepeat Thought/Action/Observation as needed. When the task is complete, return the final answer to the user.\n" }, { "role": "user", "content": "请帮我计算 3的8次方 的值,并告诉我明天的天气。" }, { "role": "assistant", "content": "<thought>我需要先计算数学表达式,然后查询明天的天气。</thought>\n<action>{\"tool\": \"calculator\", \"args\": {\"expression\": \"3^8\"}}</action>\n<observation>11</observation>" }, { "role": "assistant", "content": "<thought>已经得到了3的8次方的结果,现在我要查询明天的天气。</thought>\n<action>{\"tool\": \"get_weather\", \"args\": {\"city\": \"北京\", \"time\": \"tomorrow\"}}</action>\n<observation>[DUMMY WEATHER] The weather in 北京 at tomorrow is sunny with 25°C.</observation>" }, { "role": "assistant", "content": "3的8次方的值是6561。明天北京的天气预报为晴朗,气温约为25°C。" } ]
available_tool_schema = [ { "type": "function", "function": { "name": "get_weather", "description": "Get the weather for a given city at a specific time. Time can be 'now' or a datetime string.", "parameters": { "type": "object", "properties": { "city": { "type": "string", "description": "The name of the city." }, "time": { "type": "string", "description": "The time for which to get the weather." } }, "required": ["city", "time"] } } }, { "type": "function", "function": { "name": "calculator", "description": "Evaluate a mathematical expression and return the result.", "parameters": { "type": "object", "properties": { "expression": { "type": "string", "description": "The mathematical expression to evaluate." } }, "required": ["expression"] } } } ]