Copy
---
title: Agent with Structured Outputs
---
## Code
```python cookbook/models/cohere/structured_output.py
from typing import List
from agno.agent import Agent, RunResponse # noqa
from agno.models.cohere import Cohere
from pydantic import BaseModel, Field
from rich.pretty import pprint # noqa
class MovieScript(BaseModel):
setting: str = Field(
..., description="提供一个适合大片的场景。"
)
ending: str = Field(
...,
description="电影的结局。如果不可用,请提供一个快乐的结局。",
)
genre: str = Field(
...,
description="电影的类型。如果不可用,请选择动作、惊悚或浪漫喜剧。",
)
name: str = Field(..., description="给这部电影起个名字")
characters: List[str] = Field(..., description="这部电影的角色名字。")
storyline: str = Field(
..., description="电影的剧情梗概,三句话。要激动人心!"
)
json_mode_agent = Agent(
model=Cohere(id="command-r-08-2024"),
description="你帮助人们撰写电影剧本。",
response_model=MovieScript,
# debug_mode=True,
)
# Get the response in a variable
# json_mode_response: RunResponse = json_mode_agent.run("New York")
# pprint(json_mode_response.content)
json_mode_agent.print_response("New York")
Usage
1
创建虚拟环境
打开
Terminal
并创建一个 python 虚拟环境。Copy
python3 -m venv .venv
source .venv/bin/activate
2
Set your API key
Copy
export CO_API_KEY=xxx
3
Install libraries
Copy
pip install -U cohere agno
4
Run Agent
Copy
python cookbook/models/cohere/structured_output.py