Class: Kodo::Tools::WebSearch

Inherits:
RubyLLM::Tool
  • Object
show all
Extended by:
PromptContributor
Defined in:
lib/kodo/tools/web_search.rb

Constant Summary collapse

DISABLED_GUIDANCE_WITH_SECRET_STORAGE =
"Tavily is the easiest option (free tier, 1000 searches/month, no credit card).\n" \
"Get an API key from https://app.tavily.com/sign-in\n" \
'They can paste the key right here in chat and you will store it securely.'
MAX_PER_TURN =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PromptContributor

capability_name, capability_primary, disabled_guidance, enabled_guidance

Constructor Details

#initialize(search_provider:, audit:) ⇒ WebSearch

Returns a new instance of WebSearch.



38
39
40
41
42
43
44
# File 'lib/kodo/tools/web_search.rb', line 38

def initialize(search_provider:, audit:)
  super()
  @search_provider = search_provider
  @audit = audit
  @turn_count = 0
  @turn_context = nil
end

Instance Attribute Details

#turn_context=(value) ⇒ Object (writeonly)

Sets the attribute turn_context

Parameters:

  • value —

    the value to set the attribute turn_context to.



36
37
38
# File 'lib/kodo/tools/web_search.rb', line 36

def turn_context=(value)
  @turn_context = value
end

Instance Method Details

#execute(query:, max_results: '5') ⇒ Object



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
75
# File 'lib/kodo/tools/web_search.rb', line 50

def execute(query:, max_results: '5')
  max_results = max_results.to_i.clamp(1, 10)

  @turn_count += 1
  if @turn_count > MAX_PER_TURN
    return "Rate limit reached (max #{MAX_PER_TURN} searches per message). Try again next message."
  end

  results = @search_provider.search(query, max_results: max_results)

  @audit.log(
    event: 'web_search',
    detail: "query:#{query} results:#{results.length}"
  )

  if results.empty?
    @turn_context&.web_fetched!
    return "No results found for: #{query}"
  end

  @turn_context&.web_fetched!

  wrap_as_untrusted(query, format_results(results))
rescue Kodo::Error => e
  e.message
end

#name ⇒ Object



77
78
79
# File 'lib/kodo/tools/web_search.rb', line 77

def name
  'web_search'
end

#reset_turn_count! ⇒ Object



46
47
48
# File 'lib/kodo/tools/web_search.rb', line 46

def reset_turn_count!
  @turn_count = 0
end