> ## 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.

# Playground App

> 托管 Agent 作为 Playground 应用。

Playground App 用于通过具有多个端点的 FastAPI 服务器来提供 Agent、Team 和 Workflow，以管理和交互 [Agno Playground](/introduction/playground) 上的 `Agents`、`Workflows` 和 `Teams`。

### 示例用法

创建一个 Agent，并使用 `Playground` 来托管它：

```python theme={null}
from agno.agent import Agent
from agno.memory.agent import AgentMemory
from agno.memory.db.postgres import PgMemoryDb
from agno.models.openai import OpenAIChat
from agno.playground import Playground
from agno.storage.postgres import PostgresStorage

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

basic_agent = Agent(
    name="Basic Agent",
    model=OpenAIChat(id="gpt-4o"), # 确保已设置 OPENAI_API_KEY
    memory=AgentMemory(
        db=PgMemoryDb(
            table_name="agent_memory",
            db_url=db_url,
        ),
        create_user_memories=True,
        update_user_memories_after_run=True,
        create_session_summary=True,
        update_session_summary_after_run=True,
    ),
    storage=PostgresStorage(
        table_name="agent_sessions", db_url=db_url, auto_upgrade_schema=True
    ),
    add_history_to_messages=True,
    num_history_responses=3,
    add_datetime_to_instructions=True,
    markdown=True,
)

playground = Playground(
    agents=[
        basic_agent,
    ],
    name="Basic Agent",
    description="A playground for basic agent",
    app_id="basic-agent",
)
app = playground.get_app()

if __name__ == "__main__":
    playground.serve(app="basic:app", reload=True)
```

**运行方法：**

1. 确保你的 PostgreSQL 服务器正在运行并通过 `db_url` 即可访问。
2. 设置 `OPENAI_API_KEY` 环境变量。
3. Playground UI 将在 `http://localhost:7777` 可用。API 文档（如果在设置中启用）通常在 `http://localhost:7777/docs`。
4. 通过 [Agent Playground](/introduction/playground) 使用 playground。

## 核心组件

* `Playground`: 将 Agno agent、team 或 workflow 封装在 API 中。
* `Playground.serve`: 使用 Uvicorn 来托管 Playground FastAPI 应用。

`Playground` 类是创建 Agno Playground 应用的主要入口点。它允许你通过 Web 界面，借助 [Agent Playground](/introduction/playground) 或 [Agent UI](/agent-ui/introduction) 轻松地暴露你的 agent、team 和 workflow。

## `Playground` 类

### 初始化参数

| 参数            | 类型                             | 默认值    | 描述                                  |
| ------------- | ------------------------------ | ------ | ----------------------------------- |
| `agents`      | `Optional[List[Agent]]`        | `None` | Agno `Agent` 实例列表。                  |
| `teams`       | `Optional[List[Team]]`         | `None` | Agno `Team` 实例列表。                   |
| `workflows`   | `Optional[List[Workflow]]`     | `None` | Agno `Workflow` 实例列表。               |
| `settings`    | `Optional[PlaygroundSettings]` | `None` | Playground 配置。如果为 `None` 则使用默认值。    |
| `api_app`     | `Optional[FastAPI]`            | `None` | 现有的 FastAPI 应用。如果为 `None` 则会创建一个新的。 |
| `router`      | `Optional[APIRouter]`          | `None` | 现有的 APIRouter。如果为 `None` 则会创建一个新的。  |
| `app_id`      | `Optional[str]`                | `None` | 应用标识符（如果未设置则自动生成）。                  |
| `name`        | `Optional[str]`                | `None` | 应用的名称。                              |
| `description` | `Optional[str]`                | `None` | 应用的描述。                              |

*至少提供 `agents`、`teams` 或 `workflows` 中的一个。*

### 主要方法

| 方法                 | 参数                                                  | 返回类型        | 描述                                             |
| ------------------ | --------------------------------------------------- | ----------- | ---------------------------------------------- |
| `get_app`          | `use_async: bool = True`<br />`prefix: str = "/v1"` | `FastAPI`   | 返回配置好的 FastAPI 应用（默认为异步）。设置前缀、错误处理程序、CORS 和文档。 |
| `get_router`       |                                                     | `APIRouter` | 返回 playground 端点的同步 APIRouter。                 |
| `get_async_router` |                                                     | `APIRouter` | 返回 playground 端点的异步 APIRouter。                 |

### 端点

端点位于指定的 `prefix`（默认为 `/v1`）和 playground 路由器的前缀（`/playground`）组合处。例如，状态端点通常是 `/v1/playground/status`。

### 参数

| 参数       | 类型                    | 默认值           | 描述                      |
| -------- | --------------------- | ------------- | ----------------------- |
| `app`    | `Union[str, FastAPI]` | `N/A`         | FastAPI 应用实例或导入字符串（必需）。 |
| `host`   | `str`                 | `"localhost"` | 绑定的主机。                  |
| `port`   | `int`                 | `7777`        | 绑定的端口。                  |
| `reload` | `bool`                | `False`       | 为开发启用自动重载。              |
