Metadata-Version: 2.4
Name: generic-llm-memorizer
Version: 0.1.5
Summary: A library for managing LLM conversations and context
Author-email: Your Name <your.email@example.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/yourusername/generic-llm-memorizer
Project-URL: Documentation, https://github.com/yourusername/generic-llm-memorizer#readme
Project-URL: Repository, https://github.com/yourusername/generic-llm-memorizer
Keywords: llm,conversation,chat,ai,prompt,context-management
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: dotenv>=0.9.9
Requires-Dist: pydantic>=2.12.5
Requires-Dist: watchdog>=6.0.0
Requires-Dist: rdflib>=7.1.0
Provides-Extra: test
Requires-Dist: pydantic-ai>=1.76.0; extra == "test"
Requires-Dist: pytest>=9.0.2; extra == "test"
Requires-Dist: pytest-asyncio>=1.3.0; extra == "test"
Dynamic: license-file

# 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

```bash
uv sync
```

## Usage

### Basic Setup

```python
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:
```json
{
  "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:
```json
{
  "scheme": {"prefLabel": "User Knowledge Database - user_123"},
  "concepts": [
    {
      "prefLabel": "python-developer",
      "definition": "A developer specialized in Python",
      "broader": ["developer"],
      "related": ["programming", "software-engineering"]
    }
  ]
}
```

### Searching

```python
# 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)
