YouTubeKnowledgeBase 遍历 YouTube URL 列表,提取视频字幕,将它们转换为向量嵌入,并将它们加载到向量数据库中。

用法

我们在此示例中使用本地 PgVector 数据库。确保它正在运行

pip install bs4
knowledge_base.py
from agno.knowledge.youtube import YouTubeKnowledgeBase
from agno.vectordb.pgvector import PgVector

knowledge_base = YouTubeKnowledgeBase(
    urls=["https://www.youtube.com/watch?v=CDC3GOuJyZ0"],
    # 表名: ai.website_documents
    vector_db=PgVector(
        table_name="youtube_documents",
        db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
    ),
)

然后将 knowledge_baseAgent 一起使用:

agent.py
from agno.agent import Agent
from knowledge_base import knowledge_base

agent = Agent(
    knowledge=knowledge_base,
    search_knowledge=True,
)
agent.knowledge.load(recreate=False)

agent.print_response("Ask me about something from the knowledge base")

YouTubeKnowledgeBase 也支持异步加载。

pip install qdrant-client

我们在此示例中使用本地 Qdrant 数据库。确保它正在运行

async_knowledge_base.py
import asyncio

from agno.agent import Agent
from agno.knowledge.youtube import YouTubeKnowledgeBase, YouTubeReader
from agno.vectordb.qdrant import Qdrant

COLLECTION_NAME = "youtube-reader"

vector_db = Qdrant(collection=COLLECTION_NAME, url="http://localhost:6333")

knowledge_base = YouTubeKnowledgeBase(
    urls=[
        "https://www.youtube.com/watch?v=CDC3GOuJyZ0",
        "https://www.youtube.com/watch?v=JbF_8g1EXj4",
    ],
    vector_db=vector_db,
    reader=YouTubeReader(chunk=True),
)

agent = Agent(
    knowledge=knowledge_base,
    search_knowledge=True,
)

if __name__ == "__main__":
    # 首次运行时注释掉此行
    asyncio.run(knowledge_base.aload(recreate=False))

    # 创建并使用代理
    asyncio.run(
        agent.aprint_response(
            "What is the major focus of the knowledge provided in both the videos, explain briefly.",
            markdown=True,
        )
    )

参数

参数类型默认值描述
urlsList[str][]要读取的视频的 URL
readerOptional[YouTubeReader]None一个 YouTubeReader,用于读取 URL 处的视频字幕,并将它们转换为 Documents 以便导入向量数据库。

YouTubeKnowledgeBaseAgentKnowledge 类的一个子类,可以访问相同的参数。

开发者资源