-
Notifications
You must be signed in to change notification settings - Fork 139
/
chatCallback.ts
32 lines (27 loc) · 952 Bytes
/
chatCallback.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
import "dotenv/config.js";
import { createConsoleReader } from "examples/helpers/io.js";
import { BaseMessage, Role } from "bee-agent-framework/llms/primitives/message";
import { OllamaChatLLM } from "bee-agent-framework/adapters/ollama/chat";
const llm = new OllamaChatLLM();
const reader = createConsoleReader();
for await (const { prompt } of reader) {
const response = await llm
.generate(
[
BaseMessage.of({
role: Role.USER,
text: prompt,
}),
],
{},
)
.observe((emitter) =>
emitter.match("*", (data, event) => {
reader.write(`LLM 🤖 (event: ${event.name})`, JSON.stringify(data));
// if you want to close the stream prematurely, just uncomment the following line
// callbacks.abort()
}),
);
reader.write(`LLM 🤖 (txt) : `, response.getTextContent());
reader.write(`LLM 🤖 (raw) : `, JSON.stringify(response.finalResult));
}