from typing import Optional
import typer
from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.vectordb.lancedb import LanceDb
from agno.vectordb.search import SearchType
from rich.prompt import Prompt
vector_db = LanceDb(
table_name="recipes",
uri="tmp/lancedb",
search_type=SearchType.hybrid,
)
knowledge_base = PDFUrlKnowledgeBase(
urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=vector_db,
)
def lancedb_agent(user: str = "user"):
agent = Agent(
user_id=user,
knowledge=knowledge_base,
search_knowledge=True,
)
while True:
message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")
if message in ("exit", "bye"):
break
agent.print_response(message)
if __name__ == "__main__":
# Comment out after first run
knowledge_base.load(recreate=False)
typer.run(lancedb_agent)
创建虚拟环境
打开 Terminal
并创建一个 python 虚拟环境。
python3 -m venv .venv
source .venv/bin/activate
设置您的 API 密钥
export OPENAI_API_KEY=xxx
安装库
pip install -U lancedb tantivy pypdf openai agno
运行 Agent
python cookbook/agent_concepts/knowledge/vector_dbs/lance_db/lance_db_hybrid_search.py