openai.rb 0.0.3 → 0.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f049bfa7ff5419acf81a7cabc6e34b6bd223b8f9fcb634008544d93d87170c2f
4
- data.tar.gz: d614701b82eaf3ec84e9ddb432505bbbd786f71d75378534b23400446ddb8e7b
3
+ metadata.gz: a36b209f6540c340ca79cfa0e245d373db12c20b1d33f4fdd418c56bfa33961d
4
+ data.tar.gz: a30abc667ce81ffa59ec3a65c260193554cff814d03805921f745ec9140f2942
5
5
  SHA512:
6
- metadata.gz: c321d0184079826648569ab13b04675d9cbfb04027fed85b191d462c6585cc375d9144dede8ecf6d9995e48be935bf6879dba969e17492cb9c4ffe100f468fea
7
- data.tar.gz: 3fd0d70f0007e3900e6df1f39ef18dcbd618b7e3384de229b50936924bb4bf80e79d19bd7a531be6caade2b915c03581cccabc32f01b5921c79bd88e224c27a0
6
+ metadata.gz: 0aacdfd81c1a7bbd88c2fceed1e9c3aaf38f7cc948467b45a2c3e607d5eaf6bafa084ba2a8accd08aa7430c67b08df4da810ef2e07349359b601258c9f14c550
7
+ data.tar.gz: c6c89ccafded9440f5a472afbcee736307e7f2daa4f82b2028515cec86cd22a224e3006bd1d858c7faa7b495e7d889691c4af76ea4720972b50022890b01d18a
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # v0.0.5
2
+
3
+ * Added `OpenAI#without_cache`
4
+
5
+ # v0.0.4
6
+
7
+ * Added support for specifying `OpenAI.new(api_key, organization: 'org-123')`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- openai.rb (0.0.3)
4
+ openai.rb (0.0.5)
5
5
  abstract_type (~> 0.0.7)
6
6
  anima (~> 0.3)
7
7
  concord (~> 0.1)
data/README.md CHANGED
@@ -51,6 +51,13 @@ openai.api.completions.create(model: 'text-davinci-002', prompt: 'Say hi')
51
51
 
52
52
  NOTE: Delete requests are not cached
53
53
 
54
+ To temporarily use the client without caching:
55
+
56
+ ```ruby
57
+ openai = OpenAI.create(ENV.fetch('OPENAI_API_KEY'), cache: cache_dir)
58
+ openai.without_cache.api.completions.create(...)
59
+ ```
60
+
54
61
  ### Tokens
55
62
 
56
63
  ```ruby
data/bin/console CHANGED
@@ -17,5 +17,3 @@ openai = OpenAI.create(
17
17
  )
18
18
 
19
19
  Pry.start_without_pry_byebug(binding, quiet: true)
20
-
21
- start_repl
@@ -29,6 +29,10 @@ class OpenAI
29
29
  end
30
30
  end
31
31
 
32
+ def without_cache
33
+ client
34
+ end
35
+
32
36
  private
33
37
 
34
38
  def read_cache_or_apply(...)
@@ -3,18 +3,18 @@
3
3
  class OpenAI
4
4
  class API
5
5
  class Client
6
- include Concord.new(:api_key, :http)
6
+ include Concord.new(:api_key, :organization_id, :http)
7
7
 
8
8
  public :api_key
9
9
 
10
10
  HOST = Addressable::URI.parse('https://api.openai.com/v1')
11
11
 
12
- def initialize(api_key, http: HTTP)
13
- super(api_key, http)
12
+ def initialize(api_key, organization_id: nil, http: HTTP)
13
+ super(api_key, organization_id, http)
14
14
  end
15
15
 
16
16
  def inspect
17
- "#<#{self.class}>"
17
+ "#<#{self.class} organization_id=#{organization_id.inspect}>"
18
18
  end
19
19
 
20
20
  def get(route)
@@ -62,6 +62,10 @@ class OpenAI
62
62
  unwrap_response(http_client.post(url_for(route), form: body))
63
63
  end
64
64
 
65
+ def without_cache
66
+ self
67
+ end
68
+
65
69
  private
66
70
 
67
71
  def url_for(route)
@@ -79,7 +83,9 @@ class OpenAI
79
83
  end
80
84
 
81
85
  def http_client
82
- http.headers('Authorization' => "Bearer #{api_key}")
86
+ headers = { 'Authorization' => "Bearer #{api_key}" }
87
+ headers['OpenAI-Organization'] = organization_id if organization_id
88
+ http.headers(headers)
83
89
  end
84
90
  end
85
91
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class OpenAI
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.5'
5
5
  end
data/lib/openai.rb CHANGED
@@ -28,8 +28,8 @@ class OpenAI
28
28
 
29
29
  ROOT = Pathname.new(__dir__).parent.expand_path.freeze
30
30
 
31
- def self.create(api_key, cache: nil, logger: Logger.new('/dev/null'))
32
- client = API::Client.new(api_key)
31
+ def self.create(api_key, cache: nil, organization: nil, logger: Logger.new('/dev/null'))
32
+ client = API::Client.new(api_key, organization_id: organization)
33
33
 
34
34
  if cache.is_a?(Pathname) && cache.directory?
35
35
  client = API::Cache.new(
@@ -41,8 +41,17 @@ class OpenAI
41
41
  new(client, logger)
42
42
  end
43
43
 
44
+ # @api private
45
+ def self.build(api_client, logger)
46
+ new(api_client, logger)
47
+ end
48
+
44
49
  private_class_method :new
45
50
 
51
+ def without_cache
52
+ self.class.build(api_client.without_cache, logger)
53
+ end
54
+
46
55
  def api
47
56
  API.new(api_client)
48
57
  end
@@ -37,6 +37,25 @@ RSpec.describe OpenAI do
37
37
  )
38
38
  end
39
39
 
40
+ context 'when the organization ID is given to the client' do
41
+ let(:api_client) do
42
+ OpenAI::API::Client.new(
43
+ 'sk-123',
44
+ organization_id: 'org-123',
45
+ http: http
46
+ )
47
+ end
48
+
49
+ it 'authenticates the request and includes the organization id' do
50
+ resource.create(model: 'text-davinci-002', prompt: 'Hello, world!')
51
+
52
+ expect(http).to have_received(:headers).with(
53
+ 'OpenAI-Organization' => 'org-123',
54
+ 'Authorization' => 'Bearer sk-123'
55
+ )
56
+ end
57
+ end
58
+
40
59
  context 'when the request is not 2xx' do
41
60
  let(:response_body) do
42
61
  {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openai.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - John
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-04-02 00:00:00.000000000 Z
12
+ date: 2023-04-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: abstract_type
@@ -127,6 +127,7 @@ files:
127
127
  - ".rspec"
128
128
  - ".rubocop.yml"
129
129
  - ".ruby-version"
130
+ - CHANGELOG.md
130
131
  - Gemfile
131
132
  - Gemfile.lock
132
133
  - README.md