realcoloride/node_characterai

(discord) bot forgets conversation

Closed this issue · 1 comments

I'm pretty sure I'm doing something wrong and this is not a bug. basically, the bot forgets the conversation every time a new message is sent in discord. below is my code. any help is appreciated.

// INITIALIZATION
const { Client, GatewayIntentBits } = require('discord.js');
const CharacterAI = require('node_characterai');
const accessToken = "private"
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
  ],
});
const characterAI = new CharacterAI();

// CharacterAI character ID
const characterId = 'private';
let chat;


// MAIN
async function authenticateAndCreateChat() {
    try {
      await characterAI.authenticateWithToken(accessToken);
      chat = await characterAI.createOrContinueChat(characterId);
      console.log('bot is ready.');
    } catch (error) {
      console.error('Authentication error:', error);
    }
}

client.once('ready', authenticateAndCreateChat);

client.on('messageCreate', async (message) => {
  // check if ready
  if (!chat) {
    console.log('bot is not ready yet.');
    return;
  }

  // ignore messages from the bot itself
  if (message.author.bot) return;
  try {
    const response = await chat.sendAndAwaitResponse(message.content, true);
    await message.reply(response.text);
  } catch (error) {
    console.error(error);
  }});

client.login('private');

Your code seems correct, however the reason why the character might be "forgetting" your conversation is because you probably send your prompt incorrectly.

When using multiple people to talk with, you need to specify in a way to the AI who it is talking to.

The potential cause

An LLM is a "text completer", which means it is supposed to complete a text from its training, eg:

coloride: hello!
AI: 

...and anything after "AI:" will be the response you would get.


Knowing this, what your AI is receiving is (under the hood):

YourCharacterAIUsername: {message.context}
AI: response

...and it has no way of discerning who it is talking to and could be perhaps confused since it would always think its talking to "YourCharacterAIUsername".

To prove what I am saying, you should check that in characterai, the messages you send are still in that chat.


The solution:

You could use this instead when using chat.sendAndAwaitResponse:
\n{message.author.name}:{message.content}, which basically creates a new line with the name of the person it is speaking to.

Example (if message.author.name is coloride):

YourCharacterAIUsername: 
coloride: {message.context}
AI: response

Hopefully this response might help.
Cheers