Class: Rubyagents::WebSearch

Inherits:
Tool
  • Object
show all
Defined in:
lib/rubyagents/tools/web_search.rb

Overview

NOTE: DuckDuckGo HTML scraping is fragile - selectors may break if DDG changes their markup.

Instance Method Summary collapse

Methods inherited from Tool

description, inherited, input, inputs, output_type, to_prompt, to_schema, tool_name

Instance Method Details

#call(query:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubyagents/tools/web_search.rb', line 16

def call(query:)
  uri = URI("https://html.duckduckgo.com/html/")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri)
  request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
  request.set_form_data(q: query)

  response = http.request(request)

  # Parse basic results from HTML
  results = response.body.scan(/<a rel="nofollow" class="result__a" href="(.*?)".*?>(.*?)<\/a>/m)
  snippets = response.body.scan(/<a class="result__snippet".*?>(.*?)<\/a>/m)

  output = results.first(5).each_with_index.map do |r, i|
    url = CGI.unescapeHTML(r[0])
    title = r[1].gsub(/<.*?>/, "").strip
    snippet = snippets[i] ? snippets[i][0].gsub(/<.*?>/, "").strip : ""
    "#{i + 1}. #{title}\n   #{url}\n   #{snippet}"
  end

  output.empty? ? "No results found for: #{query}" : output.join("\n\n")
rescue => e
  "Search error: #{e.message}"
end