lammy 0.7.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: 160025e8046546a063a9a75deb7e1f29186973f9b7fe2096018734c6d9e1a523
4
- data.tar.gz: 0402c839e540115883c2b3bc99b065ab2b7f590824aa05aa9b50ac42e288fd76
3
+ metadata.gz: ec35c0b162ca174c4baaa39d040632e10ce1e18a8b79e281161ff79f99a0758b
4
+ data.tar.gz: 88542e30eddbd8e1d26e2fbef989e7e2467f0503abc1544410f9f6f9490f6083
5
5
  SHA512:
6
- metadata.gz: 3db10fe3e0615d2a0c8a739e6a9baf5c406aa3ed8d7dc18f9521a470ffcd51a2544a9365f51614ff2f0f70d34719b485de3bbd18a82cacde05b529314808b93e
7
- data.tar.gz: 65ab65d92be1cca2f88382ab3c347c83c2182493dc4bf358379c35fb16d06d13053ed57d3b266dd2674e337c5db07090cc937e5ffa05ed4d113225d8cabf0a06
6
+ metadata.gz: 9f2230a56f061fb8595ad27a6539c42807e0507fbd86542866f79a9a40f8c98cd345d45a3d3611ebc7c738b5e0919b55f5567b27c60db83c393b64be25edcbde
7
+ data.tar.gz: d2c855118e9cfb30287ba8902c1dd74aed6bcd5161ba59b8752eeb755b75735ec1a634166b2493f718c5a6572d496f796e370c3232ec0e05dc01a0f30772441c
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
@@ -35,13 +36,11 @@ module L
35
36
  }.compact
36
37
  )
37
38
 
38
- if stream.nil?
39
- response = request.dig('choices', 0, 'message', 'content')
40
- content = schema ? ::Hashie::Mash.new(JSON.parse(response)) : response
41
- array?(schema) ? content.items : content
42
- else
43
- stream
44
- end
39
+ return stream if stream
40
+
41
+ response = request.dig('choices', 0, 'message', 'content')
42
+ content = schema ? ::Hashie::Mash.new(JSON.parse(response)) : response
43
+ array?(schema) ? content.items : content
45
44
  end
46
45
 
47
46
  # OpenAI’s text embeddings measure the relatedness of text strings. An embedding is a vector of floating point
@@ -78,10 +77,26 @@ module L
78
77
 
79
78
  [
80
79
  system_message ? L.system(system_message) : nil,
81
- L.user(user_message)
80
+ vision(L.user(user_message))
82
81
  ].compact
83
82
  end
84
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
+
85
100
  def array?(schema)
86
101
  schema.is_a?(Hash) && schema.dig('json_schema', 'schema', 'properties', 'items', 'type') == 'array'
87
102
  end
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.7.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-06 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