Class: Langchain::Tool::Weather
- Defined in:
- lib/langchain/tool/weather/weather.rb
Constant Summary collapse
- NAME =
A weather tool that gets current weather data
Current weather data is free for 1000 calls per day (home.openweathermap.org/api_keys) Forecast and historical data require registration with credit card, so not supported yet.
Gem requirements:
gem "open-weather-ruby-client", "~> 0.3.0" api_key: https://home.openweathermap.org/api_keys
Usage:
weather = Langchain::Tool::Weather.new(api_key: ENV["OPEN_WEATHER_API_KEY"]) weather.execute(input: "Boston, MA; imperial")
"weather"
- ANNOTATIONS_PATH =
Langchain.root.join("./langchain/tool/#{NAME}/#{NAME}.json").to_path
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#units ⇒ Object
readonly
Returns the value of attribute units.
Instance Method Summary collapse
-
#execute(input:) ⇒ String
Returns current weather for a city.
-
#initialize(api_key:, units: "metric") ⇒ Langchain::Tool::Weather
constructor
Initializes the Weather tool.
Methods inherited from Base
logger_options, #method_annotations, #name, #to_openai_tools
Methods included from DependencyHelper
Constructor Details
#initialize(api_key:, units: "metric") ⇒ Langchain::Tool::Weather
Initializes the Weather tool
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/langchain/tool/weather/weather.rb', line 28 def initialize(api_key:, units: "metric") depends_on "open-weather-ruby-client" require "open-weather-ruby-client" OpenWeather::Client.configure do |config| config.api_key = api_key config.user_agent = "Langchainrb Ruby Client" end @client = OpenWeather::Client.new end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
22 23 24 |
# File 'lib/langchain/tool/weather/weather.rb', line 22 def client @client end |
#units ⇒ Object (readonly)
Returns the value of attribute units.
22 23 24 |
# File 'lib/langchain/tool/weather/weather.rb', line 22 def units @units end |
Instance Method Details
#execute(input:) ⇒ String
Returns current weather for a city
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/langchain/tool/weather/weather.rb', line 44 def execute(input:) Langchain.logger.info("Executing for \"#{input}\"", for: self.class) input_array = input.split(";") city, units = *input_array.map(&:strip) data = client.current_weather(city: city, units: units) weather = data.main.map { |key, value| "#{key} #{value}" }.join(", ") "The current weather in #{data.name} is #{weather}" end |