Skip to content

Instantly share code, notes, and snippets.

@iwootten
Created September 20, 2024 18:58
Show Gist options
  • Save iwootten/26b088a41abe1c48d016b6fc07f165e3 to your computer and use it in GitHub Desktop.
Save iwootten/26b088a41abe1c48d016b6fc07f165e3 to your computer and use it in GitHub Desktop.
Chaining Structured Outputs
from dotenv import load_dotenv
load_dotenv()
import ell
from pydantic import BaseModel, Field
ell.init(store='./logdir', autocommit=True)
class MovieSuggestion(BaseModel):
title: str = Field(description="The title of the movie")
@ell.complex(model="gpt-4o-mini", response_format=MovieSuggestion)
def generate_movie_suggestion(year: int) -> MovieSuggestion:
"""You are a movie suggestion generator. Given a year suggest a random, non-obvious movie"""
return f"generate a suggesiton for the yea {year}"
class MovieReview(BaseModel):
title: str = Field(description="The title of the movie")
rating: int = Field(description="The rating of the movie out of 10")
summary: str = Field(description="A brief summary of the movie")
@ell.complex(model="gpt-4o-mini", response_format=MovieReview)
def generate_movie_review(movie: str) -> MovieReview:
"""You are a movie review generator. Given the name of a movie, you need to return a structured review."""
return f"generate a review for the movie {movie}"
message = generate_movie_suggestion(2015)
suggestion = message.parsed
message = generate_movie_review(suggestion.title)
review = message.parsed
# Access individual fields
print(f"Movie Title: {review.title}")
print(f"Rating: {review.rating}/10")
print(f"Summary: {review.summary}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment