-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.ts
73 lines (62 loc) · 1.96 KB
/
chat.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { openai } from './openai'
import readline from 'node:readline' // to read data from a readable stream
interface Message {
role: 'system' | 'user' | 'assistant'
content: string
}
// setting up a readline User interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
// create a message where you call the open ai API to send the chat history
// and the latest message to OpenAI, gets the AI's response and returns it
const newMessage = async (history: Message[], message: Message) => {
const chatCompletion = await openai.chat.completions.create({
messages: [...history, message],
model: 'gpt-3.5-turbo',
temperature: 0, // the accuracy, between 0 and 2
})
return chatCompletion.choices[0].message
}
// This function formats the user message
const formatMessage = (userInput: string): Message => ({
role: 'user',
content: userInput,
})
// Chat function handles the chat loop
const chat = () => {
// create the history
const history: Message[] = [
// initial story
{
role: 'system',
content: `You are a helpful AI assistant. Answer the user's questions to the best of you ability.`,
},
]
const start = () => {
rl.question('You: ', async (userInput) => {
// how to exit
if (userInput.toLowerCase() === 'exit') {
rl.close()
return
}
const userMessage = formatMessage(userInput)
const response = await newMessage(history, userMessage)
if (response.content !== null) {
history.push(userMessage, {
role: response.role,
content: response.content || '',
}) // update history
console.log(`\n\nAI: ${response.content}\n\n`)
} else {
console.log(`\n\nAI: Sorry, I couldn't generate a response.\n\n`)
}
start() // recursion
})
}
console.log('\n\nAI: How can I help you today?\n\n')
start()
}
console.log("Chatbot initialized. Type 'exit' to end the chat.")
chat()