Async Structured Output Agent

Code

1import asyncio
2from typing import List
3
4from kern.agent import Agent, RunOutput # noqa
5from kern.models.mistral import MistralChat
6from kern.tools.hackernews import HackerNewsTools
7from pydantic import BaseModel, Field
8from rich.pretty import pprint # noqa
9
10class MovieScript(BaseModel):
11 setting: str = Field(
12 ..., description="Provide a nice setting for a blockbuster movie."
13 )
14 ending: str = Field(
15 ...,
16 description="Ending of the movie. If not available, provide a happy ending.",
17 )
18 genre: str = Field(
19 ...,
20 description="Genre of the movie. If not available, select action, thriller or romantic comedy.",
21 )
22 name: str = Field(..., description="Give a name to this movie")
23 characters: List[str] = Field(..., description="Name of characters for this movie.")
24 storyline: str = Field(
25 ..., description="3 sentence storyline for the movie. Make it exciting!"
26 )
27
28agent = Agent(
29 model=MistralChat(
30 id="mistral-small-latest",
31 ),
32 tools=[HackerNewsTools()],
33 description="You help people write movie scripts.",
34 output_schema=MovieScript,
35)
36
37asyncio.run(agent.aprint_response("Find a cool movie idea about London and write it."))

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set your API key

1export MISTRAL_API_KEY=xxx

Install dependencies

1uv pip install -U mistralai kern-ai

Run Agent

1python cookbook/11_models/mistral/async_structured_output.py