"Good programmers worry about data structures and their relationships." - Linus Torvalds
How can we keep our attention on the data structures and their relationships while designing our AI workflows?
ModeLLM makes this possible by turning your Pydantic models into powerful AI pipeline components.
Key benefits of this approach:
- Declarative Power: Define what you want, not how to get it - LLMs are smart enough to pick up the cue
- Composable Pipelines: Allows you to stack LLM powered Pydantic models like Lego blocks
- Self-Documenting: Write documentation for humans and LLMs at the same time!
- Clean Interface: Complex AI operations hidden behind simple data models
Let's go over an example!
We will build a simple AI pipeline that generates an HTML story from a story idea.
Full code is available here.
Required setup can be found in the "Installation" part of the README.
1. Choose your preferred LLM
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini")
2. Import the modellm library
# We will use the add_llm decorator to connect our LLM to the data models
from modellm import add_llm
3. Design your AI pipeline with clean data models
We will use the add_llm
decorator to connect our LLM to the data models.
Please note that the Pydantic docstrings will be used by the LLM to generate the output.
This approach makes our code easy to understand and easy to maintain!
from pydantic import BaseModel
@add_llm(llm)
class Story(BaseModel):
title: str
content: str
genre: str
@add_llm(llm)
class StoryForBabies(Story):
"""
A story that is specifically tailored to be a baby's story.
Appropriate for infants and toddlers aged 0-2 years old.
Features:
- Simple, repetitive language
- Basic concepts
- Short sentences
- Sensory-rich descriptions
"""
# The class inherits all fields from Story
pass
@add_llm(llm)
class HtmlModel(BaseModel):
"""
An HTML representation with beautiful CSS styling.
"""
html: str
4. Execute the pipeline with a single line of code
story_idea = "A story about nature and life"
# That's it. Only one line of code
html_story = story_idea | Story | StoryForBabies | HtmlModel
5. Evaluate the end result
In very few lines of code, we were able to generate a nicely looking HTML site with an appropriate story for babies.
Exercises
To consolidate your knowledge:
- Execute the full code example successfully on your computer
- Generate the story for teenagers (uncomment one line of code)
- Create a
StorySummarized
Pydantic model that should summarize the story - Create your own Pydantic model and inject it to the pipeline
Conclusion
In this example, we've managed to harness the power of AI
in (only!) 8 additional lines of code to go from story idea to a nicely displayed HTML site - thanks to the ModeLLM library
By defining our Pydantic data models (and decorating them) we were able to execute our AI pipeline with a single line of code:
html_story = story_idea | Story | StoryForBabies | HtmlModel
What do you think about this approach? I would love to hear your thoughts and suggestions.