Class: RCrewAI::Knowledge::Base
- Inherits:
-
Object
- Object
- RCrewAI::Knowledge::Base
- Defined in:
- lib/rcrewai/knowledge/base.rb
Overview
A knowledge base: loads sources, chunks their text, embeds the chunks, and
answers similarity queries. Attach one to an Agent (role-specific) or a
Crew (shared) via the knowledge_sources: option.
Instance Attribute Summary collapse
-
#sources ⇒ Object
readonly
Returns the value of attribute sources.
Instance Method Summary collapse
-
#build! ⇒ Object
Loads, chunks, and embeds all sources.
- #empty? ⇒ Boolean
-
#initialize(sources: [], embedder: nil, chunk_size: 1000, overlap: 100) ⇒ Base
constructor
A new instance of Base.
-
#search(query, k: 3) ⇒ Object
Returns up to k chunks most relevant to the query string.
Constructor Details
#initialize(sources: [], embedder: nil, chunk_size: 1000, overlap: 100) ⇒ Base
Returns a new instance of Base.
16 17 18 19 20 21 22 |
# File 'lib/rcrewai/knowledge/base.rb', line 16 def initialize(sources: [], embedder: nil, chunk_size: 1000, overlap: 100) @sources = Array(sources) @embedder = || Embedder.new @chunker = Chunker.new(chunk_size: chunk_size, overlap: overlap) @store = Store.new @built = false end |
Instance Attribute Details
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
14 15 16 |
# File 'lib/rcrewai/knowledge/base.rb', line 14 def sources @sources end |
Instance Method Details
#build! ⇒ Object
Loads, chunks, and embeds all sources. Idempotent.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rcrewai/knowledge/base.rb', line 25 def build! return self if @built chunks = @sources.flat_map { |source| @chunker.chunk(source.read) } unless chunks.empty? vectors = @embedder.(chunks) chunks.zip(vectors).each { |text, vector| @store.add(text, vector) } end @built = true self end |
#empty? ⇒ Boolean
47 48 49 |
# File 'lib/rcrewai/knowledge/base.rb', line 47 def empty? @sources.empty? end |
#search(query, k: 3) ⇒ Object
Returns up to k chunks most relevant to the query string.
39 40 41 42 43 44 45 |
# File 'lib/rcrewai/knowledge/base.rb', line 39 def search(query, k: 3) build! unless @built return [] if @store.empty? query_vector = @embedder.([query]).first @store.search(query_vector, k: k) end |