SiliconFlow
API手册

创建对话请求(Anthropic)

根据给定的聊天对话创建模型响应。

POST
/messages
AuthorizationBearer <token>required

添加 Header 'Authorization: Bearer {账户 API Key}' 进行鉴权

In: header

modelstringrequired

对应的模型名称。为更好地提升服务质量,我们会对本服务提供的模型进行定期变更,包括但不限于模型上下线和模型服务能力的调整。在可行的情况下,我们会通过公告或消息推送等适当方式通知您此类变更。完整可用模型列表请查看 对话模型

Example"deepseek-ai/DeepSeek-V4-Flash"
messagesarray<object>required

对话消息列表

systemstring | array<Text>

系统提示词。 系统提示词是一种为 LLM 提供上下文和指令的方式,例如指定特定的目标或角色。

stop_sequencesStop Sequences

将导致模型停止生成的自定义文本序列。 我们的模型通常会在自然完成其回合时停止,这将导致响应的 stop_reason"end_turn"。 如果你希望模型在遇到自定义文本字符串时停止生成,可以使用 stop_sequences 参数。如果模型遇到其中一个自定义序列,响应的 stop_reason 值将为 "stop_sequence",且响应的 stop_sequence 值将包含匹配的停止序列。

streamboolean

如果设置,token 将以 SSE(Server-Sent Events)的形式流式输出。流式传输通常以 data: [DONE] 结束

Value infalse | true
Exampletrue
max_tokensintegerrequired

要生成的最大 token 数量,不包含思维链部分。建议不要将 max_tokens 设置为窗口上限,为输入和系统开销预留约 10k token 的缓冲区。

temperaturenumber

使用的采样温度,取值范围在 0 到 2 之间。较高的值(如 0.8)会使输出更加随机,而较低的值(如 0.2)会使其更加集中和确定。

Formatfloat
Rangevalue <= 2
Example0.7
top_pnumber

一种替代温度采样的方法,称为核采样(nucleus sampling),模型会考虑具有 top_p 概率质量的 token 的结果。因此 0.1 表示仅考虑构成前 10% 概率质量的 token。 我们通常建议调整此参数或温度,但不要同时调整两者。

Formatfloat
Rangevalue <= 1
Example0.7
top_knumber
Formatfloat
Rangevalue <= 100
Example50
toolsarray<object>

每个工具定义包括:

  • name:工具名称。

  • description:工具的描述,可选但强烈建议提供。

  • input_schema:模型在 tool_use 输出内容块中将生成的工具 input 形状的 JSON schema

tool_choiceAuto | Tool | None

模型应如何使用提供的工具。模型可以使用特定工具、任何可用工具、自行决定,或完全不使用工具。

模型将自动决定是否使用工具。

disable_parallel_tool_useDisable Parallel Tool Use

是否禁用并行工具使用。 默认值为 false。如果设置为 true,模型将最多输出一个工具使用。

typeTyperequired
Value in"auto"

模型将使用通过 tool_choice.name 指定的工具。

disable_parallel_tool_useDisable Parallel Tool Use

是否禁用并行工具使用。 默认值为 false。如果设置为 true,模型将恰好输出一个工具使用。

nameNamerequired

要使用的工具名称。

typeTyperequired
Value in"tool"

模型将不被允许使用工具。

typeTyperequired
Value in"none"
thinkingThinkingDisabled | ThinkingEnabled

思维链控制参数。用于开启或关闭模型的思维链(thinking)功能。

关闭思维链。

typestringrequired

关闭思维链。

Value in"disabled"

开启思维链,并通过 budget_tokens 控制思维链长度。

typestringrequired

开启思维链。

Value in"enabled"
budget_tokensintegerrequired

思维链的 token 预算,控制思维链的长度。

Example1024
modelstringrequired

对应的模型名称。为更好地提升服务质量,我们会对本服务提供的模型进行定期变更,包括但不限于模型上下线和模型服务能力的调整。在可行的情况下,我们会通过公告或消息推送等适当方式通知您此类变更。完整可用视觉输入模型列表请查看模型广场

Example"moonshotai/Kimi-K2.7-Code"
messagesarray<object>required

对话消息列表

systemstring | array<Text>

系统提示词。 系统提示词是一种为 LLM 提供上下文和指令的方式,例如指定特定的目标或角色。

stop_sequencesStop Sequences

将导致模型停止生成的自定义文本序列。 我们的模型通常会在自然完成其回合时停止,这将导致响应的 stop_reason"end_turn"。 如果你希望模型在遇到自定义文本字符串时停止生成,可以使用 stop_sequences 参数。如果模型遇到其中一个自定义序列,响应的 stop_reason 值将为 "stop_sequence",且响应的 stop_sequence 值将包含匹配的停止序列。

streamboolean

如果设置,token 将以 SSE(Server-Sent Events)的形式流式输出。流式传输通常以 data: [DONE] 结束

Value infalse | true
Exampletrue
max_tokensintegerrequired

要生成的最大 token 数量,不包含思维链部分。建议不要将 max_tokens 设置为窗口上限,为输入和系统开销预留约 10k token 的缓冲区。

temperaturenumber

使用的采样温度,取值范围在 0 到 2 之间。较高的值(如 0.8)会使输出更加随机,而较低的值(如 0.2)会使其更加集中和确定。

Formatfloat
Rangevalue <= 2
Example0.7
top_pnumber

一种替代温度采样的方法,称为核采样(nucleus sampling),模型会考虑具有 top_p 概率质量的 token 的结果。因此 0.1 表示仅考虑构成前 10% 概率质量的 token。 我们通常建议调整此参数或温度,但不要同时调整两者。

Formatfloat
Rangevalue <= 1
Example0.7
top_knumber
Formatfloat
Rangevalue <= 100
Example50
toolsarray<object>

每个工具定义包括:

  • name:工具名称。

  • description:工具的描述,可选但强烈建议提供。

  • input_schema:模型在 tool_use 输出内容块中将生成的工具 input 形状的 JSON schema

tool_choiceAuto | Tool | None

模型应如何使用提供的工具。模型可以使用特定工具、任何可用工具、自行决定,或完全不使用工具。

模型将自动决定是否使用工具。

disable_parallel_tool_useDisable Parallel Tool Use

是否禁用并行工具使用。 默认值为 false。如果设置为 true,模型将最多输出一个工具使用。

typeTyperequired
Value in"auto"

模型将使用通过 tool_choice.name 指定的工具。

disable_parallel_tool_useDisable Parallel Tool Use

是否禁用并行工具使用。 默认值为 false。如果设置为 true,模型将恰好输出一个工具使用。

nameNamerequired

要使用的工具名称。

typeTyperequired
Value in"tool"

模型将不被允许使用工具。

typeTyperequired
Value in"none"
thinkingThinkingDisabled | ThinkingEnabled

思维链控制参数。用于开启或关闭模型的思维链(thinking)功能。

关闭思维链。

typestringrequired

关闭思维链。

Value in"disabled"

开启思维链,并通过 budget_tokens 控制思维链长度。

typestringrequired

开启思维链。

Value in"enabled"
budget_tokensintegerrequired

思维钾的 token 预算,控制思维链的长度。

Example1024

Response Body

模型响应。响应头中包含 x-siliconcloud-trace-id 字段,作为请求的唯一追踪标识,便于日志查询和问题排查。

TypeScript Definitions

Use the response body type in TypeScript.

idstring
typeType

对象类型。

对于 Messages,此值始终为 "message"

Default"message"
Value in"message"
roleRole

生成消息的对话角色。

此值始终为 "assistant"

Default"assistant"
Value in"assistant"
contentContent

模型生成的内容。 这是一个内容块数组,每个内容块都有一个决定其结构的 type。 示例:

[{"type": "text", "text": "Hi"}]

如果请求输入的 messagesassistant 角色结尾,那么响应的 content 将直接延续 该最后一条消息。您可以利用这一点来约束 模型的输出。 例如,如果输入的 messages 为:

[
  {"role": "user", "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"},
  {"role": "assistant", "content": "The best answer is ("}
]

那么响应的 content 可能为:

[{"type": "text", "text": "B)"}]
modelModel

处理请求的模型。

stop_reasonStop Reason

我们停止生成的原因。 此值可能是以下之一:

  • "end_turn": 模型到达了一个自然的停止点,或者您提供的自定义 stop_sequences was generated
  • "max_tokens": 我们超出了请求的 max_tokens or the model's maximum
  • "tool_use": 模型调用了一个或多个工具
  • "refusal": 流式分类器介入处理 潜在的政策违规 在非流式模式下,此值始终为非 null。在 流式模式下,它在 message_start 事件中为 null, 其他情况下为非 null。
stop_sequenceStop Sequence

如果存在,则生成的是哪个自定义停止序列。 如果您的一个自定义停止序列被生成,此值将为非 null 字符串。 被生成。

usageUsage

curl --request POST \
  --url https://api.siliconflow.cn/v1/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "deepseek-ai/DeepSeek-V4-Flash",
    "messages": [
      {"role": "user", "content": "你好,请介绍一下你自己"}
    ],
    "max_tokens": 4096
  }'
import requests

url = "https://api.siliconflow.cn/v1/messages"
payload = {
    "model": "deepseek-ai/DeepSeek-V4-Flash",
    "messages": [
        {"role": "user", "content": "你好,请介绍一下你自己"}
    ],
    "max_tokens": 4096
}
headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
curl --request POST \
  --url https://api.siliconflow.cn/v1/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ***" \
  -d '{
    "model": "moonshotai/Kimi-K2.7-Code",
    "messages": [
      {
        "role": "user",
        "content": [
          {"type": "text", "text": "描述这张图片"},
          {"type": "image", "source": {"type": "url", "url": "https://example.com/image.jpg"}}
        ]
      }
    ],
    "max_tokens": 4096
  }'
import requests

url = "https://api.siliconflow.cn/v1/messages"
payload = {
    "model": "deepseek-ai/DeepSeek-V4-Flash",
    "messages": [
        {"role": "user", "content": "波士顿今天天气怎么样?"},
        {"role": "assistant", "content": "波士顿今天晴,气温 20°C。"},
        {"role": "user", "content": "那纽约呢?"}
    ],
    "max_tokens": 4096,
    "stream": True,
    "tools": [
        {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "input_schema": {
                "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": {"type": "auto"}
}
headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers, stream=True)
for line in response.iter_lines():
    if line:

{
  "content": [
    {
      "type": "thinking",
      "thinking": "...",
      "signature": "tvshsltrjs"
    },
    {
      "text": "Hello! I'm GLM, trained by Z.ai. How can I assist you today? Whether you have questions or just want to chat, I'm happy to help.",
      "type": "text"
    }
  ],
  "id": "msg_T15jjp718fACotrwiLp3KwVu",
  "model": "deepseek-ai/DeepSeek-V4-Flash",
  "role": "assistant",
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "type": "message",
  "usage": {
    "input_tokens": 6,
    "output_tokens": 215
  }
}

{
  "code": 20012,
  "message": "string",
  "data": "string"
}
"Invalid token"
"Forbidden"
"404 page not found"
{
  "message": "Request was rejected due to rate limiting. If you want more, please contact contact@siliconflow.cn. Details:TPM limit reached.",
  "data": "string"
}
{
  "code": 50505,
  "message": "Model service overloaded. Please try again later.",
  "data": "string"
}
"string"
创建对话请求(Anthropic)