Skip to main content

A library for managing LLM conversations and context

Project description

Generic LLM Memorizer

A Python library for managing LLM conversations and context with automatic fact extraction and structured data handling.

Installation

pip install generic-llm-memorizer

Usage

Basic Conversation Management

from generic_llm_memorizer import Conversation

# Create a new conversation
conv = Conversation(system_prompt="You are a helpful assistant.")

# Add messages
conv.add_user_message("Hello, how are you?")
conv.add_assistant_message("I'm doing well, thank you! How can I help you today?")

# Get conversation history
messages = conv.get_messages()

# Format for LLM API (expects list of dicts with role and content)
from generic_llm_memorizer import format_conversation_for_prompt
formatted = format_conversation_for_prompt(conv)
# Output: [{"role": "system", "content": "You are a helpful assistant."}, ...]
from generic_llm_memorizer import Conversation

# Create a new conversation
conv = Conversation(system_prompt="You are a helpful assistant.")

# Add messages
conv.add_user_message("Hello, how are you?")
conv.add_assistant_message("I'm doing well, thank you! How can I help you today?")

# Get conversation history
messages = conv.get_messages()

# Format for LLM API (expects list of dicts with role and content)
from generic_llm_memorizer import format_conversation_for_prompt
formatted = format_conversation_for_prompt(conv)
# Output: [{"role": "system", "content": "You are a helpful assistant."}, ...]

Persisting Conversations

from generic_llm_memorizer import Conversation

# Save conversation to JSON
conv_json = conv.to_json()
with open("conversation.json", "w") as f:
    f.write(conv_json)

# Load conversation from JSON
with open("conversation.json", "r") as f:
    conv_json = f.read()
loaded_conv = Conversation.from_json(conv_json)
from generic_llm_memorizer import Conversation

# Save conversation to JSON
conv_json = conv.to_json()
with open("conversation.json", "w") as f:
    f.write(conv_json)

# Load conversation from JSON
with open("conversation.json", "r") as f:
    conv_json = f.read()
loaded_conv = Conversation.from_json(conv_json)

Utility Functions

Extending with Custom Models

You can create your own Pydantic models for domain-specific extractions:

from pydantic import BaseModel
from typing import List
from enum import Enum

class Priority(str, Enum):
    LOW = "low"
    MEDIUM = "medium"
    HIGH = "high"

class Task(BaseModel):
    title: str
    description: str
    priority: Priority
    assignee: str

class TaskList(BaseModel):
    tasks: List[Task]

# Use with the generic llm_call function
tasks = llm_call("Extract tasks from this conversation", TaskList)

Advanced Usage with Structured Data Extraction

The library now supports a generic llm_call function that works with Pydantic models for structured data extraction:

from typing import Type, TypeVar
from pydantic import BaseModel
from generic_llm_memorizer.conversation import Conversation
from generic_llm_memorizer.memory import Summary, Memory, Fact

# Define a generic type variable bounded by BaseModel
T = TypeVar("T", bound=BaseModel)

def llm_call(prompt: str, model_class: Type[T]) -> T:
    """
    Generic LLM call function that returns structured data.
    
    Args:
        prompt: The prompt to send to the LLM
        model_class: The Pydantic model class to parse the response into
        
    Returns:
        An instance of the specified model_class
    """
    # In a real implementation, this would call an actual LLM API
    # and parse the response into the specified Pydantic model
    
    # Mock implementation for demonstration
    if model_class == Summary:
        return Summary(content="Mock summary of the conversation")
    elif model_class == Memory:
        return Memory(facts=[
            Fact(
                content="Example fact extracted from conversation",
                kind="example",
                timestamp="2023-01-01T10:00:00Z",
                validity="forever"
            )
        ])
    
    # Return a default instance of the model
    return model_class()

# Usage with LLMMemorizer
from generic_llm_memorizer.llm_memorizer import LLMMemorizer

conversation = Conversation(system_prompt="You are a helpful assistant")
conversation.add_user_message("Hello, my name is John Doe and I'm a software engineer")

memorizer = LLMMemorizer(
    user_id="user_123",
    session_id="session_456",
    llm_call=llm_call,
    conversation=conversation
)

# Extract structured facts
memory = memorizer.extract_facts()
print(f"Extracted {len(memory.facts)} facts")

# Generate structured summary
summary = memorizer.summarize()
print(f"Summary: {summary.content}")

## API Reference

```python
from generic_llm_memorizer import extract_last_user_message, extract_last_assistant_message

# Extract last user message
last_user = extract_last_user_message(conv)

# Extract last assistant message
last_assistant = extract_last_assistant_message(conv)

API Reference

Conversation Class

Manages a conversation with an LLM.

Methods

  • add_message(role, content, metadata=None): Add a message to the conversation
  • add_user_message(content, metadata=None): Add a user message
  • add_assistant_message(content, metadata=None): Add an assistant message
  • get_messages(): Get all messages
  • get_recent_messages(count): Get the most recent messages
  • clear(): Clear all messages
  • to_dict(): Convert to dictionary format
  • to_json(indent=2): Convert to JSON string
  • from_dict(data): Create from dictionary data
  • from_json(json_str): Create from JSON string

Message Dataclass

Represents a single message in a conversation.

Attributes

  • role: Role of the message sender ("user", "assistant", "system", etc.)
  • content: Message content
  • timestamp: ISO format timestamp (automatically set)
  • metadata: Additional metadata dictionary

Utility Functions

  • format_conversation_for_prompt(conversation): Format conversation for LLM API
  • extract_last_user_message(conversation): Get last user message content
  • extract_last_assistant_message(conversation): Get last assistant message content

License

MIT

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.1.tar.gz (16.1 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.1-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for generic_llm_memorizer-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e3bfd1dbcb31f191f8425e28bb6e25a0a5cd1cc1ffe20a07e79660b1b0427a57
MD5 0ca2822271e4660d501590cfe6888d7a
BLAKE2b-256 0c7383efc1f8db10c10ad796ecf573a66d806d5a721ee76e52998fc2f455ac88

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for generic_llm_memorizer-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 09324d9690e1391be33c2f256e4945356cfa285fc6fb1a34e42fd5b45fc3c0ca
MD5 854ca22a4d7c54b2cb7edffeca157578
BLAKE2b-256 3a09693abaa4afb258774791d64b3d2690b1b892f49f14b2c018dfc2fcb8eaaf

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