-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai_transcript_attachment_summary.js
92 lines (80 loc) · 3.32 KB
/
openai_transcript_attachment_summary.js
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
86
87
88
89
90
91
92
let settings = input.config({
title: "Summarize Text Attachment with OpenAI",
description: "This script reads a .txt file from an attachment field, sends the text to OpenAI for summarization, and stores the summary.",
items: [
input.config.field("attachmentField", {
label: "📎 Attachment Field",
description: "Select the field where the .txt attachment is stored",
parentTable: "table"
}),
input.config.field("summaryField", {
label: "📝 Summary Field",
description: "Select the field where the summary will be stored",
parentTable: "table"
}),
input.config.text("openAiApiKey", {
label: "🔑 OpenAI API Key",
description: "Enter your OpenAI API key"
}),
input.config.text("prompt", {
label: "💬 Custom Prompt",
description: "Enter the custom prompt for summarization (e.g., 'Summarize the following text:')",
default: "Summarize the following text:"
}),
input.config.select("model", {
label: "🤖 OpenAI Model",
description: "Choose the OpenAI model",
options: [
{ label: "GPT-3.5", value: "gpt-3.5-turbo" },
{ label: "GPT-4", value: "gpt-4" }
]
}),
input.config.table("table", {
label: "📄 Table",
description: "Select the table that contains the records"
})
]
});
let { attachmentField, summaryField, openAiApiKey, prompt, model, table } = settings;
async function main() {
// Fetch records from the table
let records = await table.selectRecordsAsync();
for (let record of records.records) {
let attachment = record.getCellValue(attachmentField);
if (attachment && attachment.length > 0) {
// Get the first attachment URL
let fileUrl = attachment[0].url;
// Fetch the .txt file contents
let response = await fetch(fileUrl);
let textContent = await response.text();
// Create the full prompt to send to OpenAI
let fullPrompt = `${prompt}\n\n${textContent}`;
// Send request to OpenAI API
let openAiResponse = await remoteFetchAsync("https://api.openai.com/v1/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${openAiApiKey}`
},
body: JSON.stringify({
model: model,
prompt: fullPrompt,
max_tokens: 150
})
});
if (!openAiResponse.ok) {
throw new Error('Failed to fetch data from OpenAI API.');
}
let result = await openAiResponse.json();
let summary = result.choices[0].text.trim();
// Update the record with the summary
await table.updateRecordAsync(record.id, {
[summaryField.name]: summary
});
output.markdown(`**Summary for Record [${record.id}]:**\n${summary}`);
} else {
output.text(`No .txt attachment found for Record ID ${record.id}.`);
}
}
}
await main();