{"id":23403073,"url":"https://github.com/stadiello/ShortTerm-memory","last_synced_at":"2025-08-24T19:32:10.742Z","repository":{"id":251752956,"uuid":"838331992","full_name":"sebDtSci/ShortTerm-memory","owner":"sebDtSci","description":"To manage short-term memory in chatbots, using a combination of storage techniques and automatic summarization to optimize conversational context.","archived":false,"fork":false,"pushed_at":"2024-09-13T16:01:21.000Z","size":29,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-21T10:19:13.472Z","etag":null,"topics":["ai","chatbot","llm","short-term-memory"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sebDtSci.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-05T12:30:06.000Z","updated_at":"2024-10-01T13:06:37.000Z","dependencies_parsed_at":"2024-08-23T14:11:38.069Z","dependency_job_id":"5c648e3b-fe97-44cd-be73-4f9f0a54b75c","html_url":"https://github.com/sebDtSci/ShortTerm-memory","commit_stats":null,"previous_names":["sebdtsci/shortterm-memory"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebDtSci%2FShortTerm-memory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebDtSci%2FShortTerm-memory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebDtSci%2FShortTerm-memory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebDtSci%2FShortTerm-memory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sebDtSci","download_url":"https://codeload.github.com/sebDtSci/ShortTerm-memory/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230832750,"owners_count":18287085,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","chatbot","llm","short-term-memory"],"created_at":"2024-12-22T12:38:54.081Z","updated_at":"2025-08-24T19:32:05.456Z","avatar_url":"https://github.com/sebDtSci.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Introduction\n\nWe 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.\n\n# How to Use the `shortterm-memory` Package\n\nThis section explains how to use the `shortterm-memory` package to manage a chatbot's memory.\n\n## Installation\n```bash\npip install torch transformers\n```\n```bash\npip install shortterm-memory\n```\n```bash\npip show shortterm-memory\n```\n## Usage\n```python\nfrom shortterm_memory.ChatbotMemory import ChatbotMemory\n```\n## Usage Exemple\n```python\t\nfrom shortterm_memory.ChatbotMemory import ChatbotMemory\n\n# Initialisation de la mémoire du chatbot\nchat_memory = ChatbotMemory()\n\n# Mettre à jour la mémoire avec un nouvel échange\nuser_input = \"Bonjour, comment allez-vous?\"\nbot_response = \"Je vais bien, merci ! Et vous ?\"\nchat_memory.update_memory(user_input, bot_response)\n\n# Obtenir l'historique des conversations\nhistorique = chat_memory.get_memory()\nprint(historique)\n```\n\n## Available Features\n\n- **update_memory(user_input: str, bot_response: str)**: Updates the conversation history with a new question-response pair.\n\n- **get_memory()**: Returns the complete conversation history as a list.\n\n- **memory_counter(conv_hist: list) -\u003e int**: Counts the total number of words in the conversation history.\n\n- **compressed_memory(conv_hist: list) -\u003e list**: Compresses the conversation history using a summarization model.\n\n## Error Handling\n\nEnsure that user inputs and bot responses are valid strings. If the history becomes too large, the package automatically compresses older conversations to save memory.\n\n# Mathematical Modeling of Conversation Management\n\nIn 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.\n\n## Conversation Memory Structure\n\nThe 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}$:\n\n$$\n\\mathcal{C} = [(u_1, d_1), (u_2, d_2), \\ldots, (u_n, d_n)]\n$$\n\nwhere $n$ is the total number of exchanges in the current history.\n\n## Memory Update\n\nWhen 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:\n\n$$\n\\mathcal{C} = \n\\begin{cases} \n\\mathcal{C} \\cup \\{(u_{n+1}, d_{n+1})\\}, \u0026 \\text{si } |\\mathcal{C}| \u003c M_{\\text{max}} \\\\\n(\\mathcal{C} \\setminus \\{(u_1, d_1)\\}) \\cup \\{(u_{n+1}, d_{n+1})\\}, \u0026 \\text{si } |\\mathcal{C}| = M_{\\text{max}}\n\\end{cases}\n$$\n\n## Word Count\n\nTo manage memory space and decide when compression is necessary, we calculate the total number of words $W(\\mathcal{C})$ in memory:\n\n$$\nW(\\mathcal{C}) = \\sum_{(u_i, d_i) \\in \\mathcal{C}} (|u_i| + |d_i|)\n$$\n\nwhere $|u_i|$ and $|d_i|$ are respectively the number of words in $u_i$ and $d_i$.\n\n## Memory Compression\n\nWhen $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:\n\n$$\n\\mathcal{C}_{\\text{compressed}} = \\mathcal{S}(\\mathcal{C})\n$$\n\nwhere $\\mathcal{C}_{\\text{compressed}}$ is the compressed version of the memory, reducing the total number of words while preserving the essence of past interactions.\n\n## Integration into the Language Model\n\nThe language model uses the compressed context to generate relevant responses. The prompt $P$ used by the model is constructed as follows:\n\n$$\nP = f(\\mathcal{C}_{\\text{compressed}}, \\text{context})\n$$\n\nwhere $\\text{context}$ is additional context retrieved from a RAG pipeline, and $f$ is a concatenation function that prepares the text for the model.\n\nThis approach ensures that the chatbot always has an up-to-date conversational context, enabling more natural and engaging interactions with the user.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstadiello%2FShortTerm-memory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstadiello%2FShortTerm-memory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstadiello%2FShortTerm-memory/lists"}