This directory contains examples demonstrating the @generative decorator for creating type-safe, composable LLM functions.
Basic introduction to generative stubs with sentiment classification and text summarization.
Key Features:
- Using
@generativedecorator for type-safe LLM functions - Literal types for constrained outputs
- Docstrings as prompts for the LLM
- Simple function composition
Shows how to use generative stubs with custom context and grounding information.
Demonstrates using generative stubs for mathematical reasoning tasks (GSM8K dataset).
Combines generative stubs with requirements for validated outputs.
A more complex example using generative stubs for financial analysis.
Subdirectory with examples of composing multiple generative functions together.
- Type Safety: Using Python type hints for structured outputs
- Docstring Prompts: Leveraging docstrings as instructions to the LLM
- Composition: Building complex workflows from simple generative functions
- Requirements: Adding validation to generative functions
- Context Management: Using grounding context with generative stubs
from mellea import generative, start_session
from typing import Literal
@generative
def classify_sentiment(text: str) -> Literal["positive", "negative"]:
"""Classify the sentiment of the given text."""
@generative
def generate_summary(text: str) -> str:
"""Generate a concise summary under 20 words."""
with start_session() as m:
sentiment = classify_sentiment(m, text="I love this!")
summary = generate_summary(m, text="Long document...")- See
mellea/stdlib/components/genstub.pyfor implementation - See
docs/dev/mellea_library.mdfor design philosophy