Created
March 3, 2023 13:14
-
-
Save safa-dayo/91dea7081b7dc55195e64b00ba9b859f to your computer and use it in GitHub Desktop.
A CLI application for conducting conversations using the ChatGPT API.
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
const readline = require('readline'); | |
const { Configuration, OpenAIApi } = require('openai'); | |
(async () => { | |
const configuration = new Configuration({ | |
apiKey: process.env.OPENAI_API_KEY, | |
}); | |
const openai = new OpenAIApi(configuration); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}) | |
let isFirstTime = true; | |
const messages = []; | |
console.log("=== どんなチャットボットとお話したいですか?人格を打ち込んでください。 ==="); | |
rl.on("line", async (input) => { | |
if (isFirstTime) { | |
messages.push({ role: "system", content: input }) | |
isFirstTime = false; | |
console.log("=== OKです。それでは会話を開始しましょう。終了するときは「さようなら」と打ち込んでください ===") | |
} else if (input === "さようなら") { | |
console.log("さようなら!") | |
rl.close(); | |
} else { | |
messages.push({ role: "user", content: input }); | |
// 会話の文脈を確認したいときはこちらのコメントアウトを解除する | |
// console.log('DEBUG:', messages) | |
const response = await openai.createChatCompletion({ | |
model: "gpt-3.5-turbo", | |
messages, | |
}) | |
console.log(`> ChatGPT: ${response.data.choices[0].message.content}`); | |
// 会話の文脈を記録していく | |
messages.push(response.data.choices[0].message); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment