大模型技术之Function Calling

什么是Function Calling

function calling 让模型能够调用外部工具,来增强自身的能力

样例代码

  • 安装依赖
    • pip install openai
  • demo
    • from openai import OpenAI
      
      def send_messages(messages):
          response = client.chat.completions.create(
              model = 'deepseek-chat',
              messages=messages,
              tools=tools
          )
          return response.choices[0].message
      
      client = OpenAI(
          api_key="sk-fa13e5a983e548d9a2a7c8885ef50935",
          base_url="https://api.deepseek.com"
      )
      
      # get_weather 函数需要自己实现
      tools = [
          {
              "type": "function",
              "function": {
                  "name": "get_weather",
                  "description": "Get weather of a location, the user should supply a location first.",
                  "parameters": {
                      "type": "object",
                      "properties": {
                          "location": {
                              "type": "string",
                              "description": "The city and state, e.g. San Francisco, CA"
                          }
                      },
                      "required": ["location"]
                  },
              }
          },
      ]
      #
      messages = [{"role":"user","content":"How's the weather in Hangzhou, Zhejiang?"}]
      message = send_messages(messages)
      print(f"User>\t {messages[0]['content']}")
      
      tool = message.tool_calls[0]
      messages.append(message)
      
      # 模拟get_weather 函数结果加入messages送入模型
      messages.append({"role":"tool","tool_call_id":tool.id, "content":"24"})
      message = send_messages(messages)
      print(f"Model>\t {message.content}")
      
      
      # 执行流程
      # 用户:询问现在的天气
      # 模型:返回 function get_weather({location: 'Hangzhou'})
      # 用户:调用 function get_weather({location: 'Hangzhou'}),并传给模型。
      # 模型:返回自然语言,"The current temperature in Hangzhou is 24°C."
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值