Skip to content

Instantly share code, notes, and snippets.

@u1and0
Created March 29, 2024 23:45
Show Gist options
  • Save u1and0/efabf2981c8b8fe34515295fcc74c404 to your computer and use it in GitHub Desktop.
Save u1and0/efabf2981c8b8fe34515295fcc74c404 to your computer and use it in GitHub Desktop.
Simple GPT client Using deno
const apiKey = Deno.env.get("CHATGPT_API_KEY");
const url = "https://api.openai.com/v1/chat/completions";
const content = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`,
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
max_tokens: 1000,
temperature: 1.0,
messages: [
{ "role": "user", "content": "hi" },
],
}),
})
.then((response) => response.json())
.then((data) => {
if (data.error) {
console.error(data);
}
else {
return data.choices[0].message.content;
}
})
.catch((error) => {
console.error(`Fetch request failed: ${error}`);
});
console.log(content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment