Skip to content

surrealdb/agent-memory

Repository files navigation

Building Agent Memory with Knowledge Graphs

Demo code for the talk on building production agent memory using knowledge graphs and vectors in SurrealDB.

Prerequisites

  • SurrealDB (nightly or tobiemh/ai branch for DEFINE AGENT teaser)
  • uv (Python package manager)
  • An OpenAI API key

Setup

  1. Copy the env file and add your OpenAI API key:
cp .env.example .env
# Edit .env and set your key

Or export it directly:

export OPENAI_API_KEY=sk-proj-...
export OPENROUTER_API_KEY=sk-or-...
  1. Start SurrealDB in-memory:
surreal start --log trace --user root --pass root --allow-funcs --allow-net --allow-experimental memory
  1. Load the knowledge graph (schema, data, and embeddings):
uv run load.py
  1. Run the agent:
uv run agent.py

Alternative Agent Implementations

The same agent is also available using popular frameworks:

uv run agent_pydantic.py   # PydanticAI
uv run agent_agno.py       # Agno
uv run agent_langchain.py  # LangChain
uv run agent_langgraph.py  # LangGraph (enforced review-first flow)

Demo Flow

Step File What it shows
1 surql/01-schema.surql Tables, vector indexes, graph relations -- the knowledge graph schema
2 surql/02-ingest.surql Documents, entities, RELATE statements -- building the graph
3 surql/03-query.surql Vector search, graph traversal, then hybrid queries -- the retrieval layer
4 agent.py Python agent using OpenAI + SurrealDB -- the production agent
5 surql/04-agent.surql DEFINE AGENT teaser -- replacing the Python app with SurrealQL

Project Structure

surql/
  00-configure.surql     # AI provider config (for DEFINE AGENT / ai::embed)
  01-schema.surql        # Tables, fields, vector indexes, relation types
  02-ingest.surql        # Sample data and graph edges (embeddings via async events)
  03-query.surql         # Standalone query patterns for the walkthrough
  04-agent.surql         # DEFINE AGENT teaser (experimental)
load.py                  # Load schema + data + embeddings into SurrealDB
agent.py                 # Python agent with tool-calling loop (raw OpenAI SDK)
agent_pydantic.py        # Same agent using PydanticAI
agent_agno.py            # Same agent using Agno
agent_langchain.py       # Same agent using LangChain
agent_langgraph.py       # Same agent using LangGraph (forced review step)
walkthrough.md           # Code slides for the talk
pyproject.toml           # Dependencies (managed by uv)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors