fromslm_builderimportSLMBuilderbuilder=SLMBuilder(project_name="postgres-qa",base_model="google/flan-t5-small")# Load from PostgreSQL
builder.load_from_database(connection_params={"dialect":"postgresql","host":"localhost","port":5432,"database":"qa_database","user":"your_user","password":"your_password"},query="SELECT question, answer FROM qa_pairs WHERE active=true")# Train and evaluate
builder.train(epochs=3,method="lora")metrics=builder.evaluate()
Advanced Query
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Complex query with joins
query="""
SELECT
q.question_text as question,
a.answer_text as answer,
q.category,
q.difficulty
FROM questions q
JOIN answers a ON q.id = a.question_id
WHERE q.verified = true
AND a.upvotes > 5
ORDER BY q.created_at DESC
LIMIT 10000
"""builder.load_from_database(connection_params={...},query=query)
fromslm_builderimportSLMBuilderbuilder=SLMBuilder(project_name="mysql-qa",base_model="google/flan-t5-small")# Load from MySQL
builder.load_from_database(connection_params={"dialect":"mysql","host":"localhost","port":3306,"database":"customer_support","user":"root","password":"password"},query="SELECT ticket_question, ticket_answer FROM support_tickets")builder.train(epochs=3,method="lora")
SQLite
Load data from a local SQLite database.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fromslm_builderimportSLMBuilderbuilder=SLMBuilder(project_name="sqlite-qa",base_model="google/flan-t5-small")# Load from SQLite (no host/port needed)
builder.load_from_database(connection_params={"dialect":"sqlite","database":"./my_data.db"},query="SELECT question, answer FROM qa_table")builder.train(epochs=3,method="lora")
fromslm_builderimportSLMBuilderbuilder=SLMBuilder(project_name="mongo-qa",base_model="google/flan-t5-small")# Load from MongoDB
builder.load_from_mongodb(connection_params={"host":"localhost","port":27017,"database":"qa_database","collection":"qa_pairs","user":"admin","password":"password"},query={"status":"approved"},projection={"question":1,"answer":1,"_id":0})builder.train(epochs=3,method="lora")
# Use aggregation for complex transformations
pipeline=[{"$match":{"verified":True}},{"$lookup":{"from":"answers","localField":"answer_id","foreignField":"_id","as":"answer_data"}},{"$unwind":"$answer_data"},{"$project":{"question":"$question_text","answer":"$answer_data.text","_id":0}},{"$limit":10000}]builder.load_from_mongodb_aggregation(connection_params={...},pipeline=pipeline)
fromslm_builderimportSLMBuilderbuilder=SLMBuilder(project_name="redis-qa",base_model="google/flan-t5-small")# Load from Redis
builder.load_from_redis(connection_params={"host":"localhost","port":6379,"db":0,"password":"your_password"},key_pattern="qa:*",# Match keys
value_type="json"# or "string", "hash"
)builder.train(epochs=3,method="lora")
Redis Hash Example
1
2
3
4
5
6
7
# Load from Redis hashes
builder.load_from_redis(connection_params={...},key_pattern="qa:question:*",value_type="hash",hash_fields=["question","answer","category"])
fromslm_builderimportSLMBuilderbuilder=SLMBuilder(project_name="elastic-qa",base_model="google/flan-t5-small")# Load from Elasticsearch
builder.load_from_elasticsearch(connection_params={"host":"localhost","port":9200,"user":"elastic","password":"password"},index="qa_documents",query={"match_all":{}})builder.train(epochs=3,method="lora")
fromslm_builderimportSLMBuilderbuilder=SLMBuilder(project_name="multi-db",base_model="google/flan-t5-base")# Load from PostgreSQL
builder.load_from_database(connection_params={"dialect":"postgresql",...},query="SELECT question, answer FROM qa_table")# Add MongoDB data
builder.load_from_mongodb(connection_params={...},query={"verified":True})# Add Elasticsearch data
builder.load_from_elasticsearch(connection_params={...},index="qa_index",query={"match_all":{}})print(f"Total samples from all sources: {builder.dataset_size}")# Train on combined data
builder.train(epochs=5,method="lora")
Connection Pooling
Efficient database connections for large datasets.
fromslm_builderimportSLMBuilderfromslm_builder.dataimportDatabaseLoaderfromsqlalchemyimportcreate_enginefromsqlalchemy.poolimportQueuePool# Create engine with connection pooling
engine=create_engine("postgresql://user:pass@localhost/db",poolclass=QueuePool,pool_size=10,max_overflow=20)# Use with builder
loader=DatabaseLoader(engine=engine)data=loader.load_from_query("SELECT question, answer FROM qa_pairs")builder=SLMBuilder(project_name="pooled-connection")builder.load_data(data)builder.train(epochs=3,method="lora")
Incremental Loading
Load large datasets incrementally to manage memory.
fromslm_builderimportSLMBuilderfromslm_builder.exceptionsimportDatabaseConnectionErrorimporttimedefload_with_retry(builder,connection_params,query,max_retries=3):"""Load data with automatic retry on failure."""forattemptinrange(max_retries):try:builder.load_from_database(connection_params=connection_params,query=query)print(f"✅ Data loaded successfully")returnTrueexceptDatabaseConnectionErrorase:print(f"❌ Attempt {attempt+1} failed: {e}")ifattempt<max_retries-1:time.sleep(2**attempt)# Exponential backoff
else:print("❌ All retries failed")raisereturnFalse# Usage
builder=SLMBuilder(project_name="retry-example")load_with_retry(builder,connection_params={...},query="SELECT question, answer FROM qa_table")