Returns a Chat object that keeps a chat history and calls OpenAI API to receive replies.
createChat(initialMessage?, model?)
initialMessage optional
The initial message to start the chat with. Used with the system role.
model optional
The model to use for the chat. Defaults to "gpt-3.5-turbo".
An instance of Chat class with the following methods:
async send(message?, role = "user")
Adds a message to the chat history under the given role ("user" by default),
sends the history of messages to the OpenAI API, stores the response under the assistant role,
and returns the response as a single string.
Note that Chat tries to keep the prompt under 4096 tokens, which is the maximum length of the prompt allowed by the API. It does this by calculating the approximate number of tokens (number of characters divided by 4), and removing the oldest messages, except the initial one, until the prompt is short enough.
If the message is not given, sends the existing history.
push(message, role = "user")
Appends a message to the chat history under the given role ("user" by default) without sending it.
history()
Returns the chat history as a table.
reset()
Resets the chat history to its initial state.
let chat = openai.createChat("You are a nice assistant")
await chat.send("Hello")
"Hello, how are you?"
await chat.send("I am fine")
"That's good to hear."