Created
June 28, 2023 13:02
-
-
Save Reine0017/583e1b874c7a4b42b59028e8f9398fbf to your computer and use it in GitHub Desktop.
callOpenAI function
This file contains 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
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