Skip to main content

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)

import { eliceContents } from 'src/constants';

eliceContents.ai.chat.prompt(
'I will create problems that elementary school students can easily solve...',
{ systemInstruction: '...' }
);
// { session: '550e8400-e29b-41d4-a716-446655440000', contentResponse: 'I will create problems that elementary school students can easily solve...' }

This method is used to generate AI chat responses. You can set a prompt to generate a response. The first argument is the prompt content, and the second argument is configuration. The configuration is optional and offers the following options:

  • systemInstruction: You can set the system instructions, and if you pass this argument, it will always override the system instructions.

⚠️ The response returns a session ID and the AI content response. You need to save this session ID in a KV store or local storage to reload chat messages even after the user refreshes the page.

load (AI Chat Load)

import { eliceContents } from 'src/constants';

const initChat = async () => {
const sessions = await eliceContents.getKeyValue({ key: 'sessions' });
const firstSession = sessions[0];

if (firstSession) {
eliceContents.ai.chat.load(firstSession); // [{ role: 'system', content: 'I will create problems that elementary school students can easily solve...', ts: 1634025600000 }, ...]
}
};

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.

The response returns an array of chat messages. Each chat message includes role, content, and ts.

role is currently classified into three types:

  • system: System instruction content
  • assistant: AI response content
  • user: User input content

clear (AI Chat Clear)

This method can be used to delete chat messages for a specific session stored on the server. The first argument is the session ID obtained from the prompt method.

import { eliceContents } from 'src/constants';

const clearChat = async () => {
await eliceContents.ai.chat.clear('550e8400-e29b-41d4-a716-446655440000');
};

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 the messages.

import { eliceContents } from 'src/constants';

const startNewChat = () => {
eliceContents.ai.chat.reset();
eliceContents.ai.chat.prompt(
'I will create problems that elementary school students can easily solve...',
{ systemInstruction: '...' }
);
};

subscribes (AI Chat Subscribe)

subscribe is a method that allows you to listen to various chat events. You pass an event listener as the first argument that will execute when an event occurs.

import { eliceContents } from 'src/constants';

eliceContents.ai.chat.subscribe(event => {
console.log(event); // { type: 'comment', payload: { role: 'assistant', content: 'I will create problems that elementary school students can easily solve...', ts: 1634025600000 } }
});

Events are structured with type and payload, which are differentiated as follows:

  • comment: The event occurs when a chat message is added. The payload is the chatMessage.
  • clear: The event occurs when the chat session is removed. The payload is the sessionId.
  • reset: The event occurs when the current chat session is reset. The payload is null.
  • load: The event occurs when chat messages are loaded. The payload is the chatMessage array.
import { eliceContents } from 'src/constants';

const { unsubscribe } = eliceContents.ai.chat.subscribe(/* event listener */);

unsubscribe(); // Unsubscribe from the event listener

This method returns an object containing the unsubscribe method. You can use this method to cancel the subscription of the event listener.

2. Properties

sessionId (Session ID)

This property is the unique identifier for the AI chat. Initially, it is null. A session ID is generated when you call the prompt or load method to load chat messages.

import { eliceContents } from 'src/constants';

eliceContents.subscribe(event => {
switch (event.type) {
case 'prompt':
console.log(eliceContents.ai.chat.sessionId); // '550e8400-e29b-41d4-a716-446655440000'
break;

case 'reset':
console.log(eliceContents.ai.chat.sessionId); // null
break;
}
});

chatMessages (Chat Messages)

This property is an array of chat messages. It contains the chat messages for the current session. Initially, it is an empty array. It will be filled with chat messages when you call the load or prompt method. It will be emptied when you call the reset method.

import { eliceContents } from 'src/constants';

eliceContents.subscribe(event => {
switch (event.type) {
case 'load':
console.log(eliceContents.ai.chat.chatMessages); // [{ role: 'system', content: 'I will create problems that elementary school students can easily solve...', ts: 1634025600000 }, ...]
break;
case 'comment':
console.log(eliceContents.ai.chat.chatMessages); // [{ role: 'system', content: 'I will create problems that elementary school students can easily solve...', ts: 1634025600000 }, ...]
break;
case 'clear':
console.log(eliceContents.ai.chat.chatMessages); // []
break;
case 'reset':
console.log(eliceContents.ai.chat.chatMessages); // []
break;
}
});