Created
March 2, 2023 02:07
-
-
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.
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
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