设置
pip install lancedb
示例
agent_with_knowledge.py
import typer
from typing import Optional
from rich.prompt import Prompt
from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.vectordb.lancedb import LanceDb
from agno.vectordb.search import SearchType
# LanceDB Vector DB
vector_db = LanceDb(
table_name="recipes",
uri="/tmp/lancedb",
search_type=SearchType.keyword,
)
# 知识库
knowledge_base = PDFUrlKnowledgeBase(
urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=vector_db,
)
def lancedb_agent(user: str = "user"):
run_id: Optional[str] = None
agent = Agent(
run_id=run_id,
user_id=user,
knowledge=knowledge_base,
show_tool_calls=True,
debug_mode=True,
)
if run_id is None:
run_id = agent.run_id
print(f"Started Run: {run_id}\n")
else:
print(f"Continuing Run: {run_id}\n")
while True:
message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")
if message in ("exit", "bye"):
break
agent.print_response(message)
if __name__ == "__main__":
# 首次运行时注释此行
knowledge_base.load(recreate=True)
typer.run(lancedb_agent)
异步支持 ⚡
LanceDB 还支持异步操作,可实现并发并带来更好的性能。
async_lance_db.py
# 安装 lancedb - `pip install lancedb`
import asyncio
from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.vectordb.lancedb import LanceDb
# 初始化 LanceDB
vector_db = LanceDb(
table_name="recipes",
uri="tmp/lancedb", # 您可以将此路径更改为存储其他数据的位置
)
# 创建知识库
knowledge_base = PDFUrlKnowledgeBase(
urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=vector_db,
)
agent = Agent(knowledge=knowledge_base, show_tool_calls=True, debug_mode=True)
if __name__ == "__main__":
# 异步加载知识库
asyncio.run(knowledge_base.aload(recreate=False)) # 首次运行时注释此行
# 异步创建并使用 Agent
asyncio.run(agent.aprint_response("How to make Tom Kha Gai", markdown=True))
在吞吐量高的应用程序中使用
aload() 和 aprint_response() 方法配合 asyncio.run() 进行非阻塞操作。LanceDb 参数
| 参数 | 类型 | 默认 | 描述 |
|---|---|---|---|
uri | str | - | 要连接的 URI。 |
table | LanceTable | - | 要使用的 Lance 表。 |
table_name | str | - | 要使用的表的名称。 |
connection | DBConnection | - | 要使用的数据库连接。 |
api_key | str | - | 要使用的 API 密钥。 |
embedder | Embedder | - | 要使用的 embedder。 |
search_type | SearchType | vector | 要使用的搜索类型。 |
distance | Distance | cosine | 要使用的距离。 |
nprobes | int | - | 要使用的探针数量。更多信息 |
reranker | Reranker | - | 要使用的 reranker。更多信息 |
use_tantivy | bool | - | 是否使用 tantivy。 |