Skip to main content

A generic, high-performance, low-dependency LLM programming framework inspired by dspy

Project description

LogiLLM

Program language models with structured inputs and outputs, built for production.

LogiLLM lets you define what you want from an LLM using Python signatures, then handles prompt engineering, output parsing, and optimization automatically.

Quick Example

from logillm.core.predict import Predict
from logillm.providers import create_provider, register_provider

# Setup (one time)
provider = create_provider("openai")
register_provider(provider, set_default=True)

# Define what you want
analyzer = Predict("text -> sentiment: str")

# Use it
result = await analyzer(text="I love this new feature!")
print(result.sentiment)  # "positive"

Instead of crafting prompts and parsing responses, you specify the input/output structure. LogiLLM handles the rest.

What You Can Build

๐Ÿ“ง Smart Email Processing - Classify, extract entities, determine urgency
๐Ÿ“ Document Generation - Create structured reports from data
๐Ÿค– Reasoning Agents - Multi-step analysis with external tool integration
๐ŸŽฎ Interactive Systems - Games, chatbots, personalized experiences
๐Ÿ’พ Persistent AI - Systems that remember and learn from interactions

Key Features

  • ๐ŸŽฏ Structured I/O - Type-safe inputs and outputs with automatic validation
  • โšก Zero Dependencies - Core library uses only Python standard library
  • ๐Ÿš€ Production Ready - Full async support, comprehensive error handling, observability
  • ๐Ÿ”ง Auto-Optimization - Improve performance by optimizing prompts AND hyperparameters
  • ๐Ÿ’พ Built-in Persistence - Save and load optimized models instantly
  • ๐Ÿ“Š JSONL Logging - Track optimization runs with complete reproducibility
  • ๐Ÿ” Complete Debug Logging - Capture full request/response data from LLM APIs

Installation

# Core library 
pip install logillm

# With LLM providers
pip install logillm[openai]     # For GPT models
pip install logillm[anthropic]  # For Claude models

Learning Path

๐Ÿ“š Complete Documentation

โ†’ Full Tutorial Index - 6 complete tutorials from beginner to advanced

Working Examples

Basic Classification

# Classify text into categories
classifier = Predict("text -> category: str")
result = await classifier(text="I need help with my billing account")
print(result.category)  # "Billing"

Multi-Output Prediction

# Get multiple outputs in one call
analyzer = Predict("text -> sentiment: str, confidence: float")
result = await analyzer(text="This product is amazing!")
print(result.sentiment)   # "positive"  
print(result.confidence)  # 0.97

Complex Type Support (New!)

# Work with lists, dictionaries, and optional types
extractor = Predict("document: str -> entities: dict[str, list[str]], summary: Optional[str]")
result = await extractor(document="Apple Inc. announced iPhone 15 in Cupertino.")
print(result.entities)  # {"companies": ["Apple Inc."], "products": ["iPhone 15"], "locations": ["Cupertino"]}

# Use Union types with pipe syntax
processor = Predict("data: str | bytes -> processed: bool, format: str")

Multimodal Capabilities (New!)

from logillm.core.signatures.types import Image, Audio, Tool

# Vision analysis
vision = Predict("image: Image -> description: str, objects: list[str], confidence: float")
result = await vision(image=Image.from_path("photo.jpg"))
print(result.objects)  # ["person", "laptop", "coffee"]

# Audio processing
transcriber = Predict("audio: Audio -> transcript: str, language: str")
result = await transcriber(audio=Audio.from_url("https://example.com/speech.mp3"))

# Tool/Function calling
tool_agent = Predict("query: str -> tool_calls: list[Tool], explanation: str")

Structured Signatures with Field Validation (Enhanced!)

from logillm.core.signatures import Signature, InputField, OutputField

class TextAnalysis(Signature):
    # Required and optional fields with defaults
    text: str = InputField(description="Text to analyze")
    max_length: int = InputField(default=100, description="Maximum response length")
    
    # Output fields with type hints
    category: str = OutputField(description="Category like support, sales, billing")
    priority: int = OutputField(description="Priority 1-5 where 5 is most urgent")
    entities: list[str] = OutputField(description="Named entities found")

analyzer = Predict(signature=TextAnalysis)
result = await analyzer(text="My account is locked and I cannot access my files")
# Automatically validates inputs and outputs against field specifications
print(result.category)  # "Account Access"
print(result.priority)  # 1

Production Features

Multiple Providers

# Set up multiple LLM providers  
openai_provider = create_provider("openai", model="gpt-4.1")
register_provider(openai_provider, "fast")

# Use the registered provider (becomes default)
result = await Predict("text -> summary: str")(text="Long document to summarize...")
print(result.summary)  # "This document discusses..."

Optimization (requires training data)

# Optimize performance on your data
from logillm.optimizers import HybridOptimizer
from logillm.core.optimizers import AccuracyMetric

# Your labeled training examples
training_data = [
    {"inputs": {"text": "Great service!"}, "outputs": {"sentiment": "positive"}},
    {"inputs": {"text": "Poor quality"}, "outputs": {"sentiment": "negative"}},
    # ... more examples
]

# Optimize both prompts and hyperparameters  
classifier = Predict("text -> sentiment: str")
optimizer = HybridOptimizer(metric=AccuracyMetric(key="sentiment"))
result = await optimizer.optimize(module=classifier, dataset=training_data)

# Save optimized model
result.optimized_module.save("sentiment_model.json")

# Load in production
classifier = Predict.load("sentiment_model.json")

Optimizer Comparison

LogiLLM provides a comprehensive suite of optimizers, each designed for different optimization scenarios:

Optimizer Type What it Optimizes Best For Key Features
HybridOptimizer โญ Hybrid Prompts + Hyperparameters Production systems LogiLLM's killer feature - simultaneous optimization
SIMBA Hybrid Hyperparameters + Demos Complex tasks Introspective rule generation with mini-batches
MIPROv2 Prompt Instructions + Demos Multi-stage optimization Bayesian optimization with multi-objective support
COPRO Prompt Instructions Instruction refinement Breadth-first search with temperature control
BootstrapFewShot Prompt Few-shot examples Learning from data Teacher-student demonstration generation
KNNFewShot Prompt Example selection Dynamic examples Semantic similarity-based selection
FormatOptimizer Prompt Output format Format discovery Tests JSON vs XML vs Markdown
InstructionOptimizer Prompt Task instructions Instruction clarity LLM-based instruction generation
LabeledFewShot Prompt Hand-crafted examples Baseline comparison Traditional few-shot approach
HyperparameterOptimizer Hyperparameter Temperature, top_p, etc. Parameter tuning Bayesian, Grid, Random search
AvatarOptimizer Ensemble Multiple personas Complex reasoning Multi-perspective ensemble
ReflectiveEvolution Prompt Execution traces Self-improvement LLM reflection on past runs
MultiObjective Hybrid Multiple metrics Trade-off optimization Balance accuracy, cost, latency

Optimizer Taxonomy

graph LR
    subgraph "Prompt Optimizers"
        direction TB
        P1[BootstrapFewShot<br/>๐Ÿ“š Teacher-Student Learning]
        P2[COPRO<br/>๐Ÿ”„ Instruction Refinement]
        P3[KNNFewShot<br/>๐ŸŽฏ Semantic Selection]
        P4[FormatOptimizer<br/>๐Ÿ“ JSON/XML/Markdown]
        P5[InstructionOptimizer<br/>๐Ÿ’ก LLM Generation]
        P6[LabeledFewShot<br/>โœ‹ Manual Examples]
    end
    
    subgraph "Hyperparameter Optimizers"
        direction TB
        H1[HyperparameterOptimizer<br/>๐ŸŽ›๏ธ Bayesian Search]
        H2[GridSearch<br/>๐Ÿ“Š Systematic]
        H3[RandomSearch<br/>๐ŸŽฒ Stochastic]
    end
    
    subgraph "Hybrid Optimizers"
        direction TB
        Y1[HybridOptimizer โญ<br/>๐Ÿš€ Prompts + Params]
        Y2[SIMBA<br/>๐Ÿง  Rules + Params]
        Y3[MIPROv2<br/>๐Ÿ”€ Multi-stage Pipeline]
        Y4[MultiObjective<br/>โš–๏ธ Balance Metrics]
    end
    
    subgraph "Specialized"
        direction TB
        S1[AvatarOptimizer<br/>๐Ÿ‘ฅ Multi-persona]
        S2[ReflectiveEvolution<br/>๐Ÿ” Self-improvement]
    end
    
    style Y1 fill:#f96,stroke:#333,stroke-width:3px
    style Y2 fill:#fcc,stroke:#333,stroke-width:2px
    style Y3 fill:#fcc,stroke:#333,stroke-width:2px

Optimization Workflow

graph LR
    A[๐Ÿ“Š Training Data] --> B{Choose Optimizer}
    B -->|Simple Task| C[Prompt Only]
    B -->|Parameter Tuning| D[Hyperparameter Only]
    B -->|Best Results| E[Hybrid Strategy โญ]
    
    E --> F{Select Strategy}
    F -->|Alternating| G[Prompts โ†’ Params โ†’ Repeat]
    F -->|Joint| H[Simultaneous Optimization]
    F -->|Sequential| I[Params โ†’ Then Prompts]
    
    C --> J[Evaluate Performance]
    D --> J
    G --> J
    H --> J
    I --> J
    
    J --> K{Good Enough?}
    K -->|No| L[Adjust & Retry]
    K -->|Yes| M[๐Ÿ’พ Save Model]
    
    L --> B
    M --> N[๐Ÿš€ Deploy to Production]
    
    N --> O[Load & Use]
    O --> P[Monitor Performance]
    
    style E fill:#f96,stroke:#333,stroke-width:3px
    style M fill:#9f9,stroke:#333,stroke-width:2px
    style N fill:#9f9,stroke:#333,stroke-width:2px

Choosing the Right Optimizer

For most use cases: Start with HybridOptimizer - it optimizes both prompts and hyperparameters:

optimizer = HybridOptimizer(
    metric=your_metric,
    strategy="alternating"  # or "joint", "sequential"
)

For specific scenarios:

  • Limited training data? โ†’ Use LabeledFewShot with hand-crafted examples
  • Need dynamic examples? โ†’ Use KNNFewShot for semantic similarity selection
  • Complex multi-step tasks? โ†’ Use MIPROv2 for sophisticated pipeline optimization
  • Want to understand why it works? โ†’ Use SIMBA for introspective rule generation
  • Multiple competing objectives? โ†’ Use MultiObjective to balance trade-offs

JSONL Logging for Optimization Tracking

# Track and analyze optimization runs
from logillm.core.jsonl_logger import OptimizationLogger

logger = OptimizationLogger(filepath="optimization.jsonl")
result = await logger.log_optimization(
    optimizer=optimizer,
    module=classifier,
    dataset=training_data,
    validation_set=validation_data
)

# Analyze the log
import json
with open("optimization.jsonl") as f:
    events = [json.loads(line) for line in f]
    
# See score progression, hyperparameters, prompts, and more
for event in events:
    if event['event_type'] == 'evaluation_end':
        print(f"Score: {event['score']:.2%}")

โ†’ Full JSONL Logging Documentation

Why LogiLLM?

Coming from prompt engineering? Stop writing brittle string templates. Define structured inputs/outputs and let LogiLLM handle prompt construction.

Coming from dspy? Get better performance through hybrid optimization (prompts + hyperparameters), zero-dependency deployment, and more modern Python standards.

Building production systems? Native async support, comprehensive error handling, automatic retries, and observability built-in.

What's Different from DSPy?

  • Hybrid Optimization - Optimize both prompts AND hyperparameters simultaneously
  • Zero Dependencies - Core framework uses only Python standard library
  • Production First - Built for scaling, monitoring, and maintenance from day one
  • Type Safety - Full Pydantic integration with IDE support
  • Modern Python - Async/await throughout, Python 3.9+ features

Getting Help


Ready to build? Start with the LLM Text Generation tutorial (15 minutes) or browse all tutorials to find your perfect starting point.

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

logillm-0.2.17.tar.gz (473.2 kB view details)

Uploaded Source

Built Distribution

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

logillm-0.2.17-py3-none-any.whl (281.6 kB view details)

Uploaded Python 3

File details

Details for the file logillm-0.2.17.tar.gz.

File metadata

  • Download URL: logillm-0.2.17.tar.gz
  • Upload date:
  • Size: 473.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.12

File hashes

Hashes for logillm-0.2.17.tar.gz
Algorithm Hash digest
SHA256 e55c3cb27eba6d2151fa3b844a6d2a37e2783d41ed72d0f5926229d04f590fa1
MD5 cdd702ef9372bb8e03759411429f5566
BLAKE2b-256 d17d9f1dde5118e4b183d22fdd4ba091682d381e563c8df9cae19ad31dbd8240

See more details on using hashes here.

File details

Details for the file logillm-0.2.17-py3-none-any.whl.

File metadata

  • Download URL: logillm-0.2.17-py3-none-any.whl
  • Upload date:
  • Size: 281.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.12

File hashes

Hashes for logillm-0.2.17-py3-none-any.whl
Algorithm Hash digest
SHA256 b68d004436bacfaeabe3af0af18d1c9101cdf7c4a9d8e8865ef3c685ad0c5a02
MD5 a934b6c823fa187d58972eaa4f85ee05
BLAKE2b-256 e09be8a09b70de2857c6712deb816f00861a342b682839139b6d3749342c2d49

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