Skip to main content

A library for managing LLM conversations and context

Project description

Generic LLM Memorizer

Library for managing AI agent memory through automatic fact extraction, knowledge graph building, and SKOS ontology storage.

Features

  • Fact Extraction: Automatically extracts relevant facts from conversations (preferences, skills, context, events)
  • User Profile: Builds and maintains a user profile (name, age, hobbies, occupation)
  • Conversation Summary: Generates concise summaries of conversations
  • Knowledge Graph: Builds an RDF-style knowledge graph as triplets (Subject → Predicate → Object)
  • SKOS Knowledge Base: Stores ontological concepts following the SKOS standard
  • Search: Semantic search through facts and SKOS concepts
  • Persistence: All data stored in JSON format

Installation

uv sync

Usage

Basic Setup

import asyncio
from generic_llm_memorizer.llm_memorizer import LLMMemorizer
from pydantic import BaseModel

# Define your LLM call function
async def llm_call(prompt: str, system_prompt: str, response_model: type[BaseModel]):
    # Implement your LLM call here (OpenAI, Anthropic, Ollama, etc.)
    # Return an instance of response_model
    pass

# Initialize memorizer with a list of conversation messages (strings)
memorizer = LLMMemorizer(
    user_id="user_123",
    session_id="session_1",
    llm_call=llm_call,
    conversation=[
        "User: Bonjour, je m'appelle Marie et je suis développeuse Python",
        "Assistant: Enchantée Marie ! Bienvenue."
    ]
)

# Consolidate memory (extract facts, summary, knowledge graph, SKOS)
memory, summary = await memorizer.consolidate()
print(f"Summary: {summary}")
print(f"Extracted {len(memory.facts)} facts")

Fact Structure

Each fact contains:

  • content: The fact text
  • kind: Category (preference, context, skill, event, opinion, demographic)
  • timestamp: Extraction date
  • duration_validity: Validity period (P1Y, P3M, P7D, etc.)

Knowledge Graph

Concepts stored as RDF triplets:

{
  "concepts": [
    {
      "subject": {"name": "Marie", "type": "person"},
      "predicate": {"name": "has profession", "type": "relation"},
      "object": {"name": "developer", "type": "occupation"}
    }
  ]
}

SKOS Knowledge Base

Ontological concepts following SKOS standard:

{
  "scheme": {"prefLabel": "User Knowledge Database - user_123"},
  "concepts": [
    {
      "prefLabel": "python-developer",
      "definition": "A developer specialized in Python",
      "broader": ["developer"],
      "related": ["programming", "software-engineering"]
    }
  ]
}

Searching

# Search facts
relevant_facts = await memorizer.search_facts("What does the user like?")

# Search SKOS concepts
relevant_concepts = await memorizer.search_skos("development skills")

Storage

Data persisted to ./memories/{user_id}/:

  • memory.json - User profile and facts
  • knowledge_graph.json - RDF triplets
  • skos_knowledge_db.json - SKOS ontology
  • {session_id}/conversation.json - Conversation history

Architecture

LLMMemorizer
├── memory/          → User profile + facts (memory.json)
├── knowledge_graph  → RDF triplets (knowledge_graph.json)
├── skos_knowledge_db → SKOS concepts (skos_knowledge_db.json)
└── conversation     → Message history

Dependencies

  • Python 3.12+
  • pydantic (data models)
  • pytest (testing)

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

generic_llm_memorizer-0.1.5.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

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

generic_llm_memorizer-0.1.5-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file generic_llm_memorizer-0.1.5.tar.gz.

File metadata

File hashes

Hashes for generic_llm_memorizer-0.1.5.tar.gz
Algorithm Hash digest
SHA256 ee67e3748b0a2fd9749d3bb238db0d840c7d6ab55a9167674d5e600a92f3a210
MD5 347a5026720d2d81a92530b48d8bb30a
BLAKE2b-256 a6095159434986b37bb8d91c80a8a577543469ff95b4bb22cf75ed6410e90e73

See more details on using hashes here.

File details

Details for the file generic_llm_memorizer-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for generic_llm_memorizer-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 898295f2e7eacbdf46be3e0433f6c5d40204c8b6a4a79b2424a04431ee36d347
MD5 04f3b759370d1905e357908f90f180de
BLAKE2b-256 37d67b0cda87c74379d8c549e2c15aa4776484276b353eae6a8f1736813c3ee8

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