ruby-openai 4.0.0 → 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/Gemfile.lock +2 -2
- data/README.md +28 -4
- data/lib/openai/http.rb +18 -2
- data/lib/openai/version.rb +1 -1
- data/lib/openai.rb +2 -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: 5654d4f5edeb9b912b06916b13c915d4a795e4ad64df4ae4be792ca562bdfd99
|
4
|
+
data.tar.gz: b3b6457c556d9a4355afe7da1c514fc6029df5dc20e56a893981ec5410d14eac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '08e9e61ecc221384d0460ed5692ff5ac482e558219768402d05ed5b321e4aaf50ed94b26c8b938ae546da29db609b79f45439c0942f252f9059c6053a416f2dd'
|
7
|
+
data.tar.gz: ddd59e60920f73d230a068e4d25a98704ea3e39203c45f0b295837d42e72a16a1d4064772cb6da9be35c65fb512a29cc617d325e3646e3a3c677263693ab9725
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
+
## [4.2.0] - 2023-06-20
|
9
|
+
|
10
|
+
### Added
|
11
|
+
|
12
|
+
- Add Azure OpenAI Service support. Thanks to [@rmachielse](https://github.com/rmachielse) and [@steffansluis](https://github.com/steffansluis) for the PR and to everyone who requested this feature!
|
13
|
+
|
14
|
+
## [4.1.0] - 2023-05-15
|
15
|
+
|
16
|
+
### Added
|
17
|
+
|
18
|
+
- Add the ability to trigger any callable object as stream chunks come through, not just Procs. Big thanks to [@obie](https://github.com/obie) for this change.
|
19
|
+
|
8
20
|
## [4.0.0] - 2023-04-25
|
9
21
|
|
10
22
|
### Added
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ruby-openai (4.
|
4
|
+
ruby-openai (4.2.0)
|
5
5
|
faraday (>= 1)
|
6
6
|
faraday-multipart (>= 1)
|
7
7
|
|
@@ -16,7 +16,7 @@ GEM
|
|
16
16
|
rexml
|
17
17
|
diff-lcs (1.5.0)
|
18
18
|
dotenv (2.8.1)
|
19
|
-
faraday (2.7.
|
19
|
+
faraday (2.7.6)
|
20
20
|
faraday-net_http (>= 2.0, < 3.1)
|
21
21
|
ruby2_keywords (>= 0.0.4)
|
22
22
|
faraday-multipart (1.0.4)
|
data/README.md
CHANGED
@@ -10,6 +10,10 @@ Stream text with GPT-4, transcribe and translate audio with Whisper, or create i
|
|
10
10
|
|
11
11
|
[Ruby AI Builders Discord](https://discord.gg/k4Uc224xVD)
|
12
12
|
|
13
|
+
[Quick guide to streaming ChatGPT with Rails 7 and Hotwire](https://gist.github.com/alexrudall/cb5ee1e109353ef358adb4e66631799d)
|
14
|
+
|
15
|
+
Follow me on [Twitter](https://twitter.com/alexrudall) for more Ruby / AI content
|
16
|
+
|
13
17
|
### Bundler
|
14
18
|
|
15
19
|
Add this line to your application's Gemfile:
|
@@ -36,8 +40,8 @@ require "openai"
|
|
36
40
|
|
37
41
|
## Usage
|
38
42
|
|
39
|
-
- Get your API key from [https://
|
40
|
-
- If you belong to multiple organizations, you can get your Organization ID from [https://
|
43
|
+
- Get your API key from [https://platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys)
|
44
|
+
- If you belong to multiple organizations, you can get your Organization ID from [https://platform.openai.com/account/org-settings](https://platform.openai.com/account/org-settings)
|
41
45
|
|
42
46
|
### Quickstart
|
43
47
|
|
@@ -87,6 +91,21 @@ OpenAI.configure do |config|
|
|
87
91
|
end
|
88
92
|
```
|
89
93
|
|
94
|
+
### Azure
|
95
|
+
|
96
|
+
To use the [Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/) API, you can configure the gem like this:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
OpenAI.configure do |config|
|
100
|
+
config.access_token = ENV.fetch("AZURE_OPENAI_API_KEY")
|
101
|
+
config.uri_base = ENV.fetch("AZURE_OPENAI_URI")
|
102
|
+
config.api_type = :azure
|
103
|
+
config.api_version = "2023-03-15-preview"
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
where `AZURE_OPENAI_URI` is e.g. `https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo`
|
108
|
+
|
90
109
|
### Models
|
91
110
|
|
92
111
|
There are different models that can be used to generate text. For a full list and to retrieve information about a single model:
|
@@ -128,7 +147,9 @@ puts response.dig("choices", 0, "message", "content")
|
|
128
147
|
|
129
148
|
### Streaming ChatGPT
|
130
149
|
|
131
|
-
|
150
|
+
[Quick guide to streaming ChatGPT with Rails 7 and Hotwire](https://gist.github.com/alexrudall/cb5ee1e109353ef358adb4e66631799d)
|
151
|
+
|
152
|
+
You can stream from the API in realtime, which can be much faster and used to create a more engaging user experience. Pass a [Proc](https://ruby-doc.org/core-2.6/Proc.html) (or any object with a `#call` method) to the `stream` parameter to receive the stream of text chunks as they are generated. Each time one or more chunks is received, the proc will be called once with each chunk, parsed as a Hash. If OpenAI returns an error, `ruby-openai` will pass that to your proc as a Hash.
|
132
153
|
|
133
154
|
```ruby
|
134
155
|
client.chat(
|
@@ -179,12 +200,15 @@ puts response.dig("choices", 0, "text")
|
|
179
200
|
You can use the embeddings endpoint to get a vector of numbers representing an input. You can then compare these vectors for different inputs to efficiently check how similar the inputs are.
|
180
201
|
|
181
202
|
```ruby
|
182
|
-
client.embeddings(
|
203
|
+
response = client.embeddings(
|
183
204
|
parameters: {
|
184
205
|
model: "babbage-similarity",
|
185
206
|
input: "The food was delicious and the waiter..."
|
186
207
|
}
|
187
208
|
)
|
209
|
+
|
210
|
+
puts response.dig("data", 0, "embedding")
|
211
|
+
# => Vector representation of your embedding
|
188
212
|
```
|
189
213
|
|
190
214
|
### Files
|
data/lib/openai/http.rb
CHANGED
@@ -8,9 +8,11 @@ module OpenAI
|
|
8
8
|
|
9
9
|
def json_post(path:, parameters:)
|
10
10
|
to_json(conn.post(uri(path: path)) do |req|
|
11
|
-
if parameters[:stream].
|
11
|
+
if parameters[:stream].respond_to?(:call)
|
12
12
|
req.options.on_data = to_json_stream(user_proc: parameters[:stream])
|
13
13
|
parameters[:stream] = true # Necessary to tell OpenAI to stream.
|
14
|
+
elsif parameters[:stream]
|
15
|
+
raise ArgumentError, "The stream parameter must be a Proc or have a #call method"
|
14
16
|
end
|
15
17
|
|
16
18
|
req.headers = headers
|
@@ -68,10 +70,17 @@ module OpenAI
|
|
68
70
|
end
|
69
71
|
|
70
72
|
def uri(path:)
|
71
|
-
|
73
|
+
if OpenAI.configuration.api_type == :azure
|
74
|
+
base = File.join(OpenAI.configuration.uri_base, path)
|
75
|
+
"#{base}?api-version=#{OpenAI.configuration.api_version}"
|
76
|
+
else
|
77
|
+
File.join(OpenAI.configuration.uri_base, OpenAI.configuration.api_version, path)
|
78
|
+
end
|
72
79
|
end
|
73
80
|
|
74
81
|
def headers
|
82
|
+
return azure_headers if OpenAI.configuration.api_type == :azure
|
83
|
+
|
75
84
|
{
|
76
85
|
"Content-Type" => "application/json",
|
77
86
|
"Authorization" => "Bearer #{OpenAI.configuration.access_token}",
|
@@ -79,6 +88,13 @@ module OpenAI
|
|
79
88
|
}
|
80
89
|
end
|
81
90
|
|
91
|
+
def azure_headers
|
92
|
+
{
|
93
|
+
"Content-Type" => "application/json",
|
94
|
+
"api-key" => OpenAI.configuration.access_token
|
95
|
+
}
|
96
|
+
end
|
97
|
+
|
82
98
|
def multipart_parameters(parameters)
|
83
99
|
parameters&.transform_values do |value|
|
84
100
|
next value unless value.is_a?(File)
|
data/lib/openai/version.rb
CHANGED
data/lib/openai.rb
CHANGED
@@ -15,7 +15,7 @@ module OpenAI
|
|
15
15
|
|
16
16
|
class Configuration
|
17
17
|
attr_writer :access_token
|
18
|
-
attr_accessor :api_version, :organization_id, :uri_base, :request_timeout
|
18
|
+
attr_accessor :api_type, :api_version, :organization_id, :uri_base, :request_timeout
|
19
19
|
|
20
20
|
DEFAULT_API_VERSION = "v1".freeze
|
21
21
|
DEFAULT_URI_BASE = "https://api.openai.com/".freeze
|
@@ -23,6 +23,7 @@ module OpenAI
|
|
23
23
|
|
24
24
|
def initialize
|
25
25
|
@access_token = nil
|
26
|
+
@api_type = nil
|
26
27
|
@api_version = DEFAULT_API_VERSION
|
27
28
|
@organization_id = nil
|
28
29
|
@uri_base = DEFAULT_URI_BASE
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-openai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|