Marvin 빠른 시작 — AI Models·Classifiers·Functions

Marvin 빠른 시작

Marvin을 설치하고 나면, 제일 빠른 시작 방법은 이미 잘 아는 추상화에 AI를 얹는 것이에요. 클래스와 함수에 데코레이터 하나 붙이면 자연어를 다루는 버전이 되죠.

AI Models

@ai_model 데코레이터는 Pydantic 모델의 드롭인 대체재예요. 자연어를 구조화된 스키마로 바로 파싱해요.

from marvin import ai_model
from pydantic import BaseModel, Field

@ai_model
class Location(BaseModel):
    city: str
    state: str = Field(..., description="Two-letter abbreviation")

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

어떤 문자열이든 인스턴스로 만들 수 있어서, 데이터 구조화·엔티티 추출·합성 데이터 생성에 아주 유용해요.

AI Classifiers

@ai_classifier로 Enum을 훈련 데이터 없이 분류기로 만들 수 있어요.

from marvin import ai_classifier
from enum import Enum

@ai_classifier
class Sentiment(Enum):
    POSITIVE = 1
    NEUTRAL = 0
    NEGATIVE = -1

Sentiment("That sounds great!")
# Sentiment.POSITIVE

AI Functions

@ai_fn 데코레이터로 일반 함수를 AI 함수로 바꿔요. 복잡한 비즈니스 로직과 변환을 AI가 수행하고, 다중 문자열도 처리할 수 있어요.

from marvin import ai_fn

@ai_fn
def sentiment_list(texts: list[str]) -> list[float]:
    """Given a list of `texts`, returns a list of numbers between 1 (positive) and -1 (negative) indicating their respective sentiment scores."""
    ...

sentiment_list(["That was surprisingly easy!", "Oh no, not again."])
# [0.5, ...]

AI Applications

AIApplication은 상호작용형 사용 사례의 베이스 클래스예요. 한 번 이상 호출되도록 설계되고, state(구조화된 앱 상태), plan(고수준 계획), history(LLM 상호작용 이력) 세 가지 상태를 자동 유지해요. 챗봇, 도구 사용 에이전트, 개발자 어시스턴트 같은 "전형적인" LLM 사용 사례를 구현하기 좋아요.

더 알아보기