Describe your data and let AI do the rest! (With only 8 extra lines of code)

Eduard Knezovic - Feb 16 - - Dev Community

"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:

  1. Declarative Power: Define what you want, not how to get it - LLMs are smart enough to pick up the cue
  2. Composable Pipelines: Allows you to stack LLM powered Pydantic models like Lego blocks
  3. Self-Documenting: Write documentation for humans and LLMs at the same time!
  4. 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")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Image description

Exercises

To consolidate your knowledge:

  1. Execute the full code example successfully on your computer
  2. Generate the story for teenagers (uncomment one line of code)
  3. Create a StorySummarized Pydantic model that should summarize the story
  4. 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
Enter fullscreen mode Exit fullscreen mode

What do you think about this approach? I would love to hear your thoughts and suggestions.

. .