Last active
May 27, 2023 11:30
-
-
Save made-by-chris/f23c96637a76aabd7a9266cb9802a80d to your computer and use it in GitHub Desktop.
javascript scripts for prompting openAI's chat completion API
This file contains hidden or 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
// dont forget to npm install openai | |
// run with node chatcompletion.mjs | |
import { Configuration, OpenAIApi } from "openai"; | |
import fs from "fs"; | |
const configuration = new Configuration({ | |
apiKey: "YOUR_DEVELOPER_API_KEY_FROM_OPENAI", // https://platform.openai.com/account/api-keys | |
}); | |
const openai = new OpenAIApi(configuration); | |
const modelTypes = ["gpt-3.5-turbo", "gpt4", "gpt-4-32k"]; | |
const modelType = modelTypes[0]; | |
async function main(prompt) { | |
const filename = `output.${modelType}.${Date.now()}.txt`; | |
try { | |
console.log(`writing to ${filename}`); | |
const completion = await openai.createChatCompletion({ | |
model: modelType, | |
messages: [ | |
{ | |
role: "system", | |
content: `you are a helpful to do list creator. you should output markdown lists based on the user's input.`, | |
}, | |
{ | |
role: "user", | |
content: prompt, | |
}, | |
], | |
}); | |
console.log(completion.data); | |
fs.appendFileSync( | |
`${filename}.txt`, | |
JSON.stringify(completion.data.choices[0]) | |
); | |
} catch (e) { | |
console.log(e.message); | |
fs.appendFileSync(`error.${filename}.txt`, e.message); | |
} | |
} | |
main("i need to buy eggs, bread, and celery later today."); |
This file contains hidden or 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
// dont forget to npm install openai | |
// run with node chatStreamcompletion.mjs | |
import { Configuration, OpenAIApi } from "openai"; | |
import fs from "fs"; | |
const configuration = new Configuration({ | |
apiKey: "YOUR_DEVELOPER_API_KEY_FROM_OPENAI", // https://platform.openai.com/account/api-keys | |
}); | |
const openai = new OpenAIApi(configuration); | |
const modelTypes = ["gpt-3.5-turbo", "gpt4", "gpt-4-32k"]; | |
const modelType = modelTypes[0]; | |
async function main(prompt) { | |
const filename = `output.${modelType}.${Date.now()}.txt`; | |
try { | |
console.log(`writing to ${filename}`); | |
const completion = await openai.createChatCompletion( | |
{ | |
model: modelType, | |
messages: [ | |
{ | |
role: "system", | |
content: `you are a helpful to do list creator. you should output markdown lists based on the user's input.`, | |
}, | |
{ | |
role: "user", | |
content: prompt, | |
}, | |
], | |
stream: true, | |
}, | |
{ responseType: "stream" } | |
); | |
const stream = completion.data; | |
stream.on("data", (chunk) => { | |
const str = chunk.toString(); | |
const arr = str.split("data: "); | |
const jsonPackets = arr.filter((item) => validateJSON(item)); | |
if (jsonPackets) { | |
const content = JSON.parse(jsonPackets[0]).choices[0].delta.content; | |
console.log(content); | |
if (content !== undefined) { | |
fs.appendFileSync(filename, content); | |
} | |
} | |
}); | |
stream.on("end", () => { | |
console.log(`finished writing to ${filename}`); | |
}); | |
} catch (e) { | |
console.log(e.message); | |
fs.appendFileSync(`error.${filename}.txt`, e.message); | |
} | |
} | |
function validateJSON(str) { | |
try { | |
return JSON.parse(str); | |
} catch (e) { | |
return false; | |
} | |
} | |
main("i need to buy eggs, bread, and celery later today."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment