Class: Desiru::Modules::Retrieve
- Inherits:
-
Desiru::Module
- Object
- Desiru::Module
- Desiru::Modules::Retrieve
- Defined in:
- lib/desiru/modules/retrieve.rb
Overview
Retrieve module for RAG (Retrieval Augmented Generation) Implements vector search capabilities with pluggable backends
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
Attributes inherited from Desiru::Module
#config, #demos, #metadata, #model, #signature
Instance Method Summary collapse
-
#add_documents(documents, embeddings: nil) ⇒ Object
Add documents to the retrieval index.
-
#clear_index ⇒ Object
Clear the retrieval index.
-
#document_count ⇒ Object
Get the current document count.
- #forward(**inputs) ⇒ Object
-
#initialize(signature = nil, backend: nil) ⇒ Retrieve
constructor
A new instance of Retrieve.
Methods inherited from Desiru::Module
#call, #reset, #to_h, #with_demos
Methods included from AsyncCapable
#call_async, #call_batch_async
Methods included from ErrorHandling
#safe_execute, #with_error_context, #with_retry
Methods included from Core::Traceable
#call, #disable_trace!, #enable_trace!, #trace_enabled?
Constructor Details
#initialize(signature = nil, backend: nil) ⇒ Retrieve
Returns a new instance of Retrieve.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/desiru/modules/retrieve.rb', line 10 def initialize(signature = nil, backend: nil, **) # Default signature for retrieval operations signature ||= 'query: string, k: integer? -> documents: list, scores: list' super(signature, **) # Initialize backend @backend = backend || InMemoryBackend.new validate_backend! end |
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
8 9 10 |
# File 'lib/desiru/modules/retrieve.rb', line 8 def backend @backend end |
Instance Method Details
#add_documents(documents, embeddings: nil) ⇒ Object
Add documents to the retrieval index
39 40 41 |
# File 'lib/desiru/modules/retrieve.rb', line 39 def add_documents(documents, embeddings: nil) backend.add(documents, embeddings: ) end |
#clear_index ⇒ Object
Clear the retrieval index
44 45 46 |
# File 'lib/desiru/modules/retrieve.rb', line 44 def clear_index backend.clear end |
#document_count ⇒ Object
Get the current document count
49 50 51 |
# File 'lib/desiru/modules/retrieve.rb', line 49 def document_count backend.size end |
#forward(**inputs) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/desiru/modules/retrieve.rb', line 21 def forward(**inputs) query = inputs[:query] # Handle k parameter - it might come as nil if optional # Note: 'k' is the standard parameter name in information retrieval k = inputs.fetch(:k, 5) k = 5 if k.nil? # Ensure we have a value even if nil was passed # Perform retrieval using the backend results = backend.search(query, k: k) # Separate documents and scores documents = results.map { |r| r[:document] } scores = results.map { |r| r[:score] } { documents: documents, scores: scores } end |