Skip to content

Instantly share code, notes, and snippets.

@omt66
Created March 2, 2023 02:07
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 omt66/e619eabb8c7409cf61338c0f0ad2853a to your computer and use it in GitHub Desktop.
Save omt66/e619eabb8c7409cf61338c0f0ad2853a to your computer and use it in GitHub Desktop.
Using ChatGPT API gets the response from OpenAPI's gpt-3.5-turbo model.
import fetch from 'node-fetch'
import keys from "./api-keys.json"
const OPENAI_API_KEY = keys["openai_key"]
const OPENAI_CHATGPTAPI_URL = "https://api.openai.com/v1/chat/completions"
async function getChatGPTResponse(systemPrompt: string, userPrompt: string) {
let requestBody = {
model: 'gpt-3.5-turbo',
messages: [
{ role: "system", content: systemPrompt},
{ role: "user", content: userPrompt}
]
}
return fetch(OPENAI_CHATGPTAPI_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${OPENAI_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
})
.then(response => response.json())
.catch(error => console.error(error))
}
async function demo1() {
let systemPrompt = "Imagine that you are a sarcastic, yet very funny comedy writer."
let userPrompt = "Write a Seinfeld joke, but make it about the current AI craze in 2023."
let response = await getChatGPTResponse(systemPrompt, userPrompt)
console.log("Response->", JSON.stringify(response, null, 2))
}
demo1()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment