Spaces:
Sleeping
Sleeping
File size: 4,658 Bytes
01d5a5d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# Chat API Documentation
## Overview
This API provides chat functionality compatible with OpenAI V1 Chat Completions API, supporting streaming responses for interactive conversations with AI assistants.
## API Details
- **URL**: `/api/kernel2/chat`
- **Method**: POST
- **Description**: Chat interface - Streaming response (compatible with OpenAI V1 API)
- **Access**: Available through local endpoint at `localhost:8002`
## Request Parameters
The request body is compatible with OpenAI Chat Completions API, using JSON format:
```json
{
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, who are you?"},
{"role": "assistant", "content": "I am a helpful assistant."},
{"role": "user", "content": "What can you do for me?"}
],
"metadata": {
"enable_l0_retrieval": true,
"role_id": "uuid-string"
},
"stream": true,
"model": "gpt-3.5-turbo",
"temperature": 0.1,
"max_tokens": 2000
}
```
### Parameter Description
| Parameter | Type | Required | Description |
|------|------|------|------|
| messages | Array | Yes | Standard OpenAI message list containing conversation history |
| metadata | Object | No | Additional parameters for request processing |
| metadata.enable_l0_retrieval | Boolean | No | Whether to enable basic knowledge retrieval |
| metadata.role_id | String | No | System customized role UUID |
| stream | Boolean | No | Whether to return streaming response (default: true) |
| model | String | No | Model identifier (default: configured model) |
| temperature | Float | No | Controls randomness (default: 0.1) |
| max_tokens | Integer | No | Maximum number of tokens to generate (default: 2000) |
## Response Format
The response format is compatible with OpenAI Chat Completions API, using Server-Sent Events (SSE) format for streaming responses:
```json
{
"id": "chatcmpl-123",
"object": "chat.completion.chunk",
"created": 1677652288,
"model": "gpt-3.5-turbo",
"system_fingerprint": "fp_44709d6fcb",
"choices": [
{
"index": 0,
"delta": {"content": "Hello"},
"finish_reason": null
}
]
}
```
### Format of Each Chunk in Streaming Response
| Field | Type | Description |
|------|------|------|
| id | String | Unique identifier for the response |
| object | String | Fixed as "chat.completion.chunk" |
| created | Integer | Timestamp |
| model | String | Model identifier |
| system_fingerprint | String | System fingerprint |
| choices | Array | List of generated results |
| choices[0].index | Integer | Result index, usually 0 |
| choices[0].delta | Object | Incremental content of the current chunk |
| choices[0].delta.content | String | Incremental text content |
| choices[0].finish_reason | String | Reason for completion, null or "stop" |
## Usage Examples
### cURL Request Example
```bash
curl -X POST \
'http://localhost:8002/api/kernel2/chat' \
-H 'Content-Type: application/json' \
-H 'Accept: text/event-stream' \
-d '{
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me about artificial intelligence."}
],
"stream": true
}'
```
### Python Request Example
```python
import json
import http.client
url = "localhost:8002"
path = "/api/kernel2/chat"
headers = {
"Content-Type": "application/json",
"Accept": "text/event-stream"
}
data = {
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me about artificial intelligence."}
],
"stream": True
}
conn = http.client.HTTPConnection(url)
conn.request("POST", path, body=json.dumps(data), headers=headers)
response = conn.getresponse()
for line in response:
if line:
decoded_line = line.decode('utf-8').strip()
if decoded_line == 'data: [DONE]':
break
if decoded_line.startswith('data: '):
try:
json_str = decoded_line[6:]
chunk = json.loads(json_str)
content = chunk['choices'][0]['delta'].get('content', '')
if content:
print(content, end='', flush=True)
except json.JSONDecodeError:
pass
conn.close()
```
## Error Handling
When an error occurs, the API will return standard HTTP error status codes and error details in JSON format:
```json
{
"success": false,
"message": "Error message",
"code": 400
}
```
| Error Code | Description |
|------|------|
| 400 | Bad Request |
| 401 | Unauthorized |
| 404 | Not Found |
| 500 | Internal Server Error |
|