... made with 💙 by the team at Prefect.
pip install marvin
Getting started? Head over to our setup guide.
Marvin's high-level abstractions are familiar Python interfaces that make it easy to leverage AI in your application. These interfaces aim to be simple and self-documenting, adding a touch of AI magic to everyday objects.
🪄 AI Functions for complex business logic and transformations
🧩 AI Models for structuring text into type-safe schemas
🤖 (beta) Assistants for building stateful natural language interfaces
AI Functions look like regular functions, but have no source code. Instead, an AI interprets their description and inputs to generate their outputs, making them ideal for general NLP applications like sentiment analysis.
You can learn more about AI Functions here.
from marvin import ai_fn
@ai_fn
def sentiment(text: str) -> float:
"""Given `text`, returns a number between 1 (positive) and -1 (negative)
indicating its sentiment score.
"""
sentiment("I love working with Marvin!") # 0.8
sentiment("These examples could use some work...") # -0.2
🎬 You can define your own types for AI Functions to return, using things like:
from typing_extensions import TypedDict
class DetailedSentiment(TypedDict):
"""A detailed sentiment analysis result.
- `sentiment_score` is a number between 1 (positive) and -1 (negative)
- `summary_in_a_word` is a one-word summary of the general sentiment,
use any apt word that captures the nuance of the sentiment
"""
sentiment_score: float
summary_in_a_word: str
@ai_fn
def detailed_sentiment(text: str) -> DetailedSentiment:
"""What do you think the sentiment of `text` is?
Use your theory of mind to put yourself in the shoes of its author.
"""
detailed_sentiment("I'ma Mario, and I'ma gonna wiiiiin!")
# {'sentiment_score': 0.8, 'summary_in_a_word': 'energetic'}
AI models are based on Pydantic's BaseModel, but with a twist: they are instantiated with plain text, and will use an LLM to infer their values.
You can learn more about AI models here.
from marvin import ai_model
from pydantic import BaseModel, Field
@ai_model
class Location(BaseModel):
"""A city in the United States"""
city: str
state: str = Field(..., description="The two-letter state abbreviation")
Location("The Big Apple")
# Location(city='New York', state='NY')
Based on OpenAI's Assistant API, Marvin's Assistants are the easiest way to build a stateful natural language interface equipped with familiar tools (i.e. python functions).
from marvin.beta.assistants import Assistant, Thread
def multiply(x: float, y: float) -> float:
return x * y
def divide(x: float, y: float) -> float:
return x / y
with Assistant(tools=[multiply, divide]) as assistant:
thread = Thread()
while True:
message = input("You: ")
if message.lower() in ["exit", ":q", "bye"]:
break
thread.add(message)
thread.run(assistant)
print("\n\n".join(m.content[0].text.value for m in thread.get_messages()))
# what is the speed of light (m/s) times the number of days in a year?
# what is that number divided by 42?
Read more about our SDK and/or the OpenAI docs.
💡 Have an idea for a feature? toss it in #development
in our Discord
🐛 found a bug? feel free to open an issue