掘金 人工智能 18小时前
Python调用Openai的Function calling功能—源码
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文深入浅出地介绍了大语言模型中的Function Calling技术,即模型如何理解并调用外部函数来获取信息或执行任务。文章详细阐述了Function Calling的核心流程:识别用户意图、提取函数参数、执行外部函数以及整合结果。通过一个完整的Python代码示例,展示了如何定义工具(如查询天气、获取时间)以及如何构建一个能够与模型交互的聊天代理。代码部分详细解析了工具定义类`ToolsUsage`和聊天代理类`ChatAgent`的关键组成,并一步步讲解了从用户提问到模型响应的整个执行流程,包括意图识别、多轮工具调用循环和最终结果的整合,帮助读者理解并实践这一强大的AI能力。

✨ **Function Calling的核心机制**:Function Calling技术使大语言模型能够识别用户请求中需要调用外部工具的意图,并自动提取调用函数所需的参数。模型会根据用户输入,决定调用哪个函数,获取函数所需的参数,然后执行函数,最后将函数返回的结果整合到自然语言回答中,实现更智能化的交互。

🛠️ **工具定义与实现**:通过`ToolsUsage`类,文章展示了如何定义可供模型调用的函数。每个工具都包含`name`、`description`和`parameters`。`description`帮助模型理解何时使用该工具,`parameters`则使用JSON Schema定义参数的类型和要求。实际的函数如`get_current_weather`和`get_current_time`则提供了具体的实现逻辑,确保工具能够正确执行并返回结果。

💬 **聊天代理的交互流程**:`ChatAgent`类封装了与大语言模型的交互逻辑。`request_chat`方法负责将用户消息和工具定义发送给模型,并设置`tool_choice='auto'`让模型自主选择是否调用工具。`execute_chat`方法则贯穿了整个对话生命周期,包括接收模型的第一响应,判断是否需要工具调用;如需调用,则进入循环,解析模型返回的工具调用信息,执行工具,并将工具结果反馈给模型,直至模型给出最终答案。

🔄 **多轮工具调用与结果整合**:当模型需要调用多个工具或多次调用同一个工具时,Function Calling能够支持多轮对话。模型会根据用户输入和前一次工具调用的结果,决定下一步操作。这个过程会持续进行,直到模型认为所有必要的信息都已获取,并能生成一个完整的、包含工具执行结果的自然语言回答。

💡 **代码示例的实践价值**:文章提供的完整Python代码示例,包括`pip install openai`的依赖安装,以及`ToolsUsage`和`ChatAgent`类的具体实现,为开发者提供了一个可直接参考和运行的框架。通过这个示例,用户可以亲身体验Function Calling的工作原理,并将其应用于自己的AI应用开发中。

一、什么是 Function Calling?

Function Calling是一种让大语言模型能够理解何时需要调用外部函数,以及如何正确调用这些函数的技术。当用户提出需要外部工具协助的问题时,模型会:

    识别意图:理解用户问题需要调用哪个工具提取参数:从用户输入中提取调用函数所需的参数执行调用:调用相应函数获取结果整合回答:将函数结果整合到自然语言回答中

二、代码架构解析

让我们通过一个实际的代码示例来理解Function Calling的实现:

1.安装第三方库

首先安装一下第三方库

pip install openai

2. 完整示例代码

# -*- coding: utf-8 -*-  # @Author :Kan # @Date :2025/8/1 9:12  # @File :1_function_calling.py  import json  import random  from openai import OpenAI  from datetime import datetime      # ================================================================  # 1. Function Calling(函数调用)  # ================================================================    class ToolsUsage:      tools = [          # 工具2 获取指定城市的天气          {              "type": "function",              "function": {                  "name": "get_current_weather",                  "description": "获取指定地点的天气信息",                  "parameters": {                      "type": "object",                      "properties": {                          "location": {"type": "string", "description": "地点名称"}                      },                      "required": ["location"]                  }              }          },          # 工具1 获取当前时刻的时间          {              "type": "function",              "function": {                  "name": "get_current_time",                  "description": "当你想知道现在的时间时非常有用。",                  # 因为获取当前时间无需输入参数,因此parameters为空字典                  "parameters": {},              },          },      ]        @staticmethod      # 天气查询工具。返回结果示例:“北京今天是雨天。”      def get_current_weather(location):          weather_conditions = ["晴天", "多云", "雨天"]          # 随机选择一个天气条件          random_weather = random.choice(weather_conditions)          # 返回格式化信息          return f"{location}今天是{random_weather}。"        @staticmethod      # 查询当前时间的工具。返回结果示例:“当前时间:2024-04-15 17:15:18。“      def get_current_time():          current_datetime = datetime.now()          # 格式化日期          formatted_time = current_datetime.strftime("%Y-%m-%d %H:%M:%S")          return f"当前时间:{formatted_time}。"        @staticmethod      def execute_tools(func_name, func_args):          func_dic = {              "get_current_weather": ToolsUsage.get_current_weather,              "get_current_time": ToolsUsage.get_current_time,          }          return func_dic[func_name](**func_args)      class ChatAgent:      def __init__(self, api_key: str, url: str, model_name: str):          self.client = OpenAI(api_key=api_key, base_url=url)          self.model_name = model_name        def request_chat(self, messages: list):          response = self.client.chat.completions.create(              model=self.model_name,              messages=messages,              tools=ToolsUsage.tools,              extra_body={"enable_thinking": False},              tool_choice="auto",          )          return response        def execute_chat(self):          print("\n")          messages = [              {                  # 提问示例:"现在几点了?" "一个小时后几点" "北京天气如何?"                  "content": input(                      "请输入问题:"                  ),                  "role": "user",              }          ]          print("-*" * 60)          # 模型调用次数          i = 1          first_response = self.request_chat(messages)          assistant_output = first_response.choices[0].message          print(f"\n第{i}轮大模型输出信息:{assistant_output}\n")          # 不需要调用工具,则直接返回答案          if not assistant_output.tool_calls:              print(f"无需调用工具,直接回复:{assistant_output.content}")              return          tool_calls_result = assistant_output.tool_calls          # 如果需要调用工具,则进行模型的多轮调用,直到模型判断无需调用工具          while tool_calls_result:              # 执行工具调用              for tool_call in assistant_output.tool_calls:                  tool_info = {                      "content": "",                      "role": "tool",                      "tool_call_id": assistant_output.tool_calls[0].id,                  }                  func_name = tool_call.function.name                  func_args = json.loads(tool_call.function.arguments)                  tools_result = ToolsUsage.execute_tools(func_name, func_args)                  print(f"当前调用工具:{func_name},参数:{func_args},输出信息:{tools_result}\n")                  tool_info["content"] = tools_result                  messages.append(tool_info)                  print("-*" * 60)              second_response = self.request_chat(messages)              assistant_output = second_response.choices[0].message              i += 1              print(f"第{i}轮大模型输出信息:{assistant_output}\n")              # 指导调用工具为空时终止              if not assistant_output.tool_calls:                  tool_calls_result = None          print(f"最终答案:\n {assistant_output.content}")      if __name__ == "__main__":      api_key = 'xxxxxx'      base_url = "http://xxxxxxxx/v1"      model = "qwen3"      chat_service = ChatAgent(api_key, base_url, model)      chat_service.execute_chat()

3. 工具定义类 - ToolsUsage

class ToolsUsage:    tools = [        # 天气查询工具        {            "type": "function",            "function": {                "name": "get_current_weather",                "description": "获取指定地点的天气信息",                "parameters": {                    "type": "object",                    "properties": {                        "location": {"type": "string", "description": "地点名称"}                    },                    "required": ["location"]                }            }        },        # 时间查询工具        {            "type": "function",             "function": {                "name": "get_current_time",                "description": "当你想知道现在的时间时非常有用。",                "parameters": {},            },        },    ]

这个类承担了两个核心职责:

工具声明:通过tools列表定义了可用的函数。每个工具定义包含:

工具实现:提供了实际的函数实现,包括天气查询和时间获取功能。

4. 聊天代理类 - ChatAgent

class ChatAgent:    def __init__(self, api_key: str, url: str, model_name: str):        self.client = OpenAI(api_key=api_key, base_url=url)        self.model_name = model_name

ChatAgent类负责与大语言模型的交互,核心方法包括:

请求处理request_chat方法向模型发送消息,关键参数:

执行逻辑execute_chat方法实现了完整的对话流程。

三、Function Calling 执行流程详解

第一阶段:意图识别

first_response = self.request_chat(messages)assistant_output = first_response.choices[0].message

当用户输入问题时,模型首先分析是否需要调用外部工具。比如:

第二阶段:工具调用循环

while tool_calls_result:    for tool_call in assistant_output.tool_calls:        func_name = tool_call.function.name        func_args = json.loads(tool_call.function.arguments)        tools_result = ToolsUsage.execute_tools(func_name, func_args)

如果模型判断需要调用工具,会进入多轮对话循环:

    参数解析:模型从用户输入中提取函数参数工具执行:调用相应的工具函数获取结果结果反馈:将工具执行结果作为新的消息加入对话历史继续推理:模型基于工具结果继续生成回答

第三阶段:结果整合

if not assistant_output.tool_calls:    print(f"最终答案:\n {assistant_output.content}")

当所有必要的工具调用完成后,模型将结果整合成自然语言回答返回给用户。

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Function Calling 大语言模型 AI开发 工具调用 Python
相关文章