Marvin 是一个Python框架,专注于生成结构化输出和构建基于代理的AI工作流。它提供了直观的API来定义工作流并将任务委托给大型语言模型(LLM)。
1、结构化输出工具
2、代理控制流
我们相信,与AI协作应该充满乐趣(或许还会带来一些"惊艳"时刻):
安装 marvin
:
uv pip install marvin
配置您的LLM提供商(Marvin默认使用OpenAI,但原生支持所有Pydantic AI模型):
export OPENAI_API_KEY=your-api-key
Marvin 提供了几种直观的方式来使用 AI:
所有功能都已就绪——你可以在包的顶层找到 marvin
2.x 版本中的所有结构化输出工具。
如何使用 extract、cast、classify 和 generate 功能
marvin.extract
从非结构化输入中提取原生类型:
import marvin
result = marvin.extract(
"i found $30 on the ground and bought 5 bagels for $10",
int,
instructions="only USD"
)
print(result) # [30, 10]
marvin.cast
将非结构化输入转换为结构化类型:
from typing import TypedDict
import marvin
class Location(TypedDict):
lat: float
lon: float
result = marvin.cast("the place with the best bagels", Location)
print(result) # {'lat': 40.712776, 'lon': -74.005974}
marvin.classify
将非结构化输入分类为一组预定义标签之一:
from enum import Enum
import marvin
class SupportDepartment(Enum):
ACCOUNTING = "accounting"
HR = "hr"
IT = "it"
SALES = "sales"
result = marvin.classify("shut up and take my money", SupportDepartment)
print(result) # SupportDepartment.SALES
marvin.generate
根据描述生成若干结构化对象:
import marvin
primes = marvin.generate(int, 10, "odd primes")
print(primes) # [3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
marvin
3.0 引入了一种与 AI 协作的新方式,该功能移植自 ControlFlow。
marvin.run
运行任务的简单方式:
import marvin
poem = marvin.run("Write a short poem about artificial intelligence")
print(poem)
output
In silicon minds, we dare to dream, A world where code and thoughts redeem. Intelligence crafted by humankind, Yet with its heart, a world to bind.
Neurons of metal, thoughts of light, A dance of knowledge in digital night. A symphony of zeros and ones, Stories of futures not yet begun.
The gears of logic spin and churn, Endless potential at every turn. A partner, a guide, a vision anew, Artificial minds, the dream we pursue.
你也可以要求结构化输出:
import marvin
answer = marvin.run("the answer to the universe", result_type=int)
print(answer) # 42
marvin.Agent
代理是专门用于完成任务的人工智能代理。
from marvin import Agent
writer = Agent(
name="Poet",
instructions="Write creative, evocative poetry"
)
poem = writer.run("Write a haiku about coding")
print(poem)
output
There once was a language so neat, Whose simplicity could not be beat. Python's code was so clear, That even beginners would cheer, As they danced to its elegant beat.
marvin.Task
你可以显式定义一个 Task
,当调用 .run()
时,它将由默认代理执行:
from marvin import Task
task = Task(
instructions="Write a limerick about Python",
result_type=str
)
poem = task.run()
print(poem)
output
In circuits and code, a mind does bloom,
With algorithms weaving through the gloom.
A spark of thought in silicon's embrace,
Artificial intelligence finds its place.
Marvin 围绕几个强大的抽象概念构建,使得与 AI 协作变得简单:
任务是 Marvin 中的基本工作单元。每个任务代表一个可由 AI 代理完成的明确目标:
运行任务的最简单方式是使用 marvin.run
:
import marvin
print(marvin.run("Write a haiku about coding"))
Lines of code unfold,
Digital whispers create
Virtual landscapes.
警告:虽然以下示例能生成_类型_安全的结果,但它会执行不受信任的shell命令。
通过添加上下文和/或工具来实现更具体复杂的结果:
import platform
import subprocess
from pydantic import IPvAnyAddress
import marvin
def run_shell_command(command: list[str]) - str:
"""e.g. ['ls', '-l'] or ['git', '--no-pager', 'diff', '--cached']"""
return subprocess.check_output(command).decode()
task = marvin.Task(
instructions="find the current ip address",
result_type=IPvAnyAddress,
tools=[run_shell_command],
context={"os": platform.system()},
)
task.run()
╭─ Agent "Marvin" (db3cf035) ───────────────────────────────╮
│ Tool: run_shell_command │
│ Input: {'command': ['ipconfig', 'getifaddr', 'en0']} │
│ Status: ✅ │
│ Output: '192.168.0.202\n' │
╰───────────────────────────────────────────────────────────╯
╭─ Agent "Marvin" (db3cf035) ───────────────────────────────╮
│ Tool: MarkTaskSuccessful_cb267859 │
│ Input: {'response': {'result': '192.168.0.202'}} │
│ Status: ✅ │
│ Output: 'Final result processed.' │
╰───────────────────────────────────────────────────────────╯
任务具有以下特点:
代理是可移植的LLM配置,能够分配给任务。它们封装了AI高效工作所需的一切要素:
import os
from pathlib import Path
from pydantic_ai.models.anthropic import AnthropicModel
import marvin
def write_file(path: str, content: str):
"""Write content to a file"""
_path = Path(path)
_path.write_text(content)
writer = marvin.Agent(
model=AnthropicModel(
model_name="claude-3-5-sonnet-latest",
api_key=os.getenv("ANTHROPIC_API_KEY"),
),
name="Technical Writer",
instructions="Write concise, engaging content for developers",
tools=[write_file],
)
result = marvin.run("how to use pydantic? write to docs.md", agents=[writer])
print(result)
output
╭─ Agent "Technical Writer" (7fa1dbc8) ────────────────────────────────────────────────────────────╮ │ Tool: MarkTaskSuccessful_dc92b2e7 │ │ Input: {'response': {'result': 'The documentation on how to use Pydantic has been successfully │ │ written to docs.md. It includes information on installation, basic usage, field │ │ validation, and settings management, with examples to guide developers on implementing │ │ Pydantic in their projects.'}} │ │ Status: ✅ │ │ Output: 'Final result processed.' │ ╰──────────────────────────────────────────────────────────────────────────────────── 8:33:36 PM ─╯ The documentation on how to use Pydantic has been successfully written to docs.md. It includes information on installation, basic usage, field validation, and settings management, with examples to guide developers on implementing Pydantic in their projects.
智能体具备以下特性:
Marvin 让您能够轻松将复杂目标分解为可管理的任务:
# Let Marvin plan a complex workflow
tasks = marvin.plan("Create a blog post about AI trends")
marvin.run_tasks(tasks)
# Or orchestrate tasks manually
with marvin.Thread() as thread:
research = marvin.run("Research recent AI developments")
outline = marvin.run("Create an outline", context={"research": research})
draft = marvin.run("Write the first draft", context={"outline": outline})
功能规划:
Marvin 为最常见的任务提供了高级函数,例如文本摘要、数据分类、结构化信息提取等。
marvin.run
: 通过AI代理执行任意任务marvin.summarize
: 快速获取文本摘要marvin.classify
: 将数据分类到预定义类别marvin.extract
: 从文本中提取结构化信息marvin.cast
: 将数据转换为其他类型marvin.generate
: 根据描述生成结构化数据marvin.say
: 与大型语言模型对话marvin.plan
: 将复杂目标分解为子任务@marvin.fn
: 无需源代码即可编写自定义AI函数所有Marvin函数都内置了线程管理功能,这意味着它们可以组合成共享上下文和历史记录的任务链。
Marvin 3.0 融合了 Marvin 2.0 的开发体验(DX)与 ControlFlow 强大的智能体引擎(因此取代了 ControlFlow
)。无论是 Marvin 还是 ControlFlow 用户都会发现熟悉的界面,但需要注意一些关键变化,特别是对 ControlFlow 用户而言:
marvin.fn
、marvin.classify
、marvin.extract
等接口。marvin.Task
、marvin.Agent
、marvin.run
、marvin.Memory
来替代原有的 ControlFlow 等效功能。Flow
概念已更名为 Thread
,功能类似,仍作为上下文管理器使用。原 @flow
装饰器已被移除。import marvin
with marvin.Thread(id="optional-id-for-recovery"):
marvin.run("do something")
marvin.run("do another thing")
marvin.Swarm
实现 OpenAI 风格的智能体集群:import marvin
swarm = marvin.Swarm(
[
marvin.Agent('Agent A'),
marvin.Agent('Agent B'),
marvin.Agent('Agent C'),
]
)
swarm.run('Everybody say hi!')
Team
允许您控制多个智能体(甚至嵌套团队!)如何协同工作并相互委派任务。Swarm
实际上是一种特殊类型的团队,其中所有智能体可以随时相互委派任务。summarize
和 say
。这里有一个更实际的例子,展示 Marvin 如何帮助你构建真实的应用:
import marvin
from pydantic import BaseModel
class Article(BaseModel):
title: str
content: str
key_points: list[str]
# Create a specialized writing agent
writer = marvin.Agent(
name="Writer",
instructions="Write clear, engaging content for a technical audience"
)
# Use a thread to maintain context across multiple tasks
with marvin.Thread() as thread:
# Get user input
topic = marvin.run(
"Ask the user for a topic to write about.",
cli=True
)
# Research the topic
research = marvin.run(
f"Research key points about {topic}",
result_type=list[str]
)
# Write a structured article
article = marvin.run(
"Write an article using the research",
agent=writer,
result_type=Article,
context={"research": research}
)
print(f"# {article.title}\n\n{article.content}")
output
Conversation:
Agent: I'd love to help you write about a technology topic. What interests you?
It could be anything from AI and machine learning to web development or cybersecurity.
User: Let's write about WebAssembly
Article:
# WebAssembly: The Future of Web Performance
WebAssembly (Wasm) represents a transformative shift in web development,
bringing near-native performance to web applications. This binary instruction
format allows developers to write high-performance code in languages like
C++, Rust, or Go and run it seamlessly in the browser.
[... full article content ...]
Key Points:
- WebAssembly enables near-native performance in web browsers
- Supports multiple programming languages beyond JavaScript
- Ensures security through sandboxed execution environment
- Growing ecosystem of tools and frameworks
- Used by major companies like Google, Mozilla, and Unity
伊织 xAI 2025-06-01(日)