ruby-openai 4.1.0 → 4.2.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: '06199aaabd11e965f6d07f3948323b5226fdda82f09326c065cda4b5b2cc9237'
4
- data.tar.gz: 59cce00dfeb08270e11b2c33b23af4558dfc53796c87ebb6cf64b1cb76739790
3
+ metadata.gz: 5654d4f5edeb9b912b06916b13c915d4a795e4ad64df4ae4be792ca562bdfd99
4
+ data.tar.gz: b3b6457c556d9a4355afe7da1c514fc6029df5dc20e56a893981ec5410d14eac
5
5
  SHA512:
6
- metadata.gz: 1d9b9409e915f49284f8acdf4be10cde81d7065fadb254dc135aeaad5572c2278c3bb6115752e8c43619d4ea60d696bc55a8128af8981ec544db2320c2d4b89a
7
- data.tar.gz: a202ca506f515e8933f00e0302dcd8a59b8d15f6b11c13263499ac140b9b045162089141f27cab5dda2dbde3d5b960c05c8081415a721b29cbdffcd3d375ddd0
6
+ metadata.gz: '08e9e61ecc221384d0460ed5692ff5ac482e558219768402d05ed5b321e4aaf50ed94b26c8b938ae546da29db609b79f45439c0942f252f9059c6053a416f2dd'
7
+ data.tar.gz: ddd59e60920f73d230a068e4d25a98704ea3e39203c45f0b295837d42e72a16a1d4064772cb6da9be35c65fb512a29cc617d325e3646e3a3c677263693ab9725
data/CHANGELOG.md CHANGED
@@ -5,6 +5,12 @@ 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
+
8
14
  ## [4.1.0] - 2023-05-15
9
15
 
10
16
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-openai (4.1.0)
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.4)
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
@@ -91,6 +91,21 @@ OpenAI.configure do |config|
91
91
  end
92
92
  ```
93
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
+
94
109
  ### Models
95
110
 
96
111
  There are different models that can be used to generate text. For a full list and to retrieve information about a single model:
@@ -185,12 +200,15 @@ puts response.dig("choices", 0, "text")
185
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.
186
201
 
187
202
  ```ruby
188
- client.embeddings(
203
+ response = client.embeddings(
189
204
  parameters: {
190
205
  model: "babbage-similarity",
191
206
  input: "The food was delicious and the waiter..."
192
207
  }
193
208
  )
209
+
210
+ puts response.dig("data", 0, "embedding")
211
+ # => Vector representation of your embedding
194
212
  ```
195
213
 
196
214
  ### Files
data/lib/openai/http.rb CHANGED
@@ -70,10 +70,17 @@ module OpenAI
70
70
  end
71
71
 
72
72
  def uri(path:)
73
- OpenAI.configuration.uri_base + OpenAI.configuration.api_version + path
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
74
79
  end
75
80
 
76
81
  def headers
82
+ return azure_headers if OpenAI.configuration.api_type == :azure
83
+
77
84
  {
78
85
  "Content-Type" => "application/json",
79
86
  "Authorization" => "Bearer #{OpenAI.configuration.access_token}",
@@ -81,6 +88,13 @@ module OpenAI
81
88
  }
82
89
  end
83
90
 
91
+ def azure_headers
92
+ {
93
+ "Content-Type" => "application/json",
94
+ "api-key" => OpenAI.configuration.access_token
95
+ }
96
+ end
97
+
84
98
  def multipart_parameters(parameters)
85
99
  parameters&.transform_values do |value|
86
100
  next value unless value.is_a?(File)
@@ -1,3 +1,3 @@
1
1
  module OpenAI
2
- VERSION = "4.1.0".freeze
2
+ VERSION = "4.2.0".freeze
3
3
  end
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.1.0
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-05-15 00:00:00.000000000 Z
11
+ date: 2023-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday