Created
April 18, 2023 09:35
-
-
Save iceener/cad5803bfe0b1dfd2adbf73c18b74dc8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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