Module: OllamaChat::WebSearching
- Included in:
- Chat
- Defined in:
- lib/ollama_chat/web_searching.rb
Overview
A module that provides web search functionality for OllamaChat.
The WebSearching module encapsulates the logic for performing web searches using configured search engines. It handles query construction, location information integration, and delegates to engine-specific implementations for retrieving search results. The module supports multiple search engines including SearxNG and DuckDuckGo, making it flexible for different deployment scenarios and privacy preferences.
Instance Method Summary collapse
-
#search_engine ⇒ String
private
The search_engine method returns the currently configured web search engine to be used for online searches.
-
#search_web(query, n = nil) ⇒ Array<String>?
The search_web method performs a web search using the configured search engine.
-
#search_web_with_duckduckgo(query, n) ⇒ Array<String>
private
The search_web_with_duckduckgo method performs a web search using the DuckDuckGo search engine and extracts URLs from the search results.
-
#search_web_with_searxng(query, n) ⇒ Array<String>
private
The search_web_with_searxng method performs a web search using the SearxNG engine and returns the URLs of the first n search results.
Instance Method Details
#search_engine ⇒ String (private)
The search_engine method returns the currently configured web search engine to be used for online searches.
45 46 47 |
# File 'lib/ollama_chat/web_searching.rb', line 45 def search_engine config.web_search.use end |
#search_web(query, n = nil) ⇒ Array<String>?
The search_web method performs a web search using the configured search engine. It appends location information to the query if available and limits the number of results. The method delegates to engine-specific search methods based on the configured search engine.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ollama_chat/web_searching.rb', line 25 def search_web(query, n = nil) l = @messages.at_location.full? and query += " #{l}" n = n.to_i.clamp(1..) query = URI.encode_uri_component(query) search_command = :"search_web_with_#{search_engine}" if respond_to?(search_command, true) send(search_command, query, n) else STDOUT.puts "Search engine #{bold{search_engine}} not implemented!" nil end end |
#search_web_with_duckduckgo(query, n) ⇒ Array<String> (private)
The search_web_with_duckduckgo method performs a web search using the DuckDuckGo search engine and extracts URLs from the search results.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ollama_chat/web_searching.rb', line 77 def search_web_with_duckduckgo(query, n) url = config.web_search.engines.duckduckgo.url % { query: } OllamaChat::Utils::Fetcher.get( url, headers: config.request_headers?.to_h, debug: ) do |tmp| result = [] doc = Nokogiri::HTML(tmp) doc.css('.results_links').each do |link| if n > 0 url = link.css('.result__a').first&.[]('href') url.sub!(%r(\A(//duckduckgo\.com)?/l/\?uddg=), '') url.sub!(%r(&rut=.*), '') url = URI.decode_uri_component(url) url = URI.parse(url) url.host =~ /duckduckgo\.com/ and next url = url.to_s links.add(url) result << url n -= 1 else break end end result end end |
#search_web_with_searxng(query, n) ⇒ Array<String> (private)
The search_web_with_searxng method performs a web search using the SearxNG engine and returns the URLs of the first n search results.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ollama_chat/web_searching.rb', line 56 def search_web_with_searxng(query, n) url = config.web_search.engines.searxng.url % { query: } OllamaChat::Utils::Fetcher.get( url, headers: config.request_headers?.to_h, debug: ) do |tmp| data = JSON.parse(tmp.read, object_class: JSON::GenericObject) data.results.first(n).map(&:url) end end |