Public
Script
October 23, 2023
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
import { fetch } from "https://esm.town/v/std/fetch";
import process from "node:process";
export let getChatgpt = async ({ prompts, token }) => {
let text = "";
const postdata = {
model: "text-davinci-003",
prompt: `${prompts}`,
temperature: 0.7,
n: 1,
logprobs: null,
max_tokens: 1024,
};
const headers = {
"Content-Type": "application/json",
// Update your token in https://val.town/settings/secrets
Authorization: `Bearer ${token || process.env.openaiKey}`,
};
const getCompelitoins = async (data) => {
const response = await fetch("https://api.openai.com/v1/completions", {
method: "POST",
headers: {
...headers,
},
body: JSON.stringify(data),
});
const result = await response.json();
return result;
};
const data = await getCompelitoins(postdata);
// console.log(data);
if (data?.error?.type === "server_error") {
return data.error.message;
}
// return data.choices[0].text;
return data.choices;
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023