跳转到主要内容

推荐 endpoint

最小请求

{
  "model": "gpt-4.1",
  "input": "查询今天上海天气。",
  "tools": [
    {
      "type": "function",
      "name": "get_weather",
      "description": "查询天气",
      "parameters": {
        "type": "object",
        "properties": {
          "city": { "type": "string" }
        },
        "required": ["city"]
      }
    }
  ]
}

cURL 示例

FIRST_RESPONSE=$(curl -s https://mass.apigo.ai/v1/responses \
  -H "Authorization: Bearer $TIDEMIND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4.1",
    "input": "查询今天上海天气。",
    "tools": [
      {
        "type": "function",
        "name": "get_weather",
        "description": "查询天气",
        "parameters": {
          "type": "object",
          "properties": {
            "city": { "type": "string" }
          },
          "required": ["city"],
          "additionalProperties": false
        }
      }
    ]
  }')

CALL_ID=$(echo "$FIRST_RESPONSE" | jq -r '.output[] | select(.type == "function_call") | .call_id')

FINAL_RESPONSE=$(curl -s https://mass.apigo.ai/v1/responses \
  -H "Authorization: Bearer $TIDEMIND_API_KEY" \
  -H "Content-Type: application/json" \
  -d "{
    \"model\": \"gpt-4.1\",
    \"previous_response_id\": \"$(echo "$FIRST_RESPONSE" | jq -r '.id')\",
    \"input\": [
      {
        \"type\": \"function_call_output\",
        \"call_id\": \"$CALL_ID\",
        \"output\": \"{\\\"city\\\":\\\"上海\\\",\\\"condition\\\":\\\"多云\\\",\\\"temperature\\\":24}\"
      }
    ]
  }")

echo "$FINAL_RESPONSE" | jq -r '.output_text'

Python 示例

import json
from openai import OpenAI

client = OpenAI(
    base_url="https://mass.apigo.ai/v1",
    api_key="<TIDEMIND_API_KEY>",
)

tool_response = client.responses.create(
    model="gpt-4.1",
    input="查询今天上海天气。",
    tools=[
        {
            "type": "function",
            "name": "get_weather",
            "description": "查询天气",
            "parameters": {
                "type": "object",
                "properties": {"city": {"type": "string"}},
                "required": ["city"],
                "additionalProperties": False,
            },
        }
    ],
)

tool_call = next(item for item in tool_response.output if item.type == "function_call")
tool_result = {
    "city": "上海",
    "condition": "多云",
    "temperature": 24,
}

final_response = client.responses.create(
    model="gpt-4.1",
    previous_response_id=tool_response.id,
    input=[
        {
            "type": "function_call_output",
            "call_id": tool_call.call_id,
            "output": json.dumps(tool_result, ensure_ascii=False),
        }
    ],
)

print(final_response.output_text)

Node.js 示例

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://mass.apigo.ai/v1",
  apiKey: process.env.TIDEMIND_API_KEY,
});

const toolResponse = await client.responses.create({
  model: "gpt-4.1",
  input: "查询今天上海天气。",
  tools: [
    {
      type: "function",
      name: "get_weather",
      description: "查询天气",
      parameters: {
        type: "object",
        properties: {
          city: { type: "string" }
        },
        required: ["city"],
        additionalProperties: false
      }
    }
  ]
});

const toolCall = toolResponse.output.find((item) => item.type === "function_call");

const finalResponse = await client.responses.create({
  model: "gpt-4.1",
  previous_response_id: toolResponse.id,
  input: [
    {
      type: "function_call_output",
      call_id: toolCall.call_id,
      output: JSON.stringify({
        city: "上海",
        condition: "多云",
        temperature: 24
      })
    }
  ]
});

console.log(finalResponse.output_text);

最佳实践

  • 函数 schema 写清参数类型和必填字段
  • 不要默认只会返回一次工具调用,工具层要支持多轮往返
  • 服务端保留原始 tool call 和 tool result,便于重放与排错