Skip to content

Instantly share code, notes, and snippets.

@iceener
Created April 18, 2023 09:35
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save iceener/cad5803bfe0b1dfd2adbf73c18b74dc8 to your computer and use it in GitHub Desktop.
Save iceener/cad5803bfe0b1dfd2adbf73c18b74dc8 to your computer and use it in GitHub Desktop.
const API_URL = "https://api.openai.com/v1/chat/completions";
const MAX_TOKENS = 1500;
const TEMPERATURE = 0.5;
const SYSTEM_PROMPT = 'Act as assistant';
const MESSAGES = ["hello", "hi!", "how are you?"];
async function openAICompletion(msg) {
const options = {
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8",
Authorization: `Bearer sk-.......`,
},
body: JSON.stringify({
model: "gpt-4",
max_tokens: MAX_TOKENS,
temperature: TEMPERATURE,
messages: [
{ role: "system", content: SYSTEM_PROMPT },
{ role: "user", content: msg },
],
stream: false,
stop: ["#-#"],
}),
};
return fetch(API_URL, options);
}
async function processMsgs() {
const promises = MESSAGES.map(async (msg) => {
const res = await openAICompletion(msg);
const data = await res.json();
console.log(data.choices[0].message.content);
return data;
});
await Promise.all(promises);
}
processMsgs();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment