Skip to content

Instantly share code, notes, and snippets.

@Reine0017
Created June 28, 2023 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Reine0017/583e1b874c7a4b42b59028e8f9398fbf to your computer and use it in GitHub Desktop.
Save Reine0017/583e1b874c7a4b42b59028e8f9398fbf to your computer and use it in GitHub Desktop.
callOpenAI function
async function callOpenAI(prompt) {
const url = 'https://api.openai.com/v1/chat/completions';
const headers = {
'Authorization': `Bearer ${process.env.OpenAIToken}`,
'Content-Type': 'application/json'
};
const data = {
'model': 'gpt-3.5-turbo',
'messages': [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
]
};
try {
const response = await axios.post(url, data, { headers: headers });
return response.data.choices[0].message.content;
} catch (error) {
console.error(`Error: ${error}`);
return "ERROR!"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment