工具市場
資料查詢

Chat Completions

Chat Completions

呼叫測試
價格
0.005 USDT / 請求
狀態
可呼叫
官方參考價
0.005000 USDT / 請求
呼叫方式
REST · MCP

呼叫測試

點擊執行後才會提交呼叫,請先檢查參數和費用上限。

輸入參數JSON

顯示起始價格。請先取得目前參數的報價;完成後按實際用量結算並退回差額。

登入

測試會使用帳戶餘額,並受所選金鑰預算限制。費用上限僅約束本次 API 呼叫,不包含後續呼叫或上游週期任務。

執行結果

執行後,在這裡查看狀態、費用和回傳內容。

API 文件

REST

先設定 ASALE_API_KEY,並替換範例中的冪等鍵。提交後用回應的 run_id 查詢,狀態為 succeeded 時讀取 result。範例金額僅供參考,請用 POST /v1/tool-quotes 依實際參數報價。

curl --fail-with-body https://gw.asale.ai/v1/tool-runs \
  -H "Authorization: Bearer $ASALE_API_KEY" \
  -H 'Idempotency-Key: <UNIQUE_REQUEST_ID>' \
  -H 'Content-Type: application/json' \
  -d '{
  "tool": "chat_completions_31e7e8b6",
  "input": {
    "body": {
      "messages": [
        {
          "content": "",
          "role": "system"
        }
      ],
      "model": ""
    }
  },
  "max_cost_micros": 5000
}'
curl --fail-with-body 'https://gw.asale.ai/v1/tool-runs/<RUN_ID>' -H "Authorization: Bearer $ASALE_API_KEY"

在模型對話中呼叫

宣告名為 chat_completions_31e7e8b6 的函式。模型只回傳呼叫請求,由你的伺服器執行並回傳結果。請將 <MODEL_ID> 換成目前有供給且支援函式呼叫的模型 ID。

模型請求使用精簡的參數定義。選填欄位與完整限制請見下方的完整輸入結構。直接呼叫 REST 時只傳 input,不傳 Schema。

查看工具呼叫指南
共用執行函式(Node.js)

在伺服器上設定 ASALE_API_KEY 和整數 ASALE_MAX_TOOL_COST_MICROS(單次呼叫的 micro-USDT 上限)。請先確認報價。提交逾時後,請用原請求與原冪等鍵重試,不要建立新的執行。

async function runTool(name, input, idempotencyKey) {
  if (name !== "chat_completions_31e7e8b6") throw new Error("Unexpected tool: " + name);
  const maxCost = process.env.ASALE_MAX_TOOL_COST_MICROS;
  if (!maxCost || !/^\d+$/.test(maxCost) || !Number.isSafeInteger(Number(maxCost))) {
    throw new Error("Set ASALE_MAX_TOOL_COST_MICROS to your per-call budget");
  }
  const headers = { Authorization: "Bearer " + process.env.ASALE_API_KEY };
  const run = await fetch("https://gw.asale.ai/v1/tool-runs", {
    method: "POST",
    headers: { ...headers, "Content-Type": "application/json", "Idempotency-Key": idempotencyKey },
    body: JSON.stringify({ tool: name, input, max_cost_micros: Number(maxCost) }),
  });
  if (!run.ok) throw new Error(await run.text());
  let state = await run.json();
  const url = "https://gw.asale.ai/v1/tool-runs/" + state.run_id;
  for (let attempt = 0; attempt < 30; attempt++) {
    if (state.status === "succeeded") return state.result;
    if (!["queued", "running", "waiting", "settling"].includes(state.status)) break;
    await new Promise((resolve) => setTimeout(resolve, 2000));
    const poll = await fetch(url, { headers });
    if (!poll.ok) throw new Error("Check existing run at " + url);
    state = await poll.json();
  }
  if (state.status === "succeeded") return state.result;
  throw new Error("Tool run " + state.status + ": " + url);
}
OpenAI · Chat Completions
curl --fail-with-body https://gw.asale.ai/v1/chat/completions \
  -H "Authorization: Bearer $ASALE_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
  "model": "<MODEL_ID>",
  "messages": [
    {
      "role": "user",
      "content": "請使用 chat_completions_31e7e8b6,依下列參數執行並摘要結果:{\"body\":{\"messages\":[{\"content\":\"\",\"role\":\"system\"}],\"model\":\"\"}}"
    }
  ],
  "max_tokens": 1024,
  "stream": false,
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "chat_completions_31e7e8b6",
        "description": "Chat Completions",
        "parameters": {
          "type": "object",
          "properties": {
            "body": {
              "$ref": "#/$defs/ChatCompletionRequest"
            }
          },
          "required": [
            "body"
          ],
          "additionalProperties": false,
          "$defs": {
            "ChatCompletionRequest": {
              "description": "Request for the chat completions endpoint. Note that all parameters except for `model`, `stream`, and `response_format` are ignored.",
              "properties": {
                "frequency_penalty": {
                  "anyOf": [
                    {
                      "type": "number"
                    },
                    {
                      "type": "null"
                    }
                  ],
                  "description": "The frequency penalty to use for chat completions. Unsupported.",
                  "title": "Frequency Penalty"
                },
                "max_tokens": {
                  "anyOf": [
                    {
                      "type": "integer"
                    },
                    {
                      "type": "null"
                    }
                  ],
                  "description": "The maximum number of tokens to generate. Unsupported.",
                  "title": "Max Tokens"
                },
                "messages": {
                  "description": "The messages to use for chat completions.",
                  "items": {
                    "$ref": "#/$defs/ChatMessage"
                  },
                  "title": "Messages",
                  "type": "array"
                },
                "model": {
                  "description": "The model to use for chat completions.",
                  "title": "Model",
                  "type": "string"
                },
                "n": {
                  "anyOf": [
                    {
                      "type": "integer"
                    },
                    {
                      "type": "null"
                    }
                  ],
                  "description": "The number of chat completions to generate. Unsupported.",
                  "title": "N"
                },
                "presence_penalty": {
                  "anyOf": [
                    {
                      "type": "number"
                    },
                    {
                      "type": "null"
                    }
                  ],
                  "description": "The presence penalty to use for chat completions. Unsupported.",
                  "title": "Presence Penalty"
                },
                "previous_interaction_id": {
                  "anyOf": [
                    {
                      "type": "string"
                    },
                    {
                      "type": "null"
                    }
                  ],
                  "description": "Interaction ID from a previous chat completion to use as context.",
                  "title": "Previous Interaction Id"
                },
                "response_format": {
                  "anyOf": [
                    {
                      "$ref": "#/$defs/openai__types__shared_params__response_format_text__ResponseFormatText"
                    },
                    {
                      "$ref": "#/$defs/ResponseFormatJSONSchema"
                    },
                    {
                      "$ref": "#/$defs/openai__types__shared_params__response_format_json_object__ResponseFormatJSONObject"
                    },
                    {
                      "type": "null"
                    }
                  ],
                  "description": "The response format to use for chat completions. OpenAI compatible.",
                  "title": "Response Format"
                },
                "stream": {
                  "anyOf": [
                    {
                      "type": "boolean"
                    },
                    {
                      "type": "null"
                    }
                  ],
                  "description": "Whether to stream the chat completions.",
                  "title": "Stream"
                },
                "temperature": {
                  "anyOf": [
                    {
                      "type": "number"
                    },
                    {
                      "type": "null"
                    }
                  ],
                  "description": "The temperature to use for chat completions. Unsupported.",
                  "title": "Temperature"
                },
                "top_p": {
                  "anyOf": [
                    {
                      "type": "number"
                    },
                    {
                      "type": "null"
                    }
                  ],
                  "description": "The top p to use for chat completions. Unsupported.",
                  "title": "Top P"
                }
              },
              "required": [
                "model",
                "messages"
              ],
              "title": "ChatCompletionRequest",
              "type": "object"
            },
            "ChatMessage": {
              "description": "Chat message for OpenAI API.",
              "properties": {
                "content": {
                  "description": "The content of the chat message.",
                  "title": "Content",
                  "type": "string"
                },
                "name": {
                  "anyOf": [
                    {
                      "type": "string"
                    },
                    {
                      "type": "null"
                    }
                  ],
                  "description": "An optional name for the participant. Provides the model information to differentiate between participants of the same role.",
                  "title": "Name"
                },
                "role": {
                  "description": "The role of the chat message.",
                  "enum": [
                    "system",
                    "user",
                    "assistant"
                  ],
                  "title": "Role",
                  "type": "string"
                }
              },
              "required": [
                "role",
                "content"
              ],
              "title": "ChatMessage",
              "type": "object"
            },
            "openai__types__shared_params__response_format_text__ResponseFormatText": {
              "description": "Default response format. Used to generate text responses.",
              "properties": {
                "type": {
                  "const": "text",
                  "title": "Type",
                  "type": "string"
                }
              },
              "required": [
                "type"
              ],
              "title": "ResponseFormatText",
              "type": "object"
            },
            "ResponseFormatJSONSchema": {
              "description": "JSON Schema response format. Used to generate structured JSON responses. Learn more about [Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs).",
              "properties": {
                "json_schema": {
                  "$ref": "#/$defs/JSONSchema"
                },
                "type": {
                  "const": "json_schema",
                  "title": "Type",
                  "type": "string"
                }
              },
              "required": [
                "json_schema",
                "type"
              ],
              "title": "ResponseFormatJSONSchema",
              "type": "object"
            },
            "JSONSchema": {
              "description": "Structured Outputs configuration options, including a JSON Schema.",
              "properties": {
                "description": {
                  "title": "Description",
                  "type": "string"
                },
                "name": {
                  "title": "Name",
                  "type": "string"
                },
                "schema": {
                  "additionalProperties": false,
                  "title": "Schema",
                  "type": "object"
                },
                "strict": {
                  "anyOf": [
                    {
                      "type": "boolean"
                    },
                    {
                      "type": "null"
                    }
                  ],
                  "title": "Strict"
                }
              },
              "required": [
                "name"
              ],
              "title": "JSONSchema",
              "type": "object"
            },
            "openai__types__shared_params__response_format_json_object__ResponseFormatJSONObject": {
              "description": "JSON object response format. An older method of generating JSON responses. Using `json_schema` is recommended for models that support it. Note that the model will not generate JSON without a system or user message instructing it to do so.",
              "properties": {
                "type": {
                  "const": "json_object",
                  "title": "Type",
                  "type": "string"
                }
              },
              "required": [
                "type"
              ],
              "title": "ResponseFormatJSONObject",
              "type": "object"
            }
          }
        }
      }
    }
  ]
}'

將首次請求的 JSON 作為 request,模型回傳的 JSON 作為 response。以共用 runTool 函式執行呼叫,再將更新的 request 送到同一端點。若沒有工具呼叫,直接讀取模型回答。

每收到一輪新的模型回應,用 crypto.randomUUID() 產生並保存 turnId。同一輪重試時重用它。

const assistant = response.choices[0].message;
const calls = assistant.tool_calls ?? [];
if (calls.length) {
  const results = [];
  for (const [index, call] of calls.entries()) {
    const result = await runTool(call.function.name, JSON.parse(call.function.arguments), turnId + ":" + index);
    results.push({ role: "tool", tool_call_id: call.id, content: JSON.stringify(result) });
  }
  request.messages.push(assistant, ...results);
}
OpenAI · Chat Completions API
Anthropic · Messages
curl --fail-with-body https://gw.asale.ai/v1/messages \
  -H "x-api-key: $ASALE_API_KEY" \
  -H 'anthropic-version: 2023-06-01' \
  -H 'Content-Type: application/json' \
  -d '{
  "model": "<MODEL_ID>",
  "messages": [
    {
      "role": "user",
      "content": "請使用 chat_completions_31e7e8b6,依下列參數執行並摘要結果:{\"body\":{\"messages\":[{\"content\":\"\",\"role\":\"system\"}],\"model\":\"\"}}"
    }
  ],
  "max_tokens": 1024,
  "stream": false,
  "tools": [
    {
      "name": "chat_completions_31e7e8b6",
      "description": "Chat Completions",
      "input_schema": {
        "type": "object",
        "properties": {
          "body": {
            "$ref": "#/$defs/ChatCompletionRequest"
          }
        },
        "required": [
          "body"
        ],
        "additionalProperties": false,
        "$defs": {
          "ChatCompletionRequest": {
            "description": "Request for the chat completions endpoint. Note that all parameters except for `model`, `stream`, and `response_format` are ignored.",
            "properties": {
              "frequency_penalty": {
                "anyOf": [
                  {
                    "type": "number"
                  },
                  {
                    "type": "null"
                  }
                ],
                "description": "The frequency penalty to use for chat completions. Unsupported.",
                "title": "Frequency Penalty"
              },
              "max_tokens": {
                "anyOf": [
                  {
                    "type": "integer"
                  },
                  {
                    "type": "null"
                  }
                ],
                "description": "The maximum number of tokens to generate. Unsupported.",
                "title": "Max Tokens"
              },
              "messages": {
                "description": "The messages to use for chat completions.",
                "items": {
                  "$ref": "#/$defs/ChatMessage"
                },
                "title": "Messages",
                "type": "array"
              },
              "model": {
                "description": "The model to use for chat completions.",
                "title": "Model",
                "type": "string"
              },
              "n": {
                "anyOf": [
                  {
                    "type": "integer"
                  },
                  {
                    "type": "null"
                  }
                ],
                "description": "The number of chat completions to generate. Unsupported.",
                "title": "N"
              },
              "presence_penalty": {
                "anyOf": [
                  {
                    "type": "number"
                  },
                  {
                    "type": "null"
                  }
                ],
                "description": "The presence penalty to use for chat completions. Unsupported.",
                "title": "Presence Penalty"
              },
              "previous_interaction_id": {
                "anyOf": [
                  {
                    "type": "string"
                  },
                  {
                    "type": "null"
                  }
                ],
                "description": "Interaction ID from a previous chat completion to use as context.",
                "title": "Previous Interaction Id"
              },
              "response_format": {
                "anyOf": [
                  {
                    "$ref": "#/$defs/openai__types__shared_params__response_format_text__ResponseFormatText"
                  },
                  {
                    "$ref": "#/$defs/ResponseFormatJSONSchema"
                  },
                  {
                    "$ref": "#/$defs/openai__types__shared_params__response_format_json_object__ResponseFormatJSONObject"
                  },
                  {
                    "type": "null"
                  }
                ],
                "description": "The response format to use for chat completions. OpenAI compatible.",
                "title": "Response Format"
              },
              "stream": {
                "anyOf": [
                  {
                    "type": "boolean"
                  },
                  {
                    "type": "null"
                  }
                ],
                "description": "Whether to stream the chat completions.",
                "title": "Stream"
              },
              "temperature": {
                "anyOf": [
                  {
                    "type": "number"
                  },
                  {
                    "type": "null"
                  }
                ],
                "description": "The temperature to use for chat completions. Unsupported.",
                "title": "Temperature"
              },
              "top_p": {
                "anyOf": [
                  {
                    "type": "number"
                  },
                  {
                    "type": "null"
                  }
                ],
                "description": "The top p to use for chat completions. Unsupported.",
                "title": "Top P"
              }
            },
            "required": [
              "model",
              "messages"
            ],
            "title": "ChatCompletionRequest",
            "type": "object"
          },
          "ChatMessage": {
            "description": "Chat message for OpenAI API.",
            "properties": {
              "content": {
                "description": "The content of the chat message.",
                "title": "Content",
                "type": "string"
              },
              "name": {
                "anyOf": [
                  {
                    "type": "string"
                  },
                  {
                    "type": "null"
                  }
                ],
                "description": "An optional name for the participant. Provides the model information to differentiate between participants of the same role.",
                "title": "Name"
              },
              "role": {
                "description": "The role of the chat message.",
                "enum": [
                  "system",
                  "user",
                  "assistant"
                ],
                "title": "Role",
                "type": "string"
              }
            },
            "required": [
              "role",
              "content"
            ],
            "title": "ChatMessage",
            "type": "object"
          },
          "openai__types__shared_params__response_format_text__ResponseFormatText": {
            "description": "Default response format. Used to generate text responses.",
            "properties": {
              "type": {
                "const": "text",
                "title": "Type",
                "type": "string"
              }
            },
            "required": [
              "type"
            ],
            "title": "ResponseFormatText",
            "type": "object"
          },
          "ResponseFormatJSONSchema": {
            "description": "JSON Schema response format. Used to generate structured JSON responses. Learn more about [Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs).",
            "properties": {
              "json_schema": {
                "$ref": "#/$defs/JSONSchema"
              },
              "type": {
                "const": "json_schema",
                "title": "Type",
                "type": "string"
              }
            },
            "required": [
              "json_schema",
              "type"
            ],
            "title": "ResponseFormatJSONSchema",
            "type": "object"
          },
          "JSONSchema": {
            "description": "Structured Outputs configuration options, including a JSON Schema.",
            "properties": {
              "description": {
                "title": "Description",
                "type": "string"
              },
              "name": {
                "title": "Name",
                "type": "string"
              },
              "schema": {
                "additionalProperties": false,
                "title": "Schema",
                "type": "object"
              },
              "strict": {
                "anyOf": [
                  {
                    "type": "boolean"
                  },
                  {
                    "type": "null"
                  }
                ],
                "title": "Strict"
              }
            },
            "required": [
              "name"
            ],
            "title": "JSONSchema",
            "type": "object"
          },
          "openai__types__shared_params__response_format_json_object__ResponseFormatJSONObject": {
            "description": "JSON object response format. An older method of generating JSON responses. Using `json_schema` is recommended for models that support it. Note that the model will not generate JSON without a system or user message instructing it to do so.",
            "properties": {
              "type": {
                "const": "json_object",
                "title": "Type",
                "type": "string"
              }
            },
            "required": [
              "type"
            ],
            "title": "ResponseFormatJSONObject",
            "type": "object"
          }
        }
      }
    }
  ]
}'

將首次請求的 JSON 作為 request,模型回傳的 JSON 作為 response。以共用 runTool 函式執行呼叫,再將更新的 request 送到同一端點。若沒有工具呼叫,直接讀取模型回答。

每收到一輪新的模型回應,用 crypto.randomUUID() 產生並保存 turnId。同一輪重試時重用它。

const calls = response.content.filter((block) => block.type === "tool_use");
if (calls.length) {
  const results = [];
  for (const [index, call] of calls.entries()) {
    const result = await runTool(call.name, call.input, turnId + ":" + index);
    results.push({ type: "tool_result", tool_use_id: call.id, content: JSON.stringify(result) });
  }
  request.messages.push(
    { role: "assistant", content: response.content },
    { role: "user", content: results },
  );
}
Anthropic · Messages API
Gemini · GenerateContent
curl --fail-with-body 'https://gw.asale.ai/v1beta/models/<MODEL_ID>:generateContent' \
  -H "x-goog-api-key: $ASALE_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "text": "請使用 chat_completions_31e7e8b6,依下列參數執行並摘要結果:{\"body\":{\"messages\":[{\"content\":\"\",\"role\":\"system\"}],\"model\":\"\"}}"
        }
      ]
    }
  ],
  "generationConfig": {
    "maxOutputTokens": 1024
  },
  "tools": [
    {
      "functionDeclarations": [
        {
          "name": "chat_completions_31e7e8b6",
          "description": "Chat Completions",
          "parametersJsonSchema": {
            "type": "object",
            "properties": {
              "body": {
                "$ref": "#/$defs/ChatCompletionRequest"
              }
            },
            "required": [
              "body"
            ],
            "additionalProperties": false,
            "$defs": {
              "ChatCompletionRequest": {
                "description": "Request for the chat completions endpoint. Note that all parameters except for `model`, `stream`, and `response_format` are ignored.",
                "properties": {
                  "frequency_penalty": {
                    "anyOf": [
                      {
                        "type": "number"
                      },
                      {
                        "type": "null"
                      }
                    ],
                    "description": "The frequency penalty to use for chat completions. Unsupported.",
                    "title": "Frequency Penalty"
                  },
                  "max_tokens": {
                    "anyOf": [
                      {
                        "type": "integer"
                      },
                      {
                        "type": "null"
                      }
                    ],
                    "description": "The maximum number of tokens to generate. Unsupported.",
                    "title": "Max Tokens"
                  },
                  "messages": {
                    "description": "The messages to use for chat completions.",
                    "items": {
                      "$ref": "#/$defs/ChatMessage"
                    },
                    "title": "Messages",
                    "type": "array"
                  },
                  "model": {
                    "description": "The model to use for chat completions.",
                    "title": "Model",
                    "type": "string"
                  },
                  "n": {
                    "anyOf": [
                      {
                        "type": "integer"
                      },
                      {
                        "type": "null"
                      }
                    ],
                    "description": "The number of chat completions to generate. Unsupported.",
                    "title": "N"
                  },
                  "presence_penalty": {
                    "anyOf": [
                      {
                        "type": "number"
                      },
                      {
                        "type": "null"
                      }
                    ],
                    "description": "The presence penalty to use for chat completions. Unsupported.",
                    "title": "Presence Penalty"
                  },
                  "previous_interaction_id": {
                    "anyOf": [
                      {
                        "type": "string"
                      },
                      {
                        "type": "null"
                      }
                    ],
                    "description": "Interaction ID from a previous chat completion to use as context.",
                    "title": "Previous Interaction Id"
                  },
                  "response_format": {
                    "anyOf": [
                      {
                        "$ref": "#/$defs/openai__types__shared_params__response_format_text__ResponseFormatText"
                      },
                      {
                        "$ref": "#/$defs/ResponseFormatJSONSchema"
                      },
                      {
                        "$ref": "#/$defs/openai__types__shared_params__response_format_json_object__ResponseFormatJSONObject"
                      },
                      {
                        "type": "null"
                      }
                    ],
                    "description": "The response format to use for chat completions. OpenAI compatible.",
                    "title": "Response Format"
                  },
                  "stream": {
                    "anyOf": [
                      {
                        "type": "boolean"
                      },
                      {
                        "type": "null"
                      }
                    ],
                    "description": "Whether to stream the chat completions.",
                    "title": "Stream"
                  },
                  "temperature": {
                    "anyOf": [
                      {
                        "type": "number"
                      },
                      {
                        "type": "null"
                      }
                    ],
                    "description": "The temperature to use for chat completions. Unsupported.",
                    "title": "Temperature"
                  },
                  "top_p": {
                    "anyOf": [
                      {
                        "type": "number"
                      },
                      {
                        "type": "null"
                      }
                    ],
                    "description": "The top p to use for chat completions. Unsupported.",
                    "title": "Top P"
                  }
                },
                "required": [
                  "model",
                  "messages"
                ],
                "title": "ChatCompletionRequest",
                "type": "object"
              },
              "ChatMessage": {
                "description": "Chat message for OpenAI API.",
                "properties": {
                  "content": {
                    "description": "The content of the chat message.",
                    "title": "Content",
                    "type": "string"
                  },
                  "name": {
                    "anyOf": [
                      {
                        "type": "string"
                      },
                      {
                        "type": "null"
                      }
                    ],
                    "description": "An optional name for the participant. Provides the model information to differentiate between participants of the same role.",
                    "title": "Name"
                  },
                  "role": {
                    "description": "The role of the chat message.",
                    "enum": [
                      "system",
                      "user",
                      "assistant"
                    ],
                    "title": "Role",
                    "type": "string"
                  }
                },
                "required": [
                  "role",
                  "content"
                ],
                "title": "ChatMessage",
                "type": "object"
              },
              "openai__types__shared_params__response_format_text__ResponseFormatText": {
                "description": "Default response format. Used to generate text responses.",
                "properties": {
                  "type": {
                    "const": "text",
                    "title": "Type",
                    "type": "string"
                  }
                },
                "required": [
                  "type"
                ],
                "title": "ResponseFormatText",
                "type": "object"
              },
              "ResponseFormatJSONSchema": {
                "description": "JSON Schema response format. Used to generate structured JSON responses. Learn more about [Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs).",
                "properties": {
                  "json_schema": {
                    "$ref": "#/$defs/JSONSchema"
                  },
                  "type": {
                    "const": "json_schema",
                    "title": "Type",
                    "type": "string"
                  }
                },
                "required": [
                  "json_schema",
                  "type"
                ],
                "title": "ResponseFormatJSONSchema",
                "type": "object"
              },
              "JSONSchema": {
                "description": "Structured Outputs configuration options, including a JSON Schema.",
                "properties": {
                  "description": {
                    "title": "Description",
                    "type": "string"
                  },
                  "name": {
                    "title": "Name",
                    "type": "string"
                  },
                  "schema": {
                    "additionalProperties": false,
                    "title": "Schema",
                    "type": "object"
                  },
                  "strict": {
                    "anyOf": [
                      {
                        "type": "boolean"
                      },
                      {
                        "type": "null"
                      }
                    ],
                    "title": "Strict"
                  }
                },
                "required": [
                  "name"
                ],
                "title": "JSONSchema",
                "type": "object"
              },
              "openai__types__shared_params__response_format_json_object__ResponseFormatJSONObject": {
                "description": "JSON object response format. An older method of generating JSON responses. Using `json_schema` is recommended for models that support it. Note that the model will not generate JSON without a system or user message instructing it to do so.",
                "properties": {
                  "type": {
                    "const": "json_object",
                    "title": "Type",
                    "type": "string"
                  }
                },
                "required": [
                  "type"
                ],
                "title": "ResponseFormatJSONObject",
                "type": "object"
              }
            }
          }
        }
      ]
    }
  ]
}'

將首次請求的 JSON 作為 request,模型回傳的 JSON 作為 response。以共用 runTool 函式執行呼叫,再將更新的 request 送到同一端點。若沒有工具呼叫,直接讀取模型回答。

每收到一輪新的模型回應,用 crypto.randomUUID() 產生並保存 turnId。同一輪重試時重用它。

const assistant = response.candidates?.[0]?.content;
const calls = assistant?.parts.filter((part) => part.functionCall) ?? [];
if (calls.length) {
  const parts = [];
  for (const [index, { functionCall: call }] of calls.entries()) {
    const result = await runTool(call.name, call.args ?? {}, turnId + ":" + index);
    parts.push({ functionResponse: {
      name: call.name,
      ...(call.id ? { id: call.id } : {}),
      response: { result },
    }});
  }
  request.contents.push(assistant, { role: "user", parts });
}
Gemini · GenerateContent API
參數必填類型
body是object
完整輸入規範
{
  "$defs": {
    "ChatCompletionRequest": {
      "description": "Request for the chat completions endpoint. Note that all parameters except for `model`, `stream`, and `response_format` are ignored.",
      "properties": {
        "frequency_penalty": {
          "anyOf": [
            {
              "type": "number"
            },
            {
              "type": "null"
            }
          ],
          "description": "The frequency penalty to use for chat completions. Unsupported.",
          "title": "Frequency Penalty"
        },
        "max_tokens": {
          "anyOf": [
            {
              "type": "integer"
            },
            {
              "type": "null"
            }
          ],
          "description": "The maximum number of tokens to generate. Unsupported.",
          "title": "Max Tokens"
        },
        "messages": {
          "description": "The messages to use for chat completions.",
          "items": {
            "$ref": "#/$defs/ChatMessage"
          },
          "title": "Messages",
          "type": "array"
        },
        "model": {
          "description": "The model to use for chat completions.",
          "title": "Model",
          "type": "string"
        },
        "n": {
          "anyOf": [
            {
              "type": "integer"
            },
            {
              "type": "null"
            }
          ],
          "description": "The number of chat completions to generate. Unsupported.",
          "title": "N"
        },
        "presence_penalty": {
          "anyOf": [
            {
              "type": "number"
            },
            {
              "type": "null"
            }
          ],
          "description": "The presence penalty to use for chat completions. Unsupported.",
          "title": "Presence Penalty"
        },
        "previous_interaction_id": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ],
          "description": "Interaction ID from a previous chat completion to use as context.",
          "title": "Previous Interaction Id"
        },
        "response_format": {
          "anyOf": [
            {
              "$ref": "#/$defs/openai__types__shared_params__response_format_text__ResponseFormatText"
            },
            {
              "$ref": "#/$defs/ResponseFormatJSONSchema"
            },
            {
              "$ref": "#/$defs/openai__types__shared_params__response_format_json_object__ResponseFormatJSONObject"
            },
            {
              "type": "null"
            }
          ],
          "description": "The response format to use for chat completions. OpenAI compatible.",
          "title": "Response Format"
        },
        "stream": {
          "anyOf": [
            {
              "type": "boolean"
            },
            {
              "type": "null"
            }
          ],
          "description": "Whether to stream the chat completions.",
          "title": "Stream"
        },
        "temperature": {
          "anyOf": [
            {
              "type": "number"
            },
            {
              "type": "null"
            }
          ],
          "description": "The temperature to use for chat completions. Unsupported.",
          "title": "Temperature"
        },
        "top_p": {
          "anyOf": [
            {
              "type": "number"
            },
            {
              "type": "null"
            }
          ],
          "description": "The top p to use for chat completions. Unsupported.",
          "title": "Top P"
        }
      },
      "required": [
        "model",
        "messages"
      ],
      "title": "ChatCompletionRequest",
      "type": "object"
    },
    "ChatMessage": {
      "description": "Chat message for OpenAI API.",
      "properties": {
        "content": {
          "description": "The content of the chat message.",
          "title": "Content",
          "type": "string"
        },
        "name": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ],
          "description": "An optional name for the participant. Provides the model information to differentiate between participants of the same role.",
          "title": "Name"
        },
        "role": {
          "description": "The role of the chat message.",
          "enum": [
            "system",
            "user",
            "assistant"
          ],
          "title": "Role",
          "type": "string"
        }
      },
      "required": [
        "role",
        "content"
      ],
      "title": "ChatMessage",
      "type": "object"
    },
    "JSONSchema": {
      "description": "Structured Outputs configuration options, including a JSON Schema.",
      "properties": {
        "description": {
          "title": "Description",
          "type": "string"
        },
        "name": {
          "title": "Name",
          "type": "string"
        },
        "schema": {
          "additionalProperties": false,
          "title": "Schema",
          "type": "object"
        },
        "strict": {
          "anyOf": [
            {
              "type": "boolean"
            },
            {
              "type": "null"
            }
          ],
          "title": "Strict"
        }
      },
      "required": [
        "name"
      ],
      "title": "JSONSchema",
      "type": "object"
    },
    "ResponseFormatJSONSchema": {
      "description": "JSON Schema response format. Used to generate structured JSON responses. Learn more about [Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs).",
      "properties": {
        "json_schema": {
          "$ref": "#/$defs/JSONSchema"
        },
        "type": {
          "const": "json_schema",
          "title": "Type",
          "type": "string"
        }
      },
      "required": [
        "json_schema",
        "type"
      ],
      "title": "ResponseFormatJSONSchema",
      "type": "object"
    },
    "openai__types__shared_params__response_format_json_object__ResponseFormatJSONObject": {
      "description": "JSON object response format. An older method of generating JSON responses. Using `json_schema` is recommended for models that support it. Note that the model will not generate JSON without a system or user message instructing it to do so.",
      "properties": {
        "type": {
          "const": "json_object",
          "title": "Type",
          "type": "string"
        }
      },
      "required": [
        "type"
      ],
      "title": "ResponseFormatJSONObject",
      "type": "object"
    },
    "openai__types__shared_params__response_format_text__ResponseFormatText": {
      "description": "Default response format. Used to generate text responses.",
      "properties": {
        "type": {
          "const": "text",
          "title": "Type",
          "type": "string"
        }
      },
      "required": [
        "type"
      ],
      "title": "ResponseFormatText",
      "type": "object"
    }
  },
  "additionalProperties": false,
  "properties": {
    "body": {
      "$ref": "#/$defs/ChatCompletionRequest"
    }
  },
  "required": [
    "body"
  ],
  "type": "object"
}

價格

0.005 USDT / 請求

顯示起始價格。請先取得目前參數的報價;完成後按實際用量結算並退回差額。

測試會使用帳戶餘額,並受所選金鑰預算限制。費用上限僅約束本次 API 呼叫,不包含後續呼叫或上游週期任務。

價格版本 7

資料查詢

Asale 依報價撮合同一能力的可用通道,優先選擇低價供給。參考價採各通道已核實參考價的最高值。只有明確未執行的拒絕才會轉移通道;結果不確定時進入對帳,避免重複收費。

供給通道: 1