GPT-4o 不再支持function_call 更改为tools
AI 2024/6/14 21:22:12 点击:不统计
原文是网站制作学习网的FoAsP.cn
在使用autogen多agent 对话时,开始用GPT-4turbo,后来更改为价格更便宜的GPT-4o 因为 不再支持 function_call,更改为tools .
于是适配了一下
1. 先看官方的 tools 的使用文档
2. 看下 适配输入 function_call
3. 适配输出处理
from openai import OpenAI
GPT_MODEL = "gpt-4o"
client = OpenAI()
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use. Infer this from the users location.",
},
},
"required": ["location", "format"],
},
}
},
# 这里可以有多个函数
]
messages = []
messages.append({"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."})
messages.append({"role": "user", "content": "What's the weather like today"})
chat_response = client.chat.completions.create(
model=GPT_MODEL,
messages=messages,
tools=tools, # 告诉ai 这里告诉ai 有哪些工具可以使用
tool_choice="auto",
)
"""参数:tool_choice告诉ai怎么选择工具
auto 默认 ai根据内容选择;
required 必须选择工具;
{"type": "function", "function": {"name": "my_function"}} 指定工具
None 不选择工具 ,只生成消息
"""
print(chat_response)
"""
返回案例:
ChatCompletion(id='chatcmpl-9Zs76yr0XBJr0I6mev0bSqwC5nfRT', choices=[Choice(finish_reason='tool_calls', index=0, logprobs=None, message=ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_PM9Ecy8IqPrPVAgNgzASHkgG', function=Function(arguments='{\n "location": "Beijing",\n "format": "celsius"\n}', name='get_current_weather'), type='function')]))], created=1718336520, model='gpt-4o-2024-05-13', object='chat.completion', system_fingerprint='fp_319be4768e', usage=CompletionUsage(completion_tokens=25, prompt_tokens=182, total_tokens=207))
vim"""
assistant_message = chat_response.choices[0].message
print(assistant_message)
"""
chat_response.choices[0].message
返回案例:
ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_S89LX5upq3N1vJNlDLTd0vUz', function=Function(arguments='{"location":"Beijing","format":"celsius"}', name='get_current_weather'), type='function')])
"""
# 循环获取 tool_call ,如果是 消息内容,则没有 tool_calls 属性
for tool_call in assistant_message.tool_calls:
print("tool_call ID:", tool_call.id)
print("tool_call Type:", tool_call.type)
print("tool_call Name:", tool_call.function.name)
print("tool_call Arguments:", tool_call.function.arguments)
"""
输出 tool_call :
tool_call ID: call_M3hKQhePvKbH2EmzB9rQxm0P
tool_call Type: function
tool_call Name: get_current_weather
tool_call Arguments: {
"location": "Beijing",
"format": "celsius"
}
"""
# 当调用 function call 后
function_response = "这里是结果"
function_name = tool_call.function.name
# 将function call 的结果添加到 messages 中 ,然后可以 再次将 消息推送给ai 进行回复
messages.append(
{
"tool_call_id": tool_call.id, # 这里告知 ai 是那个 方法返回的内容
"role": "tool",
"name": tool_call.function.name,
"content": function_response,
}
)
"""参考 纯消息ai 内容返回 completion.choices[0].message.content
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "The 2020 World Series was played in Texas at Globe Life Field in Arlington.",
"role": "assistant"
},
"logprobs": null
}
],
"created": 1677664795,
"id": "chatcmpl-7QyqpwdfhqwajicIEznoc6Q47XAyW",
"model": "gpt-3.5-turbo-0613",
"object": "chat.completion",
"usage": {
"completion_tokens": 17,
"prompt_tokens": 57,
"total_tokens": 74
}
}
"""
2. 看下 适配输入 function_call
tools = []
if "functions" in config:
functions = config.pop("functions")
for function in functions:
tools.append({
"type": "function",
"function": function
})
if len(tools) > 0:
config['tools'] = tools
3. 适配输出处理
response = openai_completion.create(request_timeout=request_timeout, **config)
if "tool_calls" in response.choices[0].message and len(response.choices[0].message.tool_calls) > 0:
tool_calls = response.choices[0].message.pop("tool_calls")
function_call = tool_calls[0]
response.choices[0].message['function_call'] = {
"name": function_call.function.name,
"arguments": function_call.function.arguments
}
response.choices[0]['finish_reason'] = "function_call"
return response原载于:文章来源:www.forasp.cn网站制作学习