Use MongoDB for persistent memory storage with both sync and async support.
Installation
Install the required packages:
pip install hypertic[mongodb]
Setup
Set your MongoDB connection string:
export MONGODB_URL=mongodb://localhost:27017/mongodb
MongoDB Connection String Formats:
- Local:
mongodb://localhost:27017/database_name
- Cloud (Atlas):
mongodb+srv://username:[email protected]/database_name?retryWrites=true&w=majority
Getting MongoDB Atlas Connection String:
- Sign up at MongoDB Atlas
- Create a free cluster (M0)
- Click “Connect” → “Connect your application”
- Copy the connection string and replace
<password> with your password
- Add the database name at the end:
/your_database_name
Usage
MongoDB supports both sync and async operations. The database name is extracted from the connection string:
import os
from dotenv import load_dotenv
from hypertic.models import XAI
from hypertic.agents import Agent
from hypertic.memory import MongoServer
load_dotenv()
# MongoDB connection string (supports both local and cloud)
# Database name is extracted from URL if present
MONGO_URL = os.getenv("MONGODB_URL", "mongodb://localhost:27017/mongodb")
COLLECTION_NAME = os.getenv("MONGODB_COLLECTION", "agent")
# Setup (indexes created automatically on first use)
memory = MongoServer(
connection_string=MONGO_URL,
collection_name=COLLECTION_NAME
)
agent = Agent(
model=XAI(
model="grok-3",
max_tokens=4096,
),
memory=memory
)
user_id = "user_123"
session_id = "session_001"
# Non-streaming response
response = agent.run("My favorite color is blue.", session_id=session_id, user_id=user_id)
print(response.content)
# Streaming response
for event in agent.stream("what is my favorite colour?", session_id=session_id, user_id=user_id):
if event.type == "content":
print(event.content, end="", flush=True)
Configuration Options
- connection_string - MongoDB connection string (database name is extracted from URL if present)
- collection_name - Name of the collection to store conversations
Indexes are created automatically on first use. No manual setup required.