-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummarize.ts
45 lines (40 loc) · 1.67 KB
/
summarize.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
import { groqCreateMessage } from "./adapters.ts"
import { type Chat } from "./types.ts"
import { History } from "./storage.ts"
const HALF_EXCERPT = 100
/**
* Use a fast model to summarize a chat for display purposes. Mutate the chat
* directly
*/
async function summarize(chat: Chat): Promise<void> {
const firstMsg = chat.messages[0].content
const abridged = firstMsg.length > HALF_EXCERPT * 2
? firstMsg.slice(0, HALF_EXCERPT) + "..." + firstMsg.slice(-HALF_EXCERPT)
: firstMsg
// TODO: fall back to groq llama and then 4o-mini if cerebras key is missing
const summary = await groqCreateMessage({
chat: {
systemPrompt:
"You are summarizing LLM chats based on excerpts for use in a TUI conversation list. Be concise and accurate. Include details that help identify that chat. Only provide the summary; do not include explanation or followup questions. Do not end with a period.",
messages: [],
createdAt: new Date(),
},
input:
`Please summarize an LLM chat based on the following excerpt from the first message. Use as few words as possible. Ideally 4-6 words, but up to 10. \n\n<excerpt>${abridged}</excerpt>`,
model: "llama-3.3-70b-versatile",
tools: [],
})
chat.summary = summary.content
}
/** Create and save summaries for any chat without one */
export async function genMissingSummaries(history: Chat[]) {
if (!Deno.env.get("GROQ_API_KEY")) {
$.logWarn("Skipping summarization:", "GROQ_API_KEY not found")
return
}
const pb = $.progress("Summarizing...")
await Promise.all(history.filter((chat) => !chat.summary).map(summarize))
History.write(history)
pb.finish()
}