代码

cookbook/agent_concepts/vector_dbs/cassandra_db.py
from agno.agent import Agent
from agno.embedder.mistral import MistralEmbedder
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.models.mistral import MistralChat
from agno.vectordb.cassandra import Cassandra

try:
    from cassandra.cluster import Cluster
except (ImportError, ModuleNotFoundError):
    raise ImportError(
        "无法导入 cassandra-driver python 包。请使用 pip install cassandra-driver 进行安装。"
    )

cluster = Cluster()

session = cluster.connect()
session.execute(
    """
    CREATE KEYSPACE IF NOT EXISTS testkeyspace
    WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }
    """
)

knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    vector_db=Cassandra(
        table_name="recipes",
        keyspace="testkeyspace",
        session=session,
        embedder=MistralEmbedder(),
    ),
)

knowledge_base.load(recreate=True)  # 首次运行时注释掉此行

agent = Agent(
    model=MistralChat(),
    knowledge=knowledge_base,
    show_tool_calls=True,
)

agent.print_response(
    "Khao Niew Dam Piek Maphrao Awn 的健康益处是什么?",
    markdown=True,
    show_full_reasoning=True,
)

用法

1

创建虚拟环境

打开 Terminal 并创建一个 python 虚拟环境。

python3 -m venv .venv
source .venv/bin/activate
2

安装库

pip install -U cassandra-driver pypdf openai agno
3

运行 Agent

python cookbook/agent_concepts/vector_dbs/cassandra_db.py