Class: Langchain::Tool::Wikipedia

Inherits:
Base
  • Object
show all
Defined in:
lib/langchain/tool/wikipedia/wikipedia.rb

Constant Summary collapse

NAME =

Tool that adds the capability to search using the Wikipedia API

Gem requirements:

gem "wikipedia-client", "~> 1.17.0"

Usage:

wikipedia = Langchain::Tool::Wikipedia.new
wikipedia.execute(input: "The Roman Empire")
"wikipedia"
ANNOTATIONS_PATH =
Langchain.root.join("./langchain/tool/#{NAME}/#{NAME}.json").to_path

Instance Method Summary collapse

Methods inherited from Base

logger_options, #method_annotations, #name, #to_google_gemini_tools, #to_openai_tools

Methods included from DependencyHelper

#depends_on

Constructor Details

#initializeWikipedia

Initializes the Wikipedia tool



19
20
21
# File 'lib/langchain/tool/wikipedia/wikipedia.rb', line 19

def initialize
  depends_on "wikipedia-client", req: "wikipedia"
end

Instance Method Details

#execute(input:) ⇒ String

Executes Wikipedia API search and returns the answer

Parameters:

  • input (String)

    search query

Returns:

  • (String)

    Answer



27
28
29
30
31
32
33
# File 'lib/langchain/tool/wikipedia/wikipedia.rb', line 27

def execute(input:)
  Langchain.logger.info("Executing \"#{input}\"", for: self.class)

  page = ::Wikipedia.find(input)
  # It would be nice to figure out a way to provide page.content but the LLM token limit is an issue
  page.summary
end