Class: Ragdoll::Core::Client
- Inherits:
-
Object
- Object
- Ragdoll::Core::Client
- Defined in:
- lib/ragdoll/core/client.rb
Instance Method Summary collapse
- #add_directory(path:, recursive: false) ⇒ Object
-
#add_document(path:, force: false) ⇒ Object
Document management.
- #add_text(content:, title:, **options) ⇒ Object
- #delete_document(id:) ⇒ Object
- #document_status(id:) ⇒ Object
-
#enhance_prompt(prompt:, context_limit: 5, **options) ⇒ Object
Primary method for RAG applications Returns context-enhanced content for AI prompts.
-
#get_context(query:, limit: 10, **options) ⇒ Object
Get relevant context without prompt enhancement.
- #get_document(id:) ⇒ Object
-
#healthy? ⇒ Boolean
Health check.
-
#hybrid_search(query:, **options) ⇒ Object
Hybrid search combining semantic and full-text search.
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #list_documents(**options) ⇒ Object
-
#search(query:, **options) ⇒ Object
Semantic search++ should incorporate hybrid search.
- #search_analytics(days: 30) ⇒ Object
-
#search_similar_content(query:, **options) ⇒ Object
Search similar content (core functionality).
-
#stats ⇒ Object
Analytics and stats.
- #update_document(id:, **updates) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ragdoll/core/client.rb', line 8 def initialize # Setup configuration services @config_service = Ragdoll::ConfigurationService.new @model_resolver = Ragdoll::ModelResolver.new(@config_service) # Setup logging setup_logging # Setup database connection Database.setup(@config_service.config.database) = Ragdoll::EmbeddingService.new( client: nil, config_service: @config_service, model_resolver: @model_resolver ) @search_engine = Ragdoll::SearchEngine.new(, config_service: @config_service) end |
Instance Method Details
#add_directory(path:, recursive: false) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/ragdoll/core/client.rb', line 245 def add_directory(path:, recursive: false) results = [] pattern = recursive ? File.join(path, "**", "*") : File.join(path, "*") Dir.glob(pattern).each do |file_path| next unless File.file?(file_path) begin doc_id = add_document(path: file_path) results << { file: file_path, document_id: doc_id, status: "success" } rescue StandardError => e results << { file: file_path, error: e., status: "error" } end end results end |
#add_document(path:, force: false) ⇒ Object
Document management
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 |
# File 'lib/ragdoll/core/client.rb', line 187 def add_document(path:, force: false) # Parse the document parsed = Ragdoll::DocumentProcessor.parse(path) # Extract title from metadata or use filename title = parsed[:metadata][:title] || File.basename(path, File.extname(path)) # Add document to database doc_id = Ragdoll::DocumentManagement.add_document(path, parsed[:content], { title: title, document_type: parsed[:document_type], **parsed[:metadata] }, force: force) # Queue background jobs for processing if content is available = false if parsed[:content].present? Ragdoll::GenerateEmbeddingsJob.perform_later(doc_id) Ragdoll::GenerateSummaryJob.perform_later(doc_id) Ragdoll::ExtractKeywordsJob.perform_later(doc_id) = true end # Return success information { success: true, document_id: doc_id, title: title, document_type: parsed[:document_type], content_length: parsed[:content]&.length || 0, embeddings_queued: , message: "Document '#{title}' added successfully with ID #{doc_id}" } rescue StandardError => e # StandardError => e { success: false, error: e., message: "Failed to add document: #{e.message}" } end |
#add_text(content:, title:, **options) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/ragdoll/core/client.rb', line 229 def add_text(content:, title:, **) # Add document to database doc_id = Ragdoll::DocumentManagement.add_document(title, content, { title: title, document_type: "text", ** }) # Queue background job for embeddings Ragdoll::GenerateEmbeddingsJob.perform_later(doc_id, chunk_size: [:chunk_size], chunk_overlap: [:chunk_overlap]) doc_id end |
#delete_document(id:) ⇒ Object
307 308 309 |
# File 'lib/ragdoll/core/client.rb', line 307 def delete_document(id:) Ragdoll::DocumentManagement.delete_document(id) end |
#document_status(id:) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/ragdoll/core/client.rb', line 271 def document_status(id:) document = Ragdoll::Document.find(id) = document..count { id: document.id, title: document.title, status: document.status, embeddings_count: , embeddings_ready: .positive?, content_preview: document.content&.first(200) || "No content", message: case document.status when "processed" "Document processed successfully with #{embeddings_count} embeddings" when "processing" "Document is being processed" when "pending" "Document is pending processing" when "error" "Document processing failed" else "Document status: #{document.status}" end } rescue ActiveRecord::RecordNotFound { success: false, error: "Document not found", message: "Document with ID #{id} does not exist" } end |
#enhance_prompt(prompt:, context_limit: 5, **options) ⇒ Object
Primary method for RAG applications Returns context-enhanced content for AI prompts
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ragdoll/core/client.rb', line 29 def enhance_prompt(prompt:, context_limit: 5, **) context_data = get_context(query: prompt, limit: context_limit, **) if context_data[:context_chunks].any? enhanced_prompt = build_enhanced_prompt(prompt, context_data[:combined_context]) { enhanced_prompt: enhanced_prompt, original_prompt: prompt, context_sources: context_data[:context_chunks].map { |chunk| chunk[:source] }, context_count: context_data[:total_chunks] } else { enhanced_prompt: prompt, original_prompt: prompt, context_sources: [], context_count: 0 } end end |
#get_context(query:, limit: 10, **options) ⇒ Object
Get relevant context without prompt enhancement
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 |
# File 'lib/ragdoll/core/client.rb', line 51 def get_context(query:, limit: 10, **) search_response = search_similar_content(query: query, limit: limit, **) # Handle both old format (array) and new format (hash with results/statistics) if search_response.is_a?(Hash) && search_response.key?(:results) results = search_response[:results] else # Fallback for old format results = search_response || [] end context_chunks = results.map do |result| { content: result[:content], source: result[:document_location], similarity: result[:similarity], chunk_index: result[:chunk_index] } end combined_context = context_chunks.map { |chunk| chunk[:content] }.join("\n\n") { context_chunks: context_chunks, combined_context: combined_context, total_chunks: context_chunks.length } end |
#get_document(id:) ⇒ Object
263 264 265 266 267 268 269 |
# File 'lib/ragdoll/core/client.rb', line 263 def get_document(id:) document_hash = Ragdoll::DocumentManagement.get_document(id) return nil unless document_hash # DocumentManagement.get_document already returns a hash with all needed info document_hash end |
#healthy? ⇒ Boolean
Health check
328 329 330 331 332 |
# File 'lib/ragdoll/core/client.rb', line 328 def healthy? Database.connected? && stats[:total_documents] >= 0 rescue StandardError false end |
#hybrid_search(query:, **options) ⇒ Object
Hybrid search combining semantic and full-text search
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 |
# File 'lib/ragdoll/core/client.rb', line 120 def hybrid_search(query:, **) start_time = Time.current # Extract tracking options session_id = [:session_id] user_id = [:user_id] track_search = .fetch(:track_search, true) # Generate embedding for the query = .(query) # Perform hybrid search results = Ragdoll::Document.hybrid_search(query, query_embedding: , **) execution_time = ((Time.current - start_time) * 1000).round # Record search if tracking enabled if track_search && query && !query.empty? begin # Format results for search recording - hybrid search returns different format search_results = results.map do |result| { embedding_id: result[:embedding_id] || result[:id], similarity: result[:similarity] || result[:score] || 0.0 } end # Extract filters from options filters = .slice(:document_type, :status).compact = .slice(:limit, :semantic_weight, :text_weight).compact Ragdoll::Search.record_search( query: query, query_embedding: , results: search_results, search_type: "hybrid", filters: filters, options: , execution_time_ms: execution_time, session_id: session_id, user_id: user_id ) rescue => e # Log error but don't fail the search puts "Warning: Hybrid search tracking failed: #{e.message}" if ENV["RAGDOLL_DEBUG"] end end { query: query, search_type: "hybrid", results: results, total_results: results.length, semantic_weight: [:semantic_weight] || 0.7, text_weight: [:text_weight] || 0.3 } rescue StandardError => e { query: query, search_type: "hybrid", results: [], total_results: 0, error: "Hybrid search failed: #{e.message}" } end |
#list_documents(**options) ⇒ Object
311 312 313 |
# File 'lib/ragdoll/core/client.rb', line 311 def list_documents(**) Ragdoll::DocumentManagement.list_documents() end |
#search(query:, **options) ⇒ Object
Semantic search++ should incorporate hybrid search
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 |
# File 'lib/ragdoll/core/client.rb', line 86 def search(query:, **) # Pass through tracking options to the search engine search_response = search_similar_content(query: query, **) # Handle both old format (array) and new format (hash with results/statistics) if search_response.is_a?(Hash) && search_response.key?(:results) results = search_response[:results] statistics = search_response[:statistics] execution_time_ms = search_response[:execution_time_ms] { query: query, results: results, total_results: results.length, statistics: statistics, execution_time_ms: execution_time_ms } else # Fallback for old format results = search_response || [] { query: query, results: results, total_results: results.length } end end |
#search_analytics(days: 30) ⇒ Object
320 321 322 323 324 325 |
# File 'lib/ragdoll/core/client.rb', line 320 def search_analytics(days: 30) # This could be implemented with additional database queries Ragdoll::Embedding.where("returned_at > ?", days.days.ago) .group("DATE(returned_at)") .count end |
#search_similar_content(query:, **options) ⇒ Object
Search similar content (core functionality)
115 116 117 |
# File 'lib/ragdoll/core/client.rb', line 115 def search_similar_content(query:, **) @search_engine.search_similar_content(query, **) end |
#stats ⇒ Object
Analytics and stats
316 317 318 |
# File 'lib/ragdoll/core/client.rb', line 316 def stats Ragdoll::DocumentManagement.get_document_stats end |
#update_document(id:, **updates) ⇒ Object
303 304 305 |
# File 'lib/ragdoll/core/client.rb', line 303 def update_document(id:, **updates) Ragdoll::DocumentManagement.update_document(id, **updates) end |