本文主要是介绍function call使用基础,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
以请求GPT4 api为例子进行展示,
当使用GPT-4这样的模型并通过API调用来实现功能调用(function call)时,你可以构建一个请求,其中包含特定的指令和参数以调用外部函数。下面是一个使用GPT-4 API实现功能调用的例子,假设你已经有了访问GPT-4 API的有效方式。
示例场景:
假设你需要构建一个简单的聊天机器人,它可以回答关于天气的问题。当用户询问某个城市的天气时,该机器人会调用一个外部函数来获取天气信息,并将结果返回给用户。
请求数据示例:
{
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant that can call external functions. If the user asks about the weather, you should use the 'get_weather' function to retrieve the information."
},
{
"role": "user",
"content": "What's the weather like in New York today?"
}
],
"functions": [
{
"name": "get_weather",
"description": "Get the weather in a specific city.",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city name"
}
},
"required": ["city"]
}
}
],
"function_call": "auto"
}
解释:
1. 系统消息 (role: "system") - 定义了助手的角色以及何时调用哪个函数。
2. 用户消息 (role: "user") - 用户询问的问题。
3. 功能定义 (functions) - 定义了可被调用的函数及其参数。
4. 函数调用 (function_call: "auto") - 指示API自动选择合适的函数来响应用户的消息。
响应数据示例:
假设GPT-4选择了调用 get_weather 函数,那么它会返回一个类似下面的响应:
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652939,
"model": "gpt-4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_weather",
"arguments": "{\"city\": \"New York\"}"
}
},
"finish_reason": "function_call"
}
]
}
处理响应:
当你收到上述响应后,你需要调用 get_weather 函数并将结果传回给API。这通常需要编写一些额外的代码来处理这些交互。例如,在Python中,你可以这样做:
import openai
# 设置OpenAI API密钥
openai.api_key = "your-api-key"
def get_weather(city):
# 假设这是一个真实的天气API调用
return {"city": city, "temperature": "20°C", "forecast": "sunny"}
# 发送请求并处理响应
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant that can call external functions. If the user asks about the weather, you should use the 'get_weather' function to retrieve the information."},
{"role": "user", "content": "What's the weather like in New York today?"}
],
functions=[
{
"name": "get_weather",
"description": "Get the weather in a specific city.",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city name"
}
},
"required": ["city"]
}
}
],
function_call="auto"
)
# 获取函数调用的结果
function_call = response.choices[0].message.function_call
function_name = function_call.name
function_args = json.loads(function_call.arguments)
# 调用函数并获取结果
result = get_weather(function_args.get("city"))
# 再次发送请求,这次是将函数调用的结果发送回去
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant that can call external functions. If the user asks about the weather, you should use the 'get_weather' function to retrieve the information."},
{"role": "user", "content": "What's the weather like in New York today?"},
{"role": "assistant", "content": None, "function_call": {"name": "get_weather", "arguments": "{\"city\": \"New York\"}"}},
{"role": "function", "name": "get_weather", "content": json.dumps(result)}
]
)
# 输出最终的回复
print(response.choices[0].message.content)
请注意,上述示例代码中的 openai.ChatCompletion.create 方法调用是基于 OpenAI API 的,你需要安装 openai 库并设置你的API密钥。此外,你需要根据实际情况调整 get_weather 函数的实现来真正地获取天气信息。
这篇关于function call使用基础的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!