API中的人工智能工具应用解析 | AI生成和翻译

Home 2025.07

什么是 AI API 中的工具使用?

工具使用,通常被称为“工具调用”或“函数调用”,是 AI API 中的一项功能,允许大型语言模型(LLMs)与外部工具、函数或 API 进行交互。模型不再仅仅依赖其内部知识生成响应,而是可以决定调用预定义的函数来获取实时数据、执行计算或执行操作。这使得 AI 对于诸如查询天气、搜索数据库或与其他服务集成等任务更加动态和实用。

该过程通常如下工作:

这通常受到 OpenAI 的函数调用 API 的启发,许多提供商如 Mistral 和 DeepSeek 都支持兼容的实现。

工具使用选 Mistral 还是 DeepSeek?

Mistral AI 和 DeepSeek AI 都在其 API 中支持工具调用,这使得它们适合构建需要外部集成的智能体或应用程序。以下是基于现有信息的简要比较:

最终,对于工具使用而言,没有哪一个是绝对“更好”的——它们都运作良好。DeepSeek 可能在成本节约方面略胜一筹,而 Mistral 则提供了更完善的智能体集成。

如何使用工具使用功能

要使用工具调用,您需要从相应的提供商处获取 API 密钥(在 mistral.ai 注册 Mistral 或在 platform.deepseek.com 注册 DeepSeek)。两者都使用与 OpenAI 类似的 Python SDK。以下是一个简单天气查询工具的分步示例。

使用 Mistral AI 进行工具调用

Mistral 的 API 通过其 MistralClient 在聊天补全中支持工具调用。使用 pip install mistralai 安装 SDK。

Python 代码示例(改编自官方和社区来源):

from mistralai import Mistral

# 使用您的 API 密钥初始化客户端
api_key = "YOUR_MISTRAL_API_KEY"
model = "mistral-large-latest"  # 支持工具调用
client = Mistral(api_key=api_key)

# 定义工具(函数)
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "获取某个地点的天气。",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string", "description": "城市,例如:San Francisco"}
                },
                "required": ["location"]
            }
        }
    }
]

# 用户消息
messages = [{"role": "user", "content": "杭州的天气怎么样?"}]

# 第一次 API 调用:模型决定是否需要工具
response = client.chat.complete(
    model=model,
    messages=messages,
    tools=tools,
    tool_choice="auto"  # 自动决定是否使用工具
)

# 检查工具调用
tool_calls = response.choices[0].message.tool_calls
if tool_calls:
    # 将模型的响应附加到消息列表中
    messages.append(response.choices[0].message)
    
    # 模拟执行工具(在实际代码中,调用真实的 API)
    tool_call = tool_calls[0]
    if tool_call.function.name == "get_weather":
        location = eval(tool_call.function.arguments)["location"]
        weather_result = "24°C,晴朗"  # 替换为真实的函数调用
        
        # 附加工具结果
        messages.append({
            "role": "tool",
            "tool_call_id": tool_call.id,
            "name": tool_call.function.name,
            "content": weather_result
        })
    
    # 第二次 API 调用:模型生成最终响应
    final_response = client.chat.complete(model=model, messages=messages)
    print(final_response.choices[0].message.content)
else:
    print(response.choices[0].message.content)

此代码发送查询,检查工具调用,执行它(此处为模拟),并获取最终答案。对于基于智能体的设置,可以使用 Mistral 的 beta 版智能体 API 来处理更复杂的工作流。

使用 DeepSeek AI 进行工具调用

DeepSeek 的 API 与 OpenAI 兼容,因此您可以使用 OpenAI Python SDK。使用 pip install openai 安装。

Python 代码示例(来自官方文档):

from openai import OpenAI

# 使用 DeepSeek 基础 URL 和 API 密钥初始化客户端
client = OpenAI(
    api_key="YOUR_DEEPSEEK_API_KEY",
    base_url="https://api.deepseek.com"
)

# 定义工具
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "获取某个地点的天气,用户应首先提供一个地点",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "城市和州,例如 San Francisco, CA",
                    }
                },
                "required": ["location"]
            },
        }
    },
]

# 发送消息的函数
def send_messages(messages):
    response = client.chat.completions.create(
        model="deepseek-chat",  # 支持工具调用
        messages=messages,
        tools=tools
    )
    return response.choices[0].message

# 用户消息
messages = [{"role": "user", "content": "杭州的天气怎么样?"}]
message = send_messages(messages)
print(f"用户>\t {messages[0]['content']}")

# 处理工具调用
tool = message.tool_calls[0]
messages.append(message)

# 模拟工具执行(替换为真实函数)
messages.append({"role": "tool", "tool_call_id": tool.id, "content": "24℃"})

# 获取最终响应
message = send_messages(messages)
print(f"模型>\t {message.content}")

这遵循相同的流程:查询 → 工具调用 → 执行 → 最终响应。DeepSeek 限制了上下文长度,因此请保持消息简洁。

对于高级用法,请查看速率限制、成本和模型特定文档。在沙盒环境中进行测试以处理诸如无效参数之类的错误。

参考资料


Back Donate