-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstructured_generation_demo.py
More file actions
94 lines (78 loc) · 3.35 KB
/
Copy pathstructured_generation_demo.py
File metadata and controls
94 lines (78 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import asyncio
import os
from enum import Enum
from pydantic import BaseModel, Field
from afterimage import (
StructuredGenerator,
PersonaGenerator,
PersonaInstructionGeneratorCallback,
InMemoryDocumentProvider,
)
class SentimentCategory(str, Enum):
MUST_WATCH = "Must Watch"
WATCH = "Watch"
AVOID = "Avoid"
WASTE_OF_TIME = "Waste of Time"
class Genre(str, Enum):
ACTION = "Action"
ANIMATION = "Animation"
CRIME = "Crime"
DRAMA = "Drama"
FANTASY = "Fantasy"
HORROR = "Horror"
SCIENCE_FICTION = "Science Fiction"
THRILLER = "Thriller"
WAR = "War"
WESTERN = "Western"
# Define the output schema
class MovieReview(BaseModel):
movie_title: str = Field(description="The title of the movie")
genre: Genre = Field(description="The genre of the movie, e.g. Fantasy, Sci-Fi")
rating: int = Field(description="Rating out of 10")
summary: str = Field(description="A brief summary of the movie")
response: str = Field(description="The response to the user's question")
category: SentimentCategory = Field(
description="your strong opinion about this movie"
)
async def main():
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
print("Please set GEMINI_API_KEY environment variable.")
return
# Define some context documents (e.g. movie database entries or facts)
# we will create both personas and instruction based on these.
docs = InMemoryDocumentProvider(
[
"Terminator 2: Judgment Day is a 1991 science fiction action film directed by James Cameron. It stars Arnold Schwarzenegger.",
"The Shawshank Redemption is a 1994 American prison drama film written and directed by Frank Darabont.",
"Inception is a 2010 science fiction action film written and directed by Christopher Nolan, who also produced the film with Emma Thomas.",
"Parasite is a 2019 South Korean black comedy thriller film directed by Bong Joon-ho.",
]
)
# 2. Setup Persona Generator
persona_gen = PersonaGenerator(api_key=api_key)
# Generate personas for the documents
# This will populate the .personas attribute of each Document in the provider
await persona_gen.generate_from_documents(docs)
# Initialize callback
instruction_callback = PersonaInstructionGeneratorCallback(
api_key=api_key,
documents=docs,
# We can also generate personas if we wanted to run the persona generator first,
# but here we'll let it use default/random personas if documents don't have them yet.
)
# Initialize the generator
generator = StructuredGenerator(
output_schema=MovieReview,
respondent_prompt="You are an experienced movie critic. You always have a very sharp language and you are not afraid to use it. Generate reviews based on the user's questions.",
api_key=api_key,
model_name="gemini-2.5-flash",
instruction_generator_callback=instruction_callback,
)
# Run generation
print("Starting structured generation...")
# Generate 10 samples (it samples docs randomly)
await generator.generate(num_samples=10)
print("Generation complete. Check the output JSONL file.")
if __name__ == "__main__":
asyncio.run(main())