npm package: @j-o-r/prompt
Description: Message prompt template for various AI models
Author: Jorrit Duin
Latest version: 1.1.6

# @j-o-r/prompt

Message prompt template for various AI models.

## Table of Contents
- [Introduction](#introduction)
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [API](#api)
- [License](#license)

## Introduction

`@j-o-r/prompt` is a JavaScript library designed to create a uniform prompt/message format for AI models. It simplifies working with messages and conversations across different AI platforms and LLMs (Large Language Models). The structure is inspired by the OpenAI messages format but is not directly compatible.

## Features

- **Context Management**: Supports context window for managing prompt length.
- **Multi-modal Messages**: Handles text, image URLs, function requests, and function responses.
- **Token Counting**: Counts the number of tokens in a string.
- **Message Export/Import**: Export and import messages as JSON strings.
- **Sticky Messages**: Supports sticky messages that persist across resets.
- **Record Keeping**: Maintains records for billing and optimization.

## Installation

To install the library, use npm:

```bash
npm install @j-o-r/prompt
```

## Usage

### Basic Example

```javascript
import Prompt from '@j-o-r/prompt';

// Create a new Prompt instance
const prompt = new Prompt(1024); // contextWindow size of 1024 tokens

// Add a text message
prompt.add('user', 'Hello, how can I help you today?');

// Export messages to JSON
const exportedMessages = prompt.export();

// Import messages from JSON
prompt.import(exportedMessages);

// Get the current messages
const messages = prompt.messages;

console.log(messages);
```

### Multi-modal Example

```javascript
import Prompt from '@j-o-r/prompt';

const prompt = new Prompt(1024);

// Add a multi-modal message
const multiModalMessage = [
  { type: 'text', text: 'Here is an image for you:' },
  { type: 'image_url', image_url: 'https://example.com/image.png' }
];

prompt.addMultiModal('assistant', multiModalMessage);

console.log(prompt.messages);
```

## API

### Classes

#### `Prompt`

- **constructor(contextWindow?: number, str?: string)**
  - Creates a new Prompt instance.
  - `contextWindow`: The max size of a total prompt (default is 0 for ONE_SHOT).
  - `str`: JSON formatted string representing the initial messages.

- **get messages(): PRMessage[]**
  - Returns a copy of the messages array.

- **contentToString(content: PRContent[]): string**
  - Returns the readable content (type == text).

- **countTokens(str: string): number**
  - Counts the number of tokens in a given string.

- **export(): string**
  - Exports the messages as a JSON string.

- **import(str: string): void**
  - Imports messages from a JSON string.

- **truncate(): boolean**
  - Reduces the prompt length to fit in the contextWindow or resets if no contextWindow.

- **get length(): number**
  - Gets the length of the messages array.

- **get system_prompt(): string**
  - Gets the system prompt from the messages.

- **get hasSystemprompt(): boolean**
  - Checks if a system prompt is available.

- **add(role: string, message: string, sticky?: boolean): void**
  - Adds a text message to the messages array.

- **addMultiModal(role: string, messages: PRContent[], sticky?: boolean): void**
  - Adds a multi-modal message to the messages array.

- **reset(): string**
  - Removes all non-sticky messages and returns them as a JSON string.

- **get records(): PRRecord[]**
  - Returns a copy of the records array.

- **addRecord(record: PRRecord): void**
  - Registers a record.

- **addRecords(records: PRRecord[]): void**
  - Adds a list of records.

- **resetRecords(): PRRecord[]**
  - Empties/reset prompt records and returns all records.

## License

This project is licensed under the Apache 2.0 License.