ai.chat (AI Chat
ai.chat
is an object that provides various methods and properties for interacting with the AI Chat service.
1. Methods
prompt (AI Chat Prompt)
This method is used to generate AI chat responses. You can set the prompt to generate a response. The first argument is the prompt content
, and the second argument is the configuration
, which is optional. The configuration provides the following options:
systemInstruction
: You can set system instructions, and if you pass this argument, it will always override the system instructions.
⚠️ For the response, it returns the session ID and AI content response. To reload the chat messages even if the user refreshes the page, you need to store this session ID in KV store or local storage.
load (AI Chat Load)
This method is used to load chat messages from the AI Chat service. The first argument is the session ID obtained from the prompt method.
It returns an array of chat messages as a response. Each chat message contains role
, content
, and ts
.
The role
is currently divided into three categories:
system
: System instruction contentassistant
: AI response contentuser
: User input content
clear (AI Chat Clear)
You can use this method to clear chat messages of a specific session stored on the server. The first argument is the session ID obtained from the prompt method.
reset (AI Chat Reset)
This method is used to reset the chat messages of the current session. It initializes the current session ID and local data of messages.
subscribes (AI Chat Subscribes)
subscribe
is a method that allows you to listen to various chat events. You pass the event listener that will be executed when an event occurs as the first argument.
The event consists of type
and payload
, and it is divided as follows:
comment: This event occurs when a chat message is added. The
payload
ischatMessage
.clear: This event occurs when a chat session is removed. The
payload
issessionId
.reset: This event occurs when the current chat session is reset. The
payload
isnull
.load: This event occurs when chat messages are loaded. The
payload
is an array ofchatMessage
.
This method returns an object that includes the unsubscribe
method. You can use this method to cancel the subscription to the event listener.
2. Properties
sessionId
This property is the unique identifier of the AI chat. It is initially null
. When you load chat messages by calling the prompt or load method, a session ID is generated.
chatMessages
This property is an array of chat messages. It contains the chat messages of the current session. It is initially an empty array. When you call the load or prompt method, it will be filled with chat messages. When you call the reset method, it will be emptied.
Last updated