Class: Phronomy::KnowledgeSource::RAGKnowledge
- Defined in:
- lib/phronomy/knowledge_source/rag_knowledge.rb
Overview
A KnowledgeSource that retrieves semantically relevant chunks from a VectorStore.
On each #fetch call, the query is embedded and the k nearest documents are returned as knowledge chunks.
Instance Method Summary collapse
-
#fetch(query: nil) ⇒ Array<Hash>
Embed the query and retrieve the k nearest chunks from the vector store.
-
#initialize(store:, embeddings:, k: 5, type: :rag, source: nil) ⇒ RAGKnowledge
constructor
A new instance of RAGKnowledge.
Methods inherited from Base
Constructor Details
#initialize(store:, embeddings:, k: 5, type: :rag, source: nil) ⇒ RAGKnowledge
Returns a new instance of RAGKnowledge.
25 26 27 28 29 30 31 |
# File 'lib/phronomy/knowledge_source/rag_knowledge.rb', line 25 def initialize(store:, embeddings:, k: 5, type: :rag, source: nil) @store = store @embeddings = @k = k @type = type @source = source end |
Instance Method Details
#fetch(query: nil) ⇒ Array<Hash>
Embed the query and retrieve the k nearest chunks from the vector store.
Returns an empty array when query is nil or blank.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/phronomy/knowledge_source/rag_knowledge.rb', line 39 def fetch(query: nil) return [] if query.nil? || query.strip.empty? vector = @embeddings.(query) results = @store.search(query_embedding: vector, k: @k) results.map do |doc| chunk = {content: doc[:metadata][:content], type: @type} src = @source || doc[:metadata][:source] chunk[:source] = src if src chunk end end |