-
Notifications
You must be signed in to change notification settings - Fork 373
/
ollama.ts
48 lines (47 loc) · 1.28 KB
/
ollama.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
import { OllamaEmbedding } from "llamaindex";
import { Ollama } from "llamaindex/llm/ollama";
(async () => {
const llm = new Ollama({
model: "llama3",
config: {
host: "http://localhost:11434",
},
});
const embedModel = new OllamaEmbedding({ model: "nomic-embed-text" });
{
const response = await llm.chat({
messages: [{ content: "Tell me a joke.", role: "user" }],
});
console.log("Response 1:", response.message.content);
}
{
const response = await llm.complete({ prompt: "How are you?" });
console.log("Response 2:", response.text);
}
{
const response = await llm.chat({
messages: [{ content: "Tell me a joke.", role: "user" }],
stream: true,
});
console.log("Response 3:");
for await (const message of response) {
process.stdout.write(message.delta); // no newline
}
console.log(); // newline
}
{
const response = await llm.complete({
prompt: "How are you?",
stream: true,
});
console.log("Response 4:");
for await (const message of response) {
process.stdout.write(message.text); // no newline
}
console.log(); // newline
}
{
const embedding = await embedModel.getTextEmbedding("Hello world!");
console.log("Embedding:", embedding);
}
})();