langchainrb 0.11.3 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c678ae75bc25b0501223f5b6ffd396a9159af4d0ddd87ddb1657429ed2ba24ce
4
- data.tar.gz: df50ef0a6d9c1a3100153a06084556cac983069d1a38739bd6606f39f63bd332
3
+ metadata.gz: 7f29aad35bc35dc95eb8673b11578b51c7449a19818989d9da5e640c6fb219c7
4
+ data.tar.gz: 4d0c4d3d424a82c7f02fb9e49ca52a5bdca5dfbce19fbfa22f2d74ef46d81eb7
5
5
  SHA512:
6
- metadata.gz: 3ec9f92f4c6221184b7a0a2c118caa6a56e7bc8505a83d5b5acb4daeb769ff90d8822b43c28f57adc06435dd2df5577268721345c4061d3dad6ecb919be18efc
7
- data.tar.gz: 53d54b0c6a82082438f2e2f1ca70d097a9b916bc283b72e52ce466b6f012c9624cf094586b17e93eaa49a796bf9911051d3f4b494b9ecc93c3ac6ee6cdc7e8fe
6
+ metadata.gz: 91b6f4fc5056308eab9119dcfda1be16857e6e9e6e531977148b1e8f31b72090794b67e6855afb95633b8f836b8d20921bc5a069afdc745d1114892143a177e1
7
+ data.tar.gz: f7a7949ab2efd960eacf3a93f7beaa9104403a93619b8c95ea094901c2d3d19b89980c81d293ae16035c5ff51fe021a09f2e81e2c0ed6854bff87d30e6def925
data/CHANGELOG.md CHANGED
@@ -1,9 +1,13 @@
1
1
  ## [Unreleased]
2
- - New `Langchain::Processors::Pptx` to parse .pptx files
3
- - New `Langchain::LLM::Anthropic#chat()` support
4
- - Misc fixes
5
2
 
6
- ## [0.11.3]
3
+ ## [0.12.0] - 2024-04-22
4
+ - [BREAKING] Rename `dimension` parameter to `dimensions` everywhere
5
+
6
+ ## [0.11.4] - 2024-04-19
7
+ - New `Langchain::LLM::AWSBedrock#chat()` to wrap Bedrock Claude requests
8
+ - New `Langchain::LLM::OllamaResponse#total_tokens()` method
9
+
10
+ ## [0.11.3] - 2024-04-16
7
11
  - New `Langchain::Processors::Pptx` to parse .pptx files
8
12
  - New `Langchain::LLM::Anthropic#chat()` support
9
13
  - Misc fixes
data/README.md CHANGED
@@ -60,7 +60,7 @@ Langchain.rb wraps supported LLMs in a unified interface allowing you to easily
60
60
  | [OpenAI](https://openai.com/?utm_source=langchainrb&utm_medium=github) | ✅ | ✅ | ✅ | ❌ | Including Azure OpenAI |
61
61
  | [AI21](https://ai21.com/?utm_source=langchainrb&utm_medium=github) | ❌ | ✅ | ❌ | ✅ | |
62
62
  | [Anthropic](https://anthropic.com/?utm_source=langchainrb&utm_medium=github) | ❌ | ✅ | ✅ | ❌ | |
63
- | [AWS Bedrock](https://aws.amazon.com/bedrock?utm_source=langchainrb&utm_medium=github) | ✅ | ✅ | | ❌ | Provides AWS, Cohere, AI21, Antropic and Stability AI models |
63
+ | [AWS Bedrock](https://aws.amazon.com/bedrock?utm_source=langchainrb&utm_medium=github) | ✅ | ✅ | | ❌ | Provides AWS, Cohere, AI21, Antropic and Stability AI models |
64
64
  | [Cohere](https://cohere.com/?utm_source=langchainrb&utm_medium=github) | ✅ | ✅ | ✅ | ✅ | |
65
65
  | [GooglePalm](https://ai.google/discover/palm2?utm_source=langchainrb&utm_medium=github) | ✅ | ✅ | ✅ | ✅ | |
66
66
  | [Google Vertex AI](https://cloud.google.com/vertex-ai?utm_source=langchainrb&utm_medium=github) | ✅ | ✅ | ❌ | ✅ | |
@@ -46,7 +46,10 @@ module Langchain::LLM
46
46
  }
47
47
  }.freeze
48
48
 
49
+ attr_reader :client, :defaults
50
+
49
51
  SUPPORTED_COMPLETION_PROVIDERS = %i[anthropic cohere ai21].freeze
52
+ SUPPORTED_CHAT_COMPLETION_PROVIDERS = %i[anthropic].freeze
50
53
  SUPPORTED_EMBEDDING_PROVIDERS = %i[amazon].freeze
51
54
 
52
55
  def initialize(completion_model: DEFAULTS[:completion_model_name], embedding_model: DEFAULTS[:embedding_model_name], aws_client_options: {}, default_options: {})
@@ -91,6 +94,8 @@ module Langchain::LLM
91
94
  def complete(prompt:, **params)
92
95
  raise "Completion provider #{completion_provider} is not supported." unless SUPPORTED_COMPLETION_PROVIDERS.include?(completion_provider)
93
96
 
97
+ raise "Model #{@defaults[:completion_model_name]} only supports #chat." if @defaults[:completion_model_name].include?("claude-3")
98
+
94
99
  parameters = compose_parameters params
95
100
 
96
101
  parameters[:prompt] = wrap_prompt prompt
@@ -105,6 +110,53 @@ module Langchain::LLM
105
110
  parse_response response
106
111
  end
107
112
 
113
+ # Generate a chat completion for a given prompt
114
+ # Currently only configured to work with the Anthropic provider and
115
+ # the claude-3 model family
116
+ # @param messages [Array] The messages to generate a completion for
117
+ # @param system [String] The system prompt to provide instructions
118
+ # @param model [String] The model to use for completion defaults to @defaults[:chat_completion_model_name]
119
+ # @param max_tokens [Integer] The maximum number of tokens to generate
120
+ # @param stop_sequences [Array] The stop sequences to use for completion
121
+ # @param temperature [Float] The temperature to use for completion
122
+ # @param top_p [Float] The top p to use for completion
123
+ # @param top_k [Integer] The top k to use for completion
124
+ # @return [Langchain::LLM::AnthropicMessagesResponse] Response object
125
+ def chat(
126
+ messages: [],
127
+ system: nil,
128
+ model: defaults[:completion_model_name],
129
+ max_tokens: defaults[:max_tokens_to_sample],
130
+ stop_sequences: nil,
131
+ temperature: nil,
132
+ top_p: nil,
133
+ top_k: nil
134
+ )
135
+ raise ArgumentError.new("messages argument is required") if messages.empty?
136
+
137
+ raise "Model #{model} does not support chat completions." unless Langchain::LLM::AwsBedrock::SUPPORTED_CHAT_COMPLETION_PROVIDERS.include?(completion_provider)
138
+
139
+ inference_parameters = {
140
+ messages: messages,
141
+ max_tokens: max_tokens,
142
+ anthropic_version: @defaults[:anthropic_version]
143
+ }
144
+ inference_parameters[:system] = system if system
145
+ inference_parameters[:stop_sequences] = stop_sequences if stop_sequences
146
+ inference_parameters[:temperature] = temperature if temperature
147
+ inference_parameters[:top_p] = top_p if top_p
148
+ inference_parameters[:top_k] = top_k if top_k
149
+
150
+ response = client.invoke_model({
151
+ model_id: model,
152
+ body: inference_parameters.to_json,
153
+ content_type: "application/json",
154
+ accept: "application/json"
155
+ })
156
+
157
+ parse_response response
158
+ end
159
+
108
160
  private
109
161
 
110
162
  def completion_provider
@@ -24,8 +24,8 @@ module Langchain::LLM
24
24
  # A client for communicating with the LLM
25
25
  attr_reader :client
26
26
 
27
- def default_dimension
28
- self.class.const_get(:DEFAULTS).dig(:dimension)
27
+ def default_dimensions
28
+ self.class.const_get(:DEFAULTS).dig(:dimensions)
29
29
  end
30
30
 
31
31
  #
@@ -15,7 +15,7 @@ module Langchain::LLM
15
15
  temperature: 0.0,
16
16
  completion_model_name: "command",
17
17
  embeddings_model_name: "small",
18
- dimension: 1024,
18
+ dimensions: 1024,
19
19
  truncate: "START"
20
20
  }.freeze
21
21
 
@@ -13,7 +13,7 @@ module Langchain::LLM
13
13
  class GooglePalm < Base
14
14
  DEFAULTS = {
15
15
  temperature: 0.0,
16
- dimension: 768, # This is what the `embedding-gecko-001` model generates
16
+ dimensions: 768, # This is what the `embedding-gecko-001` model generates
17
17
  completion_model_name: "text-bison-001",
18
18
  chat_completion_model_name: "chat-bison-001",
19
19
  embeddings_model_name: "embedding-gecko-001"
@@ -16,7 +16,7 @@ module Langchain::LLM
16
16
  max_output_tokens: 1000,
17
17
  top_p: 0.8,
18
18
  top_k: 40,
19
- dimension: 768,
19
+ dimensions: 768,
20
20
  completion_model_name: "text-bison", # Optional: tect-bison@001
21
21
  embeddings_model_name: "textembedding-gecko"
22
22
  }.freeze
@@ -16,7 +16,7 @@ module Langchain::LLM
16
16
  DEFAULTS = {
17
17
  temperature: 0.0,
18
18
  embeddings_model_name: "sentence-transformers/all-MiniLM-L6-v2",
19
- dimension: 384 # Vector size generated by the above model
19
+ dimensions: 384 # Vector size generated by the above model
20
20
  }.freeze
21
21
 
22
22
  #
@@ -41,9 +41,9 @@ module Langchain::LLM
41
41
 
42
42
  # Returns the # of vector dimensions for the embeddings
43
43
  # @return [Integer] The # of vector dimensions
44
- def default_dimension
44
+ def default_dimensions
45
45
  # since Ollama can run multiple models, look it up or generate an embedding and return the size
46
- @default_dimension ||=
46
+ @default_dimensions ||=
47
47
  EMBEDDING_SIZES.fetch(defaults[:embeddings_model_name].to_sym) do
48
48
  embed(text: "test").embedding.size
49
49
  end
@@ -184,7 +184,7 @@ module Langchain::LLM
184
184
  complete(prompt: prompt)
185
185
  end
186
186
 
187
- def default_dimension
187
+ def default_dimensions
188
188
  @defaults[:dimensions] || EMBEDDING_SIZES.fetch(defaults[:embeddings_model_name])
189
189
  end
190
190
 
@@ -24,7 +24,7 @@ module Langchain::LLM
24
24
  # TODO: Design the interface to pass and use different models
25
25
  completion_model_name: "replicate/vicuna-13b",
26
26
  embeddings_model_name: "creatorrr/all-mpnet-base-v2",
27
- dimension: 384
27
+ dimensions: 384
28
28
  }.freeze
29
29
 
30
30
  #
@@ -44,5 +44,9 @@ module Langchain::LLM
44
44
  def completion_tokens
45
45
  raw_response.dig("eval_count")
46
46
  end
47
+
48
+ def total_tokens
49
+ prompt_tokens + completion_tokens
50
+ end
47
51
  end
48
52
  end
@@ -104,7 +104,7 @@ module Langchain::Vectorsearch
104
104
  end
105
105
 
106
106
  def default_vector_settings
107
- {type: "dense_vector", dims: llm.default_dimension}
107
+ {type: "dense_vector", dims: llm.default_dimensions}
108
108
  end
109
109
 
110
110
  def vector_settings
@@ -54,7 +54,7 @@ module Langchain::Vectorsearch
54
54
  @db_path = db_path
55
55
  @table_name = index_name
56
56
 
57
- @vector_dimension = llm.default_dimension
57
+ @vector_dimensions = llm.default_dimensions
58
58
 
59
59
  super(llm: llm)
60
60
  end
@@ -64,7 +64,7 @@ module Langchain::Vectorsearch
64
64
  status_code, response = @client.database.create_table(@table_name, [
65
65
  {"name" => "ID", "dataType" => "STRING", "primaryKey" => true},
66
66
  {"name" => "Doc", "dataType" => "STRING"},
67
- {"name" => "Embedding", "dataType" => "VECTOR_FLOAT", "dimensions" => @vector_dimension}
67
+ {"name" => "Embedding", "dataType" => "VECTOR_FLOAT", "dimensions" => @vector_dimensions}
68
68
  ])
69
69
  raise "Failed to create table: #{response}" if status_code != 200
70
70
 
@@ -26,7 +26,7 @@ module Langchain::Vectorsearch
26
26
 
27
27
  super(llm: llm)
28
28
 
29
- @client = ::Hnswlib::HierarchicalNSW.new(space: DEFAULT_METRIC, dim: llm.default_dimension)
29
+ @client = ::Hnswlib::HierarchicalNSW.new(space: DEFAULT_METRIC, dim: llm.default_dimensions)
30
30
  @path_to_index = path_to_index
31
31
 
32
32
  initialize_index
@@ -71,7 +71,7 @@ module Langchain::Vectorsearch
71
71
  type_params: [
72
72
  {
73
73
  key: "dim",
74
- value: llm.default_dimension.to_s
74
+ value: llm.default_dimensions.to_s
75
75
  }
76
76
  ]
77
77
  }
@@ -101,11 +101,11 @@ module Langchain::Vectorsearch
101
101
  def create_default_schema
102
102
  db.run "CREATE EXTENSION IF NOT EXISTS vector"
103
103
  namespace_column = @namespace_column
104
- vector_dimension = llm.default_dimension
104
+ vector_dimensions = llm.default_dimensions
105
105
  db.create_table? table_name.to_sym do
106
106
  primary_key :id
107
107
  text :content
108
- column :vectors, "vector(#{vector_dimension})"
108
+ column :vectors, "vector(#{vector_dimensions})"
109
109
  text namespace_column.to_sym, default: nil
110
110
  end
111
111
  end
@@ -104,7 +104,7 @@ module Langchain::Vectorsearch
104
104
  client.create_index(
105
105
  metric: DEFAULT_METRIC,
106
106
  name: index_name,
107
- dimension: llm.default_dimension
107
+ dimension: llm.default_dimensions
108
108
  )
109
109
  end
110
110
 
@@ -93,7 +93,7 @@ module Langchain::Vectorsearch
93
93
  collection_name: index_name,
94
94
  vectors: {
95
95
  distance: DEFAULT_METRIC.capitalize,
96
- size: llm.default_dimension
96
+ size: llm.default_dimensions
97
97
  }
98
98
  )
99
99
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Langchain
4
- VERSION = "0.11.3"
4
+ VERSION = "0.12.0"
5
5
  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.11.3
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Bondarev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-17 00:00:00.000000000 Z
11
+ date: 2024-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport