Class: Deepsearch::Engine::Steps::Rag::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/deepsearch/engine/steps/rag/process.rb

Overview

Implements the core Retrieval-Augmented Generation (RAG) logic. It takes a query and a set of parsed websites, then:

  1. Chunks the website content into smaller pieces.

  2. Generates embeddings for all text chunks concurrently in batches.

  3. Uses a similarity search to find the chunks most relevant to the query.

  4. Returns a result containing the relevant chunks.

Constant Summary collapse

CHUNK_BATCH_SIZE =
100
MAX_TOTAL_CHUNKS =
500
MAX_CHUNKS_PER_WEBSITE =
15
MAX_EMBEDDING_CONCURRENCY =
3

Instance Method Summary collapse

Constructor Details

#initialize(query:, parsed_websites:) ⇒ Process

Returns a new instance of Process.



28
29
30
31
32
33
34
# File 'lib/deepsearch/engine/steps/rag/process.rb', line 28

def initialize(query:, parsed_websites:)
  @query = Values::Query.new(text: query)
  @documents = parsed_websites.map do |website|
    { url: website.url, content: website.content }
  end
  @logger = Deepsearch.configuration.logger
end

Instance Method Details

#executeObject



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
# File 'lib/deepsearch/engine/steps/rag/process.rb', line 36

def execute
  chunker = Chunker.new
  all_chunks = @documents.each_with_object([]) do |doc, chunks|
    next if doc[:content].to_s.strip.empty?

    doc_chunks = chunker.chunk(doc[:content])
    if doc_chunks.count > MAX_CHUNKS_PER_WEBSITE
      @logger.debug("Truncating chunks for #{doc[:url]} from #{doc_chunks.count} to #{MAX_CHUNKS_PER_WEBSITE}")
      doc_chunks = doc_chunks.first(MAX_CHUNKS_PER_WEBSITE)
    end
    doc_chunks.each { |chunk| chunk.document_url = doc[:url] }
    chunks.concat(doc_chunks)
  end

  @logger.debug("Chunked #{@documents.count} documents into #{all_chunks.count} chunks")

  if all_chunks.count > MAX_TOTAL_CHUNKS
    @logger.debug("Chunk count (#{all_chunks.count}) exceeds limit of #{MAX_TOTAL_CHUNKS}. Truncating.")
    all_chunks = all_chunks.first(MAX_TOTAL_CHUNKS)
  end

  generate_embeddings_in_parallel(all_chunks)

  @logger.debug('Finished embedding generation, initiating similarity match..')
  chunks_with_embeddings = all_chunks.select(&:embedding)
  relevant_chunks = Similarity.new.find_relevant(@query, chunks_with_embeddings)
  @logger.debug("Found #{relevant_chunks.count} relevant chunks for query: '#{@query.text}'")

  Values::Result.new(
    query: @query,
    relevant_chunks: relevant_chunks
  )
rescue StandardError => e
  Values::Result.new(
    query: @query,
    relevant_chunks: [],
    error: e.message
  )
end