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_lmdb_onnx-1.2.tar.gz (45.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chatmemorydb_lmdb_onnx-1.2-py3-none-any.whl (45.1 MB view details)

Uploaded Python 3

File details

Details for the file chatmemorydb_lmdb_onnx-1.2.tar.gz.

File metadata

  • Download URL: chatmemorydb_lmdb_onnx-1.2.tar.gz
  • Upload date:
  • Size: 45.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for chatmemorydb_lmdb_onnx-1.2.tar.gz
Algorithm Hash digest
SHA256 81f0a87a7b4616d97f7adb2d8b2e105b8b6fb219abcc56023826305d63764983
MD5 be34ee61fb7913d2c5450a83c9d68c0e
BLAKE2b-256 178b90872db0dfc487f078d77e427d6e90e0cc4e0c725ee6325a36ae4deda485

See more details on using hashes here.

File details

Details for the file chatmemorydb_lmdb_onnx-1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for chatmemorydb_lmdb_onnx-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 befd6fc7babff51af1449d7d9f192f5777fa8f7dcd189491a0990dd0a04d9b3d
MD5 a58ed40edbb20296a5fd9ff0e6caae21
BLAKE2b-256 2b4a1d1aae53f9a6f2ff3cae778f2eedb4babee622f0ba42f00c7da971b727c4

See more details on using hashes here.

Supported by

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