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

# 多模态代理

## 代码

```python cookbook/apps/playground/multimodal_agents.py theme={null}
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.models.response import FileType
from agno.playground import Playground
from agno.storage.sqlite import SqliteStorage
from agno.tools.dalle import DalleTools
from agno.tools.eleven_labs import ElevenLabsTools
from agno.tools.fal import FalTools
from agno.tools.giphy import GiphyTools
from agno.tools.models_labs import ModelsLabTools

image_agent_storage_file: str = "tmp/image_agent.db"

image_agent = Agent(
    name="DALL-E Image Agent",
    agent_id="image_agent",
    model=OpenAIChat(id="gpt-4o"),
    tools=[DalleTools(model="dall-e-3", size="1792x1024", quality="hd", style="vivid")],
    description="您是一个可以使用 DALL-E 生成图像的 AI 代理。",
    instructions=[
        "当用户要求您创建图像时，请使用 `create_image` 工具来创建图像。",
        "请勿在响应中提供图像的 URL。只需描述生成的图像。",
    ],
    markdown=True,
    add_history_to_messages=True,
    add_datetime_to_instructions=True,
    storage=SqliteStorage(
        table_name="image_agent",
        db_file=image_agent_storage_file,
        auto_upgrade_schema=True,
    ),
)

ml_gif_agent = Agent(
    name="ModelsLab GIF Agent",
    agent_id="ml_gif_agent",
    model=OpenAIChat(id="gpt-4o"),
    tools=[ModelsLabTools(wait_for_completion=True, file_type=FileType.GIF)],
    description="您是一个可以使用 ModelsLabs API 生成 gif 的 AI 代理。",
    instructions=[
        "当用户要求您创建图像时，请使用 `generate_media` 工具来创建图像。",
        "请勿在响应中提供图像的 URL。只需描述生成的图像。",
    ],
    markdown=True,
    add_history_to_messages=True,
    add_datetime_to_instructions=True,
    storage=SqliteStorage(
        table_name="ml_gif_agent",
        db_file=image_agent_storage_file,
        auto_upgrade_schema=True,
    ),
)

ml_music_agent = Agent(
    name="ModelsLab Music Agent",
    agent_id="ml_music_agent",
    model=OpenAIChat(id="gpt-4o"),
    tools=[ModelsLabTools(wait_for_completion=True, file_type=FileType.MP3)],
    description="您是一个可以使用 ModelsLabs API 生成音乐的 AI 代理。",
    instructions=[
        "生成音乐时，请使用 `generate_media` 工具，并附带详细的提示，说明：",
        "- 音乐的流派和风格（例如，古典、爵士、电子音乐）",
        "- 要包含的乐器和声音",
        "- 节奏、情绪和情感特质",
        "- 结构（前奏、主歌、副歌、桥梁等）",
        "创建丰富且描述性的提示，以捕捉所需的音乐元素。",
        "专注于生成高质量、完整的器乐作品。",
        "保持响应简洁，仅在成功生成音乐时进行确认。",
        "请勿在响应或 markdown 中包含任何文件名、URL 或技术细节。",
    ],
    markdown=True,
    add_history_to_messages=True,
    add_datetime_to_instructions=True,
    storage=SqliteStorage(
        table_name="ml_music_agent",
        db_file=image_agent_storage_file,
        auto_upgrade_schema=True,
    ),
)

ml_video_agent = Agent(
    name="ModelsLab Video Agent",
    agent_id="ml_video_agent",
    model=OpenAIChat(id="gpt-4o"),
    tools=[ModelsLabTools(wait_for_completion=True, file_type=FileType.MP4)],
    description="您是一个可以使用 ModelsLabs API 生成视频的 AI 代理。",
    instructions=[
        "当用户要求您创建视频时，请使用 `generate_media` 工具来创建视频。",
        "请勿在响应中提供视频的 URL。只需描述生成的视频。",
    ],
    markdown=True,
    add_history_to_messages=True,
    add_datetime_to_instructions=True,
    storage=SqliteStorage(
        table_name="ml_video_agent",
        db_file=image_agent_storage_file,
        auto_upgrade_schema=True,
    ),
)

fal_agent = Agent(
    name="Fal Video Agent",
    agent_id="fal_agent",
    model=OpenAIChat(id="gpt-4o"),
    tools=[FalTools("fal-ai/hunyuan-video")],
    description="您是一个可以使用 Fal API 生成视频的 AI 代理。",
    instructions=[
        "当用户要求您创建视频时，请使用 `generate_media` 工具来创建视频。",
        "请勿在响应中提供视频的 URL。只需描述生成的视频。",
    ],
    markdown=True,
    add_history_to_messages=True,
    add_datetime_to_instructions=True,
    storage=SqliteStorage(
        table_name="fal_agent",
        db_file=image_agent_storage_file,
        auto_upgrade_schema=True,
    ),
)

gif_agent = Agent(
    name="Gif Generator Agent",
    agent_id="gif_agent",
    model=OpenAIChat(id="gpt-4o"),
    tools=[GiphyTools()],
    description="您是一个可以使用 Giphy 生成 gif 的 AI 代理。",
    instructions=[
        "当用户要求您创建 gif 时，请构思合适的 Giphy 查询并使用 `search_gifs` 工具查找合适的 gif。",
        "不要返回 URL，只需描述您创建的内容。",
    ],
    markdown=True,
    add_history_to_messages=True,
    add_datetime_to_instructions=True,
    storage=SqliteStorage(
        table_name="gif_agent",
        db_file=image_agent_storage_file,
        auto_upgrade_schema=True,
    ),
)

audio_agent = Agent(
    name="Audio Generator Agent",
    agent_id="audio_agent",
    model=OpenAIChat(id="gpt-4o"),
    tools=[
        ElevenLabsTools(
            voice_id="JBFqnCBsd6RMkjVDRZzb",
            model_id="eleven_multilingual_v2",
            target_directory="audio_generations",
        )
    ],
    description="您是一个可以使用 ElevenLabs API 生成音频的 AI 代理。",
    instructions=[
        "当用户要求您生成音频时，请使用 `text_to_speech` 工具来生成音频。",
        "您将生成适当的提示发送到工具以生成音频。",
        "您无需先找到合适的语音，我已经为用户指定了语音。",
        "请勿在您的响应或 markdown 中返回文件名或文件 URL，只需告知音频已成功创建。",
        "音频应该长且详细。",
    ],
    markdown=True,
    add_history_to_messages=True,
    add_datetime_to_instructions=True,
    storage=SqliteStorage(
        table_name="audio_agent",
        db_file=image_agent_storage_file,
        auto_upgrade_schema=True,
    ),
)


playground = Playground(
    agents=[
        image_agent,
        ml_gif_agent,
        ml_music_agent,
        ml_video_agent,
        fal_agent,
        gif_agent,
        audio_agent,
    ],
    name="Multimodal Agents",
    description="A playground for multimodal agents",
    app_id="multimodal-agents",
)
app = playground.get_app(use_async=False)

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

```

## 用法

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

  <Step title="设置您的 API 密钥">
    ```bash theme={null}
    export OPENAI_API_KEY=xxx
    export ELEVEN_LABS_API_KEY=xxx
    export MODELS_LAB_API_KEY=xxx # 从 https://console.modelslab.com/settings/api-keys 获取
    export FAL_API_KEY=xxx # 从 https://fal.ai/dashboard/keys 获取
    export GIPHY_API_KEY=xxx # 从 https://developers.giphy.com/ 获取
    ```
  </Step>

  <Step title="安装库">
    ```bash theme={null}
    pip install -U agno "uvicorn[standard]" openai elevenlabs fal-ai giphy-sdk-python sqlalchemy 
    ```
  </Step>

  <Step title="运行代理">
    <CodeGroup>
      ```bash Mac theme={null}
      python cookbook/apps/playground/multimodal_agents.py
      ```

      ```bash Windows theme={null}
      python cookbook/apps/playground/multimodal_agents.py
      ```
    </CodeGroup>
  </Step>
</Steps>
