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

# 使用 Agno 搭配 CometAPI

> 使用本指南透過 CometAPI model provider、API key 和 model ID，將 Agno 代理連接到 CometAPI。

[Agno](https://docs.agno.com/) 可讓你在 Python 中建置並執行 AI 代理。Agno 內建官方 `CometAPI` model provider，因此你可以直接從 `agno.models.cometapi` 使用 CometAPI，而不需要建立自訂 adapter。

## 先決條件

* Python 3.10+
* Agno Python 套件
* 擁有已啟用 API key 的 CometAPI 帳戶 — [在控制台取得你的 API key](https://www.cometapi.com/console/token)

## 設定整合

<Steps>
  <Step title="安裝 Agno">
    在你的 Python 環境中安裝 Agno：

    ```bash theme={null}
    pip install -U agno
    ```
  </Step>

  <Step title="設定你的 CometAPI API key">
    將你的 CometAPI API key 儲存在 `COMETAPI_KEY` 環境變數中：

    ```bash theme={null}
    read -rsp "CometAPI API key: " COMETAPI_KEY
    printf '\n'
    export COMETAPI_KEY
    ```
  </Step>

  <Step title="建立 Agno 代理">
    使用 Agno 官方的 `CometAPI` provider：

    ```python theme={null}
    from agno.agent import Agent
    from agno.models.cometapi import CometAPI

    agent = Agent(
        model=CometAPI(id="your-model-id"),
        markdown=True,
    )

    agent.print_response("Write a short product update in three bullet points.")
    ```

    將 `your-model-id` 替換為來自 [CometAPI Models page](/zh-Hant/overview/models) 的目前 model ID。
  </Step>

  <Step title="執行代理">
    將範例儲存為 `agent.py`，然後在相同的 shell 工作階段中執行：

    ```bash theme={null}
    python agent.py
    ```

    成功的回應表示 Agno 正透過 CometAPI 傳送模型呼叫。
  </Step>
</Steps>

## Agno 如何連接到 CometAPI

Agno 官方的 `CometAPI` provider 會從你的環境中讀取 `COMETAPI_KEY`，並使用 `https://api.cometapi.com/v1` 作為預設 base URL。它在內部擴充了 Agno 與 OpenAI 相容的模型介面，但你的應用程式程式碼應該匯入並實例化 `CometAPI`。

當你的執行環境無法讀取環境變數時，你也可以明確傳入 API key 或 base URL：

```python theme={null}
import os

from agno.agent import Agent
from agno.models.cometapi import CometAPI

agent = Agent(
    model=CometAPI(
        id="your-model-id",
        api_key=os.environ["COMETAPI_KEY"],
        base_url="https://api.cometapi.com/v1",
    )
)

agent.print_response("Summarize the benefits of a unified AI API.")
```

## 列出可用模型

Agno 的 provider 提供 `get_available_models()`，可用來檢查你的 CometAPI 帳戶可使用的 model ID：

```python theme={null}
from agno.models.cometapi import CometAPI

model = CometAPI()
available_models = model.get_available_models()
print(available_models)
```

## 疑難排解

<AccordionGroup>
  <Accordion title="驗證錯誤">
    請確認 `COMETAPI_KEY` 已在執行 `agent.py` 的同一個 shell 工作階段中設定。如果缺少此變數，Agno 會在送出模型請求之前引發驗證錯誤。
  </Accordion>

  <Accordion title="模型錯誤">
    請確認 `id` 值與 [CometAPI Models page](/zh-Hant/overview/models) 上的 model ID 相符。你也可以呼叫 `CometAPI().get_available_models()`，從你的帳戶檢查模型可用性。
  </Accordion>
</AccordionGroup>

## 相關資源

* [Agno CometAPI provider 文件](https://docs.agno.com/models/providers/gateways/cometapi/overview)
* [Agno CometAPI provider 原始碼](https://github.com/agno-agi/agno/blob/main/libs/agno/agno/models/cometapi/cometapi.py)
* [Agno CometAPI cookbook](https://github.com/agno-agi/agno/tree/main/cookbook/90_models/cometapi)
* [CometAPI 快速開始](/zh-Hant/overview/quick-start)
* [CometAPI Models 頁面](/zh-Hant/overview/models)

<script type="application/ld+json">
  {`
    {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "HowTo",
        "@id": "https://apidoc.cometapi.com/integrations/agno#howto",
        "name": "使用 Agno 搭配 CometAPI",
        "description": "使用本指南透過 CometAPI model provider、API key 和 model ID，將 Agno 代理連接至 CometAPI。",
        "step": [
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/agno#step-1",
            "position": 1,
            "name": "安裝 Agno",
            "text": "完成「使用 Agno 搭配 CometAPI」指南中的「安裝 Agno」步驟。"
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/agno#step-2",
            "position": 2,
            "name": "設定您的 CometAPI API key",
            "text": "將您的 CometAPI API key 儲存在此整合所使用的環境變數或設定欄位中。"
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/agno#step-3",
            "position": 3,
            "name": "建立 Agno 代理",
            "text": "完成「使用 Agno 搭配 CometAPI」指南中的「建立 Agno 代理」步驟。"
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/agno#step-4",
            "position": 4,
            "name": "執行代理",
            "text": "完成「使用 Agno 搭配 CometAPI」指南中的「執行代理」步驟。"
          }
        ]
      },
      {
        "@type": "BreadcrumbList",
        "itemListElement": [
          {
            "@type": "ListItem",
            "position": 1,
            "name": "CometAPI 文件",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "整合",
            "item": "https://apidoc.cometapi.com/integrations"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "使用 Agno 搭配 CometAPI",
            "item": "https://apidoc.cometapi.com/integrations/agno"
          }
        ]
      }
    ]
    }
    `}
</script>
