Class: Phronomy::Tools::VectorSearch

Inherits:
Agent::Context::Capability::Base show all
Defined in:
lib/phronomy/tools/vector_search.rb

Overview

A Capability::Base subclass that wraps a VectorStore::Base and a VectorStore::Embeddings::Base adapter so that an agent can perform semantic search as a tool call.

Do not instantiate this class directly. Use the factory method VectorSearch.from_store to produce a configured subclass, then pass it to your agent.

Examples:

store = Phronomy::VectorStore::InMemory.new
emb   = Phronomy::VectorStore::Embeddings::RubyLLMEmbeddings.new(model: "...")
tool  = Phronomy::Tools::VectorSearch.from_store(store, embeddings: emb,
          k: 3, tool_name: "search_docs",
          description: "Search the company knowledge base.")
agent = MyAgent.new
agent.tools tool

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Agent::Context::Capability::Base

#call, #call_async, execution_mode, max_result_size, #name, on_error, on_schema_error, param, param_enums, param_schemas, #params_schema, redact_params, requires_approval, #requires_approval, #requires_approval?, retry_on, retry_policies, scope, tool_name

Class Method Details

.from_store(store, embeddings:, k: 5, tool_name: "vector_search", description: nil) ⇒ Class

Build a VectorSearch tool backed by the given store and embeddings adapter.

Parameters:

Returns:

  • (Class)

    anonymous subclass of VectorSearch configured with the given store



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
# File 'lib/phronomy/tools/vector_search.rb', line 36

def from_store(store, embeddings:, k: 5, tool_name: "vector_search", description: nil)
  klass = Class.new(self)
  klass.tool_name(tool_name)
  klass.description(description || "Search the vector store for documents similar to the query.")

  klass.define_method(:initialize) do
    @store = store
    @embeddings = embeddings
    @k = k
  end

  klass.define_method(:execute) do |query:|
    embedding = @embeddings.embed(query)
    results = @store.search(query_embedding: embedding, k: @k)
    return "No results found." if results.empty?

    results.map.with_index(1) do |r, i|
      content = r.dig(:metadata, :content) ||
        r.dig(:metadata, :text) ||
        r[:metadata].to_s
      "[#{i}] (score: #{r[:score].round(3)}) #{content}"
    end.join("\n")
  end

  klass
end

Instance Method Details

#execute(query:) ⇒ Object

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/phronomy/tools/vector_search.rb', line 65

def execute(query:)
  raise NotImplementedError, "Use VectorSearch.from_store to create a configured instance"
end