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('Let\'s create a problem that elementary students can solve...', { systemInstruction: '...' })
// { session: '550e8400-e29b-41d4-a716-446655440000', contentResponse: 'Let\'s create a problem that elementary students can solve...'  }

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)

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: 'Let\'s create a problem that elementary students can 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.

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 content

  • assistant: AI response content

  • user: 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.

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

import { eliceContents } from 'src/constants';

const startNewChat = () => {
    eliceContents.ai.chat.reset();
    eliceContents.ai.chat.prompt('Let\'s create a problem that elementary students can solve...', { systemInstruction: '...' });
}

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.

import { eliceContents } from 'src/constants';

eliceContents.ai.chat.subscribe((event) => {
    console.log(event); // { type: 'comment', payload: { role: 'assistant', content: 'Let\'s create a problem that elementary students can solve...', ts: 1634025600000 } }
});

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 is chatMessage.

  • clear: This event occurs when a chat session is removed. The payload is sessionId.

  • reset: This event occurs when the current chat session is reset. The payload is null.

  • load: This event occurs when chat messages are loaded. The payload is an array of chatMessage.

import { eliceContents } from 'src/constants';

const { unsubscribe } = eliceContents.ai.chat.subscribe(/* Event Listener */);

unsubscribe(); // Cancel subscription to event listener

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.

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

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.

import { eliceContents } from 'src/constants';

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

Last updated