Skip to main content

Une approche pour la gestion de la mémoire à court terme dans les chatbots.

Project description

Introduction

We present here an approach for managing short-term memory in chatbots, using a combination of storage techniques and automatic summarization to optimize conversational context. The introduced method relies on a dynamic memory structure that limits data size while preserving essential information through intelligent summaries. This approach not only improves the fluidity of interactions but also ensures contextual continuity during long dialogue sessions. Additionally, the use of asynchronous techniques ensures that memory management operations do not interfere with the chatbot's responsiveness.

How to Use the shortterm-memory Package

This section explains how to use the shortterm-memory package to manage a chatbot's memory.

Installation

pip install torch transformers
pip install shortterm-memory
pip show shortterm-memory

Usage

from shortterm_memory.ChatbotMemory import ChatbotMemory

Usage Exemple

from shortterm_memory.ChatbotMemory import ChatbotMemory

# Initialisation de la mémoire du chatbot
chat_memory = ChatbotMemory()

# Mettre à jour la mémoire avec un nouvel échange
user_input = "Bonjour, comment allez-vous?"
bot_response = "Je vais bien, merci ! Et vous ?"
chat_memory.update_memory(user_input, bot_response)

# Obtenir l'historique des conversations
historique = chat_memory.get_memory()
print(historique)

Available Features

  • update_memory(user_input: str, bot_response: str): Updates the conversation history with a new question-response pair.

  • get_memory(): Returns the complete conversation history as a list.

  • memory_counter(conv_hist: list) -> int: Counts the total number of words in the conversation history.

  • compressed_memory(conv_hist: list) -> list: Compresses the conversation history using a summarization model.

Error Handling

Ensure that user inputs and bot responses are valid strings. If the history becomes too large, the package automatically compresses older conversations to save memory.

Mathematical Modeling of Conversation Management

In this section, we mathematically formalize conversation memory management in the chatbot. The memory is structured as a list of pairs representing exchanges between the user and the bot.

Conversation Memory Structure

The conversation memory can be defined as an ordered list of pairs $(u_i, d_i)$, where $u_i$ represents the user input and $d_i$ the bot response for the $i$-th exchange. This list is denoted by $\mathcal{C}$:

$$ \mathcal{C} = [(u_1, d_1), (u_2, d_2), \ldots, (u_n, d_n)] $$

where $n$ is the total number of exchanges in the current history.

Memory Update

When a new exchange occurs, a new pair $(u_{n+1}, d_{n+1})$ is added to the memory. If the size of $\mathcal{C}$ exceeds a predefined maximum limit $M_{\text{max}}$, the oldest exchange is removed:

$$ \mathcal{C} = \begin{cases} \mathcal{C} \cup {(u_{n+1}, d_{n+1})}, & \text{si } |\mathcal{C}| < M_{\text{max}} \ (\mathcal{C} \setminus {(u_1, d_1)}) \cup {(u_{n+1}, d_{n+1})}, & \text{si } |\mathcal{C}| = M_{\text{max}} \end{cases} $$

Word Count

To manage memory space and decide when compression is necessary, we calculate the total number of words $W(\mathcal{C})$ in memory:

$$ W(\mathcal{C}) = \sum_{(u_i, d_i) \in \mathcal{C}} (|u_i| + |d_i|) $$

where $|u_i|$ and $|d_i|$ are respectively the number of words in $u_i$ and $d_i$.

Memory Compression

When $W(\mathcal{C})$ exceeds a threshold $W_{\text{max}}$, the memory is compressed to maintain the relevance of the context. This compression is performed by a summarization model $\mathcal{S}$, such as BART:

$$ \mathcal{C}_{\text{compressed}} = \mathcal{S}(\mathcal{C}) $$

where $\mathcal{C}_{\text{compressed}}$ is the compressed version of the memory, reducing the total number of words while preserving the essence of past interactions.

Integration into the Language Model

The language model uses the compressed context to generate relevant responses. The prompt $P$ used by the model is constructed as follows:

$$ P = f(\mathcal{C}_{\text{compressed}}, \text{context}) $$

where $\text{context}$ is additional context retrieved from a RAG pipeline, and $f$ is a concatenation function that prepares the text for the model.

This approach ensures that the chatbot always has an up-to-date conversational context, enabling more natural and engaging interactions with the user.

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

shortterm_memory-1.1.2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

shortterm_memory-1.1.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file shortterm_memory-1.1.2.tar.gz.

File metadata

  • Download URL: shortterm_memory-1.1.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for shortterm_memory-1.1.2.tar.gz
Algorithm Hash digest
SHA256 344222032be85bd611bfb9263f18115749a86a9d18e3e95aa3a4dd0b3dbf03c6
MD5 a34d962873c90219741dcc87101626e1
BLAKE2b-256 3e50da34547c8be1ede1a60fe739daeff25dc2f607b6cd7afbb07d2495e98f3d

See more details on using hashes here.

File details

Details for the file shortterm_memory-1.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for shortterm_memory-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4b0b1309e7acc968dc32dcf7daaa28856561b76d88230e20f395a787b3970813
MD5 d6bdbec7c21b5c7b239f2488ac4272ab
BLAKE2b-256 cac1c902a0323ca50f1c86543c6cc318bb04cfc646bf3b10bf647611a4e2747e

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