Skip to content
This page was translated from the Chinese original, which has changed since — it may be out of date. The Chinese version is authoritative. View the Chinese original →

Chat Completions API ​

The most commonly used conversation API, fully compatible with the OpenAI Chat Completions interface.

POST/v1/chat/completions创建聊天对话
POST https://hub.lurus.cn/v1/chat/completions

Request Parameters ​

Required ​

ParameterTypeDescription
modelstringModel name, such as deepseek-chat or gpt-4o
messagesarrayArray of conversation messages, see the format below

Common Optional Parameters ​

ParameterTypeDefaultDescription
temperaturenumber1.0Randomness (0–2). Use 0.1 for code/math, 0.9 for creative writing
max_tokensinteger—Maximum number of tokens to generate. If unset, the model decides
streambooleanfalseStreaming outputStreamingA streaming response where the AI model returns results token by token rather than waiting for full generation, reducing time to first token. (SSE), see Streaming Responses
top_pnumber1.0Nucleus sampling; usually adjust either this or temperature, not both
stopstring/array—Stop generating when this string is encountered
ninteger1Number of candidate replies to generate at once
userstring—User identifier, used for audit logs

Messages Format ​

json
[
  {"role": "system",    "content": "你是一位专业的技术顾问。"},
  {"role": "user",      "content": "什么是 RESTful API?"},
  {"role": "assistant", "content": "RESTful API 是…"},
  {"role": "user",      "content": "能举个例子吗?"}
]
RolePurpose
systemDefines the AI’s behavior, tone, and constraints. Place it first in the array
userThe user’s input
assistantThe AI’s reply. In multi-turn conversations you need to include past replies

For the full field definitions of the request body (ChatCompletionRequest) and messages (Message), see the Schema Reference.


Basic Example ​


Streaming Responses ​

Set stream: true and the response is returned token by token via Server-Sent Events, which is ideal for chat-style applications.

Streaming Data Format ​

data: {"id":"chatcmpl-abc","choices":[{"delta":{"content":"落"},"index":0}]}
data: {"id":"chatcmpl-abc","choices":[{"delta":{"content":"叶"},"index":0}]}
...
data: [DONE]

TIP

The final data: [DONE] indicates the end of the stream, at which point delta.content is empty.


Function Calling ​

Let the AI decide when to call functions you provide, returning structured parameters (Tool CallTool CallWhen an AI model decides during reasoning to call an external tool (such as search or a database query) and returns structured call arguments.). This is useful for scenarios like search, database queries, and calling external APIs.

Workflow ​

Function Calling Workflow
graph LR; A[Your request<br/>with tools definition] --> B[Model returns<br/>finish_reason: tool_calls]; B --> C[You run the function<br/>result returned as role: tool]; C --> D[Model combines the result<br/>to produce the final answer]

Complete Example: Querying the Weather ​

Model Support

Function Calling requires model support. We recommend using gpt-4o, gpt-4o-mini, or claude-3-5-sonnet. The DeepSeek series is also supported, but the parameter format has subtle differences, so verify carefully when testing.


Multimodal Input (Vision) ​

Some models can understand image content; mix text and images together in content.

Models That Support Vision

gpt-4o, gpt-4o-mini, gemini-3-pro-preview, gemini-3-pro-image-preview

Image size limits: URL images must be publicly accessible; for Base64, we recommend no more than 5 MB per image.


Best Practices ​

Writing a System Prompt ​

json
{
  "role": "system",
  "content": "你是一位专业客服。请用友好简洁的语气回答,每条回复不超过 150 字。如遇无法回答的问题,引导用户联系 support@lurus.cn。"
}

A good System Prompt includes: role definition + tone requirements + length limit + boundary handling.

Temperature Setting Reference ​

ScenariotemperatureDescription
Code generation, math computation0.0–0.2Highly deterministic, less randomness
Q&A, summarization, classification0.3–0.6Accurate but with some flexibility
Creative writing, brainstorming0.7–1.0High diversity
Random stories, role-play1.0–1.5Highly creative, may be unstable

Managing Multi-turn Conversations ​

python
conversation = [
    {"role": "system", "content": "你是一位历史老师。"}
]

def chat(user_input: str) -> str:
    conversation.append({"role": "user", "content": user_input})
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=conversation,
        max_tokens=500
    )
    reply = response.choices[0].message.content
    conversation.append({"role": "assistant", "content": reply})
    return reply

print(chat("秦始皇是谁?"))
print(chat("他有哪些重要成就?"))  # 模型能记住上文的"秦始皇"

Context Window Management

Each model has a limited context length (for example, deepseek-chat is 64K tokens). When a conversation grows too long, you can:

  1. Truncate the oldest messages (keep system + the most recent N turns)
  2. Periodically summarize the conversation history to compress token usage

Error Handling ​

Common status codes: 400 malformed request → check the JSON structure and required parameters; 401 invalid/expired key; 403 no access to this model → contact your administrator; 429 rate limit exceeded → retry with exponential backoff; 500/502 upstream model error → retry or switch to a backup model. For the full list of error codes and retry strategies, see Error Handling.


Next Steps

Powered by Lurus Technologies