-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai_calls.ts
68 lines (52 loc) · 2.02 KB
/
openai_calls.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
import {
ChatCompletionMessage,
OpenAI,
} from 'https://deno.land/x/[email protected]/mod.ts';
const token = Deno.env.get('OPENAI_API_KEY')!;
const openai = new OpenAI(token);
const whisperModel = 'whisper-1';
const gptModel = 'gpt-3.5-turbo';
//#region Transcription
export const transcribe = async (filePath: string) => {
const resp = await openai.createTranscription({
file: filePath,
model: whisperModel,
});
return resp.text;
};
//#endregion
//#region Writing tweets and threads.
export const writeX = async (text: string) => {
const system = `You are a professional content creator that helps people generate tweets.
You will be given a regular text and have to write a tweet based on it.
You MUST respond ONLY with the tweet content that should be posted. Don't add additional information to your response.
Write tweets in the same language as the original text. But if you add tags, ONLY they should be in English.
Begin!`;
const messages: ChatCompletionMessage[] = [
{ role: 'system', content: system },
{ role: 'user', content: text },
];
const resp = await openai.createChatCompletion({
messages,
model: gptModel,
});
return resp.choices[0].message.content;
};
export const writeXThread = async (text: string) => {
const system = `You are a professional content creator that helps people generate Twitter threads.
You will be given a regular text, and have to write a Twitter thread based on it.
You ALWAYS must write a thread with two or more tweets and a summary at the beginning.
Before writing, identify the language used in a text and write in the same language.
You MUST respond ONLY with the thread content that should be posted. Don't add additional information to your response.
Begin!`;
const messages: ChatCompletionMessage[] = [
{ role: 'system', content: system },
{ role: 'user', content: text },
];
const resp = await openai.createChatCompletion({
messages,
model: gptModel,
});
return resp.choices[0].message.content;
};
//#endregion