-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstream.ts
60 lines (53 loc) · 1.88 KB
/
stream.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
50
51
52
53
54
55
56
57
58
59
60
import { LLMGateway } from "../src/index";
import { EnumLLMProvider } from "../src/types";
const llmGatewayTest = async () => {
// anthropic
const anthropicGateway = new LLMGateway({
provider: EnumLLMProvider.ANTHROPIC, // or ANTHROPIC, AZUREOPENAI
apiKey: process.env['ANTHROPIC_API_KEY'],
});
const anthropicStream = await anthropicGateway.chatCompletionStream({
messages: [{ role: 'user', content: 'Write a one sentence story about a cat.' }],
model: 'claude-3-5-sonnet-latest',
temperature: 0.7,
max_tokens: 800
});
for await (const chunk of anthropicStream) {
if(chunk.type === 'content_block_start') {
console.log(chunk.content_block.text);
}
if(chunk.type === 'message_start') {
console.log(chunk.message.content);
}
if(chunk.type === 'content_block_delta') {
console.log(chunk.delta.text);
}
if(chunk.type === 'content_block_stop') {
console.log('content_block_stop');
}
if(chunk.type === 'message_delta') {
console.log(chunk.delta);
}
if(chunk.type === 'message_stop') {
console.log('message_stop');
}
}
// OPEN AI
const openAIGateway = new LLMGateway({
provider: EnumLLMProvider.OPENAI, // or ANTHROPIC, AZUREOPENAI
apiKey: process.env['OPENAI_API_KEY'],
});
const openAIStream = await openAIGateway.chatCompletionStream({
messages: [{ role: 'user', content: 'Write a one sentence story about a cat.' }],
model: 'gpt-4o-mini',
temperature: 0.7,
});
for await (const chunk of openAIStream) {
if(chunk.choices[0].finish_reason === 'stop') {
console.log('message_stop');
} else {
console.log(chunk.choices[0].delta.content);
}
}
}
llmGatewayTest()