openai_ruby 0.5.3 → 0.5.5
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/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +49 -0
- data/lib/openai_ruby/client.rb +7 -7
- data/lib/openai_ruby/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c428b915e1985b7478318b5eee0938934ca406fec796bddab9f7499bbdc5feb6
|
|
4
|
+
data.tar.gz: d3e648abff19cd4bd266f05353500a4f8bcc1d221b376bb23ddbbd82eecdada7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2b4eaf9d508d7e904a21bec16456dd0dec8a36b2bb18d88111a61af91fcef67eecc94979c857bac14bf6f8cde9154b55ffd62e877bd766023fe1585ca2af82cb
|
|
7
|
+
data.tar.gz: 9bd6e7d5a8933e78abb8282fbbea3fb850952392247bb3fed6fb30423d4072a42e20f8673d7ffec760a6061e5e46eef64e8c584b6414cc0610eab813a90306af
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -92,8 +92,57 @@ p response.dig("choices", 0, "text") # "What is the date today?\n"
|
|
|
92
92
|
|
|
93
93
|
### Image
|
|
94
94
|
|
|
95
|
+
https://platform.openai.com/docs/api-reference/images
|
|
96
|
+
|
|
97
|
+
Generate an image:
|
|
98
|
+
|
|
95
99
|
```ruby
|
|
100
|
+
require "base64"
|
|
101
|
+
|
|
102
|
+
res = client.images.generate(
|
|
103
|
+
model: "gpt-image-2",
|
|
104
|
+
prompt: "A watercolor painting of a ruby gem on a desk",
|
|
105
|
+
size: "1024x1024"
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
response = JSON.parse(res.body)
|
|
109
|
+
image_data = response.dig("data", 0, "b64_json")
|
|
110
|
+
|
|
111
|
+
File.binwrite("ruby-gem.png", Base64.decode64(image_data)) if image_data
|
|
112
|
+
```
|
|
96
113
|
|
|
114
|
+
Edit one or more images:
|
|
115
|
+
|
|
116
|
+
```ruby
|
|
117
|
+
require "base64"
|
|
118
|
+
|
|
119
|
+
File.open("ruby-gem.png", "rb") do |image|
|
|
120
|
+
res = client.images.edit(
|
|
121
|
+
model: "gpt-image-2",
|
|
122
|
+
image: image,
|
|
123
|
+
prompt: "Add a small OpenAI logo sticker to the desk",
|
|
124
|
+
size: "1024x1024"
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
response = JSON.parse(res.body)
|
|
128
|
+
image_data = response.dig("data", 0, "b64_json")
|
|
129
|
+
|
|
130
|
+
File.binwrite("ruby-gem-edited.png", Base64.decode64(image_data)) if image_data
|
|
131
|
+
end
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
You can also pass multiple images when editing:
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
File.open("image-1.png", "rb") do |image_1|
|
|
138
|
+
File.open("image-2.png", "rb") do |image_2|
|
|
139
|
+
client.images.edit(
|
|
140
|
+
model: "gpt-image-2",
|
|
141
|
+
image: [image_1, image_2],
|
|
142
|
+
prompt: "Combine these images into one cohesive scene"
|
|
143
|
+
)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
97
146
|
```
|
|
98
147
|
|
|
99
148
|
## Development
|
data/lib/openai_ruby/client.rb
CHANGED
|
@@ -23,8 +23,7 @@ module OpenAI
|
|
|
23
23
|
def create_chat_completion(params = {})
|
|
24
24
|
parser = EventStreamParser::Parser.new
|
|
25
25
|
|
|
26
|
-
params
|
|
27
|
-
if params["stream"]
|
|
26
|
+
if streaming?(params)
|
|
28
27
|
connection.post("/v1/chat/completions") do |req|
|
|
29
28
|
req.body = params.to_json
|
|
30
29
|
req.options.on_data = proc do |chunk, _overall_received_bytes, env|
|
|
@@ -52,7 +51,6 @@ module OpenAI
|
|
|
52
51
|
end
|
|
53
52
|
|
|
54
53
|
def create_speech(params = {})
|
|
55
|
-
params.deep_stringify_keys!
|
|
56
54
|
connection.post("/v1/audio/speech", params.to_json)
|
|
57
55
|
end
|
|
58
56
|
|
|
@@ -61,17 +59,15 @@ module OpenAI
|
|
|
61
59
|
end
|
|
62
60
|
|
|
63
61
|
def create_realtime_call(sdp_offer:, session: nil)
|
|
64
|
-
uri = "#{base_uri}/v1/realtime/calls"
|
|
65
|
-
|
|
66
62
|
if session
|
|
67
63
|
boundary, body = build_multipart_body(sdp_offer, session)
|
|
68
|
-
|
|
64
|
+
connection.post("/v1/realtime/calls") do |req|
|
|
69
65
|
req.headers["Authorization"] = "Bearer #{api_key}"
|
|
70
66
|
req.headers["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
|
|
71
67
|
req.body = body
|
|
72
68
|
end
|
|
73
69
|
else
|
|
74
|
-
|
|
70
|
+
connection.post("/v1/realtime/calls") do |req|
|
|
75
71
|
req.headers["Content-Type"] = "application/sdp"
|
|
76
72
|
req.headers["Authorization"] = "Bearer #{api_key}"
|
|
77
73
|
req.body = sdp_offer
|
|
@@ -100,6 +96,10 @@ module OpenAI
|
|
|
100
96
|
default_value || maybe_json
|
|
101
97
|
end
|
|
102
98
|
|
|
99
|
+
def streaming?(params)
|
|
100
|
+
params[:stream] || params["stream"]
|
|
101
|
+
end
|
|
102
|
+
|
|
103
103
|
def build_multipart_body(sdp_offer, session)
|
|
104
104
|
boundary = "----RubyFormBoundary#{SecureRandom.hex(16)}"
|
|
105
105
|
parts = [
|
data/lib/openai_ruby/version.rb
CHANGED