gen-ai 0.2.3 → 0.3.1
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 +4 -4
- data/README.md +26 -19
- data/lib/gen_ai/image/open_ai.rb +13 -5
- data/lib/gen_ai/image/stability_ai.rb +7 -3
- data/lib/gen_ai/language/google_palm.rb +9 -1
- data/lib/gen_ai/result.rb +9 -2
- data/lib/gen_ai/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d0af08a39e94ab978107c2fafcbabf8c40906237212af8b85a8f14667892d43
|
4
|
+
data.tar.gz: 0b9392b7eab8ac6421a83ccaf0e81f658f46e050fbb8f5c34b30382cf542e564
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94def9a001633716757f550d6fc5cc601de10c53d6d542b401261fb55a90e21d43a35ac9257e9a9144cca049d4cc88734b7caee9313bccd6fa2cd905f74d2a68
|
7
|
+
data.tar.gz: 29047daf653faaa79cae27548aeb36620cb8cf8d26f634e9504356f6aa6d2768a9eefa68383f362f2c969c5e1c1ef13fe7d4bfa751a5cb31c2c46ff7dee9d3b2
|
data/README.md
CHANGED
@@ -21,29 +21,28 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
21
21
|
## Usage
|
22
22
|
|
23
23
|
Require it in you code:
|
24
|
+
|
24
25
|
```ruby
|
25
26
|
require 'gen_ai'
|
26
27
|
```
|
27
28
|
|
28
|
-
|
29
|
-
|
30
29
|
### Feature support
|
31
|
-
|
30
|
+
|
31
|
+
✅ - Supported | ❌ - Not supported | 🛠️ - Work in progress
|
32
32
|
|
33
33
|
Language models capabilities
|
34
34
|
|
35
35
|
| Provider | Embedding | Completion | Conversation | Sentiment | Summarization |
|
36
36
|
| ---------------- | :-------: | :--------: | :----------: | :-------: | :-----------: |
|
37
|
-
| **OpenAI** |
|
38
|
-
| **Google Palm2** |
|
39
|
-
|
37
|
+
| **OpenAI** | ✅ | ✅ | ✅ | 🛠️ | 🛠️ |
|
38
|
+
| **Google Palm2** | ✅ | ✅ | ✅ | 🛠️ | 🛠️ |
|
40
39
|
|
41
40
|
Image generation model capabilities
|
42
41
|
|
43
|
-
| Provider
|
44
|
-
|
|
45
|
-
| **OpenAI**
|
46
|
-
| **StabilityAI**
|
42
|
+
| Provider | Generate | Variations | Edit | Upscale |
|
43
|
+
| --------------- | :------: | :--------: | :--: | :-----: |
|
44
|
+
| **OpenAI** | ✅ | ✅ | ✅ | ❌ |
|
45
|
+
| **StabilityAI** | ✅ | ❌ | ✅ | ✅ |
|
47
46
|
|
48
47
|
### Language
|
49
48
|
|
@@ -123,15 +122,18 @@ result = model.generate('A painting of a dog')
|
|
123
122
|
# => #<GenAI::Result:0x0000000110be6f20...>
|
124
123
|
|
125
124
|
result.value
|
126
|
-
# =>
|
125
|
+
# => image binary
|
126
|
+
|
127
|
+
result.value(:base64)
|
128
|
+
# => image in base64
|
127
129
|
|
128
130
|
# Save image to file
|
129
131
|
File.open('dog.jpg', 'wb') do |f|
|
130
|
-
f.write(
|
132
|
+
f.write(result.value)
|
131
133
|
end
|
132
134
|
```
|
133
|
-

|
134
135
|
|
136
|
+

|
135
137
|
|
136
138
|
Get more **variations** of the same image
|
137
139
|
|
@@ -140,16 +142,19 @@ result = model.variations('./dog.jpg')
|
|
140
142
|
# => #<GenAI::Result:0x0000000116a1ec50...>
|
141
143
|
|
142
144
|
result.value
|
143
|
-
# =>
|
145
|
+
# => image binary
|
146
|
+
|
147
|
+
result.value(:base64)
|
148
|
+
# => image in base64
|
144
149
|
|
145
150
|
# Save image to file
|
146
151
|
File.open('dog_variation.jpg', 'wb') do |f|
|
147
|
-
f.write(
|
152
|
+
f.write(result.value)
|
148
153
|
end
|
149
154
|
|
150
155
|
```
|
151
|
-

|
152
156
|
|
157
|
+

|
153
158
|
|
154
159
|
**Editing** existing images with additional prompt
|
155
160
|
|
@@ -158,18 +163,20 @@ result = model.edit('./llama.jpg', 'A cute llama wearing a beret', mask: './mask
|
|
158
163
|
# => #<GenAI::Result:0x0000000116a1ec50...>
|
159
164
|
|
160
165
|
result.value
|
161
|
-
# =>
|
166
|
+
# => image binary
|
167
|
+
|
168
|
+
result.value(:base64)
|
169
|
+
# => image in base64
|
162
170
|
|
163
171
|
# Save image to file
|
164
172
|
File.open('dog_edited.jpg', 'wb') do |f|
|
165
|
-
f.write(
|
173
|
+
f.write(result.value)
|
166
174
|
end
|
167
175
|
```
|
168
176
|
|
169
177
|

|
170
178
|

|
171
179
|
|
172
|
-
|
173
180
|
## Development
|
174
181
|
|
175
182
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/gen_ai/image/open_ai.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'base64'
|
4
|
+
|
3
5
|
module GenAI
|
4
6
|
class Image
|
5
7
|
class OpenAI < Base
|
@@ -21,7 +23,7 @@ module GenAI
|
|
21
23
|
build_result(
|
22
24
|
raw: response,
|
23
25
|
model: parameters[:model],
|
24
|
-
parsed: response['data']
|
26
|
+
parsed: parse_response_data(response['data'])
|
25
27
|
)
|
26
28
|
end
|
27
29
|
|
@@ -32,8 +34,8 @@ module GenAI
|
|
32
34
|
|
33
35
|
build_result(
|
34
36
|
raw: response,
|
35
|
-
model:
|
36
|
-
parsed: response['data']
|
37
|
+
model: parameters[:model],
|
38
|
+
parsed: parse_response_data(response['data'])
|
37
39
|
)
|
38
40
|
end
|
39
41
|
|
@@ -44,8 +46,8 @@ module GenAI
|
|
44
46
|
|
45
47
|
build_result(
|
46
48
|
raw: response,
|
47
|
-
model:
|
48
|
-
parsed: response['data']
|
49
|
+
model: parameters[:model],
|
50
|
+
parsed: parse_response_data(response['data'])
|
49
51
|
)
|
50
52
|
end
|
51
53
|
|
@@ -64,6 +66,7 @@ module GenAI
|
|
64
66
|
{
|
65
67
|
image: image,
|
66
68
|
size: options.delete(:size) || DEFAULT_SIZE,
|
69
|
+
model: 'dall-e-2', # variation is only available on dall-e-2
|
67
70
|
response_format: options.delete(:response_format) || RESPONSE_FORMAT
|
68
71
|
}.merge(options)
|
69
72
|
end
|
@@ -73,9 +76,14 @@ module GenAI
|
|
73
76
|
image: image,
|
74
77
|
prompt: prompt,
|
75
78
|
size: options.delete(:size) || DEFAULT_SIZE,
|
79
|
+
model: 'dall-e-2', # edit is only available on dall-e-2
|
76
80
|
response_format: options.delete(:response_format) || RESPONSE_FORMAT
|
77
81
|
}.merge(options)
|
78
82
|
end
|
83
|
+
|
84
|
+
def parse_response_data(data)
|
85
|
+
data.map { |datum| Base64.decode64(datum[RESPONSE_FORMAT]) }
|
86
|
+
end
|
79
87
|
end
|
80
88
|
end
|
81
89
|
end
|
@@ -23,7 +23,7 @@ module GenAI
|
|
23
23
|
build_result(
|
24
24
|
raw: response,
|
25
25
|
model: model,
|
26
|
-
parsed: response['artifacts']
|
26
|
+
parsed: parse_response_data(response['artifacts'])
|
27
27
|
)
|
28
28
|
end
|
29
29
|
|
@@ -36,7 +36,7 @@ module GenAI
|
|
36
36
|
build_result(
|
37
37
|
raw: response,
|
38
38
|
model: model,
|
39
|
-
parsed: response['artifacts']
|
39
|
+
parsed: parse_response_data(response['artifacts'])
|
40
40
|
)
|
41
41
|
end
|
42
42
|
|
@@ -49,7 +49,7 @@ module GenAI
|
|
49
49
|
build_result(
|
50
50
|
raw: response,
|
51
51
|
model: model,
|
52
|
-
parsed: response['artifacts']
|
52
|
+
parsed: parse_response_data(response['artifacts'])
|
53
53
|
)
|
54
54
|
end
|
55
55
|
|
@@ -91,6 +91,10 @@ module GenAI
|
|
91
91
|
size = options.delete(:size) || DEFAULT_SIZE
|
92
92
|
size.split('x').map(&:to_i)
|
93
93
|
end
|
94
|
+
|
95
|
+
def parse_response_data(data)
|
96
|
+
data.map { |artifact| Base64.decode64(artifact['base64']) }
|
97
|
+
end
|
94
98
|
end
|
95
99
|
end
|
96
100
|
end
|
@@ -55,7 +55,7 @@ module GenAI
|
|
55
55
|
def build_chat_options(message, context, history, examples, options)
|
56
56
|
{
|
57
57
|
model: options.delete(:model) || CHAT_COMPLETION_MODEL,
|
58
|
-
messages: history.append(
|
58
|
+
messages: history.append(build_message(message, history)),
|
59
59
|
examples: compose_examples(examples),
|
60
60
|
context: context
|
61
61
|
}.merge(options)
|
@@ -87,6 +87,14 @@ module GenAI
|
|
87
87
|
object.respond_to?(:to_ary) ? object.to_ary || [object] : [object]
|
88
88
|
end
|
89
89
|
|
90
|
+
def build_message(message, history)
|
91
|
+
if message.is_a?(String)
|
92
|
+
{ author: history.dig(0, :role) || DEFAULT_ROLE, content: message }
|
93
|
+
else
|
94
|
+
message
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
90
98
|
def extract_embeddings(responses)
|
91
99
|
responses.map { |response| response.dig('embedding', 'value') }
|
92
100
|
end
|
data/lib/gen_ai/result.rb
CHANGED
@@ -11,8 +11,15 @@ module GenAI
|
|
11
11
|
@values = values
|
12
12
|
end
|
13
13
|
|
14
|
-
def value
|
15
|
-
|
14
|
+
def value(format = :raw)
|
15
|
+
case format
|
16
|
+
when :raw
|
17
|
+
values.first
|
18
|
+
when :base64
|
19
|
+
Base64.encode64(values.first)
|
20
|
+
else
|
21
|
+
raise "Unsupported format: #{format}"
|
22
|
+
end
|
16
23
|
end
|
17
24
|
|
18
25
|
def prompt_tokens
|
data/lib/gen_ai/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gen-ai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Chaplinsky
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|