SurrealDB Async

Insert knowledge and query it asynchronously with ainsert() and aprint_response() on SurrealDB.

The async example supplies both authenticated clients to the same namespace and database: Knowledge currently checks the schema synchronously during construction. Context managers close both connections when the example finishes.

Code

async_surreal_db.py
import asyncio

from agno.agent import Agent
from agno.knowledge.embedder.openai import OpenAIEmbedder
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.surrealdb import SurrealDb
from surrealdb import AsyncSurreal, Surreal

SURREALDB_URL = "ws://localhost:8000"
SURREALDB_NAMESPACE = "test"
SURREALDB_DATABASE = "test"
CREDENTIALS = {"username": "root", "password": "root"}

async def main():
    # Knowledge checks/creates the schema through the synchronous client.
    with Surreal(SURREALDB_URL) as sync_client:
        sync_client.signin(CREDENTIALS)
        sync_client.use(namespace=SURREALDB_NAMESPACE, database=SURREALDB_DATABASE)
        async with AsyncSurreal(SURREALDB_URL) as async_client:
            await async_client.signin(CREDENTIALS)
            await async_client.use(namespace=SURREALDB_NAMESPACE, database=SURREALDB_DATABASE)
            vector_db = SurrealDb(
                client=sync_client,
                async_client=async_client,
                collection="recipes",
                search_ef=40,
                embedder=OpenAIEmbedder(),
            )
            knowledge = Knowledge(vector_db=vector_db)
            await knowledge.ainsert(
                url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
            )
            agent = Agent(knowledge=knowledge)
            await agent.aprint_response("How do I make pad thai?", markdown=True)

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

Usage

Set up your virtual environment

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

Install dependencies

uv pip install -U surrealdb pypdf openai agno

Export your OpenAI API key

export OPENAI_API_KEY="your_openai_api_key_here"

Run SurrealDB

docker run -d --rm --pull always -p 8000:8000 surrealdb/surrealdb:latest start --user root --pass root

Run Agent

python async_surreal_db.py