class Prompt(source: str | Path, _extra_fields: dict[str, Any] = dict())
Base class for prompts.Prompts can be defined either as strings or paths to template files.
Additional attributes can be added by subclassing and will be type-checked.Methods:
Create a Prompt class from a function’s docstring and signature.Args:
fn: The function to create a prompt from. The function’s docstring will
be used as the template, and its signature will define the required
attributes.Example:
def greet(name: str, age: int):
'''
SYSTEM: You are a friendly assistant.
USER: Hi , how old are you?
ASSISTANT: I’m an AI, but you’re years old!
'''
passGreetPrompt = Prompt.from_fn(greet)
prompt = GreetPrompt(name=“Alice”, age=30)
messages = prompt.to_messages()
to_messages
Copy
Ask AI
def to_messages(self, **kwargs: Any = {}) -> list[PydanticAIMessage]
Convert the prompt to a list of messages with roles.The template can contain role markers (SYSTEM:, USER:, ASSISTANT:) to
indicate message roles. Text without a role marker is treated as a user
message.Example template:
'''
SYSTEM: You are a helpful assistant.
USER: Hi !
ASSISTANT: Hello ! How can I help you?
'''