Overview
The database utilities provide comprehensive data persistence for prediction market agents, including SQL storage, vector embeddings with Pinecone, and long-term memory management.SQL Handler
TheSQLHandler is a generic utility for managing SQLModel tables with built-in CRUD operations.
Key Methods
Sequence[SQLModelType]
Retrieves all records from the table
None
Saves multiple items to the database in a single transaction
None
Deletes multiple items from the database
None
Deletes a single item by its ID
list[SQLModelType]
Advanced query with filters, ordering, pagination
int
Counts records matching the given filters
Advanced Queries
Implementation
Implementation
Database Models
The system includes several pre-defined models for agent operations:LongTermMemories
int
Auto-generated primary key
str
required
Identifier for the task or agent
str
JSON-serialized metadata
DatetimeUTC
required
Timestamp of the memory
EvaluatedGoalModel
Tracks agent goals and completion status:Prompt
Checkpoints for agent prompts:BlockchainMessage
Messages sent via blockchain transactions:Long-Term Memory Handler
Manage agent memories with automatic serialization:ChatHistory Integration
ChatHistory Integration
The memory handler works seamlessly with
ChatHistory:Prompt Table Handler
Manage agent prompts with session tracking:Pinecone Vector Database
ThePineconeHandler provides vector embeddings for market similarity search:
Key Features
Key Features
- Automatic deduplication: Filters out duplicate markets
- Batch insertion: Processes markets in chunks of 100
- Similarity search: Finds related markets using embeddings
- Metadata filtering: Filter by category, date, volume, etc.
- SHA-256 IDs: Uses deterministic IDs based on question titles
Market Insertion
Market Insertion
Similarity Search
Similarity Search
The handler uses cosine similarity with configurable thresholds:
Evaluated Goal Handler
Track and retrieve agent goals:Usage in Agents
Think Thoroughly Agent
The Think Thoroughly Agent uses multiple database components:Best Practices
Use Transactions
The
SQLHandler uses database sessions with automatic commit/rollback. Always use save_multiple for batch operations.Index Strategy
Add database indexes on frequently queried columns like
agent_id, datetime_, and is_complete.Memory Limits
Implement pagination when fetching large result sets. Use
offset and limit parameters.Cleanup
Regularly archive or delete old records to maintain performance. Set up retention policies.
Configuration
Dependencies
Migration Tips
1
Define Models
Create SQLModel classes with
table=True2
Initialize Handler
The handler automatically creates tables if they don’t exist
3
Add Indexes
Use SQLAlchemy’s
Index in your model definitions4
Test Migrations
Use Alembic for complex schema changes in production