inst_llm 0.2.0 → 0.2.2

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: 105c04ef5d12358f4ff663bc8c1278e731c0664a40ac016e29ec2defc999b6a3
4
- data.tar.gz: 4873e34b6e2677822a6f8cabb3cac06367a6062d04301810856b33b751b260fb
3
+ metadata.gz: ca52be39233578c28b7d4b2e7a91b46c0b972d3806050ebb64cf7874cb11ca6c
4
+ data.tar.gz: ba51f3024ad3982515a265ed427c99e01c814069804e1e6a917f516a02999362
5
5
  SHA512:
6
- metadata.gz: 6d7c85ea828972e507b8c680e340fb5ad76b5784277a441ecad7fb1ee215a2bc839c426daf76207cacf63b1ca9fe54ff4d7bf1bf3b9d1f78ef413a28e64ddc33
7
- data.tar.gz: 7ffed97282a142a6adb4227334ddc333825b2de7225ec61b2f8d1796681a68b693be4799f73aee09cc4f5b482c2c8722a7633f9973d47bb42a8546880d75b997
6
+ metadata.gz: 2e713687d83aedbd2b6ab655f849dc29fb544fb5ea74b76f52547022646edba3e6ed4a1c9eaf45d44dc313d3cb39cfe7e1dafbd16483011a7fd7cba767899148
7
+ data.tar.gz: 2a8591d2ecd8019ad46e0d5adda72ddc652a2de3719f239ecc78dcac3f79482feccb97bbc86812cb673dd48c61b45bc23d0a296f60bfa38e188e83a9850c63ef
@@ -12,6 +12,9 @@ module InstLLM
12
12
  "anthropic.claude-3-sonnet-20240229-v1:0": { format: :claude, provider: :bedrock, type: :chat },
13
13
  "anthropic.claude-3-haiku-20240307-v1:0": { format: :claude, provider: :bedrock, type: :chat },
14
14
 
15
+ "meta.llama3-8b-instruct-v1:0": { format: :llama3, provider: :bedrock, type: :chat },
16
+ "meta.llama3-70b-instruct-v1:0": { format: :llama3, provider: :bedrock, type: :chat },
17
+
15
18
  "mistral.mistral-7b-instruct-v0:2": { format: :mistral, provider: :bedrock, type: :chat },
16
19
  "mistral.mixtral-8x7b-instruct-v0:1": { format: :mistral, provider: :bedrock, type: :chat },
17
20
  "mistral.mistral-large-2402-v1:0": { format: :mistral, provider: :bedrock, type: :chat },
@@ -48,7 +51,10 @@ module InstLLM
48
51
  params = params_factory(model, messages, **options)
49
52
 
50
53
  begin
51
- res = client.invoke_model(**params)
54
+ res = client.invoke_model(
55
+ content_type: "application/json",
56
+ **params
57
+ )
52
58
  rescue => error
53
59
  raise map_error_type(error)
54
60
  end
@@ -107,13 +113,14 @@ module InstLLM
107
113
  params_table = {
108
114
  claude: Parameter::ClaudeParameters,
109
115
  cohere_embed: Parameter::CohereEmbedParameters,
116
+ llama3: Parameter::Llama3Parameters,
110
117
  mistral: Parameter::MistralParameters
111
118
  }
112
119
  params_table[MODELS[model][:format]].new(model: model, messages: messages, **options)
113
120
  end
114
121
 
115
122
  def embedding_response_factory(model, response)
116
- Response::EmbeddingResponse.send(:"from_#{MODELS[model][:format]}", model: model, response: response)
123
+ Response::EmbeddingResponse.send(:"from_#{MODELS[model][:format]}", model: model, response: response)
117
124
  end
118
125
 
119
126
  def response_factory(model, response)
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'claude_parameters'
4
- require_relative 'cohere_embed_parameters'
5
- require_relative 'mistral_parameters'
3
+ require_relative "claude_parameters"
4
+ require_relative "cohere_embed_parameters"
5
+ require_relative "llama3_parameters"
6
+ require_relative "mistral_parameters"
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module InstLLM
6
+ module Parameter
7
+ class Llama3Parameters
8
+ DEFAULT_OPTIONS = {
9
+ max_gen_len: nil,
10
+ temperature: nil,
11
+ top_p: nil,
12
+ }.freeze
13
+
14
+ def initialize(model:, messages: [], **options)
15
+ @model = model
16
+ @messages = messages.map { |message| format_message(message) }
17
+ @options = DEFAULT_OPTIONS.merge(options.slice(*DEFAULT_OPTIONS.keys)).compact
18
+ end
19
+
20
+ def to_hash
21
+ { model_id: @model, body: { prompt: prompt(@messages) }.merge(@options).to_json }
22
+ end
23
+
24
+ private
25
+
26
+ def format_message(message)
27
+ if message[:content].is_a?(String)
28
+ message
29
+ elsif message[:content].is_a?(Array)
30
+ content = message[:content].map do |m|
31
+ raise UnknownArgumentError, <<~ERR unless m[:type].to_sym == :text
32
+ Model does not support multiple types. Expected :text, received #{m[:type]}"
33
+ ERR
34
+
35
+ m[:text]
36
+ end
37
+ { role: message[:role], content: content.join("\n\n") }
38
+ else
39
+ raise ArgumentError, "Message content must be a String or Array; received: #{message[:content]}"
40
+ end
41
+ end
42
+
43
+ def prompt(messages)
44
+ messages << { role: :assistant, content: "" } if messages.last[:role].to_sym != :assistant
45
+
46
+ formatted_messages = messages.map do |message|
47
+ "<|start_header_id|>#{message[:role].to_s}<|end_header_id|>\n#{message[:content]}"
48
+ end
49
+
50
+ formatted_prompt = "<|begin_of_text|>#{formatted_messages.join("<|eot_id|>")}"
51
+
52
+ if messages.last[:role] == :user
53
+ formatted_prompt += "<|eot_id|>"
54
+ end
55
+
56
+ formatted_prompt
57
+ end
58
+ end
59
+ end
60
+ end
@@ -29,6 +29,18 @@ module InstLLM
29
29
  )
30
30
  end
31
31
 
32
+ def from_llama3(model:, response:)
33
+ new(
34
+ model: model,
35
+ message: { role: :assistant, content: response["generation"] },
36
+ stop_reason: response["stop_reason"],
37
+ usage: {
38
+ input_tokens: response["prompt_token_count"],
39
+ output_tokens: response["generation_token_count"]
40
+ }
41
+ )
42
+ end
43
+
32
44
  def from_mistral(model:, response:)
33
45
  new(
34
46
  model: model,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module InstLLM
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.2"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inst_llm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Pendleton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-18 00:00:00.000000000 Z
11
+ date: 2024-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-bedrockruntime
@@ -36,6 +36,7 @@ files:
36
36
  - lib/inst_llm/parameter/all.rb
37
37
  - lib/inst_llm/parameter/claude_parameters.rb
38
38
  - lib/inst_llm/parameter/cohere_embed_parameters.rb
39
+ - lib/inst_llm/parameter/llama3_parameters.rb
39
40
  - lib/inst_llm/parameter/mistral_parameters.rb
40
41
  - lib/inst_llm/response/all.rb
41
42
  - lib/inst_llm/response/chat_response.rb