> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# SurrealDB Vector Database

> Use SurrealDB as a vector database for your Knowledge Base.

## Setup

```shell theme={null}
uv pip install -U surrealdb pypdf openai agno
```

The example uses OpenAI for embeddings and the agent model, so set your API key:

```shell theme={null}
export OPENAI_API_KEY=xxx
```

```shell theme={null}
docker run -d \
  --rm \
  --pull always \
  -p 8000:8000 \
  surrealdb/surrealdb:latest \
  start \
  --user root \
  --pass root
```

or

```shell theme={null}
./cookbook/scripts/run_surrealdb.sh
```

## Example

```python agent_with_knowledge.py theme={null}
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 Surreal

# SurrealDB connection parameters
SURREALDB_URL = "ws://localhost:8000"
SURREALDB_USER = "root"
SURREALDB_PASSWORD = "root"
SURREALDB_NAMESPACE = "test"
SURREALDB_DATABASE = "test"

# Create a client
client = Surreal(url=SURREALDB_URL)
client.signin({"username": SURREALDB_USER, "password": SURREALDB_PASSWORD})
client.use(namespace=SURREALDB_NAMESPACE, database=SURREALDB_DATABASE)

surrealdb = SurrealDb(
    client=client,
    collection="recipes",  # Collection name for storing documents
    efc=150,  # HNSW construction time/accuracy trade-off
    m=12,  # HNSW max number of connections per element
    search_ef=40,  # HNSW search time/accuracy trade-off
    embedder=OpenAIEmbedder(),
)

def sync_demo():
    """Demonstrate synchronous usage of SurrealDb"""
    knowledge_base = Knowledge(
        vector_db=surrealdb,
    )

    # Load data synchronously
    knowledge_base.insert(
        url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
    )

    # Create agent and query synchronously
    agent = Agent(knowledge=knowledge_base)
    agent.print_response(
        "What are the 3 categories of Thai SELECT is given to restaurants overseas?",
        markdown=True,
    )

if __name__ == "__main__":
    # Run synchronous demo
    print("Running synchronous demo...")
    sync_demo()
```

> **Async Support ⚡**
>
> SurrealDB also supports asynchronous operations, enabling concurrency and leading to better performance.
>
> ```python async_surrealdb_db.py theme={null}
> 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
>
> # SurrealDB connection parameters
> SURREALDB_URL = "ws://localhost:8000"
> SURREALDB_USER = "root"
> SURREALDB_PASSWORD = "root"
> SURREALDB_NAMESPACE = "test"
> SURREALDB_DATABASE = "test"
>
> # Create a client
> client = AsyncSurreal(url=SURREALDB_URL)
>
> surrealdb = SurrealDb(
>     async_client=client,
>     collection="recipes",  # Collection name for storing documents
>     efc=150,  # HNSW construction time/accuracy trade-off
>     m=12,  # HNSW max number of connections per element
>     search_ef=40,  # HNSW search time/accuracy trade-off
>     embedder=OpenAIEmbedder(),
> )
>
> async def async_demo():
>     """Demonstrate asynchronous usage of SurrealDb"""
>
>     await client.signin({"username": SURREALDB_USER, "password": SURREALDB_PASSWORD})
>     await client.use(namespace=SURREALDB_NAMESPACE, database=SURREALDB_DATABASE)
>
>     knowledge_base = Knowledge(
>         vector_db=surrealdb,
>     )
>
>     await knowledge_base.ainsert(
>         url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
>     )
>
>     agent = Agent(knowledge=knowledge_base)
>     await agent.aprint_response(
>         "What are the 3 categories of Thai SELECT is given to restaurants overseas?",
>         markdown=True,
>     )
>
> if __name__ == "__main__":
>     # Run asynchronous demo
>     print("\nRunning asynchronous demo...")
>     asyncio.run(async_demo())
> ```
>
> > [!TIP]
> >
> > Using `ainsert()` and `aprint_response()` with asyncio provides non-blocking operations, making your application more responsive under load.

## SurrealDB Params

<Snippet file="vectordb_surrealdb_params.mdx" />


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.