Chat
The Chat API is used for conversational interactions with AI models.
Endpoint
POST https://ai-tokenhub.com/v1/chat/completionsRequest Headers
| Name | Type | Required | Description |
|---|---|---|---|
| Authorization | string | Yes | Bearer YOUR_API_KEY |
| Content-Type | string | Yes | application/json |
Request Body
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| model | string | Yes | - | Model identifier |
| messages | array | Yes | - | Array of conversation messages |
| temperature | number | No | 0.7 | Generation temperature (0-2) |
| max_tokens | integer | No | - | Maximum tokens to generate |
| stream | boolean | No | false | Enable streaming |
| stop | array/string | No | - | Stop sequences |
| user | string | No | - | User identifier |
Message Roles
system: System role for setting AI behavioruser: User role for user inputassistant: Assistant role for AI responses
Request Example
bash
curl https://ai-tokenhub.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "Hello, how are you?"}
],
"temperature": 0.7,
"max_tokens": 100
}'Response Example
json
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1677858242,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! I'm doing well, thank you for asking."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 20,
"total_tokens": 30
}
}Streaming Response
Enable stream: true for Server-Sent Events format.
Error Codes
| Code | Description |
|---|---|
| 400 | Invalid request parameters |
| 401 | Invalid API Key |
| 403 | No permission to access this model |
| 429 | Rate limit exceeded |
| 500 | Internal server error |
| 503 | Service unavailable |
Use Cases
- Customer Service: Automated responses
- Content Creation: Writing assistance
- Code Assistant: Programming help
- Education: Tutoring and feedback