LightRAG Async

Load and query a LightRAG knowledge base asynchronously with ainsert() and aprint_response().

Prepare the sample input files in the directory where you will run the Python example:

mkdir -p data
curl --fail --location https://raw.githubusercontent.com/agno-agi/agno/8f36eaf2d18e91afa7b327eec66a3cd3685dcb87/cookbook/07_knowledge/testing_resources/cv_1.pdf --output data/cv_1.pdf
curl --fail --location https://raw.githubusercontent.com/agno-agi/agno/8f36eaf2d18e91afa7b327eec66a3cd3685dcb87/cookbook/07_knowledge/testing_resources/cv_2.pdf --output data/cv_2.pdf

Code

import asyncio
from os import getenv

from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.knowledge.reader.wikipedia_reader import WikipediaReader
from agno.vectordb.lightrag import LightRag

vector_db = LightRag(
    server_url=getenv("LIGHTRAG_SERVER_URL", "http://localhost:9621"),
    api_key=getenv("LIGHTRAG_API_KEY"),
)

knowledge = Knowledge(
    name="LightRAG Knowledge Base",
    description="Knowledge base using LightRAG for graph-based retrieval",
    vector_db=vector_db,
)

agent = Agent(
    knowledge=knowledge,
    search_knowledge=True,
    read_chat_history=False,
)


async def main() -> None:
    await knowledge.ainsert(
        name="CV",
        path="data/cv_1.pdf",
        metadata={"doc_type": "cv"},
    )
    await knowledge.ainsert(
        name="Manchester United",
        topics=["Manchester United"],
        reader=WikipediaReader(),
    )
    await knowledge.ainsert(
        name="CV 2",
        path="data/cv_2.pdf",
    )

    # Give the server time to index the uploaded documents
    await asyncio.sleep(60)

    await agent.aprint_response("What skills does Jordan Mitchell have?", markdown=True)
    await agent.aprint_response(
        "In what year did Manchester United change their name?",
        markdown=True,
    )

    results = await vector_db.async_search("What skills does Jordan Mitchell have?")
    if results:
        doc = results[0]
        print(f"References: {doc.meta_data.get('references', [])}")


if __name__ == "__main__":
    asyncio.run(main())

Usage

This example requires a running LightRAG server. It connects to http://localhost:9621 unless LIGHTRAG_SERVER_URL is set.

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno pypdf wikipedia openai

Set environment variables

export LIGHTRAG_API_KEY="your-lightrag-api-key"
export OPENAI_API_KEY=xxx

Run Agent

python async_lightrag_db.py