Skip to main content

No project description provided

Project description

codecov

Downloads

Downloads

Downloads

Memory

This is a Python project aimed at providing an extremely simple way to manage the “memory” of a chatbot, when using LLMs. The goal is to provide a simple interface to memorize prompts and answers, as well as store it using summarization, while providing methods to fetch the context of the conversation - considering both short and long-term memory. Please check out the code snippets below.

Memory is stored in two forms, a simple vector database for semantic search, as well as Mongita (which is a "sqlite" for MongoDB) for chat history and other metadata.

Installation

pip install chatmemorydb

Usage

from memory.brain import Memory

memory = Memory()

# Start a new session
session_id, question_id, answer_id = memory.memorize("Hello", "Hi there! How can I help you?")

# Memorize new interactions with the new session id
memory.memorize("What is the capital of Italy?", "The capital of Italy is Rome.", session_id)
memory.memorize("What is the capital of France?", "The capital of France is Paris.", session_id)
memory.memorize("What is the capital of Spain?", "The capital of Spain is Madrid.", session_id)
memory.memorize("What is the capital of Brazil?", "The capital of Brazil is Brasília.", session_id)
memory.memorize("What is the capital of Peru?", "The capital of Peru is Lima.", session_id)
memory.memorize("What is the capital of Colombia?", "The capital of Colombia is Bogotá.", session_id)
memory.memorize("What is the capital of Venezuela?", "The capital of Venezuela is Caracas.", session_id)
_, question_id, answer_id = memory.memorize("What is the capital of Ecuador?", "The capital of Ecuador is Quito.", session_id)

# Forget a given message
memory.forget_message(session_id, answer_id)

# List existing messages
messages = memory.list_messages(session_id, page=1, limit=3, recent_first=True)

# Result
[
    {
        '_id': ObjectId('65d789c5f698b7e4198e4b1f'),
        'message_id': '77942ef3-3a63-4e3c-90d3-f4c13ffef1ec',
        'question': 'What is the capital of Ecuador?',
        'question_summary': 'What is the capital of Ecuador?',
        'session_id': 'c8ea1875-9e77-4eb7-8f2a-a1dfc7c104c5',
        'timestamp': datetime.datetime(2024, 2, 22, 17, 52, 5, 930335)
    },
    {
        '_id': ObjectId('65d789c5f698b7e4198e4b1e'),
        'answer': 'The capital of Venezuela is Caracas.',
        'answer_summary': 'The capital of Venezuela is Caracas.',
        'message_id': 'bacd8e4b-0d1d-477d-bd23-f99f12068224',
        'session_id': 'c8ea1875-9e77-4eb7-8f2a-a1dfc7c104c5',
        'timestamp': datetime.datetime(2024, 2, 22, 17, 52, 5, 906237)
    },
    {
        '_id': ObjectId('65d789c5f698b7e4198e4b1d'),
        'message_id': 'a6256250-3797-4073-ae85-3d803fbc8f27',
        'question': 'What is the capital of Venezuela?',
        'question_summary': 'What is the capital of Venezuela?',
        'session_id': 'c8ea1875-9e77-4eb7-8f2a-a1dfc7c104c5',
        'timestamp': datetime.datetime(2024, 2, 22, 17, 52, 5, 905285)
    }
]

# Remember something (core functionality)
new_prompt = "I love Madrid! What can you tell me about it?"
retrieved_memory = memory.remember(session_id, new_prompt)

# Result
{
    # We get the most recent interactions by default (short-term memory)
    'recent_memory': [
        {
            'session_id': 'c8ea1875-9e77-4eb7-8f2a-a1dfc7c104c5',
            'message_id': '77942ef3-3a63-4e3c-90d3-f4c13ffef1ec',
            'question': 'What is the capital of Ecuador?',
            'question_summary': 'What is the capital of Ecuador?',
            'timestamp': datetime.datetime(2024, 2, 22, 17, 52, 5, 930335)
        },
        {
            'session_id': 'c8ea1875-9e77-4eb7-8f2a-a1dfc7c104c5',
            'message_id': 'bacd8e4b-0d1d-477d-bd23-f99f12068224',
            'answer': 'The capital of Venezuela is Caracas.',
            'answer_summary': 'The capital of Venezuela is Caracas.',
            'timestamp': datetime.datetime(2024, 2, 22, 17, 52, 5, 906237)
        },
        ...
    ],
    # We also retrieve the most similar questions and answers from the long-term memory
    # using semantic search
    'context_memory': [
        {
            'sentence': 'the capital of spain is madrid.',
            'session_id': 'c8ea1875-9e77-4eb7-8f2a-a1dfc7c104c5',
            'message_id': 'f39886b8-f5d8-4610-a4a5-fd833572bfae',
            'type': 'answer'
        },
        {
            'sentence': 'what is the capital of spain ?',
            'session_id': 'c8ea1875-9e77-4eb7-8f2a-a1dfc7c104c5',
            'message_id': '96644ee7-c388-49f5-89b8-6e03905ef92c',
            'type': 'question'
        }
    ],
    
    # Then, everything is glued together and provided as a suggestion
    # This is the text that would be injected into the new prompt,
    # as context of the existing conversation
    'suggested_context': '''
        Previous context (answer): the capital of spain is madrid.
        Previous context (prompt): what is the capital of spain ?
        
        Previous prompt: What is the capital of Ecuador?
        Previous answer: The capital of Venezuela is Caracas.
        Previous prompt: What is the capital of Venezuela?
        Previous answer: The capital of Colombia is Bogotá.
    '''
}

# Forget an entire session
memory.forget_session(session_id)

### Parameters for the memory object

### Customize the storage locations
# param mongita_storage_location : defaults to './mongita_memory' (folder)
# param vector_db_storage_location : defaults to './vector_memory.pkl' (file)

### Custom embedding function (bring your own!)
### Should be able to receive the text and return a list of embeddings
### If no OpenAI key is provided, a free model will be used
# param free_embedding_model_type: AlternativeModel = AlternativeModel.tiny
# param embedding_extraction_function = None

### Custom summarization (bring your own!)
### Should be able to receive the text and return the summary
# param summarization_function = None

### OpenAI key and models
### Default ones are the most cost-effective
# param openai_key: str = None
# param openai_embedding_model: str = "text-embedding-3-small"
# param openai_summarization_model: str = "gpt-3.5-turbo"

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chatmemorydb-1.3.tar.gz (791.3 kB view details)

Uploaded Source

Built Distribution

chatmemorydb-1.3-py3-none-any.whl (790.3 kB view details)

Uploaded Python 3

File details

Details for the file chatmemorydb-1.3.tar.gz.

File metadata

  • Download URL: chatmemorydb-1.3.tar.gz
  • Upload date:
  • Size: 791.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for chatmemorydb-1.3.tar.gz
Algorithm Hash digest
SHA256 3075b1c81c70c84c2391e3ceee4f89fc53f4b56f5f35f222549c82ddb2b7b903
MD5 1c19ee24e7b20effefd0545fb6a27cb1
BLAKE2b-256 cbcd25342df80cdf567f83be15260b0e99b5d35f3028a392a318a66c7b5c11d6

See more details on using hashes here.

File details

Details for the file chatmemorydb-1.3-py3-none-any.whl.

File metadata

  • Download URL: chatmemorydb-1.3-py3-none-any.whl
  • Upload date:
  • Size: 790.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for chatmemorydb-1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 45e09a1c832d26fd32000c57c8f267f1a573b3a95c70321c84cf368c63820dee
MD5 ddcf7304770c67ec108a4a801bcb5aa3
BLAKE2b-256 e8d361c740dedf8e95172e63dc6685052d1909bc3739fa654f642b06d5e08e45

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page