• 作者:老汪软件技巧
  • 发表时间:2024-10-15 15:05
  • 浏览量:

Function Call是什么 ?

简单来说就是给 LLM 提供了调用外部工具或函数的能力。这个函数或工具可以是可以自定义的,这样可玩性就很大了,一个是 LLM 本身能力可以依靠Function Call得到极大补充,另一个是 LLM 可以极大地适配你的业务。

应用场景获取你的内部业务数据。例如:一个智能客服系统,客户可能想查询自己订单的情况,在给 LLM 对应的订单ID后,LLM 可以调用Function Call来调取你本地的数据,获取对应的订单信息并解决客户问题。对接各种工具获取更加强大的能力。例如:LLM 可以通过 Function Call 获取天气、新闻、股票价格等实时信息。执行各种操作。例如:LLM 可以通过 Function Call 发送邮件、创建日历事件、控制智能家居设备等,对于业务上 LLM 可以通过调用 Function Call 完成对应用户的需求比如发送退款申请,查询订单状态,完成商品上架等。是不是感觉 LLM 的能力一下子就上来了?实际上通过Function Call,LLM 已经成为了一个强大的agent,通过自身的决策,完成各种外部信息的查询以及各种操作,放在家里那就是整个一智能管家呀。如何使用Function Call?

deepseek是适配open AI的Function Call的,我们调用deepseek来进行演示如何调用一个Function Call。

看看结果

首先用户先向模型提问北京天气,模型是无法知道天气的,通过Function Call调用我们识别天气的函数。最后模型根据我们给的结果对用户问题进行解答。

下面我们详细讲一下怎么实现

定义被调用的函数

def get_weather(location):
    # 这里可以添加实际的天气获取逻辑
    # 例如,调用一个天气API并返回结果
    # 为了示例,我们假设返回一个固定的天气信息
    return f"The weather in {location} is 24℃."

定义函数描述

函数描述中详细描述了这个函数的功能以及需要填入的参数类型和属性(比如是否必填),之后模型会根据你的描述使用你的函数,并给你提供对应的参数

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"]
            },
        }
    },
]

构建与模型对话的函数

_模型函数设定_4.5.3函数模型的应用

注意key是deepseek的key

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="YOUR_KEY",
    base_url="https://api.deepseek.com",
)

初始化对话

messages = [{"role": "user", "content": "How's the weather in Beijing?"}]
message = send_messages(messages)
tool = message.tool_calls[0]
messages.append(message)

调用本地Function Call

注意实际场景需要根据模型给出的回答判断调用什么Function,我们这里是默认要调用get_weather

# 解析工具调用参数
location = json.loads(message.tool_calls[0].function.arguments).get('location')
# 在这里判断调用哪个Function
# 使用对应Function
weather_info = get_weather(location)

将Function的返回值传回模型,并获取模型返回结果

messages.append({"role": "tool", "tool_call_id": tool.id, "content": weather_info})
message = send_messages(messages)
print(f"Model>\t {message.content}")

完整代码

from openai import OpenAI
import json
def send_messages(messages):
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=messages,
        tools=tools
    )
    return response.choices[0].message
def get_weather(location):
    # 这里可以添加实际的天气获取逻辑
    # 例如,调用一个天气API并返回结果
    # 为了示例,我们假设返回一个固定的天气信息
    return f"The weather in {location} is 24℃."
client = OpenAI(
    api_key="YOUR_KEY",
    base_url="https://api.deepseek.com",
)
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"]
            },
        }
    },
]
print(f"User>\t How's the weather in Beijing?")
messages = [{"role": "user", "content": "How's the weather in Beijing?"}]
message = send_messages(messages)
print(f"Model2>\t {message.tool_calls[0].function}")
# 解析工具调用参数
location = json.loads(message.tool_calls[0].function.arguments).get('location')
tool = message.tool_calls[0]
messages.append(message)
# 调用 get_weather 函数获取天气信息
weather_info = get_weather(location)
messages.append({"role": "tool", "tool_call_id": tool.id, "content": weather_info})
message = send_messages(messages)
print(f"Model>\t {message.content}")

这就是一个简单的Function Call的调用过程,复杂的Function Call可以根据实际需求进行扩展。

Function Call的优势

Function Call机制为AI模型带来了许多优势:

Function Call 也为 LLM 真正成为 agent 贡献了一大步。