-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path07-summarizer.ts
More file actions
30 lines (24 loc) · 850 Bytes
/
07-summarizer.ts
File metadata and controls
30 lines (24 loc) · 850 Bytes
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
// This example demonstrates how to summarize a large text.
//
// NOTE: This example requires more time to run, as the context size is very
// large.
import fs from "node:fs";
import { OpenAI } from "openai";
const systemPrompt = `Rewrite this text into a very brief summary.`;
// Load a large text
const text = fs.readFileSync("./data/ai_wikipedia.txt", "utf8");
const openai = new OpenAI({
baseURL: "http://localhost:11434/v1",
apiKey: "__not_needed_by_ollama__",
});
const result = await openai.chat.completions.create({
model: "phi3",
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: text },
],
});
const summary = result.choices[0].message.content ?? "";
console.log(`Original text: ${text.length} chars`);
console.log(`Summarized text (${summary.length} chars):`);
console.log(summary);