Public
Script
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
import type { ChatCompletion, ChatCompletionCreateParamsNonStreaming, Message } from "npm:@types/openai";
// a test comment for vt-backup
// another test comment for vt-backup
async function getOpenAI() {
// if you don't have a key, use our std library version
if (Deno.env.get("OPENAI_API_KEY") === undefined) {
const { OpenAI } = await import("https://esm.town/v/std/openai");
return new OpenAI();
} else {
const { OpenAI } = await import("npm:openai");
return new OpenAI();
}
}
export function startChat(chatOptions: Omit<ChatCompletionCreateParamsNonStreaming, "messages"> & { system: string } = {
max_tokens: 30,
model: "gpt-3.5-turbo",
system: "You are a helpful assistant.",
}) {
const { system, ...options } = chatOptions;
return async function gpt(strings, ...values) {
const openai = await getOpenAI();
const input = String.raw({ raw: strings }, ...values);
const messages = [{ role: "system", content: system }, { role: "user", content: input }];
const createParams: ChatCompletionCreateParamsNonStreaming = {
...options,
messages,
};
const completion = await openai.chat.completions.create(createParams);
return { ...completion, content: completion.choices[0].message.content };
};
}
April 11, 2024