langchainrb 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/Gemfile.lock +3 -1
  4. data/README.md +7 -5
  5. data/examples/store_and_query_with_pinecone.rb +5 -4
  6. data/lib/langchain/agent/base.rb +5 -0
  7. data/lib/langchain/agent/chain_of_thought_agent/chain_of_thought_agent.rb +22 -10
  8. data/lib/langchain/agent/chain_of_thought_agent/chain_of_thought_agent_prompt.yaml +26 -0
  9. data/lib/langchain/agent/sql_query_agent/sql_query_agent.rb +7 -7
  10. data/lib/langchain/agent/sql_query_agent/sql_query_agent_answer_prompt.yaml +11 -0
  11. data/lib/langchain/agent/sql_query_agent/sql_query_agent_sql_prompt.yaml +21 -0
  12. data/lib/langchain/chunker/base.rb +15 -0
  13. data/lib/langchain/chunker/text.rb +38 -0
  14. data/lib/langchain/contextual_logger.rb +60 -0
  15. data/lib/langchain/conversation.rb +35 -4
  16. data/lib/langchain/data.rb +4 -0
  17. data/lib/langchain/llm/google_palm.rb +3 -2
  18. data/lib/langchain/llm/openai.rb +16 -6
  19. data/lib/langchain/llm/prompts/summarize_template.yaml +9 -0
  20. data/lib/langchain/llm/replicate.rb +1 -1
  21. data/lib/langchain/prompt/base.rb +2 -2
  22. data/lib/langchain/tool/base.rb +9 -3
  23. data/lib/langchain/tool/calculator.rb +2 -2
  24. data/lib/langchain/tool/database.rb +3 -3
  25. data/lib/langchain/tool/{serp_api.rb → google_search.rb} +9 -9
  26. data/lib/langchain/tool/ruby_code_interpreter.rb +1 -1
  27. data/lib/langchain/tool/weather.rb +2 -2
  28. data/lib/langchain/tool/wikipedia.rb +1 -1
  29. data/lib/langchain/utils/token_length/base_validator.rb +38 -0
  30. data/lib/langchain/utils/token_length/google_palm_validator.rb +9 -29
  31. data/lib/langchain/utils/token_length/openai_validator.rb +10 -27
  32. data/lib/langchain/utils/token_length/token_limit_exceeded.rb +17 -0
  33. data/lib/langchain/vectorsearch/base.rb +6 -0
  34. data/lib/langchain/vectorsearch/chroma.rb +1 -1
  35. data/lib/langchain/vectorsearch/hnswlib.rb +2 -2
  36. data/lib/langchain/vectorsearch/milvus.rb +1 -14
  37. data/lib/langchain/vectorsearch/pgvector.rb +1 -5
  38. data/lib/langchain/vectorsearch/pinecone.rb +1 -4
  39. data/lib/langchain/vectorsearch/qdrant.rb +1 -4
  40. data/lib/langchain/vectorsearch/weaviate.rb +1 -4
  41. data/lib/langchain/version.rb +1 -1
  42. data/lib/langchain.rb +28 -12
  43. metadata +30 -11
  44. data/lib/langchain/agent/chain_of_thought_agent/chain_of_thought_agent_prompt.json +0 -10
  45. data/lib/langchain/agent/sql_query_agent/sql_query_agent_answer_prompt.json +0 -10
  46. data/lib/langchain/agent/sql_query_agent/sql_query_agent_sql_prompt.json +0 -10
  47. data/lib/langchain/llm/prompts/summarize_template.json +0 -5
@@ -45,11 +45,11 @@ module Langchain::Prompt
45
45
  end
46
46
 
47
47
  #
48
- # Save the object to a file in JSON format.
48
+ # Save the object to a file in JSON or YAML format.
49
49
  #
50
50
  # @param file_path [String, Pathname] The path to the file to save the object to
51
51
  #
52
- # @raise [ArgumentError] If file_path doesn't end with .json
52
+ # @raise [ArgumentError] If file_path doesn't end with .json or .yaml or .yml
53
53
  #
54
54
  # @return [void]
55
55
  #
@@ -9,7 +9,7 @@ module Langchain::Tool
9
9
  #
10
10
  # - {Langchain::Tool::Calculator}: Calculate the result of a math expression
11
11
  # - {Langchain::Tool::RubyCodeInterpretor}: Runs ruby code
12
- # - {Langchain::Tool::Search}: search on Google (via SerpAPI)
12
+ # - {Langchain::Tool::GoogleSearch}: search on Google (via SerpAPI)
13
13
  # - {Langchain::Tool::Wikipedia}: search on Wikipedia
14
14
  #
15
15
  # == Usage
@@ -30,13 +30,13 @@ module Langchain::Tool
30
30
  # agent = Langchain::Agent::ChainOfThoughtAgent.new(
31
31
  # llm: :openai, # or :cohere, :hugging_face, :google_palm or :replicate
32
32
  # llm_api_key: ENV["OPENAI_API_KEY"],
33
- # tools: ["search", "calculator", "wikipedia"]
33
+ # tools: ["google_search", "calculator", "wikipedia"]
34
34
  # )
35
35
  #
36
36
  # 4. Confirm that the Agent is using the Tools you passed in:
37
37
  #
38
38
  # agent.tools
39
- # # => ["search", "calculator", "wikipedia"]
39
+ # # => ["google_search", "calculator", "wikipedia"]
40
40
  #
41
41
  # == Adding Tools
42
42
  #
@@ -57,6 +57,12 @@ module Langchain::Tool
57
57
  self.class.const_get(:NAME)
58
58
  end
59
59
 
60
+ def self.logger_options
61
+ {
62
+ color: :light_blue
63
+ }
64
+ end
65
+
60
66
  #
61
67
  # Returns the DESCRIPTION constant of the tool
62
68
  #
@@ -28,14 +28,14 @@ module Langchain::Tool
28
28
  # @param input [String] math expression
29
29
  # @return [String] Answer
30
30
  def execute(input:)
31
- Langchain.logger.info("[#{self.class.name}]".light_blue + ": Executing \"#{input}\"")
31
+ Langchain.logger.info("Executing \"#{input}\"", for: self.class)
32
32
 
33
33
  Eqn::Calculator.calc(input)
34
34
  rescue Eqn::ParseError, Eqn::NoVariableValueError
35
35
  # Sometimes the input is not a pure math expression, e.g: "12F in Celsius"
36
36
  # We can use the google answer box to evaluate this expression
37
37
  # TODO: Figure out to find a better way to evaluate these language expressions.
38
- hash_results = Langchain::Tool::SerpApi
38
+ hash_results = Langchain::Tool::GoogleSearch
39
39
  .new(api_key: ENV["SERPAPI_API_KEY"])
40
40
  .execute_search(input: input)
41
41
  hash_results.dig(:answer_box, :to) ||
@@ -39,7 +39,7 @@ module Langchain::Tool
39
39
  # @return [String] schema
40
40
  #
41
41
  def schema
42
- Langchain.logger.info("[#{self.class.name}]".light_blue + ": Dumping schema")
42
+ Langchain.logger.info("Dumping schema", for: self.class)
43
43
  db.dump_schema_migration(same_db: true, indexes: false) unless db.adapter_scheme == :mock
44
44
  end
45
45
 
@@ -50,11 +50,11 @@ module Langchain::Tool
50
50
  # @return [Array] results
51
51
  #
52
52
  def execute(input:)
53
- Langchain.logger.info("[#{self.class.name}]".light_blue + ": Executing \"#{input}\"")
53
+ Langchain.logger.info("Executing \"#{input}\"", for: self.class)
54
54
 
55
55
  db[input].to_a
56
56
  rescue Sequel::DatabaseError => e
57
- Langchain.logger.error("[#{self.class.name}]".light_red + ": #{e.message}")
57
+ Langchain.logger.error(e.message, for: self.class)
58
58
  end
59
59
  end
60
60
  end
@@ -1,18 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Langchain::Tool
4
- class SerpApi < Base
4
+ class GoogleSearch < Base
5
5
  #
6
- # Wrapper around SerpAPI
6
+ # Wrapper around Google Serp SPI
7
7
  #
8
8
  # Gem requirements: gem "google_search_results", "~> 2.0.0"
9
9
  #
10
10
  # Usage:
11
- # search = Langchain::Tool::SerpApi.new(api_key: "YOUR_API_KEY")
11
+ # search = Langchain::Tool::GoogleSearch.new(api_key: "YOUR_API_KEY")
12
12
  # search.execute(input: "What is the capital of France?")
13
13
  #
14
14
 
15
- NAME = "search"
15
+ NAME = "google_search"
16
16
 
17
17
  description <<~DESC
18
18
  A wrapper around Google Search.
@@ -26,10 +26,10 @@ module Langchain::Tool
26
26
  attr_reader :api_key
27
27
 
28
28
  #
29
- # Initializes the SerpAPI tool
29
+ # Initializes the Google Search tool
30
30
  #
31
- # @param api_key [String] SerpAPI API key
32
- # @return [Langchain::Tool::SerpApi] SerpAPI tool
31
+ # @param api_key [String] Search API key
32
+ # @return [Langchain::Tool::GoogleSearch] Google search tool
33
33
  #
34
34
  def initialize(api_key:)
35
35
  depends_on "google_search_results"
@@ -54,7 +54,7 @@ module Langchain::Tool
54
54
  # @return [String] Answer
55
55
  #
56
56
  def execute(input:)
57
- Langchain.logger.info("[#{self.class.name}]".light_blue + ": Executing \"#{input}\"")
57
+ Langchain.logger.info("Executing \"#{input}\"", for: self.class)
58
58
 
59
59
  hash_results = execute_search(input: input)
60
60
 
@@ -72,7 +72,7 @@ module Langchain::Tool
72
72
  # @return [Hash] hash_results JSON
73
73
  #
74
74
  def execute_search(input:)
75
- GoogleSearch
75
+ ::GoogleSearch
76
76
  .new(q: input, serp_api_key: api_key)
77
77
  .get_hash
78
78
  end
@@ -21,7 +21,7 @@ module Langchain::Tool
21
21
  # @param input [String] ruby code expression
22
22
  # @return [String] Answer
23
23
  def execute(input:)
24
- Langchain.logger.info("[#{self.class.name}]".light_blue + ": Executing \"#{input}\"")
24
+ Langchain.logger.info("Executing \"#{input}\"", for: self.class)
25
25
 
26
26
  safe_eval(input)
27
27
  end
@@ -21,7 +21,7 @@ module Langchain::Tool
21
21
 
22
22
  description <<~DESC
23
23
  Useful for getting current weather data
24
-
24
+
25
25
  The input to this tool should be a city name followed by the units (imperial, metric, or standard)
26
26
  Usage:
27
27
  Action Input: St Louis, Missouri; metric
@@ -54,7 +54,7 @@ module Langchain::Tool
54
54
  # @param input [String] comma separated city and unit (optional: imperial, metric, or standard)
55
55
  # @return [String] Answer
56
56
  def execute(input:)
57
- Langchain.logger.info("[#{self.class.name}]".light_blue + ": Executing for \"#{input}\"")
57
+ Langchain.logger.info("Executing for \"#{input}\"", for: self.class)
58
58
 
59
59
  input_array = input.split(";")
60
60
  city, units = *input_array.map(&:strip)
@@ -26,7 +26,7 @@ module Langchain::Tool
26
26
  # @param input [String] search query
27
27
  # @return [String] Answer
28
28
  def execute(input:)
29
- Langchain.logger.info("[#{self.class.name}]".light_blue + ": Executing \"#{input}\"")
29
+ Langchain.logger.info("Executing \"#{input}\"", for: self.class)
30
30
 
31
31
  page = ::Wikipedia.find(input)
32
32
  # It would be nice to figure out a way to provide page.content but the LLM token limit is an issue
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Langchain
4
+ module Utils
5
+ module TokenLength
6
+ #
7
+ # Calculate the `max_tokens:` parameter to be set by calculating the context length of the text minus the prompt length
8
+ #
9
+ # @param content [String | Array<String>] The text or array of texts to validate
10
+ # @param model_name [String] The model name to validate against
11
+ # @return [Integer] Whether the text is valid or not
12
+ # @raise [TokenLimitExceeded] If the text is too long
13
+ #
14
+ class BaseValidator
15
+ def self.validate_max_tokens!(content, model_name, options = {})
16
+ text_token_length = if content.is_a?(Array)
17
+ content.sum { |item| token_length(item.to_json, model_name, options) }
18
+ else
19
+ token_length(content, model_name, options)
20
+ end
21
+
22
+ leftover_tokens = token_limit(model_name) - text_token_length
23
+
24
+ # Raise an error even if whole prompt is equal to the model's token limit (leftover_tokens == 0)
25
+ if leftover_tokens <= 0
26
+ raise limit_exceeded_exception(token_limit(model_name), text_token_length)
27
+ end
28
+
29
+ leftover_tokens
30
+ end
31
+
32
+ def self.limit_exceeded_exception(limit, length)
33
+ TokenLimitExceeded.new("This model's maximum context length is #{limit} tokens, but the given text is #{length} tokens long.", length - limit)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -7,7 +7,7 @@ module Langchain
7
7
  # This class is meant to validate the length of the text passed in to Google Palm's API.
8
8
  # It is used to validate the token length before the API call is made
9
9
  #
10
- class GooglePalmValidator
10
+ class GooglePalmValidator < BaseValidator
11
11
  TOKEN_LIMITS = {
12
12
  # Source:
13
13
  # This data can be pulled when `list_models()` method is called: https://github.com/andreibondarev/google_palm_api#usage
@@ -26,43 +26,23 @@ module Langchain
26
26
  # }
27
27
  }.freeze
28
28
 
29
- #
30
- # Validate the context length of the text
31
- #
32
- # @param content [String | Array<String>] The text or array of texts to validate
33
- # @param model_name [String] The model name to validate against
34
- # @return [Integer] Whether the text is valid or not
35
- # @raise [TokenLimitExceeded] If the text is too long
36
- #
37
- def self.validate_max_tokens!(google_palm_llm, content, model_name)
38
- text_token_length = if content.is_a?(Array)
39
- content.sum { |item| token_length(google_palm_llm, item.to_json, model_name) }
40
- else
41
- token_length(google_palm_llm, content, model_name)
42
- end
43
-
44
- leftover_tokens = TOKEN_LIMITS.dig(model_name, "input_token_limit") - text_token_length
45
-
46
- # Raise an error even if whole prompt is equal to the model's token limit (leftover_tokens == 0)
47
- if leftover_tokens <= 0
48
- raise TokenLimitExceeded, "This model's maximum context length is #{TOKEN_LIMITS.dig(model_name, "input_token_limit")} tokens, but the given text is #{text_token_length} tokens long."
49
- end
50
-
51
- leftover_tokens
52
- end
53
-
54
29
  #
55
30
  # Calculate token length for a given text and model name
56
31
  #
57
- # @param llm [Langchain::LLM:GooglePalm] The Langchain::LLM:GooglePalm instance
58
32
  # @param text [String] The text to calculate the token length for
59
33
  # @param model_name [String] The model name to validate against
34
+ # @param options [Hash] the options to create a message with
35
+ # @option options [Langchain::LLM:GooglePalm] :llm The Langchain::LLM:GooglePalm instance
60
36
  # @return [Integer] The token length of the text
61
37
  #
62
- def self.token_length(llm, text, model_name = "chat-bison-001")
63
- response = llm.client.count_message_tokens(model: model_name, prompt: text)
38
+ def self.token_length(text, model_name = "chat-bison-001", options)
39
+ response = options[:llm].client.count_message_tokens(model: model_name, prompt: text)
64
40
  response.dig("tokenCount")
65
41
  end
42
+
43
+ def self.token_limit(model_name)
44
+ TOKEN_LIMITS.dig(model_name, "input_token_limit")
45
+ end
66
46
  end
67
47
  end
68
48
  end
@@ -9,7 +9,7 @@ module Langchain
9
9
  # This class is meant to validate the length of the text passed in to OpenAI's API.
10
10
  # It is used to validate the token length before the API call is made
11
11
  #
12
- class OpenAIValidator
12
+ class OpenAIValidator < BaseValidator
13
13
  TOKEN_LIMITS = {
14
14
  # Source:
15
15
  # https://platform.openai.com/docs/api-reference/embeddings
@@ -17,6 +17,9 @@ module Langchain
17
17
  "text-embedding-ada-002" => 8191,
18
18
  "gpt-3.5-turbo" => 4096,
19
19
  "gpt-3.5-turbo-0301" => 4096,
20
+ "gpt-3.5-turbo-0613" => 4096,
21
+ "gpt-3.5-turbo-16k" => 16384,
22
+ "gpt-3.5-turbo-16k-0613" => 16384,
20
23
  "text-davinci-003" => 4097,
21
24
  "text-davinci-002" => 4097,
22
25
  "code-davinci-002" => 8001,
@@ -24,6 +27,7 @@ module Langchain
24
27
  "gpt-4-0314" => 8192,
25
28
  "gpt-4-32k" => 32768,
26
29
  "gpt-4-32k-0314" => 32768,
30
+ "gpt-4-32k-0613" => 32768,
27
31
  "text-curie-001" => 2049,
28
32
  "text-babbage-001" => 2049,
29
33
  "text-ada-001" => 2049,
@@ -33,31 +37,6 @@ module Langchain
33
37
  "ada" => 2049
34
38
  }.freeze
35
39
 
36
- #
37
- # Calculate the `max_tokens:` parameter to be set by calculating the context length of the text minus the prompt length
38
- #
39
- # @param content [String | Array<String>] The text or array of texts to validate
40
- # @param model_name [String] The model name to validate against
41
- # @return [Integer] Whether the text is valid or not
42
- # @raise [TokenLimitExceeded] If the text is too long
43
- #
44
- def self.validate_max_tokens!(content, model_name)
45
- text_token_length = if content.is_a?(Array)
46
- content.sum { |item| token_length(item.to_json, model_name) }
47
- else
48
- token_length(content, model_name)
49
- end
50
-
51
- max_tokens = TOKEN_LIMITS[model_name] - text_token_length
52
-
53
- # Raise an error even if whole prompt is equal to the model's token limit (max_tokens == 0) since not response will be returned
54
- if max_tokens <= 0
55
- raise TokenLimitExceeded, "This model's maximum context length is #{TOKEN_LIMITS[model_name]} tokens, but the given text is #{text_token_length} tokens long."
56
- end
57
-
58
- max_tokens
59
- end
60
-
61
40
  #
62
41
  # Calculate token length for a given text and model name
63
42
  #
@@ -65,10 +44,14 @@ module Langchain
65
44
  # @param model_name [String] The model name to validate against
66
45
  # @return [Integer] The token length of the text
67
46
  #
68
- def self.token_length(text, model_name)
47
+ def self.token_length(text, model_name, options = {})
69
48
  encoder = Tiktoken.encoding_for_model(model_name)
70
49
  encoder.encode(text).length
71
50
  end
51
+
52
+ def self.token_limit(model_name)
53
+ TOKEN_LIMITS[model_name]
54
+ end
72
55
  end
73
56
  end
74
57
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Langchain
4
+ module Utils
5
+ module TokenLength
6
+ class TokenLimitExceeded < StandardError
7
+ attr_reader :token_overflow
8
+
9
+ def initialize(message = "", token_overflow = 0)
10
+ super message
11
+
12
+ @token_overflow = token_overflow
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -155,5 +155,11 @@ module Langchain::Vectorsearch
155
155
 
156
156
  add_texts(texts: texts)
157
157
  end
158
+
159
+ def self.logger_options
160
+ {
161
+ color: :blue
162
+ }
163
+ end
158
164
  end
159
165
  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:, api_key: nil)
11
+ # chroma = Langchain::Vectorsearch::Chroma.new(url:, index_name:, llm:, llm_api_key:, api_key: nil)
12
12
  #
13
13
 
14
14
  # Initialize the Chroma client
@@ -110,12 +110,12 @@ module Langchain::Vectorsearch
110
110
  if File.exist?(path_to_index)
111
111
  client.load_index(path_to_index)
112
112
 
113
- Langchain.logger.info("[#{self.class.name}]".blue + ": Successfully loaded the index at \"#{path_to_index}\"")
113
+ Langchain.logger.info("Successfully loaded the index at \"#{path_to_index}\"", for: self.class)
114
114
  else
115
115
  # Default max_elements: 100, but we constantly resize the index as new data is written to it
116
116
  client.init_index(max_elements: 100)
117
117
 
118
- Langchain.logger.info("[#{self.class.name}]".blue + ": Creating a new index at \"#{path_to_index}\"")
118
+ Langchain.logger.info("Creating a new index at \"#{path_to_index}\"", for: self.class)
119
119
  end
120
120
  end
121
121
  end
@@ -8,17 +8,9 @@ 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:)
11
+ # milvus = Langchain::Vectorsearch::Milvus.new(url:, index_name:, llm:, llm_api_key:)
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
- #
22
14
  def initialize(url:, index_name:, llm:, api_key: nil)
23
15
  depends_on "milvus"
24
16
  require "milvus"
@@ -29,11 +21,6 @@ module Langchain::Vectorsearch
29
21
  super(llm: llm)
30
22
  end
31
23
 
32
- #
33
- # Add a list of texts to the index
34
- #
35
- # @param texts [Array] The list of texts to add
36
- #
37
24
  def add_texts(texts:)
38
25
  client.entities.insert(
39
26
  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:)
11
+ # pgvector = Langchain::Vectorsearch::Pgvector.new(url:, index_name:, llm:, llm_api_key:)
12
12
  #
13
13
 
14
14
  # The operators supported by the PostgreSQL vector search adapter
@@ -20,14 +20,10 @@ module Langchain::Vectorsearch
20
20
 
21
21
  attr_reader :operator, :quoted_table_name
22
22
 
23
- #
24
- # Initialize the PostgreSQL client
25
- #
26
23
  # @param url [String] The URL of the PostgreSQL database
27
24
  # @param index_name [String] The name of the table to use for the index
28
25
  # @param llm [Object] The LLM client to use
29
26
  # @param api_key [String] The API key for the Vectorsearch DB (not used for PostgreSQL)
30
- #
31
27
  def initialize(url:, index_name:, llm:, api_key: nil)
32
28
  require "pg"
33
29
  require "pgvector"
@@ -8,17 +8,14 @@ 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:)
11
+ # pinecone = Langchain::Vectorsearch::Pinecone.new(environment:, api_key:, index_name:, llm:, llm_api_key:)
12
12
  #
13
13
 
14
- #
15
14
  # Initialize the Pinecone client
16
- #
17
15
  # @param environment [String] The environment to use
18
16
  # @param api_key [String] The API key to use
19
17
  # @param index_name [String] The name of the index to use
20
18
  # @param llm [Object] The LLM client to use
21
- #
22
19
  def initialize(environment:, api_key:, index_name:, llm:)
23
20
  depends_on "pinecone"
24
21
  require "pinecone"
@@ -8,17 +8,14 @@ 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:)
11
+ # qdrant = Langchain::Vectorsearch::Qdrant.new(url:, api_key:, index_name:, llm:, llm_api_key:)
12
12
  #
13
13
 
14
- #
15
14
  # Initialize the Qdrant client
16
- #
17
15
  # @param url [String] The URL of the Qdrant server
18
16
  # @param api_key [String] The API key to use
19
17
  # @param index_name [String] The name of the index to use
20
18
  # @param llm [Object] The LLM client to use
21
- #
22
19
  def initialize(url:, api_key:, index_name:, llm:)
23
20
  depends_on "qdrant-ruby"
24
21
  require "qdrant"
@@ -8,17 +8,14 @@ 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:)
11
+ # weaviate = Langchain::Vectorsearch::Weaviate.new(url:, api_key:, index_name:, llm:, llm_api_key:)
12
12
  #
13
13
 
14
- #
15
14
  # Initialize the Weaviate adapter
16
- #
17
15
  # @param url [String] The URL of the Weaviate instance
18
16
  # @param api_key [String] The API key to use
19
17
  # @param index_name [String] The name of the index to use
20
18
  # @param llm [Object] The LLM client to use
21
- #
22
19
  def initialize(url:, api_key:, index_name:, llm:)
23
20
  depends_on "weaviate-ruby"
24
21
  require "weaviate"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Langchain
4
- VERSION = "0.5.5"
4
+ VERSION = "0.5.6"
5
5
  end
data/lib/langchain.rb CHANGED
@@ -46,36 +46,48 @@ require_relative "./langchain/version"
46
46
  #
47
47
  # LangChain.rb uses standard logging mechanisms and defaults to :debug level. Most messages are at info level, but we will add debug or warn statements as needed. To show all log messages:
48
48
  #
49
- # Langchain.logger.level = :info
49
+ # Langchain.logger.level = :info
50
50
  module Langchain
51
+ autoload :Loader, "langchain/loader"
52
+ autoload :Data, "langchain/data"
53
+ autoload :Conversation, "langchain/conversation"
54
+ autoload :DependencyHelper, "langchain/dependency_helper"
55
+ autoload :ContextualLogger, "langchain/contextual_logger"
56
+
51
57
  class << self
52
- # @return [Logger]
53
- attr_accessor :logger
58
+ # @return [ContextualLogger]
59
+ attr_reader :logger
60
+
61
+ # @param logger [Logger]
62
+ # @return [ContextualLogger]
63
+ def logger=(logger)
64
+ @logger = ContextualLogger.new(logger)
65
+ end
54
66
 
55
67
  # @return [Pathname]
56
68
  attr_reader :root
57
69
  end
58
70
 
59
- @logger ||= ::Logger.new($stdout, level: :warn, formatter: ->(severity, datetime, progname, msg) { "[LangChain.rb]".yellow + " #{msg}\n" })
71
+ self.logger ||= ::Logger.new($stdout, level: :warn)
60
72
 
61
73
  @root = Pathname.new(__dir__)
62
74
 
63
- autoload :Loader, "langchain/loader"
64
- autoload :Data, "langchain/data"
65
- autoload :Conversation, "langchain/conversation"
66
- autoload :DependencyHelper, "langchain/dependency_helper"
67
-
68
75
  module Agent
69
76
  autoload :Base, "langchain/agent/base"
70
77
  autoload :ChainOfThoughtAgent, "langchain/agent/chain_of_thought_agent/chain_of_thought_agent.rb"
71
78
  autoload :SQLQueryAgent, "langchain/agent/sql_query_agent/sql_query_agent.rb"
72
79
  end
73
80
 
81
+ module Chunker
82
+ autoload :Base, "langchain/chunker/base"
83
+ autoload :Text, "langchain/chunker/text"
84
+ end
85
+
74
86
  module Tool
75
87
  autoload :Base, "langchain/tool/base"
76
88
  autoload :Calculator, "langchain/tool/calculator"
77
89
  autoload :RubyCodeInterpreter, "langchain/tool/ruby_code_interpreter"
78
- autoload :SerpApi, "langchain/tool/serp_api"
90
+ autoload :GoogleSearch, "langchain/tool/google_search"
79
91
  autoload :Weather, "langchain/tool/weather"
80
92
  autoload :Wikipedia, "langchain/tool/wikipedia"
81
93
  autoload :Database, "langchain/tool/database"
@@ -95,8 +107,8 @@ module Langchain
95
107
 
96
108
  module Utils
97
109
  module TokenLength
98
- class TokenLimitExceeded < StandardError; end
99
-
110
+ autoload :BaseValidator, "langchain/utils/token_length/base_validator"
111
+ autoload :TokenLimitExceeded, "langchain/utils/token_length/token_limit_exceeded"
100
112
  autoload :OpenAIValidator, "langchain/utils/token_length/openai_validator"
101
113
  autoload :GooglePalmValidator, "langchain/utils/token_length/google_palm_validator"
102
114
  end
@@ -130,4 +142,8 @@ module Langchain
130
142
  autoload :PromptTemplate, "langchain/prompt/prompt_template"
131
143
  autoload :FewShotPromptTemplate, "langchain/prompt/few_shot_prompt_template"
132
144
  end
145
+
146
+ module Errors
147
+ class BaseError < StandardError; end
148
+ end
133
149
  end