lammy 0.6.0 → 0.8.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: 656f2074be0687f9996946697d7bc3ea5286b384749113521012bb061f88f914
4
- data.tar.gz: de4f1f7c8ec35448807230777ee4a5e75a3eacb7d66c88978296d5f30bf38efd
3
+ metadata.gz: ec35c0b162ca174c4baaa39d040632e10ce1e18a8b79e281161ff79f99a0758b
4
+ data.tar.gz: 88542e30eddbd8e1d26e2fbef989e7e2467f0503abc1544410f9f6f9490f6083
5
5
  SHA512:
6
- metadata.gz: 1dc35a49b4139d03baf530930d6157023a8b6e4eed36fdf26ad9c042a4e8c8d87ee812330aef2c047e9f6f45d4240a6c5c74abd1fc223ca011a6cc32fe1fdfff
7
- data.tar.gz: 9066b3c808e63abc01822ec355bcf49dd19aa5632c5b68640ddba5d33fd951c05f12da14ea600049d9bdbe66f3a8ad803f3e6194ea6a114080887ee4178a2a4d
6
+ metadata.gz: 9f2230a56f061fb8595ad27a6539c42807e0507fbd86542866f79a9a40f8c98cd345d45a3d3611ebc7c738b5e0919b55f5567b27c60db83c393b64be25edcbde
7
+ data.tar.gz: d2c855118e9cfb30287ba8902c1dd74aed6bcd5161ba59b8752eeb755b75735ec1a634166b2493f718c5a6572d496f796e370c3232ec0e05dc01a0f30772441c
data/lib/lammy/chat.rb CHANGED
@@ -23,6 +23,10 @@ module L
23
23
  @system_message = message
24
24
  end
25
25
 
26
+ define_singleton_method(:stream) do |proc|
27
+ @stream = proc
28
+ end
29
+
26
30
  # Call the original method to get the user message
27
31
  user_message = original_method.bind(self).call(*args, &block)
28
32
 
@@ -35,7 +39,7 @@ module L
35
39
  raise "Unsupported model: #{settings[:model]}"
36
40
  end
37
41
 
38
- client.chat(user_message, @system_message)
42
+ client.chat(user_message, @system_message, @stream)
39
43
  end
40
44
  end
41
45
  end
data/lib/lammy/openai.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'openai'
4
4
  require 'hashie'
5
+ require 'base64'
5
6
 
6
7
  module L
7
8
  # Use the OpenAI API's Ruby library
@@ -22,18 +23,22 @@ module L
22
23
  end
23
24
 
24
25
  # Generate a response with support for structured output
25
- def chat(user_message, system_message = nil)
26
+ def chat(user_message, system_message = nil, stream = nil)
26
27
  schema = schema(settings)
27
28
  messages = messages(user_message, system_message)
28
29
 
29
- response = client.chat(
30
+ request = client.chat(
30
31
  parameters: {
31
32
  model: settings[:model],
32
33
  response_format: schema,
33
- messages: messages
34
+ messages: messages,
35
+ stream: stream ? ->(chunk) { stream.call(stream_content(chunk)) } : nil
34
36
  }.compact
35
- ).dig('choices', 0, 'message', 'content')
37
+ )
38
+
39
+ return stream if stream
36
40
 
41
+ response = request.dig('choices', 0, 'message', 'content')
37
42
  content = schema ? ::Hashie::Mash.new(JSON.parse(response)) : response
38
43
  array?(schema) ? content.items : content
39
44
  end
@@ -72,14 +77,34 @@ module L
72
77
 
73
78
  [
74
79
  system_message ? L.system(system_message) : nil,
75
- L.user(user_message)
80
+ vision(L.user(user_message))
76
81
  ].compact
77
82
  end
78
83
 
84
+ def vision(message)
85
+ image = message[:_image]
86
+ base = message.except(:_image)
87
+
88
+ return base unless image
89
+
90
+ messages = [
91
+ { 'type' => 'text', 'text' => message[:content] },
92
+ {
93
+ 'type' => 'image_url', 'image_url' => { 'url' => "data:image/jpeg;base64,#{Base64.encode64(image)}" }
94
+ }
95
+ ]
96
+
97
+ base.merge(content: messages)
98
+ end
99
+
79
100
  def array?(schema)
80
101
  schema.is_a?(Hash) && schema.dig('json_schema', 'schema', 'properties', 'items', 'type') == 'array'
81
102
  end
82
103
 
104
+ def stream_content(chunk)
105
+ chunk.dig('choices', 0, 'delta', 'content')
106
+ end
107
+
83
108
  def client
84
109
  return settings[:client] if settings[:client]
85
110
 
data/lib/lammy/schema.rb CHANGED
@@ -2,15 +2,25 @@
2
2
 
3
3
  module L
4
4
  module Schema
5
+ def self.formatted?(content)
6
+ content.is_a?(Hash) && content.key?(:role) && content.key?(:content)
7
+ end
8
+
5
9
  def system(content)
10
+ return content if L::Schema.formatted?(content)
11
+
6
12
  { role: :system, content: content }
7
13
  end
8
14
 
9
- def user(content)
10
- { role: :user, content: content }
15
+ def user(content, image: nil)
16
+ return content if L::Schema.formatted?(content)
17
+
18
+ { role: :user, content: content, _image: image }
11
19
  end
12
20
 
13
21
  def assistant(content)
22
+ return content if L::Schema.formatted?(content)
23
+
14
24
  { role: :assistant, content: content }
15
25
  end
16
26
 
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.6.0
4
+ version: 0.8.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-10-05 00:00:00.000000000 Z
11
+ date: 2024-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: anthropic
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '7.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.14.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.14.2
55
69
  description: An LLM library for Ruby
56
70
  email: kamil@nicieja.co
57
71
  executables: []