Chat Completions (/v1/chat/completions)
/v1/chat/completions is one of the platform's chat endpoints (text in → text out, synchronous request-response, optional streaming) and follows the OpenAI Chat Completions format — if you are already using the OpenAI SDK (or any compatible SDK), just point base_url at the platform address and swap in your platform API key to connect directly, with zero code changes.
The platform offers three chat entry points —
/v1/chat/completions(this page),/v1/responses(see Responses API), and/v1/messages— with equivalent capabilities and identical billing; pick whichever fits your existing SDK, no migration required. The chat model list is whateverGET /v1/modelsreturns; per-model capabilities and selection guidance are in "Model Square" in the Console.
1. Quick Start (Non-Streaming)
curl https://token.yaoshu.cc/v1/chat/completions \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "<model>",
"messages": [
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Introduce yourself in one sentence."}
],
"max_tokens": 512
}'The response uses the standard Chat Completions format: the answer text is in choices[0].message.content, and usage is in usage (prompt_tokens / completion_tokens / total_tokens).
from openai import OpenAI
client = OpenAI(api_key="sk-your-api-key", base_url="https://token.yaoshu.cc/v1")
completion = client.chat.completions.create(
model="<model>",
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Introduce yourself in one sentence."},
],
)
print(completion.choices[0].message.content)2. Streaming (SSE)
With stream: true the response is an SSE stream: each data: {...} line is an incremental chunk, the answer text accumulates frame by frame in choices[0].delta.content, and the stream ends with data: [DONE].
curl https://token.yaoshu.cc/v1/chat/completions \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "<model>",
"messages": [{"role": "user", "content": "Write a short poem about autumn."}],
"stream": true
}'stream = client.chat.completions.create(
model="<model>",
messages=[{"role": "user", "content": "Write a short poem about autumn."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")Three streaming details (relevant to front-end display and billing):
- Reasoning models emit reasoning deltas first (
choices[0].delta.reasoning_content), then the answer text (delta.content) — reading onlycontentgives you the clean answer; readreasoning_contentwhen you need to display the reasoning process. - The final frame carries full usage: the last frame whose
finish_reasonisstopincludesusage(withcached_tokensfor cache hits andreasoning_tokensdetails) — in streaming scenarios you get accurate usage without an extra request. - If the client disconnects mid-stream, you are billed on the final actual usage (see section 5 of the API Integration Overview).
3. Multimodal Input (Images)
Passing an array as content makes the message multimodal (text + images). Images can be a public URL or a Base64 data URL (data:image/<format>;base64,...); choose a vision-capable model (capability labels are shown in Model Square in the Console).
Image format and size requirements:
- Supported formats: BMP, JPEG, PNG, TIFF, WEBP, HEIC (resolutions above 4K support only JPEG / PNG); GIF is not supported — convert GIF to a PNG / JPEG first frame before sending.
- Size: a single image from a public URL must not exceed 20 MB; a Base64 Data URI must not exceed 20 MB in total.
- Dimensions: both width and height must be greater than 10 pixels, and the aspect ratio must not exceed 200:1.
- Public URLs must be anonymously accessible (intranet addresses and authenticated temporary links will fail).
curl https://token.yaoshu.cc/v1/chat/completions \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "<model>",
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}}
]
}]
}'completion = client.chat.completions.create(
model="<model>",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}},
],
}],
)For audio input (speech recognition), see Audio and Video Understanding; for audio output (speech synthesis), see Text to Speech.
4. Common Parameters
| Parameter | Type | Description |
|---|---|---|
model | string | Model name (as returned by GET /v1/models, case-sensitive) |
messages | array | Chat messages (role: system / user / assistant / tool; content is a string or an array of content parts) |
stream | boolean | Streaming switch, defaults to false |
max_tokens | integer | Output limit (some models use max_completion_tokens) |
temperature / top_p / top_k | number | Sampling parameters |
stop | string / string[] | Stop sequences |
frequency_penalty / presence_penalty | number | Frequency / presence penalty |
seed | number | Random seed (for reproducible output) |
n | integer | Number of candidates |
response_format | object | Structured output (e.g. {"type": "json_object"}) |
tools / tool_choice | array / object | Function-calling declarations and selection |
reasoning_effort | string | Reasoning level: low / medium / high (only effective on reasoning models) |
user | string | End-user identifier (used by the platform for risk control and statistics) |
For the complete parameter set, refer to the OpenAI Chat Completions specification; the platform passes parameters through according to the selected model's capabilities, and advanced parameters a model does not support may be ignored by the server or return a parameter error.
Function Calling
Use tools to declare the available functions; when the model decides to call one, the response carries the tool call:
choices[0].finish_reasonistool_calls;- the actual calls are in
choices[0].message.tool_calls[], each entry containingid,type: "function",function.name, andfunction.arguments(a JSON string).
Append the execution result to messages as a {"role": "tool", "tool_call_id": "<id of that call>", "content": "<result>"} message, then send the next request to close the loop (tool_call_id must match the corresponding message.tool_calls[].id).
Answer text and tool calls can coexist: when the model gives a textual note before issuing a tool call, choices[0].message.content and message.tool_calls are present at the same time, so the two are not mutually exclusive — consume both per the standard Chat format (reading only one loses the other half). When streaming, tool-call deltas arrive in choices[0].delta.tool_calls[]; merge them by index and concatenate function.arguments frame by frame.
Conversation context: like the other two chat entry points, this is a stateless model — each request carries the full conversation history (pass prior messages in the messages array); there is no server-side conversation continuation.
5. Billing
Billing follows the same rules as the other chat entry points (see section 5 of the API Integration Overview): you are billed on actual usage from usage (reasoning tokens of reasoning models count toward output usage), platform-side interception and service failures are never charged, and a streaming request dropped by the client mid-way is billed on the final actual usage.
Streaming Failures and Mixed Tool Output
The response stream may still fail after the answer text has been emitted. The gateway immediately sends an SSE data error object (error containing type, code, message) and ends the stream; the client should treat this as a failure and keep the partial text it has already received, rather than assuming completion from HTTP 200. A single output delta can contain both answer text and tool calls — read content and tool_calls separately.
