File size: 5,155 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
# Chat Completion API

## API Overview

This API is used to create chat completions, process provided messages, and generate responses. The API supports streaming responses and is compatible with OpenAI format.

## Prerequisites

Before using this API, you need to:

1. **Register**: Execute the register operation to create your instance
2. **Status Check**: Wait until your instance status becomes "online"
3. **Get Instance ID**: Obtain your unique `{instance_id}` from the registration response
4. **API Access**: Use the instance ID to construct the API endpoint: `https://app.secondme.io/api/chat/{instance_id}`

## API Endpoints

```
POST /api/chat/{instance_id}
POST /api/chat/{instance_id}/chat/completions
```

## Path Parameters

| Parameter | Type | Required | Description |
|------|------|------|------|
| `instance_id` | string | Yes | Unique identifier for the model instance, obtained during registration |

## Request Body

| Field | Type | Required | Default | Description |
|------|------|------|------|------|
| `messages` | array | Yes | - | List of messages in the conversation |
| `metadata` | object | No | null | Additional metadata for the request |
| `temperature` | float | No | 0.7 | Controls randomness of the response, value between 0 and 1 |
| `max_tokens` | integer | No | 2000 | Maximum number of tokens to generate |
| `stream` | boolean | No | true | Whether to stream the response |

### messages Field

Each message should contain the following fields:

| Field | Type | Required | Description |
|------|------|------|------|
| `role` | string | Yes | Role of the message sender. Can be 'system', 'user', or 'assistant' |
| `content` | string | Yes | Content of the message |

### metadata Field

| Field | Type | Required | Description |
|------|------|------|------|
| `enable_l0_retrieval` | boolean | No | Whether to enable L0 level retrieval |
| `role_id` | string | No | Role ID to use for this chat |

## Response

- Server-Sent Events (SSE) stream in OpenAI-compatible format
- Each event contains a fragment of the generated response
- The last event is marked as `[DONE]`

### Response Format Example

```
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"lpm-registry-model","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}

data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"lpm-registry-model","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" world!"},"finish_reason":null}]}

data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"lpm-registry-model","system_fingerprint":null,"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]
```

## Request Examples

### cURL

```bash
curl -X POST "https://app.secondme.io/api/chat/{instance_id}" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Hello, please introduce yourself."}
    ],
    "metadata": {
      "enable_l0_retrieval": false,
      "role_id": "default_role"
    },
    "temperature": 0.7,
    "max_tokens": 2000,
    "stream": true
  }'
```

### Python

```python
import http.client
import json

url = "app.secondme.io"
path = "/api/chat/{instance_id}"           

headers = {"Content-Type": "application/json"}
data = {
    "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello, please introduce yourself."}
    ],
    "metadata": {
        "enable_l0_retrieval": False,
        "role_id": "default_role"
    },
    "temperature": 0.7,
    "max_tokens": 2000,
    "stream": True
}

# Prepare the connection
conn = http.client.HTTPSConnection(url)

# Send the POST request
conn.request("POST", path, body=json.dumps(data), headers=headers)

# Get the response
response = conn.getresponse()


# Read the body line by line
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

# Close the connection when done
conn.close()

```

## Error Codes

| Status Code | Description |
|------|------|
| 404 | Instance not found |
| 422 | Invalid request parameters |
| 503 | Instance not connected or unavailable |

## Notes

1. Before using this API, ensure that the instance is registered and connected to the server (status: "online")
2. The instance ID is unique and required for all API calls
3. For streaming responses, the client should be able to handle data in SSE format
4. Roles in the message list should follow the conversation order, typically starting with 'system' or 'user'