python
import json
import os
from openai import OpenAI
client = OpenAI(
api_key = os.getenv("OPENAI_API_KEY"),
)
# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
def get_current_weather(location, unit="fahrenheit"):
"""Get the current weather in a given location"""
if "tokyo" in location.lower():
return json.dumps({"location": "Tokyo", "temperature": "10", "unit": unit})
elif "san francisco" in location.lower():
return json.dumps({"location": "San Francisco", "temperature": "72", "unit": unit})
elif "paris" in location.lower():
return json.dumps({"location": "Paris", "temperature": "22", "unit": unit})
else:
return json.dumps({"location": location, "temperature": "unknown"})
python
# Step 1: send the conversation and available functions to the model
messages = [{"role": "user",
"content": "What's the weather like in San Francisco, Tokyo, and Paris?"}]
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
}
]
response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=messages,
tools=tools,
tool_choice="auto", # auto is default, but we'll be explicit
)
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
python
# Step 2: check if the model wanted to call a function
global second_response
if tool_calls:
# Step 3: call the function
# Note: the JSON response may not always be valid; be sure to handle errors
available_functions = {
"get_current_weather": get_current_weather,
} # only one function in this example, but you can have multiple
messages.append(response_message) # extend conversation with assistant's reply
# Step 4: send the info for each function call and function response to the model
for tool_call in tool_calls:
function_name = tool_call.function.name
function_to_call = available_functions[function_name] # 确定要调用的函数名
function_args = json.loads(tool_call.function.arguments) # 从llm的返回对象中,获取调用函数的参数
function_response = function_to_call(
location=function_args.get("location"),
unit=function_args.get("unit"),
)
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
}
) # extend conversation with function response
#再次调用模型,将message对象给大模型
second_response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=messages,
) # get a new response from the model where it can see the function response
查看执行结果:
second_response
ChatCompletion(id='chatcmpl-8aJh1gWOluIGMlaGqYkCrcRqCpuM9', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content="Currently, the weather in San Francisco is 72°C, in Tokyo it's 10°C, and in Paris it's 22°C.", role='assistant', function_call=None, tool_calls=None), logprobs=None)], created=1703666199, model='gpt-3.5-turbo-1106', object='chat.completion', system_fingerprint='fp_772e8125bb', usage=CompletionUsage(completion_tokens=29, prompt_tokens=169, total_tokens=198))
返回了一堆
second_response.choices[0].message
ChatCompletionMessage(content="Currently, the weather in San Francisco is 72°C, in Tokyo it's 10°C, and in Paris it's 22°C.", role='assistant', function_call=None, tool_calls=None)
second_response.choices[0].message.role
'assistant'
second_response.choices[0].message.content
"Currently, the weather in San Francisco is 72°C, in Tokyo it's 10°C, and in Paris it's 22°C."
可看到大模型又重新组织了语言,输出给用户
chatGPT的Function calling功能允许用户通过消息和模型进行交互,并根据用户提供的函数调用来获取所需的数据或执行特定的操作。下面是一个完整的例子:
- 用户发送一条消息给模型,包含问题和请求的函数调用,例如:"我需要计算两个数字的和。"
- 自己的程序接收到消息后,解析函数调用,找到对应的函数,并执行它。在这个例子中,程序会调用一个名为
calculate_sum
的函数,计算两个数字的和。 - 程序获取到计算结果后,将结果返回给模型。
- 模型接收到结果后,使用语言模型重新组织语言,生成回复消息,并将消息返回给用户。
整个过程至少需要两次交互:
- 第一次交互:用户向模型提问,并告知需要调用的函数,例如:"我需要计算两个数字的和。"
- 第二次交互:模型返回给自己的代码调用的函数列表,自己的代码根据函数列表,执行对应的函数,并将结果返回给模型。
在第二次交互中,自己的代码可以根据模型返回的函数列表,选择合适的函数,并将函数所需的参数传递给它。然后再将函数执行的结果返回给模型进行处理。
总结而言,Function calling功能通过将用户的函数调用传递给模型来实现更复杂的交互和操作。自己的代码根据模型返回的函数调用列表,调用相应的函数,并将结果传递给模型,模型根据结果生成回复消息,实现了更灵活和动态的对话交互。