-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (38 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#! /usr/bin/env node
const path = require("path")
require("dotenv").config({
path: path.join(__dirname, ".env")
})
const package = require("./package.json");
const readline = require("readline")
const {Configuration, OpenAIApi} = require("openai")
const messages = []
const openai = new OpenAIApi(new Configuration({
organization: process.env.OPENAI_ORGANIZATION_ID,
apiKey: process.env.OPENAI_API_KEY
}))
const cli = readline.createInterface({
input: process.stdin,
output: process.stdout
})
cli.on("line", async input => {
const userMessage = {
role: "user",
content: input
}
messages.push(userMessage)
const res = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages
})
const content = res.data.choices[0].message.content
const assistantMessage = {
role: "assistant",
content
}
messages.push(assistantMessage)
console.log(content)
cli.prompt()
})
console.log(`ChatGPT-CLI v(${package.version})`)
cli.prompt()