> ## Documentation Index
> Fetch the complete documentation index at: https://ikun.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Agent with Structured Outputs

## 代码

```python cookbook/models/together/structured_output.py theme={null}
from typing import List

from agno.agent import Agent, RunResponse  # noqa
from agno.models.together import Together
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="电影的3句剧情梗概。要写得激动人心！"
    )


# 使用 JSON 模式的 Agent
json_mode_agent = Agent(
    model=Together(id="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"),
    description="你负责编写电影剧本。",
    response_model=MovieScript,
)

# 将响应保存在一个变量中
# json_mode_response: RunResponse = json_mode_agent.run("New York")
# pprint(json_mode_response.content)

json_mode_agent.print_response("New York")
```

## 用法

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="设置你的 API 密钥">
    ```bash theme={null}
    export TOGETHER_API_KEY=xxx
    ```
  </Step>

  <Step title="安装库">
    ```bash theme={null}
    pip install -U together openai agno
    ```
  </Step>

  <Step title="运行 Agent">
    <CodeGroup>
      ```bash Mac theme={null}
      python cookbook/models/together/structured_output.py
      ```

      ```bash Windows theme={null}
      python cookbook/models/together/structured_output.py
      ```
    </CodeGroup>
  </Step>
</Steps>
