@schmitech/chatbot-api 0.4.4 โ 0.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -146
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,124 +1,51 @@
|
|
|
1
|
-
# ๐ค Chatbot API Client
|
|
1
|
+
# ๐ค ORBIT Chatbot API Client
|
|
2
2
|
|
|
3
|
-
A JavaScript
|
|
4
|
-
|
|
5
|
-
---
|
|
3
|
+
A TypeScript/JavaScript client for seamless interaction with the ORBIT server, supporting API key authentication and session tracking.
|
|
6
4
|
|
|
7
5
|
## ๐ฅ Installation
|
|
8
6
|
|
|
9
|
-
### ๐ Local Development (npm link)
|
|
10
|
-
|
|
11
|
-
Use during local development:
|
|
12
|
-
|
|
13
7
|
```bash
|
|
14
|
-
npm
|
|
15
|
-
npm link
|
|
16
|
-
|
|
17
|
-
# In your project directory
|
|
18
|
-
npm link @schmitech/chatbot-api
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
### ๐ Local Directory Install
|
|
22
|
-
|
|
23
|
-
Direct local installation:
|
|
24
|
-
|
|
25
|
-
```bash
|
|
26
|
-
npm run build
|
|
27
|
-
npm install /path/to/qa-chatbot-server/api
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
### ๐ CDN Integration
|
|
31
|
-
|
|
32
|
-
Integrate directly into websites via CDN:
|
|
33
|
-
|
|
34
|
-
```html
|
|
35
|
-
<script type="module">
|
|
36
|
-
import { configureApi, streamChat } from 'https://cdn.jsdelivr.net/npm/@schmitech/chatbot-api/dist/api.mjs';
|
|
37
|
-
|
|
38
|
-
configureApi({
|
|
39
|
-
apiUrl: 'https://your-api-server.com',
|
|
40
|
-
apiKey: 'your-api-key',
|
|
41
|
-
sessionId: 'your-session-id' // Optional
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
async function handleChat() {
|
|
45
|
-
for await (const response of streamChat('Hello', false)) {
|
|
46
|
-
console.log(response.text);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
</script>
|
|
8
|
+
npm install @schmitech/chatbot-api
|
|
50
9
|
```
|
|
51
10
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
## โ๏ธ Usage
|
|
11
|
+
## โ๏ธ Basic Usage
|
|
55
12
|
|
|
56
|
-
###
|
|
13
|
+
### Configuration
|
|
57
14
|
|
|
58
|
-
|
|
15
|
+
First, configure the API client with your server details:
|
|
59
16
|
|
|
60
|
-
```
|
|
17
|
+
```typescript
|
|
61
18
|
import { configureApi, streamChat } from '@schmitech/chatbot-api';
|
|
62
19
|
|
|
63
20
|
configureApi({
|
|
64
21
|
apiUrl: 'https://your-api-server.com',
|
|
65
22
|
apiKey: 'your-api-key',
|
|
66
|
-
sessionId: '
|
|
23
|
+
sessionId: 'optional-session-id' // Optional, for conversation tracking
|
|
67
24
|
});
|
|
68
25
|
```
|
|
69
26
|
|
|
70
|
-
###
|
|
27
|
+
### Streaming Chat Example
|
|
71
28
|
|
|
72
|
-
```
|
|
29
|
+
```typescript
|
|
73
30
|
async function chat() {
|
|
74
|
-
|
|
75
|
-
apiUrl: 'https://your-api-server.com',
|
|
76
|
-
apiKey: 'your-api-key',
|
|
77
|
-
sessionId: 'user_123_session_456' // Optional
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
for await (const response of streamChat('Hello, how can I help?', false)) {
|
|
31
|
+
for await (const response of streamChat('Hello, how can I help?', true)) {
|
|
81
32
|
console.log(response.text);
|
|
82
|
-
if (response.done)
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
chat();
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
### ๐๏ธ Voice-enabled Example
|
|
90
|
-
|
|
91
|
-
```javascript
|
|
92
|
-
async function chatWithVoice() {
|
|
93
|
-
configureApi({
|
|
94
|
-
apiUrl: 'https://your-api-server.com',
|
|
95
|
-
apiKey: 'your-api-key',
|
|
96
|
-
sessionId: 'user_123_session_456' // Optional
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
for await (const response of streamChat('Tell me a joke', true)) {
|
|
100
|
-
if (response.type === 'audio') {
|
|
101
|
-
console.log('Received audio content');
|
|
102
|
-
} else {
|
|
103
|
-
console.log(response.text);
|
|
33
|
+
if (response.done) {
|
|
34
|
+
console.log('Chat complete!');
|
|
104
35
|
}
|
|
105
|
-
if (response.done) console.log('Chat complete!');
|
|
106
36
|
}
|
|
107
37
|
}
|
|
108
|
-
|
|
109
|
-
chatWithVoice();
|
|
110
38
|
```
|
|
111
39
|
|
|
112
|
-
---
|
|
113
|
-
|
|
114
40
|
## โ๏ธ React Integration
|
|
115
41
|
|
|
116
|
-
|
|
42
|
+
Here's how to use the API in a React component:
|
|
117
43
|
|
|
118
|
-
```
|
|
44
|
+
```tsx
|
|
119
45
|
import React, { useState } from 'react';
|
|
120
46
|
import { configureApi, streamChat } from '@schmitech/chatbot-api';
|
|
121
47
|
|
|
48
|
+
// Configure once at app startup
|
|
122
49
|
configureApi({
|
|
123
50
|
apiUrl: 'https://your-api-server.com',
|
|
124
51
|
apiKey: 'your-api-key',
|
|
@@ -126,15 +53,15 @@ configureApi({
|
|
|
126
53
|
});
|
|
127
54
|
|
|
128
55
|
function ChatComponent() {
|
|
129
|
-
const [messages, setMessages] = useState([]);
|
|
56
|
+
const [messages, setMessages] = useState<Array<{ text: string; isUser: boolean }>>([]);
|
|
130
57
|
const [input, setInput] = useState('');
|
|
131
58
|
|
|
132
|
-
const handleSubmit = async (e) => {
|
|
59
|
+
const handleSubmit = async (e: React.FormEvent) => {
|
|
133
60
|
e.preventDefault();
|
|
134
61
|
setMessages(prev => [...prev, { text: input, isUser: true }]);
|
|
135
62
|
|
|
136
63
|
let responseText = '';
|
|
137
|
-
for await (const response of streamChat(input,
|
|
64
|
+
for await (const response of streamChat(input, true)) {
|
|
138
65
|
responseText += response.text;
|
|
139
66
|
setMessages(prev => [...prev, { text: responseText, isUser: false }]);
|
|
140
67
|
if (response.done) break;
|
|
@@ -144,91 +71,96 @@ function ChatComponent() {
|
|
|
144
71
|
|
|
145
72
|
return (
|
|
146
73
|
<form onSubmit={handleSubmit}>
|
|
147
|
-
<input
|
|
74
|
+
<input
|
|
75
|
+
value={input}
|
|
76
|
+
onChange={(e) => setInput(e.target.value)}
|
|
77
|
+
placeholder="Type your message..."
|
|
78
|
+
/>
|
|
148
79
|
<button type="submit">Send</button>
|
|
149
80
|
</form>
|
|
150
81
|
);
|
|
151
82
|
}
|
|
152
|
-
|
|
153
|
-
export default ChatComponent;
|
|
154
83
|
```
|
|
155
84
|
|
|
156
|
-
---
|
|
157
|
-
|
|
158
85
|
## ๐ฑ Mobile Usage
|
|
159
86
|
|
|
160
|
-
###
|
|
87
|
+
### React Native Example
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { configureApi, streamChat } from '@schmitech/chatbot-api';
|
|
161
91
|
|
|
162
|
-
|
|
163
|
-
configureApi({
|
|
164
|
-
apiUrl: 'https://your-api-server.com',
|
|
92
|
+
// Configure once at app startup
|
|
93
|
+
configureApi({
|
|
94
|
+
apiUrl: 'https://your-api-server.com',
|
|
165
95
|
apiKey: 'your-api-key',
|
|
166
96
|
sessionId: 'user_123_session_456' // Optional
|
|
167
97
|
});
|
|
168
98
|
|
|
169
|
-
async function handleChat(message) {
|
|
170
|
-
for await (const response of streamChat(message,
|
|
171
|
-
// Handle response
|
|
99
|
+
async function handleChat(message: string) {
|
|
100
|
+
for await (const response of streamChat(message, true)) {
|
|
101
|
+
// Handle streaming response
|
|
102
|
+
console.log(response.text);
|
|
103
|
+
if (response.done) {
|
|
104
|
+
console.log('Chat complete!');
|
|
105
|
+
}
|
|
172
106
|
}
|
|
173
107
|
}
|
|
174
108
|
```
|
|
175
109
|
|
|
176
|
-
|
|
110
|
+
## ๐ CDN Integration
|
|
177
111
|
|
|
178
|
-
|
|
112
|
+
You can also use the API directly in the browser via CDN:
|
|
179
113
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|-----------|-------------|----------|
|
|
184
|
-
| `apiUrl` | Chatbot API URL | โ
Yes |
|
|
185
|
-
| `apiKey` | API key for authentication | โ
Yes |
|
|
186
|
-
| `sessionId` | Session ID for tracking conversations | โ No |
|
|
187
|
-
|
|
188
|
-
---
|
|
189
|
-
|
|
190
|
-
## ๐ค Publish to npm
|
|
191
|
-
|
|
192
|
-
**Build package:**
|
|
193
|
-
|
|
194
|
-
```bash
|
|
195
|
-
npm run build
|
|
196
|
-
```
|
|
114
|
+
```html
|
|
115
|
+
<script type="module">
|
|
116
|
+
import { configureApi, streamChat } from 'https://cdn.jsdelivr.net/npm/@schmitech/chatbot-api/dist/api.mjs';
|
|
197
117
|
|
|
198
|
-
|
|
118
|
+
configureApi({
|
|
119
|
+
apiUrl: 'https://your-api-server.com',
|
|
120
|
+
apiKey: 'your-api-key',
|
|
121
|
+
sessionId: 'your-session-id' // Optional
|
|
122
|
+
});
|
|
199
123
|
|
|
200
|
-
|
|
201
|
-
|
|
124
|
+
async function handleChat() {
|
|
125
|
+
for await (const response of streamChat('Hello', true)) {
|
|
126
|
+
console.log(response.text);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
</script>
|
|
202
130
|
```
|
|
203
131
|
|
|
204
|
-
|
|
132
|
+
## ๐ API Reference
|
|
205
133
|
|
|
206
|
-
|
|
207
|
-
npm version [patch|minor|major]
|
|
208
|
-
```
|
|
134
|
+
### `configureApi(config)`
|
|
209
135
|
|
|
210
|
-
|
|
136
|
+
Configure the API client with server details.
|
|
211
137
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
138
|
+
| Parameter | Type | Required | Description |
|
|
139
|
+
|-----------|------|----------|-------------|
|
|
140
|
+
| `apiUrl` | string | Yes | Chatbot API server URL |
|
|
141
|
+
| `apiKey` | string | Yes | API key for authentication |
|
|
142
|
+
| `sessionId` | string | No | Session ID for conversation tracking |
|
|
215
143
|
|
|
216
|
-
|
|
144
|
+
### `streamChat(message: string, stream: boolean = true)`
|
|
217
145
|
|
|
218
|
-
|
|
146
|
+
Stream chat responses from the server.
|
|
219
147
|
|
|
220
|
-
|
|
148
|
+
| Parameter | Type | Default | Description |
|
|
149
|
+
|-----------|------|---------|-------------|
|
|
150
|
+
| `message` | string | - | The message to send to the chat |
|
|
151
|
+
| `stream` | boolean | true | Whether to stream the response |
|
|
221
152
|
|
|
222
|
-
|
|
223
|
-
# Test single query
|
|
224
|
-
npm run test-query "your query" "http://your-api-server.com" "your-api-key" ["your-session-id"]
|
|
153
|
+
Returns an AsyncGenerator that yields `StreamResponse` objects:
|
|
225
154
|
|
|
226
|
-
|
|
227
|
-
|
|
155
|
+
```typescript
|
|
156
|
+
interface StreamResponse {
|
|
157
|
+
text: string; // The text content of the response
|
|
158
|
+
done: boolean; // Whether this is the final response
|
|
159
|
+
}
|
|
228
160
|
```
|
|
229
161
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
## ๐ License
|
|
162
|
+
## ๐ Security
|
|
233
163
|
|
|
234
|
-
|
|
164
|
+
- Always use HTTPS for your API URL
|
|
165
|
+
- Keep your API key secure and never expose it in client-side code
|
|
166
|
+
- Consider using environment variables for sensitive configuration
|