Định Nghĩa Cấu Trúc Kịch Bản Phim Với Python
Để tạo kịch bản phim có cấu trúc, ta sử dụng Python và thư viện Pydantic để định nghĩa một lớp (class) MovieScript
. Lớp này sẽ chứa các thuộc tính (attribute) tương ứng với các yếu tố của kịch bản phim:
from pydantic import BaseModel, Field
class MovieScript(BaseModel):
setting: str = Field(description="A richly detailed, atmospheric description of the movie's primary location and setting.")
ending: str = Field(description="The movie's powerful conclusion that ties together all plot threads. Should deliver a satisfying resolution.")
genre: str = Field(description="The film's primary and secondary genres (e.g., Sci-Fi Thriller, Romantic Comedy).")
name: str = Field(description="An attention-grabbing, memorable title that captures the essence of the story and appeals to the target audience.")
characters: list[str] = Field(description="4-6 main characters with distinctive names and brief role descriptions (e.g., the tormented detective, the femme fatale).")
storyline: str = Field(description="A compelling three-sentence plot summary: Setup, Conflict, and Stakes. Hook readers quickly.")
Trong đoạn mã trên:
BaseModel
là lớp cơ sở của Pydantic, cho phép tạo ra các mô hình dữ liệu có cấu trúc.
Field
cho phép định nghĩa các thuộc tính với mô tả chi tiết, giúp AI hiểu rõ hơn về ý nghĩa của từng thuộc tính.
Các thuộc tính được định nghĩa:
setting
: Bối cảnh của bộ phim.
ending
: Kết thúc của bộ phim.
genre
: Thể loại của bộ phim.
name
: Tên của bộ phim.
characters
: Danh sách các nhân vật trong phim.
storyline
: Cốt truyện của bộ phim.
Tạo Tác Nhân (Agent) AI Với Agno
Sau khi đã định nghĩa cấu trúc kịch bản phim, ta sẽ tạo một tác nhân AI với Agno. Tác nhân này sẽ sử dụng mô hình ngôn ngữ lớn (LLM) để tạo ra kịch bản phim dựa trên cấu trúc đã định nghĩa.
from agno import Agent
from agno.models.openai import OpenAIChat
agent = Agent(
model=OpenAIChat(model_name="gpt-4o"),
description="You are an acclaimed Hollywood screenwriter known for creating unforgettable blockbusters! With the combined storytelling prowess of Christopher Nolan, Aaron Sorkin, and Quentin Tarantino you craft unique stories that captivate audiences worldwide.",
instructions=""")
When crafting movie concepts, follow these principles:
Settings should be characters:
Make locations come alive with sensory details
Include atmospheric elements that affect the story
Consider the time period’s impact on the narrative
Character Development:
Give each character a unique voice and clear motivation
Create compelling relationships and conflicts
Ensure diverse representation and authentic backgrounds
Story Structure:
Begin with a hook that grabs attention
Build tension through escalating conflicts
Deliver surprising yet inevitable endings
Genre Mastery:
Embrace genre conventions while adding fresh twists
Mix genres thoughtfully for unique combinations
Maintain consistent tone throughout
Transform every location into an unforgettable cinematic experience!"")
Trong đoạn mã trên:
Agent
là lớp đại diện cho tác nhân AI trong Agno.
OpenAIChat
là mô hình ngôn ngữ lớn của OpenAI, trong đó model_name
chỉ định mô hình cụ thể (ví dụ: gpt-4o
).
description
mô tả vai trò của tác nhân (trong trường hợp này là một nhà biên kịch Hollywood).
instructions
cung cấp các hướng dẫn chi tiết cho tác nhân về cách tạo ra kịch bản phim, bao gồm các nguyên tắc về bối cảnh, phát triển nhân vật, cấu trúc câu chuyện và thể loại.
Các hướng dẫn này giúp tác nhân AI tạo ra kịch bản phim phù hợp với yêu cầu và mong muốn của người dùng.
Kết Hợp Cấu Trúc Đầu Ra Và Tác Nhân AI
Để kết hợp cấu trúc đầu ra đã định nghĩa với tác nhân AI, ta chỉ định lớp MovieScript
làm mô hình đầu ra cho tác nhân:
response_model=MovieScript
Điều này đảm bảo rằng tác nhân AI sẽ tạo ra kịch bản phim có cấu trúc rõ ràng, với đầy đủ các yếu tố cần thiết (tiêu đề, thể loại, bối cảnh, cốt truyện, nhân vật, kết thúc).
Với cấu trúc này, người dùng có thể dễ dàng truy cập và sử dụng các thuộc tính của kịch bản phim, thay vì phải xử lý một đoạn văn bản dài và không có cấu trúc.