Documentation Index
Fetch the complete documentation index at: https://docs.contextbase.dev/llms.txt
Use this file to discover all available pages before exploring further.
Basic usage
Initialize the client
from contextbase import Contextbase
# Using the CONTEXTBASE_API_KEY environment variable (recommended)
cb = Contextbase()
# Or pass API key directly
cb = Contextbase(api_key="your-api-key")
Publish JSON data
Publish structured data to build context memory:
# Publish user interaction data
response = cb.publish(
context_name="user_interactions",
body={
"user_message": "How do I reset my password?",
"timestamp": "2024-01-15T14:30:00Z",
"user_emotion": "confused",
"session_id": "sess_123"
},
scopes={
"user_id": 123,
"project": "web-app"
}
)
# Check if successful
if response.ok:
print("✅ Data published successfully!")
else:
print(f"❌ Error: {response.error.message}")
Publish files
Upload documents, images, and other files:
from contextbase import ContextbaseFile
# Method 1: Simple file upload
response = cb.publish_file(
context_name="user_uploads",
file_path="user-manual.pdf",
scopes={"project": "web-app"}
)
# Method 2: Using ContextbaseFile for more control
file = ContextbaseFile.from_path("image.png")
response = cb.publish(
context_name="screenshots",
file=file
)
# Method 3: Create file from string content
content = "Meeting notes: Discussed new features..."
file = ContextbaseFile.from_data(content, "notes.txt")
response = cb.publish(
context_name="meeting_notes",
file=file
)
See file handling
Resolve context
Get compressed, relevant context for your prompt:
# Resolve with scopes and query
response = client.resolve_context(
context_name="chatbot-assistant",
scopes={"user_id": 123},
query="password reset help"
)
if response.ok:
context = response.json["content"]
print(f"Context for AI: {context}")
# Use this context in your AI prompt
ai_prompt = f"""
{context}
User: How do I reset my password?
Assistant: """
Using the decorator
Automatically publish function results:
from contextbase import publish
# Publish function return values
@publish("analytics", "user-actions", scopes=lambda result: {"user_id": result["user_id"]})
def track_user_action(user_id, action):
return {
"user_id": user_id,
"action": action,
"timestamp": "2024-01-15T14:30:00Z"
}
# Function runs normally and publishes result
track_user_action(123, "login")
# Publish as file
@publish("reports", "daily-summary", as_file=True, file_name="summary.txt")
def generate_daily_report():
return "Daily Report: All systems operational"
# Creates and uploads summary.txt automatically
generate_daily_report()
More examples here
Error handling
Handle errors gracefully:
from contextbase import ContextbaseError
try:
response = cb.publish("context", "component", body={"test": "data"})
response.raise_for_status() # Raises exception if failed
print("Success!")
except ContextbaseError as e:
print(f"API Error: {e.message}")
print(f"Status: {e.status_code}")
for error in e.errors:
print(f"- {error}")
except ValueError as e:
print(f"Validation Error: {e}")
Next steps
Happy building! 🚀