Created
March 13, 2023 16:36
-
-
Save georgeck/dda222d0eae3d1c3cbf121bc178ca586 to your computer and use it in GitHub Desktop.
Invoking OpenAPI ChatGPT Chat completions model (gpt-3.5-turbo) with SSE streaming
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
// Uses the eventsource-parser library to parse the stream from OpenAI's chat completion API | |
// Make sure to set the OPENAI_API_KEY environment variable to your OpenAI API key | |
import {createParser} from 'eventsource-parser' | |
function generatePrompt(prompt) { | |
return [ | |
{"role": "system", "content": "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly."}, | |
{"role": "user", "content": "Hello, who are you?"}, | |
{"role": "system", "content": `I am an AI created by OpenAI. How can I help you today?`}, | |
{"role": "user", "content": `${prompt}`}, | |
] | |
} | |
async function invokeChatCompletion() { | |
let response = await fetch( | |
"https://api.openai.com/v1/chat/completions", | |
{ | |
headers: { | |
"Content-Type": "application/json", | |
"Authorization": `Bearer ${process.env.OPENAI_API_KEY}` | |
}, | |
method: "POST", | |
body: JSON.stringify({ | |
model: "gpt-3.5-turbo", | |
messages: generatePrompt("Tell me a joke."), | |
temperature: 0.75, | |
top_p: 0.95, | |
// stop: ["\n\n"], | |
frequency_penalty: 0, | |
presence_penalty: 0, | |
max_tokens: 20, | |
stream: true, | |
n: 1, | |
}), | |
} | |
); | |
const parser = createParser(onParse) | |
for await (const value of response.body?.pipeThrough(new TextDecoderStream())) { | |
// console.log("Received", value); | |
parser.feed(value) | |
} | |
} | |
function onParse(event) { | |
if (event.type === 'event') { | |
if (event.data !== "[DONE]") { | |
console.log( JSON.parse(event.data).choices[0].delta?.content || "") | |
} | |
} else if (event.type === 'reconnect-interval') { | |
console.log('We should set reconnect interval to %d milliseconds', event.value) | |
} | |
} | |
invokeChatCompletion().then(r => console.log("Done!")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment