-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththemis_help_lora_example.cpp
More file actions
241 lines (206 loc) · 11.2 KB
/
themis_help_lora_example.cpp
File metadata and controls
241 lines (206 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
╔═════════════════════════════════════════════════════════════════════╗
║ ThemisDB - Hybrid Database System ║
╠═════════════════════════════════════════════════════════════════════╣
File: themis_help_lora_example.cpp ║
Version: 0.0.34 ║
Last Modified: 2026-03-09 03:52:20 ║
Author: unknown ║
╠═════════════════════════════════════════════════════════════════════╣
Quality Metrics: ║
• Maturity Level: 🟢 PRODUCTION-READY ║
• Quality Score: 100.0/100 ║
• Total Lines: 240 ║
• Open Issues: TODOs: 0, Stubs: 0 ║
╠═════════════════════════════════════════════════════════════════════╣
Revision History: ║
• 2a1fb0423 2026-03-03 Merge branch 'develop' into copilot/audit-src-module-docu... ║
• a629043ab 2026-02-22 Audit: document gaps found - benchmarks and stale annotat... ║
╠═════════════════════════════════════════════════════════════════════╣
Status: ✅ Production Ready ║
╚═════════════════════════════════════════════════════════════════════╝
*/
/**
* @file themis_help_lora_example.cpp
* @brief Example usage of ThemisHelpLoRA - Documentation Q&A assistant
*
* This example demonstrates:
* - Initializing the ThemisHelpLoRA application
* - Querying the documentation assistant
* - Collecting user feedback (positive and negative)
* - Training from feedback
* - Training from documentation corpus
* - Getting metrics and statistics
* - Version management and rollback
*/
#include "llm/applications/themis_help_lora.h"
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
using namespace themis::llm::applications;
void printSeparator() {
std::cout << "\n" << std::string(70, '=') << "\n\n";
}
int main() {
std::cout << "ThemisHelpLoRA Example - Documentation Q&A Assistant\n";
printSeparator();
// ========================================================================
// 1. Initialize ThemisHelpLoRA
// ========================================================================
std::cout << "1. Initializing ThemisHelpLoRA...\n";
ThemisHelpLoRA::Config config;
config.adapter_id = "themis_help_lora";
config.base_model = "llama-2-7b";
config.docs_database_path = "data/docs_database.json";
config.feedback_batch_size = 100; // Train after 100 feedback items
config.min_accuracy_threshold = 0.80f;
config.enable_ab_testing = true;
config.enable_auto_rollback = true;
ThemisHelpLoRA help(config);
std::cout << "✅ ThemisHelpLoRA initialized successfully\n";
std::cout << " Adapter ID: " << config.adapter_id << "\n";
std::cout << " Base Model: " << config.base_model << "\n";
std::cout << " Current Version: " << help.getAdapterVersion() << "\n";
// ========================================================================
// 2. Query the Documentation Assistant
// ========================================================================
printSeparator();
std::cout << "2. Querying the Documentation Assistant...\n\n";
std::vector<std::string> questions = {
"How do I enable sharding?",
"What is the replication factor?",
"How do I configure backups?",
"What are the performance optimization options?"
};
for (const auto& question : questions) {
std::cout << "Q: " << question << "\n";
std::string answer = help.query(question);
std::cout << "A: " << answer << "\n\n";
}
// ========================================================================
// 3. Collect User Feedback
// ========================================================================
printSeparator();
std::cout << "3. Collecting User Feedback...\n\n";
// Positive feedback
std::cout << "Adding positive feedback...\n";
help.addPositiveFeedback(
"How do I enable sharding?",
"To enable sharding in ThemisDB: 1. Configure the shard key..."
);
help.addPositiveFeedback(
"What is the replication factor?",
"To configure replication in ThemisDB: 1. Set replicationFactor..."
);
// Negative feedback with correction
std::cout << "Adding negative feedback with correction...\n";
help.addNegativeFeedback(
"How do I configure backups?",
"Incorrect answer about backups",
"The correct way is: Use themisdb-backup create --hot for hot backups..."
);
// Get feedback statistics
auto stats = help.getFeedbackStats();
std::cout << "\nFeedback Statistics:\n";
std::cout << " Total: " << stats["total_feedback"] << "\n";
std::cout << " Positive: " << stats["positive_feedback"] << "\n";
std::cout << " Negative: " << stats["negative_feedback"] << "\n";
std::cout << " Positive Ratio: " << stats["positive_ratio"] << "\n";
// ========================================================================
// 4. Train from Feedback
// ========================================================================
printSeparator();
std::cout << "4. Training from Feedback...\n\n";
// Collect more feedback to reach batch size
std::cout << "Collecting additional feedback for training...\n";
for (int i = 0; i < 10; i++) {
help.addPositiveFeedback(
"Question " + std::to_string(i),
"Answer " + std::to_string(i)
);
}
std::cout << "Starting training from feedback...\n";
auto training_result = help.trainFromFeedback();
if (training_result.success) {
std::cout << "✅ Training completed successfully!\n";
std::cout << " New Version: " << training_result.version << "\n";
std::cout << " Final Loss: " << training_result.final_loss << "\n";
std::cout << " Validation Accuracy: " << training_result.validation_accuracy << "\n";
std::cout << " Epochs: " << training_result.epochs_completed << "\n";
std::cout << " Training Time: " << training_result.training_time.count() << " seconds\n";
} else {
std::cout << "❌ Training failed: " << training_result.error_message << "\n";
}
// ========================================================================
// 5. Train from Documentation Corpus
// ========================================================================
printSeparator();
std::cout << "5. Training from Documentation Corpus...\n\n";
std::cout << "Starting training from documentation corpus...\n";
auto doc_training_result = help.trainFromDocumentation();
if (doc_training_result.success) {
std::cout << "✅ Documentation training completed successfully!\n";
std::cout << " Final Loss: " << doc_training_result.final_loss << "\n";
std::cout << " Validation Accuracy: " << doc_training_result.validation_accuracy << "\n";
std::cout << " Epochs: " << doc_training_result.epochs_completed << "\n";
std::cout << " Training Time: " << doc_training_result.training_time.count() << " seconds\n";
} else {
std::cout << "❌ Documentation training failed: " << doc_training_result.error_message << "\n";
}
// ========================================================================
// 6. Get Performance Metrics
// ========================================================================
printSeparator();
std::cout << "6. Performance Metrics...\n\n";
auto metrics = help.getMetrics();
std::cout << "Performance Metrics:\n";
std::cout << " Total Queries: " << metrics["total_queries"] << "\n";
std::cout << " Successful Queries: " << metrics["successful_queries"] << "\n";
std::cout << " Failed Queries: " << metrics["failed_queries"] << "\n";
std::cout << " Success Rate: " << (metrics["success_rate"].get<double>() * 100.0) << "%\n";
std::cout << " Average Latency: " << metrics["average_latency_ms"] << " ms\n";
std::cout << " Cache Hit Rate: " << (metrics["cache_hit_rate"].get<double>() * 100.0) << "%\n";
// ========================================================================
// 7. Version Management
// ========================================================================
printSeparator();
std::cout << "7. Version Management...\n\n";
std::cout << "Current Version: " << help.getAdapterVersion() << "\n";
// Check if adapter is loaded
if (help.isAdapterLoaded()) {
std::cout << "Adapter Status: ✅ Loaded\n";
} else {
std::cout << "Adapter Status: ⚠️ Not Loaded\n";
}
// Reload adapter
std::cout << "\nReloading adapter...\n";
if (help.reloadAdapter()) {
std::cout << "✅ Adapter reloaded successfully\n";
} else {
std::cout << "❌ Failed to reload adapter\n";
}
// Rollback to previous version
std::cout << "\nRolling back to previous version...\n";
if (help.rollbackToPreviousVersion()) {
std::cout << "✅ Rolled back successfully\n";
std::cout << " New Version: " << help.getAdapterVersion() << "\n";
} else {
std::cout << "❌ Failed to rollback\n";
}
// ========================================================================
// 8. Final Status
// ========================================================================
printSeparator();
std::cout << "8. Final Status...\n\n";
auto final_stats = help.getFeedbackStats();
auto final_metrics = help.getMetrics();
std::cout << "ThemisHelpLoRA Final Status:\n";
std::cout << " Adapter Version: " << help.getAdapterVersion() << "\n";
std::cout << " Total Queries Processed: " << final_metrics["total_queries"] << "\n";
std::cout << " Feedback Collected: " << final_stats["total_feedback"] << "\n";
std::cout << " Success Rate: " << (final_metrics["success_rate"].get<double>() * 100.0) << "%\n";
printSeparator();
std::cout << "Example completed successfully!\n\n";
return 0;
}