Class: Desiru::Modules::Retrieve

Inherits:
Desiru::Module show all
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

Attributes inherited from Desiru::Module

#config, #demos, #metadata, #model, #signature

Instance Method Summary collapse

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

#backendObject (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: embeddings)
end

#clear_indexObject

Clear the retrieval index



44
45
46
# File 'lib/desiru/modules/retrieve.rb', line 44

def clear_index
  backend.clear
end

#document_countObject

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