当前位置: 首页 > news >正文

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 函数的实现来真正地获取天气信息。
 


http://www.mrgr.cn/news/6478.html

相关文章:

  • 汇编语言:adc指令 和 sbb指令
  • 【前端基础篇】JavaScript基础介绍
  • 【知识图谱】2.知识抽取与知识存储
  • Java设计模式之策略模式详细讲解和案例示范
  • Objective-C中的查询大师:深入探索NSPredicate与NSExpression
  • EPCE-HDR
  • 基于单片机的仿生水母水下机器人设计
  • 电机学习记录
  • java整合Redis
  • 大话设计模式解读06-原型模式
  • Nginx服务器申请及配置免费SSL证书
  • 8.22-docker的部署及其使用
  • 使用uart串口配置TMC2209模块
  • Spring系列之Spring Cache缓存注解的使用
  • Spring Boot整合Sentry
  • 深入Objective-C:NSFilePresenter与NSFileProvider的协同艺术
  • JavaScript静态方法
  • 数学基础(六)
  • git-版本管理工具基本操作-创建仓库-拉取-推送-暂存库-版本库
  • yolov8行人车辆检测与计数系统