-
Notifications
You must be signed in to change notification settings - Fork 0
/
langchain-openai.ts
85 lines (76 loc) · 2.5 KB
/
langchain-openai.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { AIMessage } from "@langchain/core/messages";
import { logger, Config } from "./config.ts";
export async function summaryYouTubeTranscript(transcript: string) {
const systemPrompt = Config.prompt;
const model = new ChatOpenAI({
model: "gpt-4o-mini",
temperature: 0.2,
topP: 0.9,
apiKey: Config.openai_api_key,
});
const prompt = ChatPromptTemplate.fromMessages([
["system", systemPrompt],
["user", "{input}"],
]);
const chain = prompt.pipe(model);
let response: AIMessage;
try {
response = await chain.invoke({
input: transcript,
});
} catch (error) {
logger.error(`Failed to summarize the transcript: ${error}`);
return [String(error), ""];
}
if (response.content !== null && response.content !== undefined) {
// Convert flat JSON object to key=value pairs
const key_value_usage = Object.entries(
response.response_metadata.tokenUsage,
)
.map(([key, value]) => `${key}=${value}`)
.join(" ");
return [String(response.content), String(key_value_usage)];
} else {
return ["", ""];
}
}
// ----------------------------------------------------------------------------
// For testing purposes
// If you run this file directly (https://bun.sh/docs/api/import-meta)
if (import.meta.main) {
const youtube = await import("./youtube.ts");
async function test_one() {
// Podrabianie sprzętu komputerowego, w tym #cisco, #yubikey - Mateusz Chrobok
const transcript_1 = await youtube.getTranscript(
"oDlMrMgGGA4",
);
if (transcript_1 === null) {
console.log("Transcript is null");
return;
}
console.log(transcript_1);
console.log("--------------------------------------------------");
const [summary_1, usage_1] = await summaryYouTubeTranscript(transcript_1);
console.log(summary_1);
console.log(usage_1);
}
async function test_two() {
// O co chodzi z Passkeys? Pytacie, odpowiadam(y). Q&A @secfense
const transcript_1 = await youtube.getTranscript(
"rf6LriO_dy8",
);
if (transcript_1 === null) {
console.log("Transcript is null");
return;
}
console.log(transcript_1);
console.log("--------------------------------------------------");
const [summary_1, usage_1] = await summaryYouTubeTranscript(transcript_1);
console.log(summary_1);
console.log(usage_1);
}
await test_one();
// await test_two();
}