-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt.ts
67 lines (61 loc) · 1.56 KB
/
gpt.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
import OpenAI from "npm:openai@4.38.3";
export async function gpt({
model,
apiKey,
baseURL,
content,
systemContent,
}: {
model: string;
apiKey: string;
content: string;
systemContent: string;
baseURL?: string;
}): Promise<string> {
const openai = new OpenAI({
apiKey,
baseURL,
});
const chatCompletion = await openai.chat.completions.create({
model,
messages: [
{
role: "system",
content: systemContent,
},
{
role: "user",
content: content,
},
],
temperature: 0,
stream: false,
});
return chatCompletion.choices[0].message.content || "";
}
if (import.meta.main) {
let content = Deno.args.join(" ") + "\n";
if (!Deno.stdin.isTerminal()) {
const decoder = new TextDecoder();
for await (const chunk of Deno.stdin.readable) {
content += decoder.decode(chunk);
}
}
const MAX_TOKENS = 6_000;
const words = content.split(" ").length;
// console.warn({ words });
if (words > MAX_TOKENS) {
console.warn({ content, words });
console.error(`Input is too long: ${words} words`);
Deno.exit(1);
}
const response = await gpt({
model: "gpt-4o-mini",
apiKey: Deno.env.get("OCO_OPENAI_API_KEY")!,
baseURL: undefined,
content: content,
systemContent:
"You are a expert programmer. You are helping a user to write a code snippet. You should use the best practices and idiomatic code to write a code snippet for the given problem. If the code snippet is empty return only zero characters.",
});
console.log(response);
}