-
Notifications
You must be signed in to change notification settings - Fork 6
/
chat.ts
49 lines (44 loc) · 1.03 KB
/
chat.ts
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
import { Client } from '../src/index.js';
import { CHAT_MODEL } from './shared/constants.js';
const client = new Client({
apiKey: process.env.GENAI_API_KEY,
});
{
// Start a conversation
const { conversation_id, results: results1 } = await client.text.chat.create({
model_id: CHAT_MODEL,
messages: [
{
role: 'system',
content: 'Answer yes or no',
},
{
role: 'user',
content: 'Hello, are you a robot?',
},
],
});
console.log(results1[0]);
// Continue the conversation
const { results: results2 } = await client.text.chat.create({
conversation_id,
model_id: CHAT_MODEL,
messages: [
{
role: 'user',
content: 'Are you sure?',
},
],
});
console.log(results2[0]);
}
{
// Stream
const stream = await client.text.chat.create_stream({
model_id: CHAT_MODEL,
messages: [{ role: 'user', content: 'How are you?' }],
});
for await (const chunk of stream) {
console.log(chunk.results?.at(0)?.generated_text);
}
}