Skip to content
Merged

2.0 #759

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
185 commits
Select commit Hold shift + click to select a range
5899c45
Update classifier
jlowin Jan 4, 2024
824449b
Update function
jlowin Jan 4, 2024
5f70ac5
Update model
jlowin Jan 4, 2024
44d88c6
Update image
jlowin Jan 4, 2024
6abfb32
Update speech
jlowin Jan 4, 2024
3aff6d3
add cast/extract
jlowin Jan 4, 2024
47c8ac7
Continue ai prefix cleanup
jlowin Jan 4, 2024
88222fc
Improve function behavior
jlowin Jan 4, 2024
0898659
Add classify()
jlowin Jan 4, 2024
f1c60c0
add instructions for text functions
jlowin Jan 4, 2024
5c862d4
Fix component tests
jlowin Jan 4, 2024
cb91b23
Add component tests and get them passing
jlowin Jan 5, 2024
348f451
Import literal from typing for py 3.9
jlowin Jan 5, 2024
1ea9a34
Update src/marvin/components/text.py
jlowin Jan 5, 2024
313ebc5
Update text.py
jlowin Jan 5, 2024
e96a296
Merge pull request #721 from PrefectHQ/the-great-rename
jlowin Jan 5, 2024
8fcea88
WIP
jlowin Jan 6, 2024
06b9440
update api ref / docstrings
zzstoatzz Jan 6, 2024
5f0e3c2
Merge pull request #725 from PrefectHQ/2.0-api-ref
jlowin Jan 6, 2024
76b59ed
create ai module
jlowin Jan 7, 2024
8dd440b
Continue improving tests
jlowin Jan 7, 2024
9c7bf7c
Add xdist
jlowin Jan 7, 2024
7af883d
Continue refining tests
jlowin Jan 7, 2024
61ef3c3
Try using xdist
jlowin Jan 7, 2024
be1761d
Update run-tests.yml
jlowin Jan 7, 2024
e399eee
Try 3.5
jlowin Jan 7, 2024
694206f
whitespace fix
jlowin Jan 7, 2024
97127fe
Continue tweaking prompts
jlowin Jan 7, 2024
59b0805
Update transcript logic to handle roles as first line of text
jlowin Jan 7, 2024
cbf3fc1
Specialize each prompt for performance
jlowin Jan 7, 2024
917e477
Revert settings change
jlowin Jan 7, 2024
033aa84
Fix env var reference for gpt4
jlowin Jan 7, 2024
28fbe37
Add generate
jlowin Jan 8, 2024
ba771f0
stub tests
jlowin Jan 8, 2024
5dd80f2
Use model_config
jlowin Jan 8, 2024
691e46c
Merge branch '2.0' into functional-backend
jlowin Jan 8, 2024
c46c907
Simple test to ensure it can be called
jlowin Jan 8, 2024
54ab790
Improve image/speech files
jlowin Jan 8, 2024
54fe7b2
Add LLM unit tests
jlowin Jan 8, 2024
8b18522
Add v1 compat
jlowin Jan 8, 2024
bfe31f9
Fix typo
jlowin Jan 8, 2024
787cac8
Update generate instructions
jlowin Jan 8, 2024
5f8216a
Update arg name
jlowin Jan 9, 2024
d220e95
Add classifier
jlowin Jan 9, 2024
9c4c30c
Ensure enum name/docstring are taken into account
jlowin Jan 9, 2024
6c87c68
Fix test
jlowin Jan 9, 2024
94cb19c
Ensure strings can be returned properly
jlowin Jan 10, 2024
c192b18
Use classify when casting if possible
jlowin Jan 10, 2024
81db58c
Improve classify prompt for bools
jlowin Jan 10, 2024
d04bebb
Fix enum casts for classification
jlowin Jan 10, 2024
57e48c7
Merge pull request #726 from PrefectHQ/functional-backend
jlowin Jan 10, 2024
ee0f92d
WIP
jlowin Jan 11, 2024
44fe2e2
Clean 2.0
jlowin Jan 11, 2024
616ec0b
Ensure test doesnt fail for whitespace
jlowin Jan 11, 2024
31e40e5
Merge branch 'clean-2.0' into the-great-refactor-docs
jlowin Jan 11, 2024
f2bcf89
apis → core
jlowin Jan 11, 2024
3342763
Merge branch 'clean-2.0' into the-great-refactor-docs
jlowin Jan 11, 2024
9e7cbbf
Ensure docstring is templated
jlowin Jan 11, 2024
cb54e60
Merge pull request #730 from PrefectHQ/clean-2.0
jlowin Jan 11, 2024
d079b1d
WIP docs
jlowin Jan 11, 2024
52166f1
Merge
jlowin Jan 11, 2024
da132d8
Update test_settings.py
jlowin Jan 11, 2024
c9b9a44
Merge pull request #732 from PrefectHQ/the-great-refactor-docs
jlowin Jan 11, 2024
6aef213
Add docstrings and api ref for new core tools
jlowin Jan 11, 2024
25b101b
Merge pull request #733 from PrefectHQ/docstrings
jlowin Jan 11, 2024
f85a616
Continue improving docstrings
jlowin Jan 11, 2024
7286939
Merge pull request #734 from PrefectHQ/docstrings2
jlowin Jan 11, 2024
3a90fef
Update docs nav
jlowin Jan 12, 2024
c96242f
Reorganize core → ai
jlowin Jan 12, 2024
b3698b4
requests → types
jlowin Jan 12, 2024
fb4ac30
Add vision
jlowin Jan 12, 2024
a89b882
Add beta note
jlowin Jan 12, 2024
853041a
Update captioning.md
jlowin Jan 12, 2024
9a03e8e
Update signature
jlowin Jan 12, 2024
5f7db6e
Update src/marvin/ai/prompts/vision_prompts.py
jlowin Jan 12, 2024
f879156
Update src/marvin/ai/text.py
jlowin Jan 12, 2024
e803a95
Update text.py
jlowin Jan 12, 2024
712a1d5
Update test_extract.py
jlowin Jan 12, 2024
ae0f986
Merge pull request #736 from PrefectHQ/vision
jlowin Jan 12, 2024
b21e6cd
Add beta vision functions
jlowin Jan 13, 2024
4d84b8e
Add docs
jlowin Jan 13, 2024
c12c6c6
Update tests
jlowin Jan 13, 2024
b6cff43
Update tests
jlowin Jan 13, 2024
de03466
Update tests
jlowin Jan 13, 2024
6daf8e8
Merge pull request #737 from PrefectHQ/vision
jlowin Jan 13, 2024
075d502
Improve beta segmentation
jlowin Jan 13, 2024
b6f19a4
Mark flaky
jlowin Jan 13, 2024
7a7b659
Update test_cast.py
jlowin Jan 13, 2024
dd7a8a6
Add "no_llm" marker
jlowin Jan 13, 2024
82c0d37
Remove class marker
jlowin Jan 13, 2024
d5e40b5
Merge pull request #738 from PrefectHQ/move-vision
jlowin Jan 13, 2024
323a6b6
Merge branch '2.0' into no_llm-tests
jlowin Jan 13, 2024
06311d1
Update run-tests.yml
jlowin Jan 13, 2024
7428af7
Update run-tests.yml
jlowin Jan 13, 2024
1d7cd0a
Update run-tests.yml
jlowin Jan 13, 2024
da99eaf
Update run-tests.yml
jlowin Jan 13, 2024
f8111ff
Update test_extract.py
jlowin Jan 13, 2024
103b6b3
Update run-tests.yml
jlowin Jan 13, 2024
cb45748
Merge pull request #739 from PrefectHQ/no_llm-tests
jlowin Jan 13, 2024
9251c50
add azure escape hatches
zzstoatzz Jan 13, 2024
5c34d2e
add docs
zzstoatzz Jan 13, 2024
6443d30
link readme
zzstoatzz Jan 13, 2024
be141fe
rm untrue comment
zzstoatzz Jan 13, 2024
4bf1f92
more usage
zzstoatzz Jan 13, 2024
a648525
clarify language
zzstoatzz Jan 13, 2024
cc9634f
Merge pull request #740 from PrefectHQ/azure-support
zzstoatzz Jan 13, 2024
7b9e7e1
Add assistants docs
jlowin Jan 14, 2024
ac6868b
Update assistants.md
jlowin Jan 14, 2024
5011c96
Update assistants.md
jlowin Jan 14, 2024
631efcd
Merge pull request #741 from PrefectHQ/assistants-docs
jlowin Jan 14, 2024
c00415c
Remove breakpoint
jlowin Jan 14, 2024
673c574
Remove need for assistants context managers
jlowin Jan 14, 2024
3933346
Add user message to responses
jlowin Jan 14, 2024
0e306dd
Improve code interpreter example
jlowin Jan 14, 2024
84fc5a3
Merge pull request #742 from PrefectHQ/easy-assistants
jlowin Jan 14, 2024
1e6b910
update examples
zzstoatzz Jan 14, 2024
642fcf6
update pull step
zzstoatzz Jan 14, 2024
0383f76
update pull step
zzstoatzz Jan 14, 2024
2a19af3
accept mediocrity
zzstoatzz Jan 14, 2024
c052ad4
dumb
zzstoatzz Jan 14, 2024
9049835
result storage
zzstoatzz Jan 14, 2024
6d266b4
refresh cache
zzstoatzz Jan 14, 2024
e6c9329
ffs
zzstoatzz Jan 14, 2024
49c0163
🫤
zzstoatzz Jan 14, 2024
981b101
jeez
zzstoatzz Jan 14, 2024
9138588
more retries
zzstoatzz Jan 14, 2024
b66ab93
prepare for merge
zzstoatzz Jan 14, 2024
6b89608
clean up
zzstoatzz Jan 14, 2024
dddd78b
clean up
zzstoatzz Jan 14, 2024
eb6d307
Update assistants screenshots
jlowin Jan 14, 2024
ba57d91
Merge pull request #743 from PrefectHQ/assistants
jlowin Jan 14, 2024
d948aba
clean up cookbook
zzstoatzz Jan 14, 2024
06ef9b1
use JSON block for channel map
zzstoatzz Jan 14, 2024
e050f36
better box
zzstoatzz Jan 14, 2024
f00a4d6
resolve merge conflict
zzstoatzz Jan 14, 2024
b90715b
Merge branch '2.0' into 2.0-cleanup-examples
zzstoatzz Jan 14, 2024
f3e4eb1
Merge pull request #744 from PrefectHQ/2.0-cleanup-examples
zzstoatzz Jan 15, 2024
1a476ef
Add applications docs
jlowin Jan 15, 2024
fae5d4c
Merge branch '2.0' into app-docs
jlowin Jan 15, 2024
a6c1134
Add full example
jlowin Jan 15, 2024
65ff382
Update test_extract.py
jlowin Jan 15, 2024
4c7b361
Merge pull request #745 from PrefectHQ/app-docs
jlowin Jan 15, 2024
78ec729
default gpt-4, failover
zzstoatzz Jan 15, 2024
a657902
address review comments
zzstoatzz Jan 15, 2024
34f3d0b
Merge pull request #746 from PrefectHQ/default-model
zzstoatzz Jan 15, 2024
7da889a
Update docstring
jlowin Jan 15, 2024
811dfc7
Update home page
jlowin Jan 15, 2024
0735557
Clean up api ref
jlowin Jan 15, 2024
72ef069
Merge pull request #747 from PrefectHQ/home-updates
jlowin Jan 15, 2024
aec3245
Update openai.py
jlowin Jan 15, 2024
c365faf
Merge pull request #748 from PrefectHQ/refactor-fallback
jlowin Jan 15, 2024
979fb97
Add streaming support
jlowin Jan 15, 2024
9bc94be
Merge pull request #749 from PrefectHQ/streaming
jlowin Jan 15, 2024
f5e3398
Update __init__.py
jlowin Jan 15, 2024
6df82ea
Improve flaky tests
jlowin Jan 15, 2024
90576ce
Merge pull request #750 from PrefectHQ/update-cli-streaming
jlowin Jan 15, 2024
9e803aa
Add caching for generate()
jlowin Jan 15, 2024
c6319a3
Update text.py
jlowin Jan 15, 2024
3ce82fa
Update pyproject.toml
jlowin Jan 15, 2024
dfc1ae9
Add token cap
jlowin Jan 16, 2024
9e62f7b
Merge pull request #751 from PrefectHQ/generation_cache
jlowin Jan 16, 2024
054b55b
Update docs
jlowin Jan 16, 2024
eb2ff65
Adjust colors
jlowin Jan 16, 2024
f483ab2
Merge pull request #752 from PrefectHQ/docs-2
jlowin Jan 16, 2024
5f7c7af
add more cookbook examples
zzstoatzz Jan 16, 2024
9b2274d
Merge branch '2.0' into cookbook-examples
zzstoatzz Jan 16, 2024
a029609
syntax highlighting
zzstoatzz Jan 16, 2024
a60598d
Merge branch 'cookbook-examples' of https://github.com/PrefectHQ/marv…
zzstoatzz Jan 16, 2024
5b5b97f
Merge pull request #753 from PrefectHQ/cookbook-examples
zzstoatzz Jan 16, 2024
c9d54bf
rm asserts where examples non-deterministic
zzstoatzz Jan 16, 2024
479a8e3
Merge branch '2.0' into rm-asserts
zzstoatzz Jan 16, 2024
513e4ce
Merge pull request #754 from PrefectHQ/rm-asserts
zzstoatzz Jan 16, 2024
8ef5838
ai → docs
jlowin Jan 16, 2024
75209ca
Merge branch '2.0' into docs-2
jlowin Jan 16, 2024
f885cd0
Update readme
jlowin Jan 16, 2024
0e55897
Deprecate components
jlowin Jan 16, 2024
84f788a
Merge pull request #755 from PrefectHQ/docs-2
jlowin Jan 16, 2024
4337ee5
Updating tutorial
jlowin Jan 16, 2024
dbd7a36
Merge pull request #756 from PrefectHQ/tutorial
jlowin Jan 16, 2024
4c1fddb
Update what_is_marvin.md
aaazzam Jan 16, 2024
7bf5300
Tutorial update
jlowin Jan 16, 2024
d212f72
Update tutorial.md
jlowin Jan 16, 2024
0bec234
Delete quickstart_old.md
jlowin Jan 16, 2024
b97e7ba
Merge pull request #758 from PrefectHQ/tutorial
jlowin Jan 16, 2024
1a688e6
Merge branch 'main' into 2.0
jlowin Jan 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,22 @@ permissions:

jobs:
run_tests:
name: ${{ matrix.test-type }} w/ python ${{ matrix.python-version }} on ${{ matrix.os }}
name: ${{ matrix.test-type == '' && 'all' || matrix.test-type }} (Python ${{ matrix.python-version }} on ${{ matrix.os }})
timeout-minutes: 15
strategy:
matrix:
# run no_llm tests across all python versions and oses
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ['3.9', '3.10', '3.11']
test-type: ['not llm']
python-version: ['3.9', '3.10', '3.11', '3.12']
test-type: ['no_llm']

include:
# Run LLM tests on 3.9
- python-version: '3.9'
os: 'ubuntu-latest'
test-type: 'llm'

- python-version: '3.9'
os: 'ubuntu-latest'
test-type: 'not llm'

test-type: ''


runs-on: ${{ matrix.os }}

env:
Expand All @@ -63,6 +62,10 @@ jobs:
- name: Install Marvin
run: pip install ".[tests]"

- name: Run ${{ matrix.test-type }} tests
run: pytest -vv -m "${{ matrix.test-type }}"
if: ${{ !(github.event.pull_request.head.repo.fork && matrix.test-type == 'llm') }}
- name: Run tests without LLMs
run: pytest -n auto -vv -m "${{ matrix.test-type }}"
if: ${{ !(github.event.pull_request.head.repo.fork) && matrix.test-type != '' }}

- name: Run tests
run: pytest -n auto -vv
if: ${{ !(github.event.pull_request.head.repo.fork) && matrix.test-type == '' }}
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,9 @@ local_examples/
.chroma/

# Marvin
src/marvin/_version.py
src/marvin/_version.py

# Prefect
.prefect/
.prefect/*.json
.prefectignore
274 changes: 193 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,136 +1,248 @@
<p align="center">
<img src="docs/img/heroes/it_hates_me_hero.png" style="width: 95%; height: auto;"/>
<img src="docs/assets/images/heroes/it_hates_me_hero.png" style="width: 95%; height: auto;"/>
</p>

# Marvin
[![PyPI version](https://badge.fury.io/py/marvin.svg)](https://badge.fury.io/py/marvin)
[![Twitter Follow](https://img.shields.io/twitter/follow/AskMarvinAI?style=social)](https://twitter.com/AskMarvinAI)
[![Docs](https://img.shields.io/badge/docs-askmarvin.ai-blue)](https://www.askmarvin.ai)
### An AI engineering framework
... made with 💙 by the team at [Prefect](https://www.prefect.io/).
[![Twitter Follow](https://img.shields.io/twitter/follow/AskMarvinAI?style=social)](https://twitter.com/AskMarvinAI)

# Marvin

### The AI engineering toolkit

Marvin is a lightweight AI toolkit for building natural language interfaces that are reliable, scalable, and easy to trust.

Each of Marvin's tools is simple and self-documenting, using AI to solve common but complex challenges like entity extraction, classification, and generating synthetic data. Each tool is independent and incrementally adoptable, so you can use them on their own or in combination with any other library. Marvin is also multi-modal, supporting both image and audio generation as well using images as inputs for extraction and classification.

Marvin is for developers who care more about _using_ AI than _building_ AI, and we are focused on creating an exceptional developer experience. Marvin users should feel empowered to bring tightly-scoped "AI magic" into any traditional software project with just a few extra lines of code.

Marvin aims to merge the best practices for building dependable, observable software with the best practices for building with generative AI into a single, easy-to-use library. It's a serious tool, but we hope you have fun with it.

Marvin is open-source, free to use, and made with 💙 by the team at [Prefect](https://www.prefect.io/).

## Installation

Install the latest version with `pip`:

```bash
pip install marvin
pip install marvin -U
```
Getting started? Head over to our [setup guide](https://www.askmarvin.ai/welcome/installation/).

---
To verify your installation, run `marvin version` in your terminal.

## Tools

Marvin consists of a variety of useful tools, all designed to be used independently. Each one represents a common LLM use case, and is designed to package that power into a simple, self-documenting interface.

### General

🦾 [Write custom AI-powered functions](https://askmarvin.ai/docs/text/functions) without source code

### Text

🏷️ [Classify text](https://askmarvin.ai/docs/text/classification) into categories

🔍 [Extract structured entities](https://askmarvin.ai/docs/text/extraction) from text

🪄 [Transform text](https://askmarvin.ai/docs/text/transforming) into structured data

✨ [Generate synthetic data](https://askmarvin.ai/docs/text/generation) from a schema

⚠️ Marvin is a work in progress, and we'd love your [feedback](https://github.com/PrefectHQ/marvin/discussions)! ⚠️
### Images

> [Looking for info on Marvin 1.x?](/docs/help/legacy_docs.md)
🖼️ [Create images](https://askmarvin.ai/docs/images/generation) from text or functions

---
📝 [Describe images](https://askmarvin.ai/docs/vision/captioning) with natural language

## Offerings
🏷️ [Classify images](https://askmarvin.ai/docs/vision/classification) into categories

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.
🔍 [Extract structured entities](https://askmarvin.ai/docs/vision/extraction) from images

🪄 [**AI Functions**](https://www.askmarvin.ai/components/ai_function/) for complex business logic and transformations
🪄 [Transform images](https://askmarvin.ai/docs/vision/transforming) into structured data

🧩 [**AI Models**](https://www.askmarvin.ai/components/ai_model/) for structuring text into type-safe schemas
### Audio

🤖 (*beta*) [**Assistants**](/src/marvin/beta/assistants/README.md) for building stateful natural language interfaces
___
🎙️ [Generate speech](https://askmarvin.ai/docs/audio/speech) from text or functions

### 🪄 AI Functions
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.
### Interaction

You can learn more about AI Functions [here](https://www.askmarvin.ai/components/ai_function/).
🤖 [Chat with assistants](https://askmarvin.ai/docs/interactive/assistants) and use custom tools

🧭 [Build applications](https://askmarvin.ai/docs/interactive/applications) that manage persistent state

## Quickstart

Here's a whirlwind tour of a few of Marvin's main features. For more information, [check the docs](https://askmarvin.ai/welcome/what_is_marvin/)!

### 🏷️ Classify text

Marvin can `classify` text using a set of labels:

```python
from marvin import ai_fn
import marvin

@ai_fn
def sentiment(text: str) -> float:
"""Given `text`, returns a number between 1 (positive) and -1 (negative)
indicating its sentiment score.
"""
marvin.classify(
"Marvin is so easy to use!",
labels=["positive", "negative"],
)

# "positive"
```

sentiment("I love working with Marvin!") # 0.8
sentiment("These examples could use some work...") # -0.2
Learn more about classification [here](https://askmarvin.ai/docs/text/classification).

### 🔍 Extract structured entities

Marvin can `extract` structured entities from text:

```python
from pydantic import BaseModel

class Location(BaseModel):
city: str
state: str

marvin.extract("I moved from NY to CHI", target=Location)

# [
# Location(city="New York", state="New York"),
# Location(city="Chcago", state="Illinois")
# ]
```

🎬 You can define your own types for AI Functions to return, using things like:
- Pydantic [BaseModel](https://pydantic-docs.helpmanual.io/usage/models/)
- [TypedDict](https://docs.python.org/3/library/typing.html#typing.TypedDict)
- [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal) types
Almost all Marvin functions can be given `instructions` for more control. Here we extract only monetary values:

```python
from typing_extensions import TypedDict
marvin.extract(
"I paid $10 for 3 tacos and got a dollar and 25 cents back.",
target=float,
instructions="Only extract money"
)

class DetailedSentiment(TypedDict):
"""A detailed sentiment analysis result.
# [10.0, 1.25]
```

- `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.
"""
Learn more about entity extraction [here](https://askmarvin.ai/docs/text/extraction).

### ✨ Generate data

Marvin can `generate` synthetic data for you, following instructions and an optional schema:

detailed_sentiment("I'ma Mario, and I'ma gonna wiiiiin!")
# {'sentiment_score': 0.8, 'summary_in_a_word': 'energetic'}
```python
from pydantic import BaseModel

class Location(BaseModel):
city: str
state: str

marvin.generate(
n=4,
target=Location,
instructions="cities in the United States named after presidents"
)

# [
# Location(city='Washington', state='District of Columbia'),
# Location(city='Jackson', state='Mississippi'),
# Location(city='Cleveland', state='Ohio'),
# Location(city='Lincoln', state='Nebraska'),
# ]
```

### 🧩 AI Models
AI models are based on Pydantic's [BaseModel](https://pydantic-docs.helpmanual.io/usage/models/), but with a twist: they are instantiated with plain text, and will use an LLM to infer their values.
Learn more about data generation [here](https://askmarvin.ai/docs/text/generation).

### 🪄 Standardize text by casting to types

You can learn more about AI models [here](https://www.askmarvin.ai/components/ai_model/).
Marvin can `cast` arbitrary text to any Python type:

```python
from marvin import ai_model
from pydantic import BaseModel, Field
marvin.cast("one two three", list[int])

# [1, 2, 3]
```

This is useful for standardizing text inputs or matching natural language to a schema:

```python
from pydantic import BaseModel

@ai_model
class Location(BaseModel):
"""A city in the United States"""
city: str
state: str = Field(..., description="The two-letter state abbreviation")
state: str

marvin.cast("The Big Apple", Location)

# Location(city="New York", state="New York")
```

For a class-based approach, Marvin's `@model` decorator can be applied to any Pydantic model to let it be instantiated from text:

```python
@marvin.model
class Location(BaseModel):
city: str
state: str

Location("The Big Apple")
# Location(city='New York', state='NY')

# Location(city="New York", state="New York")
```

## Assistants (Beta)
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).
Learn more about casting to types [here](https://askmarvin.ai/docs/text/transformation).

### 🦾 Build AI-powered functions

Marvin functions let you combine any inputs, instructions, and output types to create custom AI-powered behaviors... without source code. These functions can can go well beyond the capabilities of `extract` or `classify`, and are ideal for complex natural language processing or mapping combinations of inputs to outputs.

```python
from marvin.beta.assistants import Assistant, Thread
@marvin.fn
def sentiment(text: str) -> float:
"""
Returns a sentiment score for `text`
between -1 (negative) and 1 (positive).
"""

def multiply(x: float, y: float) -> float:
return x * y
sentiment("I love working with Marvin!") # 0.8
sentiment("These examples could use some work...") # -0.2
```

Marvin functions look exactly like regular Python functions, except that you don't have to write any source code. When these functions are called, an AI interprets their description and inputs and generates the output.

Note that Marvin does NOT work by generating or executing source code, which would be unsafe for most use cases. Instead, it uses the LLM itself as a "runtime" to predict function outputs. That's actually the source of its power: Marvin functions can handle complex use cases that would be difficult or impossible to express as code.

def divide(x: float, y: float) -> float:
return x / y
You can learn more about functions [here](https://www.askmarvin.ai/docs/text/functions/).

### 🖼️ Generate images from text

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?
Marvin can `paint` images from text:

# what is that number divided by 42?
```python
marvin.paint("a simple cup of coffee, still warm")
```

Read more about [our SDK](/src/marvin/beta/assistants/README.md) and/or the [OpenAI docs](https://platform.openai.com/docs/assistants/overview).
<p align="center">
<img src="docs/assets/images/docs/images/coffee.png" style="width: 50%; height: auto;"/>
</p>

Learn more about image generation [here](https://askmarvin.ai/docs/images/generation).

### 🔍 Classify images (beta)

In addition to text, Marvin has beta support for captioning, classifying, transforming, and extracting entities from images using the GPT-4 vision model:

```python
marvin.beta.classify(
marvin.Image("docs/images/coffee.png"),
labels=["drink", "food"],
)

# "drink"
```

## Get in touch!

💡 **Feature idea?** share it in the `#development` channel in [our Discord](https://discord.com/invite/Kgw4HpcuYG).

## Reach out!
💡 **Have an idea for a feature?** toss it in `#development` in [our Discord](https://discord.com/invite/Kgw4HpcuYG)
🐛 **Found a bug?** feel free to [open an issue](https://github.com/PrefectHQ/marvin/issues/new/choose).

🐛 **found a bug?** feel free to [open an issue](https://github.com/PrefectHQ/marvin/issues/new/choose)
👷 **Feedback?** Marvin is under active development, and we'd love to [hear it](https://github.com/PrefectHQ/marvin/discussions).
Loading