Skip to main content

Memor: A Python Library for Managing and Transferring Conversational Memory Across LLMs

Project description

Memor Logo

Memor: A Python Library for Managing and Transferring Conversational Memory Across LLMs


PyPI version built with Python3 GitHub repo size Discord Channel

Overview

Memor is a library designed to help users manage the memory of their interactions with Large Language Models (LLMs). It enables users to seamlessly access and utilize the history of their conversations when prompting LLMs. That would create a more personalized and context-aware experience. Memor stands out by allowing users to transfer conversational history across different LLMs, eliminating cold starts where models don't have information about user and their preferences. Users can select specific parts of past interactions with one LLM and share them with another. By bridging the gap between isolated LLM instances, Memor revolutionizes the way users interact with AI by making transitions between models smoother.

PyPI Counter
Github Stars
Branch main dev
CI
Code Quality CodeFactor

Installation

PyPI

Source code

Usage

Define your prompt and the response(s) to that; Memor will wrap it into a object with a templated representation. You can create a session by combining multiple prompts and responses, gradually building it up:

>>> from memor import Session, Prompt, Response, Role
>>> from memor import PresetPromptTemplate, RenderFormat, LLMModel
>>> response = Response(message="I am fine.", model=LLMModel.GPT_4, role=Role.ASSISTANT, temperature=0.9, score=0.9, top_p=0.9)
>>> prompt = Prompt(message="Hello, how are you?",
                    responses=[response],
                    role=Role.USER,
                    template=PresetPromptTemplate.INSTRUCTION1.PROMPT_RESPONSE_STANDARD)
>>> system_prompt = Prompt(message="You are a friendly and informative AI assistant designed to answer questions on a wide range of topics.",
                    role=Role.SYSTEM)
>>> session = Session(messages=[system_prompt, prompt])
>>> session.render(RenderFormat.OPENAI)

The rendered output will be a list of messages formatted for compatibility with the OpenAI API.

[{"content": "You are a friendly and informative AI assistant designed to answer questions on a wide range of topics.", "role": "system"},
 {"content": "I'm providing you with a history of a previous conversation. Please consider this context when responding to my new question.\n"
             "Prompt: Hello, how are you?\n"
             "Response: I am fine.",
  "role": "user"}]

Prompt Templates

Preset Templates

Memor provides a variety of pre-defined prompt templates to control how prompts and responses are rendered. Each template is prefixed by an optional instruction string and includes variations for different formatting styles. Following are different variants of parameters:

Instruction Name Description
INSTRUCTION1 "I'm providing you with a history of a previous conversation. Please consider this context when responding to my new question."
INSTRUCTION2 "Here is the context from a prior conversation. Please learn from this information and use it to provide a thoughtful and context-aware response to my next questions."
INSTRUCTION3 "I am sharing a record of a previous discussion. Use this information to provide a consistent and relevant answer to my next query."
Template Title Description
PROMPT Only includes the prompt message.
RESPONSE Only includes the response message.
RESPONSE0 to RESPONSE3 Include specific responses from a list of multiple responses.
PROMPT_WITH_LABEL Prompt with a "Prompt: " prefix.
RESPONSE_WITH_LABEL Response with a "Response: " prefix.
RESPONSE0_WITH_LABEL to RESPONSE3_WITH_LABEL Labeled response for the i-th response.
PROMPT_RESPONSE_STANDARD Includes both labeled prompt and response on a single line.
PROMPT_RESPONSE_FULL A detailed multi-line representation including role, date, model, etc.

You can access them like this:

>>> from memor import PresetPromptTemplate
>>> template = PresetPromptTemplate.INSTRUCTION1.PROMPT_RESPONSE_STANDARD

Custom Templates

You can define custom templates for your prompts using the PromptTemplate class. This class provides two key parameters that control its functionality:

  • content: A string that defines the template structure, following Python string formatting conventions. You can include dynamic fields using placeholders like {field_name}, which will be automatically populated using attributes from the prompt object. Some common examples of auto-filled fields are shown below:
Prompt Object Attribute Placeholder Syntax Description
prompt.message {prompt[message]} The main prompt message
prompt.selected_response {prompt[response]} The selected response for the prompt
prompt.date_modified {prompt[date_modified]} Timestamp of the last modification
prompt.responses[2].message {responses[2][message]} Message from the response at index 2
prompt.responses[0].inference_time {responses[0][inference_time]} Inference time for the response at index 0
  • custom_map: In addition to the attributes listed above, you can define and insert custom placeholders (e.g., {field_name}) and provide their values through a dictionary. When rendering the template, each placeholder will be replaced with its corresponding value from custom_map.

Suppose you want to prepend an instruction to every prompt message. You can define and use a template as follows:

>>> template = PromptTemplate(content="{instruction}, {prompt[message]}", custom_map={"instruction": "Hi"})
>>> prompt = Prompt(message="How are you?", template=template)
>>> prompt.render()
Hi, How are you?

By using this dynamic structure, you can create flexible and sophisticated prompt templates with Memor. You can design specific schemas for your conversational or instructional formats when interacting with LLM.

Examples

You can explore real-world usage of Memor in the examples directory. This directory includes concise and practical Python scripts that demonstrate key features of Memor library.

Issues & bug reports

Just fill an issue and describe it. We'll check it ASAP! or send an email to memor@openscilab.com.

  • Please complete the issue template

You can also join our discord server

Discord Channel

Show your support

Star this repo

Give a ⭐️ if this project helped you!

Donate to our project

If you do like our project and we hope that you do, can you please support us? Our project is not and is never going to be working for profit. We need the money just so we can continue doing what we do ;-) .

Memor Donation

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Unreleased

0.8 - 2025-07-21

Added

  • Logo
  • Response class top_k property
  • Response class top_p property
  • Response class gpu property

Changed

  • AI_STUDIO render format modified
  • LLMModel enum updated
  • Model load bug fixed
  • Test system modified
  • README.md updated
  • _validate_pos_float now validates int values
  • _validate_probability now validates int values
  • None value validation bug fixed

0.7 - 2025-06-25

Added

  • Message abstract class
  • Session class _validate_extract_json method
  • Response class _validate_extract_json method
  • Prompt class _validate_extract_json method
  • PromptTemplate class _validate_extract_json method
  • Session class search method
  • Session class get_size method
  • Response class get_size method
  • Prompt class get_size method
  • PromptTemplate class get_size method
  • Session class size attribute
  • Response class size attribute
  • Prompt class size attribute
  • PromptTemplate class size attribute
  • examples directory

Changed

  • Validation bug fixed in update_messages method in Session class
  • Validation bug fixed in from_json method in PromptTemplate, Response, Prompt, and Session classes
  • AI_STUDIO render format modified
  • Session class messages status bug fixed
  • Test system modified
  • README.md updated

0.6 - 2025-05-05

Added

  • Response class id property
  • Prompt class id property
  • Response class regenerate_id method
  • Prompt class regenerate_id method
  • Session class render_counter method
  • Session class remove_message_by_index and remove_message_by_id methods
  • Session class get_message_by_index, get_message_by_id and get_message methods
  • LLMModel enum
  • AI_STUDIO render format

Changed

  • Test system modified
  • Modification handling centralized via _mark_modified method
  • Session class remove_message method modified

0.5 - 2025-04-16

Added

  • Session class check_render method
  • Session class clear_messages method
  • Prompt class check_render method
  • Session class estimate_tokens method
  • Prompt class estimate_tokens method
  • Response class estimate_tokens method
  • universal_tokens_estimator function
  • openai_tokens_estimator_gpt_3_5 function
  • openai_tokens_estimator_gpt_4 function

Changed

  • init_check parameter added to Prompt class
  • init_check parameter added to Session class
  • Test system modified
  • Python 3.6 support dropped
  • README.md updated

0.4 - 2025-03-17

Added

  • Session class __contains__ method
  • Session class __getitem__ method
  • Session class mask_message method
  • Session class unmask_message method
  • Session class masks attribute
  • Response class __len__ method
  • Prompt class __len__ method

Changed

  • inference_time parameter added to Response class
  • README.md updated
  • Test system modified
  • Python typing features added to all modules
  • Prompt class default values updated
  • Response class default values updated

0.3 - 2025-03-08

Added

  • Session class __len__ method
  • Session class __iter__ method
  • Session class __add__ and __radd__ methods

Changed

  • tokens parameter added to Prompt class
  • tokens parameter added to Response class
  • tokens parameter added to preset templates
  • Prompt class modified
  • Response class modified
  • PromptTemplate class modified

0.2 - 2025-03-01

Added

  • Session class

Changed

  • Prompt class modified
  • Response class modified
  • PromptTemplate class modified
  • README.md updated
  • Test system modified

0.1 - 2025-02-12

Added

  • Prompt class
  • Response class
  • PromptTemplate class
  • PresetPromptTemplate class

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

memor-0.8.tar.gz (40.2 kB view details)

Uploaded Source

Built Distribution

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

memor-0.8-py3-none-any.whl (31.9 kB view details)

Uploaded Python 3

File details

Details for the file memor-0.8.tar.gz.

File metadata

  • Download URL: memor-0.8.tar.gz
  • Upload date:
  • Size: 40.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for memor-0.8.tar.gz
Algorithm Hash digest
SHA256 224355c750647a3c1fbf6f205266dd21ab1860ba92364240e9a1194054bc3d84
MD5 6332a20a271c940b60e7986f81b7b627
BLAKE2b-256 c1d08b96b5d4fac38a2618f65edea99cac6dc93f4c18b5d381c5d88a42d0a859

See more details on using hashes here.

File details

Details for the file memor-0.8-py3-none-any.whl.

File metadata

  • Download URL: memor-0.8-py3-none-any.whl
  • Upload date:
  • Size: 31.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for memor-0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 bd5ed7f4ff7e6316fa97333b8262d4656bf112e057638c192e06af578c9234dc
MD5 1bad4af61b6ef8fa1be791b310830453
BLAKE2b-256 c1e0ff4945b6fe55ca941ae879cacd6f697e3c29988c53ee816f50645c6a0ac3

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