Skip to content

Instantly share code, notes, and snippets.

@SaDi-BRo
Last active March 6, 2023 19:16
Show Gist options
  • Save SaDi-BRo/54d5582f931517b5ff679bd2e2e2e244 to your computer and use it in GitHub Desktop.
Save SaDi-BRo/54d5582f931517b5ff679bd2e2e2e244 to your computer and use it in GitHub Desktop.
The code shows you how to fetch a data from OpenAI
const fetchOpenAI = async (text: string) => {
const response = await fetch('https://api.openai.com/v1/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${Deno.env.get('OPENAI_KEY')}`,
},
body: JSON.stringify({
model: 'text-davinci-003',
prompt: text,
max_tokens: 4000,
}),
});
return response.json();
};
(async () => {
const response = await fetchOpenAI('Who was the first president of Uzbekistan?');
console.log(response.choices[0].text);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment