Chat Completions
The standard chat completions endpoint for models from OpenAI, Anthropic, Google, and other major providers. Supports multi-turn conversations, system prompts, streaming output, and parameter tuning. This page covers the request format, response structure, common parameters, and error handling.
API Details
- Endpoint:
POST /v1/chat/completions - Authentication:
Authorization: Bearer sk-xxx - Protocol: OpenAI-compatible format
Basic Usage
curl https://api.onemux.net/v1/chat/completions \
-H "Authorization: Bearer $ONEMUX_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-6.1-sol",
"messages": [{"role": "user", "content": "Hello, OneMux"}]
}'Example Response
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"model": "gpt-6.1-sol",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! I'm the OneMux AI assistant..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 15,
"total_tokens": 25
}
}Multi-Turn Conversations
The messages array stores the conversation history in chronological order:
response = client.chat.completions.create(
model="gpt-6.1-sol",
messages=[
{"role": "user", "content": "My name is Xiaoming"},
{"role": "assistant", "content": "Hello, Xiaoming!"},
{"role": "user", "content": "What is my name?"}
]
)
# AI 会回答:你叫小明System Prompts
Use the system role to define how the AI should behave:
messages = [
{"role": "system", "content": "You are a professional Python engineer. Keep your answers concise and professional"},
{"role": "user", "content": "How do I read a file?"}
]Common Parameters
| Parameter | Description | Recommended Value |
|---|---|---|
| temperature | Creativity (0-2) | 0.7 (default), 1.2 for creative writing, 0.3 for code generation |
| max_tokens | Maximum output length | Set as needed to prevent overly verbose responses |
| top_p | Nucleus sampling (0-1) | 0.9 (default) |
| stream | Streaming output | true (recommended for a better experience) |