如何提升AI模型能力?Function Calling如何快速入门? | 客服服务营销数智化洞察_晓观点
       

如何提升AI模型能力?Function Calling如何快速入门?

随着人工智能(AI)技术的飞速发展,Function Calling作为一种能够增强AI模型能力的重要技术,正日益引起广泛关注。无论是在智能对话系统中,还是在复杂的数据处理任务中,函数调用为AI提供了更强大的功能。在特定任务或场景中,AI模型可以通过调用预定义的函数来获取外部系统或数据的支持。例如,当用户向AI询问天气、财务报告或产品推荐时,AI不仅能生成文本回复,还可以调用API接口来获取实时数据或执行特定操作,从而提供更准确、实用的答案。

一、Function Calling的工作原理

函数调用背后的工作原理主要分为以下几个步骤:

  1. 用户请求:用户向AI模型发送一个输入请求,例如“请帮我查找附近的餐厅”。
  2. 函数匹配:AI模型根据请求内容,判断是否需要调用外部函数,并匹配到相应的API或功能。
  3. 函数调用:模型调用相应的外部函数,例如餐厅查询API,获取精确的数据或执行特定操作。
  4. 返回结果:函数返回结果后,AI模型将其加工成自然语言回复给用户。

这一过程不仅让AI模型更智能,也让其更加贴近用户的实际需求。

如何提升AI模型能力?Function Calling如何快速入门?

二、Function Calling的优势

  1. 增强模型能力:通过函数调用,AI不再局限于文本生成或对话推理,它可以执行具体操作,提升了实际功能。
  2. 提高交互精准度:在处理用户请求时,函数调用能够提供更准确的响应,减少错误理解的几率。
  3. 降低复杂度:借助外部API和函数,AI模型可以处理复杂的任务,如数据分析、自动化执行等,减少用户操作步骤。

三、Function Calling的应用场景

  1. 智能助手:通过函数调用,智能助手不仅能够回答问题,还能帮助用户完成实际操作,如预订餐厅、设置提醒、查询天气等。
  2. 电商平台:AI可以实时查询商品信息、库存状态、价格波动等,为用户提供个性化购物体验。
  3. 金融服务:在金融行业,函数调用可以用来查询实时汇率、处理交易请求,甚至提供投资建议。
  4. IoT设备控制:通过函数调用,AI可以远程控制智能家居设备,实现设备间的无缝连接与操作。

四、Function Calling的使用方式

目前function calling已经成为主流大模型的标准能力,gpt-4、minimax、doubao均已支持,均使用相同接口协议。

Request

curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
  "model": "gpt-4o",
  "messages": [
    {
      "role": "user",
      "content": "What'\''s the weather like in Boston today?"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
              "type": "string",
              "enum": ["celsius", "fahrenheit"]
            }
          },
          "required": ["location"]
        }
      }
    }
  ],
  "tool_choice": "auto"
}'

Response

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1699896916,
  "model": "gpt-4o-mini",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call_abc123",
            "type": "function",
            "function": {
              "name": "get_current_weather",
              "arguments": "{\n\"location\": \"Boston, MA\"\n}"
            }
          }
        ]
      },
      "logprobs": null,
      "finish_reason": "tool_calls"
    }
  ],
  "usage": {
    "prompt_tokens": 82,
    "completion_tokens": 17,
    "total_tokens": 99
  }
}

五、Function Calling的使用步骤

  • 编写一个tool 实现一定的功能。
# This is the function that we want the model to be able to call
def get_delivery_date(order_id: str) -> datetime:
    # Connect to the database
    conn = sqlite3.connect('ecommerce.db')
    cursor = conn.cursor()
    # ...
  • 向模型描述这个函数,让模型知道如何调用,函数定义的部分parameters应使用 JSON Schema 进行描述。如果模型生成函数调用,它将使用此信息根据您提供的架构生成参数。
{
    "name": "get_delivery_date",
    "description": "Get the delivery date for a customer's order. Call this whenever you need to know the delivery date, for example when a customer asks 'Where is my package'",
    "parameters": {
        "type": "object",
        "properties": {
            "order_id": {
                "type": "string",
                "description": "The customer's order ID.",
            },
        },
        "required": ["order_id"],
        "additionalProperties": false,
    }
}
  • 将函数和消息一起传给大模型
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_delivery_date",
            "description": "Get the delivery date for a customer's order. Call this whenever you need to know the delivery date, for example when a customer asks 'Where is my package'",
            "parameters": {
                "type": "object",
                "properties": {
                    "order_id": {
                        "type": "string",
                        "description": "The customer's order ID.",
                    },
                },
                "required": ["order_id"],
                "additionalProperties": False,
            },
        }
    }
]

messages = [
    {"role": "system", "content": "You are a helpful customer support assistant. Use the supplied tools to assist the user."},
    {"role": "user", "content": "Hi, can you tell me the delivery date for my order?"}
]

response = openai.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    tools=tools,
)
  • 接收并处理响应
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_delivery_date",
            "description": "Get the delivery date for a customer's order. Call this whenever you need to know the delivery date, for example when a customer asks 'Where is my package'",
            "parameters": {
                "type": "object",
                "properties": {
                    "order_id": {
                        "type": "string",
                        "description": "The customer's order ID."
                    }
                },
                "required": ["order_id"],
                "additionalProperties": False
            }
        }
    }
]

messages = []
messages.append({"role": "system", "content": "You are a helpful customer support assistant. Use the supplied tools to assist the user."})
messages.append({"role": "user", "content": "Hi, can you tell me the delivery date for my order?"})
// highlight-start
messages.append({"role": "assistant", "content": "Hi there! I can help with that. Can you please provide your order ID?"})
messages.append({"role": "user", "content": "i think it is order_12345"})
// highlight-end

response = client.chat.completions.create(
    model='gpt-4o',
    messages=messages,
    tools=tools
)
  • 返回调用函数
Choice(
    finish_reason='tool_calls', 
    index=0, 
    logprobs=None, 
    message=chat.completionsMessage(
        content=None, 
        role='assistant', 
        function_call=None, 
        tool_calls=[
            chat.completionsMessageToolCall(
                id='call_62136354', 
                function=Function(
                    arguments='{"order_id":"order_12345"}', 
                    name='get_delivery_date'), 
                type='function')
        ])
)
  • 调用函数
# Extract the arguments for get_delivery_date
# Note this code assumes we have already determined that the model generated a function call. See below for a more production ready example that shows how to check if the model generated a function call
tool_call = response.choices[0].message.tool_calls[0]
arguments = json.loads(tool_call['function']['arguments'])

order_id = arguments.get('order_id')

# Call the get_delivery_date function with the extracted order_id
delivery_date = get_delivery_date(order_id)
  • 将结果返回给大模型
# Simulate the order_id and delivery_date
order_id = "order_12345"
delivery_date = datetime.now()

# Simulate the tool call response
response = {
    "choices": [
        {
            "message": {
                "tool_calls": [
                    {"id": "tool_call_1"}
                ]
            }
        }
    ]
}

# Create a message containing the result of the function call
function_call_result_message = {
    "role": "tool",
    "content": json.dumps({
        "order_id": order_id,
        "delivery_date": delivery_date.strftime('%Y-%m-%d %H:%M:%S')
    }),
    "tool_call_id": response['choices'][0]['message']['tool_calls'][0]['id']
}

# Prepare the chat completion call payload
completion_payload = {
    "model": "gpt-4o",
    "messages": [
        {"role": "system", "content": "You are a helpful customer support assistant. Use the supplied tools to assist the user."},
        {"role": "user", "content": "Hi, can you tell me the delivery date for my order?"},
        {"role": "assistant", "content": "Hi there! I can help with that. Can you please provide your order ID?"},
        {"role": "user", "content": "i think it is order_12345"},
        response['choices'][0]['message'],
        function_call_result_message
    ]
}

# Call the OpenAI API's chat completions endpoint to send the tool call result back to the model
response = openai.chat.completions.create(
    model=completion_payload["model"],
    messages=completion_payload["messages"]
)

# Print the response from the API. In this case it will typically contain a message such as "The delivery date for your order #12345 is xyz. Is there anything else I can help you with?"
print(response)

结语

综上所述,Function Calling作为人工智能领域的一项重要技术,正逐步成为提升AI模型能力、优化用户体验的关键手段。通过调用预定义的函数和API接口,AI模型能够跨越传统界限,执行更为复杂和精准的任务,从而为用户提供更加个性化、智能化的服务。随着技术的不断进步和应用的持续拓展,我们有理由相信,Function Calling将在智能对话系统、数据处理、电商、金融、IoT等多个领域发挥越来越重要的作用,推动人工智能技术的全面发展与普及。未来,随着更多创新技术的涌现和融合,Function Calling的潜力将得到进一步挖掘,为人类社会带来更加便捷、高效、智能的生活方式。

延展阅读:

Nginx对前端开发有什么帮助?Nginx是什么?

软件开发时必须使用设计模式吗?观察者模式是什么?

如何通过Qdrant数据库优化大规模相似性搜索和推荐系统?

咨询方案 获取更多方案详情                        
(0)
研发专家-晴明研发专家-晴明
上一篇 2024年9月12日 下午3:51
下一篇 2024年9月13日 下午4:24

相关推荐