1. API请求
智信AI-使用指南
  • 使用指南
    • 平台介绍
    • 使用指南
      • 快速开始
      • 注册账号
      • 账户充值
      • 新建API令牌
      • 获取BaseUrl
      • 系统令牌及用户ID
      • 余额查询接口
      • 计费规则
      • 平台管理类接口
        • 模型列表查询
        • 检索API令牌
        • 模型用量查询
        • 获取API令牌
    • 开发者文档
      • 基础核心概念
      • API请求
        • 平台通用格式
        • 文本对话(非流)
        • 文本对话(流式)
        • 输出JSON格式
        • 向量嵌入
        • 函数调用
        • 图片分析
        • Responses API
    • 接入指南
      • 编程工具
        • Cursor 配置指南
        • VSCode (Continue) 配置指南
        • Claude Code 配置指南
      • 应用集成
        • NextChat 配置指南
        • Open Claw 配置指南
        • Chatbox 配置指南
        • Dify 配置指南
  1. API请求

函数调用

OpenAI格式实现函数调用#


概述:#

Function Call(函数调用 / 工具调用),是大模型具备的结构化能力:
大模型在推理过程中,不再只输出自然语言文本,而是主动判断是否需要调用外部工具 / 自定义函数,按照约定的 JSON格式生成函数名称、入参参数,交由程序执行对应接口、代码、业务逻辑,再把执行结果回传给大模型,由大模型整合结果生成最终回答。
简单说:大模型负责决策调用哪个工具、填什么参数;程序负责实际执行函数、返回数据。

接口地址#

https://ai.hcrzx.com/v1/chat/completions

Python代码示例#

import requests
import json

# ===================== 配置 =====================
API_KEY = "sk-**********************************"  # 替换为你的 API密钥
API_URL = "https://ai.hcrzx.com/v1/chat/completions"
MODEL = "qwen3.6-plus"
# =================================================

# --------------------- 1. 函数描述(给AI看的) ---------------------
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "查询某个城市的天气",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string",
                        "description": "要查询天气的城市名称,例如:北京"
                    }
                },
                "required": ["city"]
            }
        }
    }
]


# --------------------- 2. 调用大模型接口获取匹配到的函数和函数参数---------------------
def chat_with_function_call(user_query: str):
    messages = [{"role": "user", "content": user_query}]

    # ========== 第一步:发送请求,让AI判断是否需要调用函数 ==========
    response = requests.post(
        url=API_URL,
        headers={
            "Authorization": f"Bearer {API_KEY}",
            "Content-Type": "application/json"
        },
        json={
            "model": MODEL,
            "messages": messages,
            "tools": tools,
            "tool_choice": "auto",  # 自动判断是否调用函数
            "enable_thinking": False
        }
    )

    if response.status_code != 200:
        print("请求失败:", response.text)
        return

    res_json = response.json()
    print("AI 响应:", json.dumps(res_json, indent=2, ensure_ascii=False))
    

# --------------------- 运行示例 ---------------------
if __name__ == "__main__":
    chat_with_function_call("北京天气怎么样?")

执行结果#

{
  "choices": [
    {
      "message": {
        "content": "",
        "role": "assistant",
        "tool_calls": [
          {
            "function": {
              "arguments": "{\"city\": \"北京\"}",
              "name": "get_weather"
            },
            "id": "call_af342ca644b7461082e7cef5",
            "index": 0,
            "type": "function"
          }
        ]
      },
      "finish_reason": "tool_calls",
      "index": 0,
      "logprobs": null
    }
  ],
  "object": "chat.completion",
  "usage": {
    "prompt_tokens": 286,
    "completion_tokens": 25,
    "total_tokens": 311,
    "completion_tokens_details": {
      "text_tokens": 25
    },
    "prompt_tokens_details": {
      "text_tokens": 286
    }
  },
  "created": 1778317438,
  "system_fingerprint": null,
  "model": "qwen3.6-plus",
  "id": "chatcmpl-be66386f-c634-92eb-98e2-020fa91563d9"
}
修改于 2026-05-12 07:47:57
上一页
向量嵌入
下一页
图片分析
Built with