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.

Examples:

Performing a web search

chat.search_web('ruby programming tutorials', 5)

Instance Method Summary collapse

Instance Method Details

#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.

nil if the search engine is not implemented

Parameters:

  • query (String)

    the search query string

  • n (Integer) (defaults to: nil)

    the maximum number of results to return

Returns:

  • (Array<String>, nil)

    an array of URLs from the search results or



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