Class: Langchain::Tool::Base
- Inherits:
-
Object
- Object
- Langchain::Tool::Base
- 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
-
Calculator: calculate the result of a math expression
-
Database: executes SQL queries
-
FileSystem: interacts with the file system
-
GoogleSearch: search on Google (via SerpAPI)
-
RubyCodeInterpreter: runs ruby code
-
Weather: gets current weather data
-
Wikipedia: search on Wikipedia
Usage
-
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
-
Set the environment variables listed under **ENV Requirements**
export SERPAPI_API_KEY=paste-your-serpapi-api-key-here
-
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
-
Create a new folder in lib/langchain/tool/your_tool_name/
-
Inside of this folder create a file with a class YourToolName that inherits from Base
-
Add ‘NAME=` and `ANNOTATIONS_PATH=` constants in your Tool class
-
Implement various public methods in your tool class
-
Create a sidecar .json file in the same directory as your tool file annotating the methods in the Open API format
-
Add your tool to the README
Direct Known Subclasses
Calculator, Database, FileSystem, GoogleSearch, NewsRetriever, RubyCodeInterpreter, Vectorsearch, Weather, Wikipedia
Class Method Summary collapse
Instance Method Summary collapse
-
#method_annotations ⇒ Hash
Return tool’s method annotations as JSON.
-
#name ⇒ String
Returns the NAME constant of the tool.
-
#to_google_gemini_tools ⇒ Array<Hash>
Returns the tool as a list of Google Gemini formatted functions.
-
#to_openai_tools ⇒ Array<Hash>
Returns the tool as a list of OpenAI formatted functions.
Methods included from DependencyHelper
Class Method Details
.logger_options ⇒ Object
61 62 63 64 65 |
# File 'lib/langchain/tool/base.rb', line 61 def self. { color: :light_blue } end |
Instance Method Details
#method_annotations ⇒ Hash
Return tool’s method annotations as JSON
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 |
#name ⇒ String
Returns the NAME constant of the tool
57 58 59 |
# File 'lib/langchain/tool/base.rb', line 57 def name self.class.const_get(:NAME) end |
#to_google_gemini_tools ⇒ Array<Hash>
Returns the tool as a list of 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_tools ⇒ Array<Hash>
Returns the tool as a list of OpenAI formatted functions
70 71 72 |
# File 'lib/langchain/tool/base.rb', line 70 def to_openai_tools method_annotations end |