openai.rb 0.0.4 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +1 -1
- data/.rubocop.yml +3 -0
- data/.ruby-version +1 -1
- data/CHANGELOG.md +4 -0
- data/Gemfile +2 -2
- data/Gemfile.lock +27 -23
- data/README.md +7 -0
- data/lib/openai/api/cache.rb +11 -7
- data/lib/openai/api/client.rb +4 -0
- data/lib/openai/api/resource.rb +14 -17
- data/lib/openai/api/response.rb +2 -2
- data/lib/openai/chat.rb +8 -7
- data/lib/openai/version.rb +1 -1
- data/lib/openai.rb +10 -1
- data/openai.gemspec +3 -2
- data/spec/shared/api_resource_context.rb +2 -2
- data/spec/unit/openai/api/audio_spec.rb +2 -2
- data/spec/unit/openai/api/chat_completions_spec.rb +22 -22
- data/spec/unit/openai/api/completions_spec.rb +22 -22
- data/spec/unit/openai/api/edits_spec.rb +9 -9
- data/spec/unit/openai/api/embeddings_spec.rb +9 -9
- data/spec/unit/openai/api/files_spec.rb +29 -29
- data/spec/unit/openai/api/fine_tunes_spec.rb +114 -114
- data/spec/unit/openai/api/images_spec.rb +8 -8
- data/spec/unit/openai/api/models_spec.rb +4 -4
- data/spec/unit/openai/api/moderations_spec.rb +20 -20
- data/spec/unit/openai/api/response_spec.rb +7 -7
- data/spec/unit/openai/chat_spec.rb +1 -1
- data/spec/unit/openai_spec.rb +19 -19
- metadata +11 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7504d7c806b7add3642cac52f2dede494dfd4b7ed13b4990001e7dbcf6fc3229
|
4
|
+
data.tar.gz: a128b968928677a1153fb59dae4a25f4138ca5517579183b9c40032ff5eb607b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d1fa68746301c4ebe256e2c97f66f3616bb34e082df040feead4ce9281569dc804c2e0bc32a78e720a3bad5fe0646fbef263699130823424dc869f34615e6db
|
7
|
+
data.tar.gz: b1f24c37e0fe70344c4eac77e9943d84740e33544a8cd1eb934f167da737a25a14682aa7297327258d8f5947fc668423a45310bcb7120930fddae0e31ca94451
|
data/.github/workflows/main.yml
CHANGED
data/.rubocop.yml
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
3.2.2
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
openai.rb (0.0.
|
4
|
+
openai.rb (0.0.5)
|
5
5
|
abstract_type (~> 0.0.7)
|
6
6
|
anima (~> 0.3)
|
7
7
|
concord (~> 0.1)
|
8
8
|
http (>= 4.4, < 6.0)
|
9
9
|
ice_nine (~> 0.11.x)
|
10
10
|
memoizable (~> 0.4.2)
|
11
|
-
tiktoken_ruby (~> 0.0.
|
11
|
+
tiktoken_ruby (~> 0.0.6)
|
12
12
|
|
13
13
|
GEM
|
14
14
|
remote: https://rubygems.org/
|
@@ -17,7 +17,7 @@ GEM
|
|
17
17
|
adamantium (0.2.0)
|
18
18
|
ice_nine (~> 0.11.0)
|
19
19
|
memoizable (~> 0.4.0)
|
20
|
-
addressable (2.8.
|
20
|
+
addressable (2.8.5)
|
21
21
|
public_suffix (>= 2.0.2, < 6.0)
|
22
22
|
anima (0.3.2)
|
23
23
|
abstract_type (~> 0.0.7)
|
@@ -34,7 +34,7 @@ GEM
|
|
34
34
|
unf (>= 0.0.5, < 1.0.0)
|
35
35
|
dotenv (2.8.1)
|
36
36
|
equalizer (0.0.11)
|
37
|
-
ffi (1.
|
37
|
+
ffi (1.16.3)
|
38
38
|
ffi-compiler (1.0.1)
|
39
39
|
ffi (>= 1.0.0)
|
40
40
|
rake
|
@@ -53,34 +53,36 @@ GEM
|
|
53
53
|
memoizable (0.4.2)
|
54
54
|
thread_safe (~> 0.3, >= 0.3.1)
|
55
55
|
method_source (1.0.0)
|
56
|
-
parallel (1.
|
57
|
-
parser (3.2.2.
|
56
|
+
parallel (1.23.0)
|
57
|
+
parser (3.2.2.4)
|
58
58
|
ast (~> 2.4.1)
|
59
|
-
|
59
|
+
racc
|
60
|
+
pry (0.14.2)
|
60
61
|
coderay (~> 1.1)
|
61
62
|
method_source (~> 1.0)
|
62
|
-
pry-byebug (3.
|
63
|
+
pry-byebug (3.10.1)
|
63
64
|
byebug (~> 11.0)
|
64
|
-
pry (
|
65
|
-
public_suffix (5.0.
|
65
|
+
pry (>= 0.13, < 0.15)
|
66
|
+
public_suffix (5.0.3)
|
67
|
+
racc (1.7.1)
|
66
68
|
rainbow (3.1.1)
|
67
69
|
rake (13.0.6)
|
68
|
-
rb_sys (0.9.
|
69
|
-
regexp_parser (2.
|
70
|
-
rexml (3.2.
|
70
|
+
rb_sys (0.9.82)
|
71
|
+
regexp_parser (2.8.2)
|
72
|
+
rexml (3.2.6)
|
71
73
|
rspec (3.12.0)
|
72
74
|
rspec-core (~> 3.12.0)
|
73
75
|
rspec-expectations (~> 3.12.0)
|
74
76
|
rspec-mocks (~> 3.12.0)
|
75
|
-
rspec-core (3.12.
|
77
|
+
rspec-core (3.12.2)
|
76
78
|
rspec-support (~> 3.12.0)
|
77
|
-
rspec-expectations (3.12.
|
79
|
+
rspec-expectations (3.12.3)
|
78
80
|
diff-lcs (>= 1.2.0, < 2.0)
|
79
81
|
rspec-support (~> 3.12.0)
|
80
|
-
rspec-mocks (3.12.
|
82
|
+
rspec-mocks (3.12.6)
|
81
83
|
diff-lcs (>= 1.2.0, < 2.0)
|
82
84
|
rspec-support (~> 3.12.0)
|
83
|
-
rspec-support (3.12.
|
85
|
+
rspec-support (3.12.1)
|
84
86
|
rubocop (1.31.2)
|
85
87
|
json (~> 2.3)
|
86
88
|
parallel (~> 1.10)
|
@@ -91,18 +93,20 @@ GEM
|
|
91
93
|
rubocop-ast (>= 1.18.0, < 2.0)
|
92
94
|
ruby-progressbar (~> 1.7)
|
93
95
|
unicode-display_width (>= 1.4.0, < 3.0)
|
94
|
-
rubocop-ast (1.
|
96
|
+
rubocop-ast (1.29.0)
|
95
97
|
parser (>= 3.2.1.0)
|
96
98
|
rubocop-rspec (2.11.1)
|
97
99
|
rubocop (~> 1.19)
|
98
100
|
ruby-progressbar (1.13.0)
|
99
101
|
slop (4.10.1)
|
100
102
|
thread_safe (0.3.6)
|
101
|
-
tiktoken_ruby (0.0.
|
103
|
+
tiktoken_ruby (0.0.6)
|
104
|
+
rb_sys (~> 0.9.68)
|
105
|
+
tiktoken_ruby (0.0.6-arm64-darwin)
|
102
106
|
unf (0.1.4)
|
103
107
|
unf_ext
|
104
108
|
unf_ext (0.0.8.2)
|
105
|
-
unicode-display_width (2.
|
109
|
+
unicode-display_width (2.5.0)
|
106
110
|
|
107
111
|
PLATFORMS
|
108
112
|
arm64-darwin-21
|
@@ -112,8 +116,8 @@ DEPENDENCIES
|
|
112
116
|
dotenv (~> 2.8)
|
113
117
|
http (~> 4.4)
|
114
118
|
openai.rb!
|
115
|
-
pry
|
116
|
-
pry-byebug
|
119
|
+
pry
|
120
|
+
pry-byebug
|
117
121
|
rb_sys (~> 0.9.70)
|
118
122
|
rspec (~> 3.12)
|
119
123
|
rubocop (~> 1.31.1)
|
@@ -121,7 +125,7 @@ DEPENDENCIES
|
|
121
125
|
slop (~> 4.10)
|
122
126
|
|
123
127
|
RUBY VERSION
|
124
|
-
ruby 2.
|
128
|
+
ruby 3.2.2p53
|
125
129
|
|
126
130
|
BUNDLED WITH
|
127
131
|
2.3.9
|
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/lib/openai/api/cache.rb
CHANGED
@@ -6,7 +6,7 @@ class OpenAI
|
|
6
6
|
include Concord.new(:client, :cache)
|
7
7
|
|
8
8
|
def get(route)
|
9
|
-
read_cache_or_apply(verb: :get, route:
|
9
|
+
read_cache_or_apply(verb: :get, route:) do
|
10
10
|
client.get(route)
|
11
11
|
end
|
12
12
|
end
|
@@ -18,17 +18,21 @@ class OpenAI
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def post(route, **body)
|
21
|
-
read_cache_or_apply(verb: :post, route
|
21
|
+
read_cache_or_apply(verb: :post, route:, body:, format: :json) do
|
22
22
|
client.post(route, **body)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
def post_form_multipart(route, **body)
|
27
|
-
read_cache_or_apply(verb: :post, route
|
27
|
+
read_cache_or_apply(verb: :post, route:, body:, format: :form) do
|
28
28
|
client.post_form_multipart(route, **body)
|
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(...)
|
@@ -45,11 +49,11 @@ class OpenAI
|
|
45
49
|
|
46
50
|
def cache_target(verb:, route:, body: nil, format: nil)
|
47
51
|
Target.new(
|
48
|
-
verb
|
52
|
+
verb:,
|
49
53
|
api_key: client.api_key,
|
50
|
-
route
|
51
|
-
body
|
52
|
-
format:
|
54
|
+
route:,
|
55
|
+
body:,
|
56
|
+
format:
|
53
57
|
)
|
54
58
|
end
|
55
59
|
|
data/lib/openai/api/client.rb
CHANGED
data/lib/openai/api/resource.rb
CHANGED
@@ -31,7 +31,7 @@ class OpenAI
|
|
31
31
|
chunk_response_type: full_response_type,
|
32
32
|
**kwargs
|
33
33
|
)
|
34
|
-
payload = kwargs.merge(stream:
|
34
|
+
payload = kwargs.merge(stream:)
|
35
35
|
|
36
36
|
raise 'Streaming responses require a block' if stream && !block_given?
|
37
37
|
raise 'Non-streaming responses do not support blocks' if !stream && block_given?
|
@@ -54,7 +54,7 @@ class OpenAI
|
|
54
54
|
def create(model:, **kwargs, &block)
|
55
55
|
create_and_maybe_stream(
|
56
56
|
'/v1/completions',
|
57
|
-
model
|
57
|
+
model:,
|
58
58
|
full_response_type: Response::Completion,
|
59
59
|
**kwargs,
|
60
60
|
&block
|
@@ -66,8 +66,8 @@ class OpenAI
|
|
66
66
|
def create(model:, messages:, **kwargs, &block)
|
67
67
|
create_and_maybe_stream(
|
68
68
|
'/v1/chat/completions',
|
69
|
-
model
|
70
|
-
messages
|
69
|
+
model:,
|
70
|
+
messages:,
|
71
71
|
full_response_type: Response::ChatCompletion,
|
72
72
|
chunk_response_type: Response::ChatCompletionChunk,
|
73
73
|
**kwargs,
|
@@ -79,7 +79,7 @@ class OpenAI
|
|
79
79
|
class Embedding < Resource
|
80
80
|
def create(model:, input:, **kwargs)
|
81
81
|
Response::Embedding.from_json(
|
82
|
-
post('/v1/embeddings', model
|
82
|
+
post('/v1/embeddings', model:, input:, **kwargs)
|
83
83
|
)
|
84
84
|
end
|
85
85
|
end
|
@@ -99,7 +99,7 @@ class OpenAI
|
|
99
99
|
class Moderation < Resource
|
100
100
|
def create(input:, model:)
|
101
101
|
Response::Moderation.from_json(
|
102
|
-
post('/v1/moderations', input
|
102
|
+
post('/v1/moderations', input:, model:)
|
103
103
|
)
|
104
104
|
end
|
105
105
|
end
|
@@ -107,7 +107,7 @@ class OpenAI
|
|
107
107
|
class Edit < Resource
|
108
108
|
def create(model:, instruction:, **kwargs)
|
109
109
|
Response::Edit.from_json(
|
110
|
-
post('/v1/edits', model
|
110
|
+
post('/v1/edits', model:, instruction:, **kwargs)
|
111
111
|
)
|
112
112
|
end
|
113
113
|
end
|
@@ -115,7 +115,7 @@ class OpenAI
|
|
115
115
|
class File < Resource
|
116
116
|
def create(file:, purpose:)
|
117
117
|
Response::File.from_json(
|
118
|
-
post_form_multipart('/v1/files', file: form_file(file), purpose:
|
118
|
+
post_form_multipart('/v1/files', file: form_file(file), purpose:)
|
119
119
|
)
|
120
120
|
end
|
121
121
|
|
@@ -151,7 +151,7 @@ class OpenAI
|
|
151
151
|
|
152
152
|
def create(training_file:, **kwargs)
|
153
153
|
Response::FineTune.from_json(
|
154
|
-
post('/v1/fine-tunes', training_file
|
154
|
+
post('/v1/fine-tunes', training_file:, **kwargs)
|
155
155
|
)
|
156
156
|
end
|
157
157
|
|
@@ -177,23 +177,20 @@ class OpenAI
|
|
177
177
|
class Image < Resource
|
178
178
|
def create(prompt:, **kwargs)
|
179
179
|
Response::ImageGeneration.from_json(
|
180
|
-
post('/v1/images/generations', prompt
|
180
|
+
post('/v1/images/generations', prompt:, **kwargs)
|
181
181
|
)
|
182
182
|
end
|
183
183
|
|
184
184
|
def create_variation(image:, **kwargs)
|
185
185
|
Response::ImageVariation.from_json(
|
186
|
-
post_form_multipart('/v1/images/variations',
|
187
|
-
image: form_file(image),
|
188
|
-
**kwargs
|
189
|
-
})
|
186
|
+
post_form_multipart('/v1/images/variations', image: form_file(image), **kwargs)
|
190
187
|
)
|
191
188
|
end
|
192
189
|
|
193
190
|
def edit(image:, prompt:, mask: nil, **kwargs)
|
194
191
|
params = {
|
195
192
|
image: form_file(image),
|
196
|
-
prompt
|
193
|
+
prompt:,
|
197
194
|
**kwargs
|
198
195
|
}
|
199
196
|
|
@@ -211,7 +208,7 @@ class OpenAI
|
|
211
208
|
post_form_multipart(
|
212
209
|
'/v1/audio/transcriptions',
|
213
210
|
file: form_file(file),
|
214
|
-
model
|
211
|
+
model:,
|
215
212
|
**kwargs
|
216
213
|
)
|
217
214
|
)
|
@@ -222,7 +219,7 @@ class OpenAI
|
|
222
219
|
post_form_multipart(
|
223
220
|
'/v1/audio/translations',
|
224
221
|
file: form_file(file),
|
225
|
-
model
|
222
|
+
model:,
|
226
223
|
**kwargs
|
227
224
|
)
|
228
225
|
)
|
data/lib/openai/api/response.rb
CHANGED
@@ -41,7 +41,7 @@ class OpenAI
|
|
41
41
|
register_field(name)
|
42
42
|
|
43
43
|
define_method(name) do
|
44
|
-
field(path, wrapper:
|
44
|
+
field(path, wrapper:)
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
@@ -49,7 +49,7 @@ class OpenAI
|
|
49
49
|
register_field(name)
|
50
50
|
|
51
51
|
define_method(name) do
|
52
|
-
optional_field(path, wrapper:
|
52
|
+
optional_field(path, wrapper:)
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
data/lib/openai/chat.rb
CHANGED
@@ -5,8 +5,9 @@ class OpenAI
|
|
5
5
|
include Anima.new(:messages, :api_settings, :openai, :config)
|
6
6
|
using Util::Colorize
|
7
7
|
|
8
|
-
def initialize(
|
9
|
-
|
8
|
+
def initialize(opts)
|
9
|
+
opts = { settings: {}.freeze, config: Config.create }.merge(opts)
|
10
|
+
messages = opts.fetch(:messages).map do |msg|
|
10
11
|
if msg.is_a?(Hash)
|
11
12
|
Message.new(msg)
|
12
13
|
else
|
@@ -15,10 +16,10 @@ class OpenAI
|
|
15
16
|
end
|
16
17
|
|
17
18
|
super(
|
18
|
-
messages
|
19
|
-
api_settings: settings,
|
20
|
-
config: config,
|
21
|
-
|
19
|
+
messages:,
|
20
|
+
api_settings: opts.fetch(:settings),
|
21
|
+
config: opts.fetch(:config),
|
22
|
+
openai: opts.fetch(:openai)
|
22
23
|
)
|
23
24
|
end
|
24
25
|
|
@@ -90,7 +91,7 @@ class OpenAI
|
|
90
91
|
end
|
91
92
|
|
92
93
|
def add_message(role, content)
|
93
|
-
with_message(role
|
94
|
+
with_message(role:, content:)
|
94
95
|
end
|
95
96
|
|
96
97
|
def with_message(message)
|
data/lib/openai/version.rb
CHANGED
data/lib/openai.rb
CHANGED
@@ -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
|
@@ -55,7 +64,7 @@ class OpenAI
|
|
55
64
|
def chat(model:, history: [], **kwargs)
|
56
65
|
Chat.new(
|
57
66
|
openai: self,
|
58
|
-
settings: kwargs.merge(model:
|
67
|
+
settings: kwargs.merge(model:),
|
59
68
|
messages: history
|
60
69
|
)
|
61
70
|
end
|
data/openai.gemspec
CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.require_paths = %w[lib]
|
17
17
|
spec.executables = []
|
18
18
|
|
19
|
-
spec.required_ruby_version = '>= 2
|
19
|
+
spec.required_ruby_version = '>= 3.2'
|
20
20
|
|
21
21
|
spec.add_dependency 'abstract_type', '~> 0.0.7'
|
22
22
|
spec.add_dependency 'anima', '~> 0.3'
|
@@ -24,5 +24,6 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_dependency 'http', '>= 4.4', '< 6.0'
|
25
25
|
spec.add_dependency 'ice_nine', '~> 0.11.x'
|
26
26
|
spec.add_dependency 'memoizable', '~> 0.4.2'
|
27
|
-
spec.add_dependency 'tiktoken_ruby', '~> 0.0.
|
27
|
+
spec.add_dependency 'tiktoken_ruby', '~> 0.0.6'
|
28
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
28
29
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
RSpec.shared_context 'an API Resource' do
|
4
|
-
let(:api) { OpenAI::API.new(api_client)
|
5
|
-
let(:api_client) { OpenAI::API::Client.new('sk-123', http:
|
4
|
+
let(:api) { OpenAI::API.new(api_client) }
|
5
|
+
let(:api_client) { OpenAI::API::Client.new('sk-123', http:) }
|
6
6
|
let(:http) { class_spy(HTTP) }
|
7
7
|
let(:response_status_code) { 200 }
|
8
8
|
|
@@ -9,7 +9,7 @@ RSpec.describe OpenAI::API, '#audio' do
|
|
9
9
|
context 'when transcribing audio' do
|
10
10
|
let(:response_body) do
|
11
11
|
{
|
12
|
-
|
12
|
+
text: "Imagine the wildest idea that you've ever had, and you're curious about how it might scale to something that's a 100, a 1,000 times bigger. This is a place where you can get to do that."
|
13
13
|
}
|
14
14
|
end
|
15
15
|
|
@@ -42,7 +42,7 @@ RSpec.describe OpenAI::API, '#audio' do
|
|
42
42
|
|
43
43
|
let(:response_body) do
|
44
44
|
{
|
45
|
-
|
45
|
+
text: 'Hello, my name is Wolfgang and I come from Germany. Where are you heading today?'
|
46
46
|
}
|
47
47
|
end
|
48
48
|
|
@@ -6,34 +6,34 @@ RSpec.describe OpenAI::API, '#chat_completions' do
|
|
6
6
|
let(:resource) { api.chat_completions }
|
7
7
|
let(:response_body) do
|
8
8
|
{
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
id: 'chatcmpl-123',
|
10
|
+
object: 'chat.completion',
|
11
|
+
created: 1_677_652_288,
|
12
|
+
choices: [
|
13
13
|
{
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
index: 0,
|
15
|
+
message: {
|
16
|
+
role: 'assistant',
|
17
|
+
content: "\n\nHello there, how may I assist you today?"
|
18
18
|
},
|
19
|
-
|
19
|
+
finish_reason: 'stop'
|
20
20
|
}
|
21
21
|
],
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
usage: {
|
23
|
+
prompt_tokens: 9,
|
24
|
+
completion_tokens: 12,
|
25
|
+
total_tokens: 21
|
26
26
|
}
|
27
27
|
}
|
28
28
|
end
|
29
29
|
|
30
30
|
let(:completion) do
|
31
31
|
messages = [
|
32
|
-
{
|
33
|
-
{
|
34
|
-
{
|
32
|
+
{ text: 'Hello there!', user: 'customer' },
|
33
|
+
{ text: 'Can you help me with my order?', user: 'customer' },
|
34
|
+
{ text: 'Sure, what would you like to do?', user: 'assistant' }
|
35
35
|
]
|
36
|
-
resource.create(model: 'text-davinci-002', messages:
|
36
|
+
resource.create(model: 'text-davinci-002', messages:)
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'can create a chat completion' do
|
@@ -64,10 +64,10 @@ RSpec.describe OpenAI::API, '#chat_completions' do
|
|
64
64
|
context 'when streaming is enabled' do
|
65
65
|
let(:response_chunks) do
|
66
66
|
[
|
67
|
-
chunk(role: 'assistant'),
|
68
|
-
chunk(content: 'He'),
|
69
|
-
chunk(content: 'llo,'),
|
70
|
-
chunk(content: ' world'),
|
67
|
+
chunk({ role: 'assistant' }),
|
68
|
+
chunk({ content: 'He' }),
|
69
|
+
chunk({ content: 'llo,' }),
|
70
|
+
chunk({ content: ' world' }),
|
71
71
|
chunk({ content: '!' }, finish_reason: 'stop')
|
72
72
|
]
|
73
73
|
end
|
@@ -102,7 +102,7 @@ RSpec.describe OpenAI::API, '#chat_completions' do
|
|
102
102
|
object: 'chat.completion.chunk',
|
103
103
|
created: 1_679_780_213,
|
104
104
|
model: 'gpt-3.5-turbo-0301',
|
105
|
-
choices: [delta
|
105
|
+
choices: [delta:, index: 0, finish_reason:]
|
106
106
|
}
|
107
107
|
|
108
108
|
"data: #{JSON.dump(data)}"
|
@@ -7,22 +7,22 @@ RSpec.describe OpenAI::API, '#completions' do
|
|
7
7
|
|
8
8
|
let(:response_body) do
|
9
9
|
{
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
id: 'cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7',
|
11
|
+
object: 'text_completion',
|
12
|
+
created: 1_589_478_378,
|
13
|
+
model: 'text-davinci-003',
|
14
|
+
choices: [
|
15
15
|
{
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
text: "\n\nThis is indeed a test",
|
17
|
+
index: 0,
|
18
|
+
logprobs: nil,
|
19
|
+
finish_reason: 'length'
|
20
20
|
}
|
21
21
|
],
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
usage: {
|
23
|
+
prompt_tokens: 5,
|
24
|
+
completion_tokens: 7,
|
25
|
+
total_tokens: 12
|
26
26
|
}
|
27
27
|
}
|
28
28
|
end
|
@@ -91,16 +91,16 @@ RSpec.describe OpenAI::API, '#completions' do
|
|
91
91
|
|
92
92
|
def chunk(text, finish_reason: nil)
|
93
93
|
data = {
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
94
|
+
id: 'cmpl-6y5B6Ak8wBk2nKsqVtSlFeJAG1dUM',
|
95
|
+
object: 'text_completion',
|
96
|
+
created: 1_679_777_604,
|
97
|
+
choices: [{
|
98
|
+
text:,
|
99
|
+
index: 0,
|
100
|
+
logprobs: nil,
|
101
|
+
finish_reason:
|
102
102
|
}],
|
103
|
-
|
103
|
+
model: 'text-davinci-002'
|
104
104
|
}
|
105
105
|
|
106
106
|
"data: #{JSON.dump(data)}"
|