A minimalist LLM modeling library
Project description
ModeLLM
"Controlling complexity is the essence of computer programming."
Overview
ModeLLM makes your AI workflows dead simple to build and maintain.
Your Pydantic data models become AI-powered with one line of code:
class Recipe(BaseModelAI(llm)):
name: str
ingredients: list[str]
recipe = Recipe.generate_from("chocolate chip cookies")
Chain transformations with clean, explicit syntax:
result = Recipe.generate_from(input_text)
improved = ImprovedRecipe.generate_from(result)
# Or use the pipe operator
result = input_text | Recipe | ImprovedRecipe
More Benefits
- Prompts as Documentation: Keep prompts and code together by writing prompts in Pydantic docstrings, making them easy to understand for both humans and LLMs
- Self-Documenting Workflows: Understand the entire pipeline at a glance through clear model definitions and model chains
- Production-Ready Design: Built on battle-tested libraries like Pydantic and LangChain
- Rapid Prototyping and Maintenance: Iterate quickly by swapping models and transformations with minimal code changes.
Installation
pip install modellm
ModeLLM requires Python 3.10+. All required dependencies (pydantic, langchain) will be installed automatically.
Currently, ModeLLM supports the following LangChain LLM providers:
- ChatOpenAI
- ChatAnthropic
To use them, you will need to add your OPENAI_API_KEY and/or ANTHROPIC_API_KEY to your environment variables.
Quick Start
Here's a minimal example to get you started:
from modellm import BaseModelAI
from langchain_openai import ChatOpenAI
# Set up your LLM
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.7)
# Define your model with AI capabilities
class Recipe(BaseModelAI(llm)):
"""A cooking recipe."""
name: str
ingredients: list[str]
instructions: list[str]
cooking_time: str
# Generate structured output - THREE ways:
# 1. Explicit method (recommended - full IDE autocomplete!)
recipe: Recipe = Recipe.generate_from("Fish and chips")
# 2. Pipe operator (functional style)
recipe = "Fish and chips" | Recipe
# 3. Direct call (coming soon!)
# recipe = Recipe("Fish and chips")
print(recipe)
Why BaseModelAI?
- ✨ Full IDE support -
generate_from()method autocompletes perfectly - 🎯 Conventional syntax - Follows established Python patterns like
Generic[T] - 📖 Explicit - Clear which LLM is used for each model
- 🔍 Easy to grep - Find configurations easily:
grep "BaseModelAI(" - 🚀 Type safe - Full type hints for better code quality
Alternative: Decorator Style
If you prefer decorators, you can also use the @add_llm decorator:
from pydantic import BaseModel
from modellm import add_llm
@add_llm(llm)
class Recipe(BaseModel):
"""A cooking recipe."""
name: str
ingredients: list[str]
instructions: list[str]
cooking_time: str
# Use with pipe operator
recipe = "Fish and chips" | Recipe
print(recipe)
The decorator style works great for the pipe operator pattern!
Detailed Usage
ModeLLM supports complex transformation chains and multiple LLM providers:
from modellm import BaseModelAI
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic
# Use different LLMs for different models
gpt4 = ChatOpenAI(model="gpt-4o-mini", temperature=0.7)
claude = ChatAnthropic(model="claude-3-5-sonnet-20240620", temperature=0.2)
class Recipe(BaseModelAI(gpt4)):
"""A cooking recipe."""
name: str
ingredients: list[str]
instructions: list[str]
cooking_time: str
class HealthyRecipe(BaseModelAI(claude)):
"""Recipe with minimal calories and maximal nutrients."""
name: str
ingredients: list[str]
instructions: list[str]
cooking_time: str
nutritional_info: str
class SimplifiedRecipe(BaseModelAI(gpt4)):
"""Simplified recipe with basic, everyday ingredients."""
name: str
ingredients: list[str]
instructions: list[str]
cooking_time: str
difficulty_level: str
# Chain transformations - three equivalent ways:
# Method chaining (explicit, recommended)
recipe = Recipe.generate_from("Fish and chips")
healthy = HealthyRecipe.generate_from(recipe)
simple = SimplifiedRecipe.generate_from(healthy)
# Pipe operator (functional)
simple = "Fish and chips" | Recipe | HealthyRecipe | SimplifiedRecipe
# Mixed style
recipe = Recipe.generate_from("Fish and chips")
simple = recipe | HealthyRecipe | SimplifiedRecipe
print(simple)
Contributing
Contributions are welcome! Feel free to submit a pull request!
For major changes, please open an issue first to discuss what you would like to change.
Acknowledgments
- Built on top of Pydantic and LangChain
- Inspired by the Eric S. Raymond's Art of Unix Programming
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file modellm-0.2.0.tar.gz.
File metadata
- Download URL: modellm-0.2.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
62bd68d7edb4eda6a5bb8ec408b06d34c5ce6fac5c3314f27e40f715b2e9de0a
|
|
| MD5 |
6d153969cb91391b456cbe8ab31f3eb6
|
|
| BLAKE2b-256 |
5c76f628d1884e13c40553426283be347652073f92ecb32c8a2e55f0c510c78b
|
File details
Details for the file modellm-0.2.0-py3-none-any.whl.
File metadata
- Download URL: modellm-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbdc43bec1e55b38dd5e9aad153d82794f7380ab5b30a844d15085396f68599b
|
|
| MD5 |
ca517c48af8f20f4023cf3b032c5e506
|
|
| BLAKE2b-256 |
06a6e28445dbedd0965c3bcf4255b385b4ee4de862c56e9a065077ba5e5c9ce0
|