-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
84 lines (65 loc) · 2.15 KB
/
test.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
import { GetObjectCommand } from 's3Client';
import { PutObjectCommand } from 's3Client';
import { S3Client } from 's3Client';
import { bucket } from './types.ts';
import { parse } from '@std/path/parse';
import OpenAI from 'openai';
// 🔥 the JSON input and HTML output files are relative to
// this S3 { bucket }
// 👉 default used for testing; keep it as a template for data entry
const sourceDflt = `November 19 PB Hearing (Raw).txt`;
// 👇 solicit the JSON data file
const sourceFile = prompt(
'⌨️ Enter the raw minutes file to interpret:',
sourceDflt,
) as string ?? sourceDflt;
// 👉 HTML output file
const targetFile = `${parse(sourceFile).name}.html`;
// 👉 S3 client to read and write into bucket
const s3Client = new S3Client({});
// 👉 OpenAI to produce summary from raw minutes
const openai = new OpenAI();
// 👇 get the JSON transcription
// see transcriber.ts
const s3Response = await s3Client.send(
new GetObjectCommand({
Bucket: bucket,
Key: sourceFile,
}),
);
// @ts-ignore 🔥 who knows why Body isn't allowed?
const str = await s3Response.Body.transformToString();
// 👇 these dots signify a long-running operation in the CLI
const encoder = new TextEncoder();
Deno.stdout.write(
encoder.encode(`👉 Summarizing raw minutes.`),
);
const timerID = setInterval(() => {
Deno.stdout.write(encoder.encode('.'));
}, 100);
// 👇 ask OpenAI to produce a summary from the transcription
const aiResponse = await openai.chat.completions.create({
messages: [{
role: 'user',
content:
`Convert the following text into grammatical English. Break it into short paragraphs. Format the result as HTML.\n\n${str}`,
}],
model: 'gpt-4o',
});
clearInterval(timerID);
Deno.stdout.write(encoder.encode('\n'));
// 🔥 ugh! no idea how to suppress this crap
const summary = aiResponse.choices[0].message.content
.replaceAll('```html', '')
.replaceAll('```', '');
// 👇 we're done!
// save the HTML into its S3 bucket
console.log(`👉 Saving ${targetFile}`);
await s3Client.send(
new PutObjectCommand({
Bucket: bucket,
Key: targetFile,
Body: summary,
ContentType: 'text/html',
}),
);