lammy 0.8.3 → 0.10.0

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: d44c524678b02c6e7bb07135102e42750ff70786f2941d7768f2d1598b7a8f98
4
- data.tar.gz: 8d94092fc72aecf3d1b2bad51f864785a99761448ea423f68bc82437fe8ed270
3
+ metadata.gz: d2e0b41537900b2d85dbdc0615b3fdbaa4d2c299c7bb725d96537aa80a92779e
4
+ data.tar.gz: f23d7da872f98736f60a25008f3e819e757ca0ffcc4745f83ba029d97141a410
5
5
  SHA512:
6
- metadata.gz: 0ef97bb570e1879206760211b0d7f2c76382281f73b6f3d2bd38801cf94348e22837dc79465d9b55c352ae7f917e3bdb4e33db5eb8057237f20ed206f25a9dc7
7
- data.tar.gz: 6db191e6da9b0b7f762d65e194fee4484b86965ebb74d6e6e67957cdd06d9a90d6162df86d8727bdf67754f3b1d3fedce1ff186956fe7fc35c8ef98245a7ae42
6
+ metadata.gz: fb5968d47e3f60ccd0235146fd46c65b777dccc0860c310bedfcd4e5415412fee1bd6b1f73c9e52849eb27b21b5ac2bfc72b0fc00790199adcafbdec3451361c
7
+ data.tar.gz: 33c1932bc38493bac9b71414057554cd46672270dc135dc33a51477ddadd3292eb8bed2ac57b33708b730c8eaec816ab051fa7c8fc7c99cc029b6465037ebd9f
data/lib/lammy/chat.rb CHANGED
@@ -23,16 +23,20 @@ module Lammy
23
23
  # Call the original method to get the user message
24
24
  user_message = original_method.bind(self).call(*args, &block)
25
25
 
26
- client = case settings[:model]
27
- when *OpenAI::MODELS
28
- OpenAI.new(settings)
29
- when *Claude::MODELS
30
- Claude.new(settings)
31
- else
32
- raise "Unsupported model: #{settings[:model]}"
33
- end
26
+ model = settings[:model] || Lammy.configuration.model
27
+ client = if OpenAI::MODELS.any? { |regex| model =~ regex }
28
+ OpenAI.new(settings)
29
+ elsif Claude::MODELS.any? { |regex| model =~ regex }
30
+ Claude.new(settings)
31
+ else
32
+ raise "Unsupported model: #{model}"
33
+ end
34
34
 
35
- client.chat(user_message, @system_message, @stream)
35
+ if !settings[:async] || @_with_sync_lammy
36
+ client.chat(user_message, @system_message, @stream)
37
+ else
38
+ ::Lammy::Job.perform_async(klass.name, self.id, method_name, *args)
39
+ end
36
40
  end
37
41
  end
38
42
  end
data/lib/lammy/claude.rb CHANGED
@@ -6,11 +6,12 @@ require 'hashie'
6
6
  module Lammy
7
7
  # Use the Claude API's Ruby library
8
8
  class Claude
9
- MODELS = %w[
10
- claude-3-5-sonnet-latest claude-3-5-haiku-latest claude-3-opus-latest
11
- claude-3-5-sonnet-20240620 claude-3-5-haiku-20241022
12
- claude-3-opus-20240229 claude-3-sonnet-20240229 claude-3-haiku-20240307
13
- claude-2.1 claude-2.0 claude-instant-1.2
9
+ MODELS = [
10
+ /\Aclaude-3-7-(?:sonnet|haiku|opus)(?:-\d{8}|-latest)?\z/,
11
+ /\Aclaude-3-5-(?:sonnet|haiku|opus)(?:-\d{8}|-latest)?\z/,
12
+ /\Aclaude-3-(?:sonnet|haiku|opus)(?:-\d{8}|-latest)?\z/,
13
+ /\Aclaude-2(?:\.\d)?\z/,
14
+ /\Aclaude-instant-(?:\d\.\d)?\z/
14
15
  ].freeze
15
16
 
16
17
  attr_reader :settings
@@ -24,7 +25,7 @@ module Lammy
24
25
  response = client.messages(
25
26
  parameters: {
26
27
  system: system_message,
27
- model: settings[:model],
28
+ model: settings[:model] || Lammy.configuration.model,
28
29
  max_tokens: settings[:max_tokens] || 4096,
29
30
  stream: stream ? ->(chunk) { stream.call(stream_content(chunk)) } : nil,
30
31
  messages: user_message.is_a?(Array) ? user_message : [vision(L.user(user_message))]
@@ -57,6 +58,7 @@ module Lammy
57
58
 
58
59
  def client
59
60
  return settings[:client] if settings[:client]
61
+ return Lammy.configuration.client if Lammy.configuration.client
60
62
 
61
63
  @client ||= ::Anthropic::Client.new(
62
64
  access_token: ENV.fetch('ANTHROPIC_API_KEY')
data/lib/lammy/openai.rb CHANGED
@@ -7,20 +7,15 @@ require 'base64'
7
7
  module Lammy
8
8
  # Use the OpenAI API's Ruby library
9
9
  class OpenAI
10
- MODELS = %w[
11
- gpt-4o gpt-4o-2024-08-06 gpt-4o-2024-05-13 gpt-4o chatgpt-4o-latest
12
-
13
- gpt-4o-mini gpt-4o-mini-2024-07-18
14
-
15
- o1-preview o1-preview-2024-09-12
16
-
17
- o1-mini o1-mini-2024-09-12
18
-
19
- gpt-3.5-turbo
20
-
21
- gpt-4-turbo gpt-4-turbo-2024-04-09 gpt-4 gpt-4-32k gpt-4-0125-preview gpt-4-1106-preview gpt-4-vision-preview
22
- gpt-3.5-turbo-0125 gpt-3.5-turbo-instruct gpt-3.5-turbo-1106 gpt-3.5-turbo-0613 gpt-3.5-turbo-16k-0613
23
- gpt-3.5-turbo-0301 davinci-002 babbage-002
10
+ MODELS = [
11
+ /^gpt-4o(?:-\d{4}-\d{2}-\d{2})?$/, /^chatgpt-4o-latest$/,
12
+ /^gpt-4o-mini(?:-\d{4}-\d{2}-\d{2})?$/,
13
+ /^o1(?:-preview(?:-\d{4}-\d{2}-\d{2})?)?$/,
14
+ /^o1-mini(?:-\d{4}-\d{2}-\d{2})?$/,
15
+ /^gpt-3\.5-turbo$/,
16
+ /^gpt-4(?:-turbo(?:-\d{4}-\d{2}-\d{2})?|-32k|-\d{4}-preview|-vision-preview)?$/,
17
+ /^gpt-3\.5-turbo-(?:\d{4}|\d{2}k-\d{4}|-instruct)$/,
18
+ /^(?:davinci|babbage)-002$/
24
19
  ].freeze
25
20
 
26
21
  EMBEDDINGS = %w[
@@ -40,7 +35,7 @@ module Lammy
40
35
 
41
36
  request = client.chat(
42
37
  parameters: {
43
- model: settings[:model],
38
+ model: settings[:model] || Lammy.configuration.model,
44
39
  response_format: schema,
45
40
  messages: messages,
46
41
  stream: stream ? ->(chunk) { stream.call(stream_content(chunk)) } : nil
@@ -118,6 +113,7 @@ module Lammy
118
113
 
119
114
  def client
120
115
  return settings[:client] if settings[:client]
116
+ return Lammy.configuration.client if Lammy.configuration.client
121
117
 
122
118
  @client ||= ::OpenAI::Client.new(
123
119
  access_token: ENV.fetch('OPENAI_ACCESS_TOKEN')
data/lib/lammy.rb CHANGED
@@ -1,5 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ module Lammy
4
+ class Configuration
5
+ attr_accessor :model, :client
6
+
7
+ def initialize
8
+ @model = nil
9
+ @client = nil
10
+ end
11
+ end
12
+
13
+ def self.configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def self.configure
18
+ yield(configuration)
19
+ end
20
+ end
21
+
3
22
  require 'lammy/embeddings'
4
23
  require 'lammy/claude'
5
24
  require 'lammy/openai'
@@ -13,6 +32,12 @@ module L
13
32
  base.extend ClassMethods
14
33
  end
15
34
 
35
+ def _lammy_perform_now(&block)
36
+ @_with_sync_lammy = true
37
+ block.call
38
+ @_with_sync_lammy = false
39
+ end
40
+
16
41
  # Wrap generative methods with handlers
17
42
  module ClassMethods
18
43
  def llm(**kwargs)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lammy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kamil Nicieja
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-29 00:00:00.000000000 Z
11
+ date: 2025-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: anthropic