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

# RAG Agent

## Code

```python cookbook/models/ibm/watsonx/knowledge.py theme={null}
from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.models.ibm import WatsonX
from agno.vectordb.pgvector import PgVector

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

knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    vector_db=PgVector(table_name="recipes", db_url=db_url),
)
knowledge_base.load(recreate=True)  # Comment out after first run

agent = Agent(
    model=WatsonX(id="ibm/granite-20b-code-instruct"),
    knowledge=knowledge_base,
    show_tool_calls=True,
)
agent.print_response("How to make Thai curry?", markdown=True)
```

## Usage

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

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

  <Step title="安装库">
    ```bash theme={null}
    pip install -U ibm-watsonx-ai sqlalchemy pgvector psycopg pypdf openai agno
    ```
  </Step>

  <Step title="使用 pgvector 设置 PostgreSQL">
    您需要一个安装了 pgvector 扩展的 PostgreSQL 数据库。请调整代码中的 `db_url` 以匹配您的数据库配置。
  </Step>

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

      ```bash Windows theme={null}
      python cookbook\models\ibm\watsonx\knowledge.py
      ```
    </CodeGroup>
  </Step>

  <Step title="后续运行">
    首次运行后，注释掉 `knowledge_base.load(recreate=True)` 行，以避免重新加载 PDF。
  </Step>
</Steps>

此示例展示了如何将知识库与 IBM WatsonX 集成。它会从一个 URL 加载 PDF，将其处理到向量数据库（在此示例中为带 pgvector 的 PostgreSQL），然后创建一个可以查询该知识库的 Agent。

注意：您需要安装多个包（`pgvector`、`pypdf` 等）并拥有一个安装了 pgvector 扩展的 PostgreSQL 数据库。
