Skip to content

Commit 5bdff83

Browse files
committed
init
0 parents  commit 5bdff83

File tree

10 files changed

+1541
-0
lines changed

10 files changed

+1541
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.env

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true
4+
}

chat.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'dotenv/config'
2+
import readline from 'node:readline'
3+
import { openai } from './openai.js'
4+
5+
const rl = readline.createInterface({
6+
input: process.stdin,
7+
output: process.stdout,
8+
})
9+
10+
const newMessage = async (history, message) => {
11+
const chatCompletion = await openai.chat.completions.create({
12+
messages: [...history, message],
13+
model: 'gpt-3.5-turbo',
14+
})
15+
16+
return chatCompletion.choices[0].message
17+
}
18+
19+
const formatMessage = (userInput) => ({ role: 'user', content: userInput })
20+
21+
const chat = () => {
22+
const history = [
23+
{
24+
role: 'system',
25+
content: `You are a helpful AI assistant. Answer the user's questions to the best of you ability.`,
26+
},
27+
]
28+
const start = () => {
29+
rl.question('You: ', async (userInput) => {
30+
if (userInput.toLowerCase() === 'exit') {
31+
rl.close()
32+
return
33+
}
34+
35+
const userMessage = formatMessage(userInput)
36+
const response = await newMessage(history, userMessage)
37+
38+
history.push(userMessage, response)
39+
console.log(`\n\nAI: ${response.content}\n\n`)
40+
start()
41+
})
42+
}
43+
44+
start()
45+
console.log('\n\nAI: How can I help you today?\n\n')
46+
}
47+
48+
console.log("Chatbot initialized. Type 'exit' to end the chat.")
49+
chat()

index.js

Whitespace-only changes.

openai.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import OpenAI from 'openai'
2+
3+
export const openai = new OpenAI({
4+
apiKey: process.env.OPENAI_API_KEY,
5+
})

0 commit comments

Comments
 (0)