Marvin AI Tutorial: A Lightweight and Powerful AI Engineering Toolkit
Marvin is a Python library designed to make AI engineering as easy as possible. Unlike heavyweight frameworks such as LangChain or LlamaIndex, Marvin takes a minimalist approach by providing simple building blocks that you can use right away: AI functions, classifiers, extractors, and models. Just one decorator or one function call, and the LLM works for you.In this tutorial, we will explore all of Marvin's key features from text classification to building complex data transformation pipelines.
What is Marvin?
Marvin is an AI engineering toolkit developed by the Prefect team. The library is built on the philosophy that AI should be a regular software component - not something that requires complex setup or layers of abstraction.
Key features of Marvin:
- aifn: Turn regular Python functions into AI-powered functions
- aiclassifier: Classify text into predefined categories
- aiextract: Extract entities and structured information from text
- aimodel: Generate structured data using Pydantic models
- Image processing: Analyze and process images with AI
- Casting/Mapping: Transform data with AI
- Async support: Full support for asynchronous operations
Installation and Setup
Package Installation
# Install Marvin
pip install marvin
For image processing features
pip install "marvin[image]"
For all features
pip install "marvin[all]"
API Key Configuration
# Set environment variable for OpenAI
export OPENAIAPIKEY="sk-your-api-key-here"
# Or configure via Python
import marvin
marvin.settings.openai.apikey = "sk-your-api-key-here"
Optional: set default model
marvin.settings.openai.chat.completions.model = "gpt-4o-mini"
AI Functions (aifn)
aifn is the most iconic feature of Marvin. You simply write a function signature and docstring, and Marvin uses an LLM to implement the logic.
Basic Examples
import marvin
@marvin.fn
def sentiment(text: str) -> str:
"""Analyze the sentiment of the text and return 'positive', 'negative', or 'neutral'."""
result = sentiment("This product is amazing! Highly satisfied!")
print(result) # Output: positive
result = sentiment("The service was very disappointing")
print(result) # Output: negative
AI Functions with Complex Return Types
from typing import List, Dict
import marvin
@marvin.fn
def generatehashtags(topic: str, count: int = 5) -> List[str]:
"""Generate relevant hashtags for the given topic.
Hashtags should be popular and relevant for social media."""
hashtags = generatehashtags("machine learning tutorials", count=7)
print(hashtags)
Output: ['#MachineLearning', '#MLTutorial', '#DataScience', ...]
@marvin.fn
def translatephrases(phrases: List[str], targetlanguage: str) -> Dict[str, str]:
"""Translate each phrase to the target language.
Return a dictionary with original phrase as key and translation as value."""
result = translatephrases(
["Selamat pagi", "Terima kasih", "Apa kabar?"],
targetlanguage="English"
)
print(result)
Output: {'Selamat pagi': 'Good morning', 'Terima kasih': 'Thank you', ...}
AI Functions for Business Logic
import marvin
@marvin.fn
def categorizeexpense(description: str) -> str:
"""Categorize a business expense into one of these categories:
'travel', 'meals', 'officesupplies', 'software', 'marketing',
'utilities', 'payroll', 'other'."""
@marvin.fn
def extractactionitems(meetingnotes: str) -> List[str]:
"""Extract action items from meeting notes.
Each item should be clear and actionable."""
@marvin.fn
def generateemailreply(
originalemail: str,
tone: str = "professional",
keypoints: List[str] = None
) -> str:
"""Generate an email reply based on the original email.