SiliconFlow
API手册

创建对话请求(Anthropic)

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

POST
/messages
AuthorizationBearer <token>required

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

In: header

modelstringrequired

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

Example"Pro/zai-org/GLM-4.7"
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 数量。

请注意,我们的模型可能会在此最大值之前停止。此参数仅指定要生成的绝对最大 token 数量。

不同模型对此参数有不同的最大值。详情请参见 models

temperaturenumber

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

Formatfloat
Rangevalue <= 2
Example0.7
top_pnumber

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

Formatfloat
Range0.1 <= value <= 1
Example0.7
top_knumber
Formatfloat
Rangevalue <= 50
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"

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": "Pro/zai-org/GLM-4.7",
    "messages": [
      {"role": "system", "content": "你是一个有用的助手"},
      {"role": "user", "content": "你好,请介绍一下你自己"}
    ],
    "stream": true
  }'
import requests
url = "https://api.siliconflow.cn/v1/messages"
payload = {
    "model": "Pro/zai-org/GLM-4.7",
    "messages": [
        {
            "role": "user",
            "content": "What opportunities and challenges will the Chinese large model industry face in 2025?"
        }
    ],
    "stream": True
}
headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
fetch('https://api.siliconflow.cn/v1/messages', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_API_KEY'
  },
  body: JSON.stringify({
    model: 'Pro/zai-org/GLM-4.7',
    messages: [
      {role: 'system', content: '你是一个有用的助手'},
      {role: 'user', content: '你好,请介绍一下你自己'}
    ],
    stream: true
  })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
curl --location 'https://api.siliconflow.cn/v1/messages' \
--header 'x-api-key: YOUR_API_KEY' \
--header 'content-type: application/json' \
--data '{
  "model": "Pro/zai-org/GLM-4.7",
  "tools": [
    {
    "name": "get_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"
      }
      },
      "required": ["location"]
    }
    }
  ],
  "tool_choice": {"type": "any"},
  "messages": [
    {
    "role": "user",
    "content": "What is the weather like in San Francisco?"
    }
  ]
}' 
import requests
url = "https://api.siliconflow.cn/v1/messages"
payload = {
  "model": "Pro/zai-org/GLM-4.7",
  "tools": [
    {
    "name": "get_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"
      }
      },
      "required": ["location"]
    }
    }
  ],
  "tool_choice": {"type": "any"},
  "messages": [
    {
    "role": "user",
    "content": "What is the weather like in San Francisco?"
    }
  ]
}
headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
const url = 'https://api.siliconflow.cn/v1/messages';
const apiKey = 'YOUR_API_KEY';
const requestData = {
  model: "Pro/zai-org/GLM-4.7",
  tools: [
    {
      name: "get_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"
          }
        },
        required: ["location"]
      }
    }
  ],
  tool_choice: { type: "any" },
  messages: [
    {
      role: "user",
      content: "What is the weather like in San Francisco?"
    }
  ]
};
fetch(url, {
  method: 'POST',
  headers: {
    'x-api-key': apiKey,
    'content-type': 'application/json'
  },
  body: JSON.stringify(requestData)
})
  .then(response => {
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    return response.json();
  })
  .then(data => {
    console.log('Response:', data);
  })
  .catch(error => {
    console.error('Error:', error);
  });

{
  "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": "Pro/zai-org/GLM-4.7",
  "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)