Overview
The Twitter Knowledge Base system provides two key components:- TweetKnowledgeBase: Vector-based storage and retrieval of tweets using ChromaDB
- TwitterState: SQLite-based tracking of interactions (replies, reposts) to avoid duplicates
TweetKnowledgeBase
TheTweetKnowledgeBase class uses ChromaDB and sentence transformers to store and query tweets using semantic search.
Initialization
twitter_knowledge_base.py:22-54
Embedding Model
The knowledge base uses theall-mpnet-base-v2 model from SentenceTransformers for high-quality semantic embeddings:
- Model:
all-mpnet-base-v2 - Dimensions: 768
- Performance: Excellent for semantic similarity tasks
- Speed: Optimized for production use
Adding Tweets
Add tweets to the knowledge base:twitter_knowledge_base.py:56-72
Querying the Knowledge Base
Search for relevant tweets using semantic search:twitter_knowledge_base.py:74-118
Formatting Results
Format query results into a readable string:twitter_knowledge_base.py:120-133
Collection Statistics
Get statistics about the knowledge base:twitter_knowledge_base.py:135-153
Clearing the Collection
Clear all tweets from the knowledge base:twitter_knowledge_base.py:155-168
Updating Knowledge Base from KOLs
Theupdate_knowledge_base function populates the knowledge base with tweets from key opinion leaders (KOLs):
twitter_knowledge_base.py:170-330
KOL List Format
The KOL list should be a list of dictionaries with username and user_id:TwitterState
TheTwitterState class tracks interactions to prevent duplicate replies and reposts.
Initialization
twitter_state.py:10-19
Database Schema
TwitterState uses SQLite with three tables:twitter_state.py:34-62
Tracking Replied Tweets
Check if already replied and add to database:twitter_state.py:96-104,106-110
Tracking Reposted Tweets
Check if already reposted and add to database:twitter_state.py:132-151
Rate Limiting
Manage mention check intervals:twitter_state.py:7-8,112-130
Persisting State
Save and load state from SQLite:twitter_state.py:64-93
Using Knowledge Base as a Tool
Fromchatbot.py:302-308, the knowledge base is added as a LangChain tool:
Environment Configuration
Usage Examples
Query Knowledge Base
Check Before Replying
Avoid Duplicate Reposts
Best Practices
Knowledge Base Management
- Update regularly with fresh content from KOLs
- Clear stale data periodically
- Monitor collection size and performance
- Use appropriate
n_resultsfor queries (default: 10)
State Management
- Always check state before interactions
- Save state after each operation
- Use character-specific databases
- Back up state databases regularly
Rate Limiting
- Respect
MENTION_CHECK_INTERVAL - Monitor
MAX_MENTIONS_PER_INTERVAL - Implement exponential backoff for errors
- Use
wait_on_rate_limit=Truein TwitterClient
Troubleshooting
ChromaDB Issues
If ChromaDB fails to initialize:SQLite Database Locked
If you get database locked errors:- Ensure only one agent instance is running
- Check file permissions on
.dbfiles - Close any open database connections
Missing Embeddings
If embeddings fail to generate:Next Steps
- Twitter Operations - Learn about posting and interaction tools
- Blockchain Tools - Explore blockchain capabilities