langchainrb 0.5.4 → 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87647b8a7e2dc49359f3f6d655eda501dcac26ebdd14247ad6c583be8dc1a71c
4
- data.tar.gz: fb7b4321caa4ff026439158f5ccfc2ae9e7b515a69c35cba87385f2cb367fa85
3
+ metadata.gz: 9781999daf45e5fedb0c7a905268866fbefd4581fd35a1a512ebb5844598f2c7
4
+ data.tar.gz: 93e2161a331151218cb94706827ab1ca2d94cb363613a5117ef7ce0c36cb9469
5
5
  SHA512:
6
- metadata.gz: 4f80677c43c00e6d50e0494aa79cb7648b9f3878ed8d2a5f4f2dc90e308a3639589f8457a4615821b70b44c5a43ae4f26fcf00d7548684740e4c05dbcc165bf8
7
- data.tar.gz: 4722233dbed83d21f2dadff19a9b79a30d8fd208d6e30bd057f018060c602b7f00f0526ee0364823597806388e2a8da48e883a5e6fa77b31490199685644b4d2
6
+ metadata.gz: 58255ecc90b645cf6b276bee83a385d567122fb8b90ad7a075f4b2ec90ba2f6871156c6e5762b0bd67b259371d1c35b3973fd6b4e631be858ea8a368aac163b7
7
+ data.tar.gz: ffcaba1dc980b1f3175269a223ebe522bae1a3e17931b1f911adf387f8fc4a32692d45712ab0032e5a9c273ba304d97ca0ce2086978381d17e716efa1941072a
data/.env.example CHANGED
@@ -5,6 +5,7 @@ HUGGING_FACE_API_KEY=
5
5
  MILVUS_URL=
6
6
  OPENAI_API_KEY=
7
7
  GOOGLE_PALM_API_KEY=
8
+ OPEN_WEATHER_API_KEY=
8
9
  PINECONE_API_KEY=
9
10
  PINECONE_ENVIRONMENT=
10
11
  POSTGRES_URL=
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.5.5] - 2023-06-12
4
+ - [BREAKING] Rename `Langchain::Chat` to `Langchain::Conversation`
5
+ - 🛠️ Tools
6
+ - Introducing `Langchain::Tool::Weather`, a tool that calls Open Weather API to retrieve the current weather
7
+
3
8
  ## [0.5.4] - 2023-06-10
4
9
  - 🔍 Vectorsearch
5
10
  - Introducing support for HNSWlib
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- langchainrb (0.5.4)
4
+ langchainrb (0.5.5)
5
5
  colorize (~> 0.8.1)
6
6
  tiktoken_ruby (~> 0.0.5)
7
7
 
@@ -135,6 +135,7 @@ GEM
135
135
  activesupport (>= 3.0)
136
136
  graphql
137
137
  hashery (2.1.2)
138
+ hashie (5.0.0)
138
139
  hnswlib (0.8.1)
139
140
  httparty (0.21.0)
140
141
  mini_mime (>= 1.0.0)
@@ -167,6 +168,11 @@ GEM
167
168
  racc (~> 1.4)
168
169
  nokogiri (1.14.3-x86_64-linux)
169
170
  racc (~> 1.4)
171
+ open-weather-ruby-client (0.3.0)
172
+ activesupport
173
+ faraday (>= 1.0.0)
174
+ faraday_middleware
175
+ hashie
170
176
  parallel (1.23.0)
171
177
  parser (3.2.2.1)
172
178
  ast (~> 2.4.1)
@@ -318,6 +324,7 @@ DEPENDENCIES
318
324
  langchainrb!
319
325
  milvus (~> 0.9.0)
320
326
  nokogiri (~> 1.13)
327
+ open-weather-ruby-client (~> 0.3.0)
321
328
  pdf-reader (~> 1.4)
322
329
  pg (~> 1.5)
323
330
  pgvector (~> 0.2)
data/README.md CHANGED
@@ -309,6 +309,7 @@ agent.run(question: "How many users have a name with length greater than 5 in th
309
309
  | "database" | Useful for querying a SQL database | | `gem "sequel", "~> 5.68.0"` |
310
310
  | "ruby_code_interpreter" | Interprets Ruby expressions | | `gem "safe_ruby", "~> 1.0.4"` |
311
311
  | "search" | A wrapper around Google Search | `ENV["SERPAPI_API_KEY"]` (https://serpapi.com/manage-api-key) | `gem "google_search_results", "~> 2.0.0"` |
312
+ | "weather" | Calls Open Weather API to retrieve the current weather | `ENV["OPEN_WEATHER_API_KEY]` (https://home.openweathermap.org/api_keys) | `gem "open-weather-ruby-client", "~> 0.3.0"` |
312
313
  | "wikipedia" | Calls Wikipedia API to retrieve the summary | | `gem "wikipedia-client", "~> 1.17.0"` |
313
314
 
314
315
  #### Loaders 🚚
@@ -1,11 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Langchain
4
- class Chat
5
- attr_reader :context
4
+ #
5
+ # A high-level API for running a conversation with an LLM.
6
+ # Currently supports: OpenAI and Google PaLM LLMs.
7
+ #
8
+ # Usage:
9
+ # llm = Langchain::LLM::OpenAI.new(api_key: "YOUR_API_KEY")
10
+ # chat = Langchain::Conversation.new(llm: llm)
11
+ # chat.set_context("You are a chatbot from the future")
12
+ # chat.message("Tell me about future technologies")
13
+ #
14
+ class Conversation
15
+ attr_reader :context, :examples, :messages
6
16
 
17
+ # Intialize Conversation with a LLM
18
+ #
19
+ # @param llm [Object] The LLM to use for the conversation
20
+ # @param options [Hash] Options to pass to the LLM, like temperature, top_k, etc.
21
+ # @return [Langchain::Conversation] The Langchain::Conversation instance
7
22
  def initialize(llm:, **options)
8
23
  @llm = llm
24
+ @options = options
9
25
  @context = nil
10
26
  @examples = []
11
27
  @messages = []
@@ -36,7 +52,7 @@ module Langchain
36
52
  private
37
53
 
38
54
  def llm_response(prompt)
39
- @llm.chat(messages: @messages, context: @context, examples: @examples)
55
+ @llm.chat(messages: @messages, context: @context, examples: @examples, **@options)
40
56
  end
41
57
 
42
58
  def append_ai_message(message)
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Langchain::Tool
4
+ class Weather < Base
5
+ #
6
+ # A weather tool that gets current weather data
7
+ #
8
+ # Current weather data is free for 1000 calls per day (https://home.openweathermap.org/api_keys)
9
+ # Forecast and historical data require registration with credit card, so not supported yet.
10
+ #
11
+ # Gem requirements:
12
+ # gem "open-weather-ruby-client", "~> 0.3.0"
13
+ # api_key: https://home.openweathermap.org/api_keys
14
+ #
15
+ # Usage:
16
+ # weather = Langchain::Tool::Weather.new(api_key: "YOUR_API_KEY")
17
+ # weather.execute(input: "Boston, MA; imperial")
18
+ #
19
+
20
+ NAME = "weather"
21
+
22
+ description <<~DESC
23
+ Useful for getting current weather data
24
+
25
+ The input to this tool should be a city name followed by the units (imperial, metric, or standard)
26
+ Usage:
27
+ Action Input: St Louis, Missouri; metric
28
+ Action Input: Boston, Massachusetts; imperial
29
+ Action Input: Dubai, AE; imperial
30
+ Action Input: Kiev, Ukraine; metric
31
+ DESC
32
+
33
+ attr_reader :client, :units
34
+
35
+ #
36
+ # Initializes the Weather tool
37
+ #
38
+ # @param api_key [String] Open Weather API key
39
+ # @return [Langchain::Tool::Weather] Weather tool
40
+ #
41
+ def initialize(api_key:, units: "metric")
42
+ depends_on "open-weather-ruby-client"
43
+ require "open-weather-ruby-client"
44
+
45
+ OpenWeather::Client.configure do |config|
46
+ config.api_key = api_key
47
+ config.user_agent = "Langchainrb Ruby Client"
48
+ end
49
+
50
+ @client = OpenWeather::Client.new
51
+ end
52
+
53
+ # Returns current weather for a city
54
+ # @param input [String] comma separated city and unit (optional: imperial, metric, or standard)
55
+ # @return [String] Answer
56
+ def execute(input:)
57
+ Langchain.logger.info("[#{self.class.name}]".light_blue + ": Executing for \"#{input}\"")
58
+
59
+ input_array = input.split(";")
60
+ city, units = *input_array.map(&:strip)
61
+
62
+ data = client.current_weather(city: city, units: units)
63
+ weather = data.main.map { |key, value| "#{key} #{value}" }.join(", ")
64
+ "The current weather in #{data.name} is #{weather}"
65
+ end
66
+ end
67
+ end
@@ -8,7 +8,7 @@ module Langchain::Vectorsearch
8
8
  # Gem requirements: gem "chroma-db", "~> 0.3.0"
9
9
  #
10
10
  # Usage:
11
- # chroma = Langchain::Vectorsearch::Chroma.new(url:, index_name:, llm:, llm_api_key:, api_key: nil)
11
+ # chroma = Langchain::Vectorsearch::Chroma.new(url:, index_name:, llm:, api_key: nil)
12
12
  #
13
13
 
14
14
  # Initialize the Chroma client
@@ -8,9 +8,17 @@ module Langchain::Vectorsearch
8
8
  # Gem requirements: gem "milvus", "~> 0.9.0"
9
9
  #
10
10
  # Usage:
11
- # milvus = Langchain::Vectorsearch::Milvus.new(url:, index_name:, llm:, llm_api_key:)
11
+ # milvus = Langchain::Vectorsearch::Milvus.new(url:, index_name:, llm:)
12
12
  #
13
13
 
14
+ #
15
+ # Initialize the Milvus client
16
+ #
17
+ # @param url [String] The URL of the Milvus server
18
+ # @param api_key [String] The API key to use
19
+ # @param index_name [String] The name of the index to use
20
+ # @param llm [Object] The LLM client to use
21
+ #
14
22
  def initialize(url:, index_name:, llm:, api_key: nil)
15
23
  depends_on "milvus"
16
24
  require "milvus"
@@ -21,6 +29,11 @@ module Langchain::Vectorsearch
21
29
  super(llm: llm)
22
30
  end
23
31
 
32
+ #
33
+ # Add a list of texts to the index
34
+ #
35
+ # @param texts [Array] The list of texts to add
36
+ #
24
37
  def add_texts(texts:)
25
38
  client.entities.insert(
26
39
  collection_name: index_name,
@@ -8,7 +8,7 @@ module Langchain::Vectorsearch
8
8
  # Gem requirements: gem "pgvector", "~> 0.2"
9
9
  #
10
10
  # Usage:
11
- # pgvector = Langchain::Vectorsearch::Pgvector.new(url:, index_name:, llm:, llm_api_key:)
11
+ # pgvector = Langchain::Vectorsearch::Pgvector.new(url:, index_name:, llm:)
12
12
  #
13
13
 
14
14
  # The operators supported by the PostgreSQL vector search adapter
@@ -20,10 +20,14 @@ module Langchain::Vectorsearch
20
20
 
21
21
  attr_reader :operator, :quoted_table_name
22
22
 
23
+ #
24
+ # Initialize the PostgreSQL client
25
+ #
23
26
  # @param url [String] The URL of the PostgreSQL database
24
27
  # @param index_name [String] The name of the table to use for the index
25
28
  # @param llm [Object] The LLM client to use
26
29
  # @param api_key [String] The API key for the Vectorsearch DB (not used for PostgreSQL)
30
+ #
27
31
  def initialize(url:, index_name:, llm:, api_key: nil)
28
32
  require "pg"
29
33
  require "pgvector"
@@ -8,14 +8,17 @@ module Langchain::Vectorsearch
8
8
  # Gem requirements: gem "pinecone", "~> 0.1.6"
9
9
  #
10
10
  # Usage:
11
- # pinecone = Langchain::Vectorsearch::Pinecone.new(environment:, api_key:, index_name:, llm:, llm_api_key:)
11
+ # pinecone = Langchain::Vectorsearch::Pinecone.new(environment:, api_key:, index_name:, llm:)
12
12
  #
13
13
 
14
+ #
14
15
  # Initialize the Pinecone client
16
+ #
15
17
  # @param environment [String] The environment to use
16
18
  # @param api_key [String] The API key to use
17
19
  # @param index_name [String] The name of the index to use
18
20
  # @param llm [Object] The LLM client to use
21
+ #
19
22
  def initialize(environment:, api_key:, index_name:, llm:)
20
23
  depends_on "pinecone"
21
24
  require "pinecone"
@@ -8,14 +8,17 @@ module Langchain::Vectorsearch
8
8
  # Gem requirements: gem "qdrant-ruby", "~> 0.9.0"
9
9
  #
10
10
  # Usage:
11
- # qdrant = Langchain::Vectorsearch::Qdrant.new(url:, api_key:, index_name:, llm:, llm_api_key:)
11
+ # qdrant = Langchain::Vectorsearch::Qdrant.new(url:, api_key:, index_name:, llm:)
12
12
  #
13
13
 
14
+ #
14
15
  # Initialize the Qdrant client
16
+ #
15
17
  # @param url [String] The URL of the Qdrant server
16
18
  # @param api_key [String] The API key to use
17
19
  # @param index_name [String] The name of the index to use
18
20
  # @param llm [Object] The LLM client to use
21
+ #
19
22
  def initialize(url:, api_key:, index_name:, llm:)
20
23
  depends_on "qdrant-ruby"
21
24
  require "qdrant"
@@ -8,14 +8,17 @@ module Langchain::Vectorsearch
8
8
  # Gem requirements: gem "weaviate-ruby", "~> 0.8.0"
9
9
  #
10
10
  # Usage:
11
- # weaviate = Langchain::Vectorsearch::Weaviate.new(url:, api_key:, index_name:, llm:, llm_api_key:)
11
+ # weaviate = Langchain::Vectorsearch::Weaviate.new(url:, api_key:, index_name:, llm:)
12
12
  #
13
13
 
14
+ #
14
15
  # Initialize the Weaviate adapter
16
+ #
15
17
  # @param url [String] The URL of the Weaviate instance
16
18
  # @param api_key [String] The API key to use
17
19
  # @param index_name [String] The name of the index to use
18
20
  # @param llm [Object] The LLM client to use
21
+ #
19
22
  def initialize(url:, api_key:, index_name:, llm:)
20
23
  depends_on "weaviate-ruby"
21
24
  require "weaviate"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Langchain
4
- VERSION = "0.5.4"
4
+ VERSION = "0.5.5"
5
5
  end
data/lib/langchain.rb CHANGED
@@ -62,7 +62,7 @@ module Langchain
62
62
 
63
63
  autoload :Loader, "langchain/loader"
64
64
  autoload :Data, "langchain/data"
65
- autoload :Chat, "langchain/chat"
65
+ autoload :Conversation, "langchain/conversation"
66
66
  autoload :DependencyHelper, "langchain/dependency_helper"
67
67
 
68
68
  module Agent
@@ -76,6 +76,7 @@ module Langchain
76
76
  autoload :Calculator, "langchain/tool/calculator"
77
77
  autoload :RubyCodeInterpreter, "langchain/tool/ruby_code_interpreter"
78
78
  autoload :SerpApi, "langchain/tool/serp_api"
79
+ autoload :Weather, "langchain/tool/weather"
79
80
  autoload :Wikipedia, "langchain/tool/wikipedia"
80
81
  autoload :Database, "langchain/tool/database"
81
82
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: langchainrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Bondarev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-10 00:00:00.000000000 Z
11
+ date: 2023-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tiktoken_ruby
@@ -248,6 +248,20 @@ dependencies:
248
248
  - - "~>"
249
249
  - !ruby/object:Gem::Version
250
250
  version: '1.13'
251
+ - !ruby/object:Gem::Dependency
252
+ name: open-weather-ruby-client
253
+ requirement: !ruby/object:Gem::Requirement
254
+ requirements:
255
+ - - "~>"
256
+ - !ruby/object:Gem::Version
257
+ version: 0.3.0
258
+ type: :development
259
+ prerelease: false
260
+ version_requirements: !ruby/object:Gem::Requirement
261
+ requirements:
262
+ - - "~>"
263
+ - !ruby/object:Gem::Version
264
+ version: 0.3.0
251
265
  - !ruby/object:Gem::Dependency
252
266
  name: pg
253
267
  requirement: !ruby/object:Gem::Requirement
@@ -446,7 +460,7 @@ files:
446
460
  - lib/langchain/agent/sql_query_agent/sql_query_agent.rb
447
461
  - lib/langchain/agent/sql_query_agent/sql_query_agent_answer_prompt.json
448
462
  - lib/langchain/agent/sql_query_agent/sql_query_agent_sql_prompt.json
449
- - lib/langchain/chat.rb
463
+ - lib/langchain/conversation.rb
450
464
  - lib/langchain/data.rb
451
465
  - lib/langchain/dependency_helper.rb
452
466
  - lib/langchain/llm/ai21.rb
@@ -476,6 +490,7 @@ files:
476
490
  - lib/langchain/tool/database.rb
477
491
  - lib/langchain/tool/ruby_code_interpreter.rb
478
492
  - lib/langchain/tool/serp_api.rb
493
+ - lib/langchain/tool/weather.rb
479
494
  - lib/langchain/tool/wikipedia.rb
480
495
  - lib/langchain/utils/token_length/google_palm_validator.rb
481
496
  - lib/langchain/utils/token_length/openai_validator.rb
@@ -497,6 +512,7 @@ metadata:
497
512
  homepage_uri: https://rubygems.org/gems/langchainrb
498
513
  source_code_uri: https://github.com/andreibondarev/langchainrb
499
514
  changelog_uri: https://github.com/andreibondarev/langchainrb/CHANGELOG.md
515
+ documentation_uri: https://rubydoc.info/gems/langchainrb
500
516
  post_install_message:
501
517
  rdoc_options: []
502
518
  require_paths:
@@ -512,7 +528,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
512
528
  - !ruby/object:Gem::Version
513
529
  version: '0'
514
530
  requirements: []
515
- rubygems_version: 3.2.3
531
+ rubygems_version: 3.3.7
516
532
  signing_key:
517
533
  specification_version: 4
518
534
  summary: Build LLM-backed Ruby applications with Ruby's LangChain