Examples
Frameworks, RAG and web
Plug MenteDB into a framework, a retrieval pipeline or a web app.
Two ways to plug MenteDB into a framework: the published packages (LangChain and CrewAI, in Frameworks & tools), or, for a framework without a package yet, the process_turn loop as the memory step. The examples below use the loop, so they work anywhere.
LangGraph (memory node)
Put MenteDB in a node: recall before the model, store after. Or use MenteDBChatHistory from mentedb-langchain as the per-thread store.
from langgraph.graph import StateGraph, MessagesState
from mentedb import MenteDB
db = MenteDB("./agent-memory")
def call_model(state: MessagesState):
user = state["messages"][-1].content
result = db.process_turn(user_message=user, turn_id=len(state["messages"]))
memory = "\n".join(m.content for m in result.context)
reply = model.invoke([("system", memory), *state["messages"]])
return {"messages": [reply]}
graph = StateGraph(MessagesState)
graph.add_node("model", call_model)
graph.set_entry_point("model")
app = graph.compile()AutoGen (memory step)
No dedicated package yet, so wrap the agent: recall into its system message, then let it reply.
from mentedb import MenteDB
db = MenteDB("./autogen-memory")
def remember_and_reply(agent, user_message, turn_id):
result = db.process_turn(user_message=user_message, turn_id=turn_id)
memory = "\n".join(m.content for m in result.context)
agent.update_system_message(f"What you remember:\n{memory}")
return agent.generate_reply(messages=[{"role": "user", "content": user_message}])LlamaIndex (long-term memory + your index)
Combine cross-session memory with your RAG index: recall the user context, then feed it into the query.
from mentedb import MenteDB
db = MenteDB("./llamaindex-memory")
def chat(query, turn_id, query_engine):
result = db.process_turn(user_message=query, turn_id=turn_id)
memory = "\n".join(m.content for m in result.context)
answer = query_engine.query(f"Context about the user:\n{memory}\n\nQuestion: {query}")
return str(answer)MenteDB as a retriever (RAG)
Store your knowledge base once, then use hybrid search (vector + keyword) to pull the top matches at query time.
import os
from mentedb import MenteDB
db = MenteDB(
"./kb-memory",
embedding_provider="openai",
embedding_api_key=os.environ["OPENAI_API_KEY"],
)
for doc in documents: # index your docs once
db.store(doc)
hits = db.search_text("how do refunds work?", k=5)
context = "\n".join(db.get_memory(h.id)["content"] for h in hits)
answer = call_your_llm(system=context, user="how do refunds work?")Next.js route handler (hosted API)
Call the REST endpoint from a server route and hand the memory to your model (for example the Vercel AI SDK streamText system prompt). The mdb_ key stays server-side.
// app/api/chat/route.ts
const API = "https://api.mentedb.com/v1/process_turn";
export async function POST(req: Request) {
const { userId, message, turnId } = await req.json();
const res = await fetch(API, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.MENTEDB_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
user_message: message,
assistant_response: "",
turn_id: turnId,
project_context: `user:${userId}`,
}),
});
const { context } = await res.json();
const memory = context.map((m: { content: string }) => m.content).join("\n");
// pass `memory` into your model call, then store the reply on the next turn
return Response.json({ memory });
}Images & PDFs (multimodal)
The hosted service extracts text from an image or PDF and stores that (the raw file is never persisted). It runs through the MCP tool endpoint (JSON-RPC tools/call), not /v1/process_turn.
import os, base64, requests
MCP = "https://api.mentedb.com/mcp"
HEADERS = {"Authorization": f"Bearer {os.environ['MENTEDB_API_KEY']}"}
with open("whiteboard.png", "rb") as f:
data = base64.b64encode(f.read()).decode()
resp = requests.post(MCP, headers=HEADERS, json={
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "store_memory_multimodal",
"arguments": {
"data": data,
"media_type": "image/png", # image/jpeg, image/webp, image/gif, application/pdf
"memory_type": "semantic",
},
},
}).json()
print(resp) # extracted text is stored and searchable like any other memory