Class: Langchain::Tool::Base

Inherits:
Object
  • Object
show all
Includes:
DependencyHelper
Defined in:
lib/langchain/tool/base.rb

Overview

Tools

Tools are used by Agents to perform specific tasks. A ‘Tool’ is a collection of functions (“methods”).

Available Tools

Usage

  1. Pick the tools you’d like to pass to an Agent and install the gems listed under **Gem Requirements**

    # For example to use the Calculator, GoogleSearch, and Wikipedia:
    gem install eqn
    gem install google_search_results
    gem install wikipedia-client
    
  2. Set the environment variables listed under **ENV Requirements**

    export SERPAPI_API_KEY=paste-your-serpapi-api-key-here
    
  3. Pass the tools when Agent is instantiated.

    agent = Langchain::Assistant.new(
      llm: Langchain::LLM::OpenAI.new(api_key: "YOUR_API_KEY"), # or other LLM that supports function calling (coming soon)
      thread: Langchain::Thread.new,
      tools: [
        Langchain::Tool::GoogleSearch.new(api_key: "YOUR_API_KEY"),
        Langchain::Tool::Calculator.new,
        Langchain::Tool::Wikipedia.new
      ]
    )
    

Adding Tools

  1. Create a new folder in lib/langchain/tool/your_tool_name/

  2. Inside of this folder create a file with a class YourToolName that inherits from Base

  3. Add ‘NAME=` and `ANNOTATIONS_PATH=` constants in your Tool class

  4. Implement various public methods in your tool class

  5. Create a sidecar .json file in the same directory as your tool file annotating the methods in the Open API format

  6. Add your tool to the README

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DependencyHelper

#depends_on

Class Method Details

.logger_optionsObject



61
62
63
64
65
# File 'lib/langchain/tool/base.rb', line 61

def self.logger_options
  {
    color: :light_blue
  }
end

Instance Method Details

#method_annotationsHash

Return tool’s method annotations as JSON

Returns:

  • (Hash)

    Tool’s method annotations



87
88
89
90
91
92
93
# File 'lib/langchain/tool/base.rb', line 87

def method_annotations
  JSON.parse(
    File.read(
      self.class.const_get(:ANNOTATIONS_PATH)
    )
  )
end

#nameString

Returns the NAME constant of the tool

Returns:

  • (String)

    tool name



57
58
59
# File 'lib/langchain/tool/base.rb', line 57

def name
  self.class.const_get(:NAME)
end

#to_google_gemini_toolsArray<Hash>

Returns the tool as a list of Google Gemini formatted functions

Returns:

  • (Array<Hash>)

    List of hashes representing the tool as Google Gemini formatted functions



77
78
79
80
81
82
# File 'lib/langchain/tool/base.rb', line 77

def to_google_gemini_tools
  method_annotations.map do |annotation|
    # Slice out only the content of the "function" key
    annotation["function"]
  end
end

#to_openai_toolsArray<Hash>

Returns the tool as a list of OpenAI formatted functions

Returns:

  • (Array<Hash>)

    List of hashes representing the tool as OpenAI formatted functions



70
71
72
# File 'lib/langchain/tool/base.rb', line 70

def to_openai_tools
  method_annotations
end