0.0.3 • Published 1 year ago

chat-ai-history v0.0.3

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

chat-history

Chat-history is a utility for storing and retrieving conversation history in chat interfaces. It provides a simple interface for adding new prompt/response messages to a buffer, and retrieving the most recent messages up to a specified token limit.

Installation

To install chat-history, run:

Copy code

npm install chat-history

Usage

Configuration

Before using Chatmem, you should configure the maximum number of tokens to store in the conversation history buffer. This is important to avoid excessive charges from the OpenAI API, which charges for each token processed.

To set the maximum number of tokens, call the config() function:

`const chatmem = require('chatmem');

chatmem.config(1000); // Set maximum number of tokens to 1000`

Adding Messages

To add a new prompt/response message to the conversation history buffer, call the add() function:

chatmem.add({ prompt: "Hello", response: "Hi there" }, "user1", Date.now());

The add() function takes three arguments:

  • message: An object with two properties, prompt and response, representing the prompt message and the response message.
  • user: A string representing the user who sent the message.
  • timestamp: A timestamp representing the time when the message was sent.

The function calculates the number of tokens in the message using the token_calculate() function, and stores the message along with its token count in the buffer. If the buffer size exceeds the maximum number of tokens, the oldest messages will be removed to make room for new messages.

Retrieving Messages

To retrieve the most recent prompt/response messages from the conversation history buffer, call the get_prompt() function:

const promptMessages = chatmem.get_prompt();

The get_prompt() function returns an array of prompt/response message objects, sorted in reverse chronological order (i.e., newest messages first), up to the token limit. Each message object has the following properties:

  • prompt: The prompt message.
  • response: The response message.
  • user: The user who sent the message.
  • timestamp: The timestamp when the message was sent.

Token Calculation

The token_calculate() function calculates the number of tokens in a message. By default, it uses a rough estimate of the number of characters in the message divided by 4. This can be inaccurate for messages with non-ASCII characters, emojis, or URLs. You can override this function with a more precise token calculation method if needed.

chatmem.token_calculate = function(message) {
  // TODO: Implement more precise token calculation algorithm
  // For now, use a rough estimate of number of characters divided by 4
  return Math.ceil((message.prompt.length + message.response.length) / 4);
};

License

Chatmem is licensed under the Apache-2.0.