SwissAI API
一个兼容 OpenAI 的 chat completions API。在门户中创建密钥、设置 Bearer 令牌即可开始——现有的 OpenAI SDK 只需更改 base_url 即可使用。
快速上手
三步获得第一个响应:
1. 在开发者控制台的 API Keys 下创建密钥。它仅显示一次——请妥善保存。格式:sk-souheng-…
2. 向 chat 端点发送请求:
curlcurl -N https://api.swiss-ai.one/api/v1/external/chat/completions \
-H "Authorization: Bearer sk-souheng-YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{
"messages": [{ "role": "user", "content": "用瑞士德语打个招呼。" }],
"stream": true
}'
3. 你会收到一个兼容 OpenAI 的 SSE 流(见流式)。
身份验证
每个请求都用你的 API 密钥在 Authorization 头中进行身份验证:
httpAuthorization: Bearer sk-souheng-YOUR_KEY
模型与密钥绑定。密钥使用哪个模型在门户中设置。请求体中的
model 字段会被忽略——可以发送任意值(方便未修改的 OpenAI SDK)。API 密钥仅在门户中管理(创建、列出、撤销)。每个账户最多 20 个有效密钥。
基础 URL
| 用途 | URL |
|---|---|
| API 基址 | https://api.swiss-ai.one |
| Chat Completions | /api/v1/external/chat/completions |
OpenAI SDK base_url | https://api.swiss-ai.one/api/v1/external |
POST/api/v1/external/chat/completions
生成一条 chat 响应。默认开启流式。
请求体
| 字段 | 类型 | 默认 | 说明 |
|---|---|---|---|
messages 必填 | array | — | { "role", "content" } 的列表。角色:system、user、assistant、tool。 |
stream | boolean | true | SSE 流式开/关。 |
temperature | number | 0.7 | 0.0–2.0. |
max_tokens | integer | 4096 | 1–128000. |
tools | array | null | OpenAI 格式的工具定义。 |
tool_choice | string | null | 例如 "auto"。 |
注意:
model 会被接受但忽略(模型由密钥决定)。未知字段会被忽略。流式 (SSE)
当 "stream": true 时,API 以 OpenAI chunk 格式返回 text/event-stream。流以 data: [DONE] 结束。
ssedata: {"id":"chatcmpl-1a2b3c4d","object":"chat.completion.chunk","created":1750000000,"model":"swissai","choices":[{"index":0,"delta":{"content":"Hallo"},"finish_reason":null}]}
data: {"id":"chatcmpl-1a2b3c4d","object":"chat.completion.chunk","created":1750000000,"model":"swissai","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}
data: {"id":"chatcmpl-1a2b3c4d","object":"chat.completion.chunk","created":1750000000,"model":"swissai","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
非流式
当 "stream": false 时,你会收到单个 chat.completion 对象:
json{
"id": "chatcmpl-1a2b3c4d",
"object": "chat.completion",
"created": 1750000000,
"model": "swissai",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "你好!有什么可以帮你的?" },
"finish_reason": "stop"
}
]
}
工具 / 函数调用
以 OpenAI 格式传入工具。模型调用工具时会返回 tool_calls(流式与非流式相同)。把结果作为带有对应 tool_call_id 的 tool 消息发回。
json{
"messages": [{ "role": "user", "content": "苏黎世天气怎么样?" }],
"stream": false,
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "城市的当前天气",
"parameters": {
"type": "object",
"properties": { "city": { "type": "string" } },
"required": ["city"]
}
}
}],
"tool_choice": "auto"
}
Python
requests(流式)
pythonimport json, requests
resp = requests.post(
"https://api.swiss-ai.one/api/v1/external/chat/completions",
headers={"Authorization": "Bearer sk-souheng-YOUR_KEY"},
json={"messages": [{"role": "user", "content": "你好"}], "stream": True},
stream=True,
)
for line in resp.iter_lines():
if not line:
continue
line = line.decode()
if line.startswith("data: "):
data = line[6:]
if data == "[DONE]":
break
delta = json.loads(data)["choices"][0]["delta"]
print(delta.get("content", ""), end="", flush=True)
JavaScript / Node
javascriptconst res = await fetch(
"https://api.swiss-ai.one/api/v1/external/chat/completions",
{
method: "POST",
headers: {
"Authorization": "Bearer sk-souheng-YOUR_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [{ role: "user", content: "你好" }],
stream: true,
}),
}
);
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
for (;;) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop();
for (const line of lines) {
if (!line.startsWith("data: ")) continue;
const data = line.slice(6);
if (data === "[DONE]") { reader.cancel(); break; }
const delta = JSON.parse(data).choices[0].delta;
if (delta.content) process.stdout.write(delta.content);
}
}
OpenAI SDK
现有的 OpenAI SDK 只需设置 base_url 和 api_key 即可使用。model 是 SDK 必填字段,但服务端会忽略它。
pythonfrom openai import OpenAI
client = OpenAI(
api_key="sk-souheng-YOUR_KEY",
base_url="https://api.swiss-ai.one/api/v1/external",
)
stream = client.chat.completions.create(
model="swissai", # 任意值——会被忽略
messages=[{"role": "user", "content": "你好"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
javascriptimport OpenAI from "openai";
const client = new OpenAI({
apiKey: "sk-souheng-YOUR_KEY",
baseURL: "https://api.swiss-ai.one/api/v1/external",
});
const stream = await client.chat.completions.create({
model: "swissai", // 任意值——会被忽略
messages: [{ role: "user", content: "你好" }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
计费与限制
- 请求按 token 从你的余额(wallet)计费。
- 余额和用量可在控制台查看,也可在那里充值。
- 每个账户最多 20 个有效 API 密钥。
错误码
| 状态 | 含义 |
|---|---|
401 | API 密钥缺失、无效或未激活。 |
402 | 余额已用尽——请在控制台充值。 |
404 | 未知路径 / 端点。 |
429 | 超出速率限制——请稍后重试。 |
5xx | 临时的服务器 / 上游问题。 |