openai_ruby 0.3.9 → 0.4.1
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/.rubocop.yml +4 -1
- data/Gemfile +1 -0
- data/Gemfile.lock +4 -1
- data/README.md +3 -10
- data/lib/openai_ruby/client.rb +23 -7
- data/lib/openai_ruby/version.rb +1 -1
- data/lib/openai_ruby.rb +1 -0
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5873de02b645485ae0e1850fe232ac3a4825f083f227fc0eccd886a2ae9c6b1
|
4
|
+
data.tar.gz: 4e1fdaca2c2dd90aa5e1f20f7628d1896e3dfc0f0cf9a57682f44a622232268c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a608e43e0b62805b44a817db7a0000f219c2d6fe82bf742a0f2f5f176d4d2e536fb2a723071fef5e69520b193e4af7fa6b1a2834ac502438d2c39903a34681f
|
7
|
+
data.tar.gz: 691b0d2f5c0ca192a5e9079157407ee79d044d7fce237cc1eedacaf0eb02dd95bce07f89273f6e4580f4f0d259eb488eb9c0033b6bfc6599549da33518fef8cf
|
data/.rubocop.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
openai_ruby (0.
|
4
|
+
openai_ruby (0.4.0)
|
5
|
+
event_stream_parser (~> 1.0)
|
5
6
|
faraday (~> 2.7)
|
6
7
|
|
7
8
|
GEM
|
@@ -9,6 +10,7 @@ GEM
|
|
9
10
|
specs:
|
10
11
|
ast (2.4.2)
|
11
12
|
diff-lcs (1.5.0)
|
13
|
+
event_stream_parser (1.0.0)
|
12
14
|
faraday (2.7.4)
|
13
15
|
faraday-net_http (>= 2.0, < 3.1)
|
14
16
|
ruby2_keywords (>= 0.0.4)
|
@@ -56,6 +58,7 @@ PLATFORMS
|
|
56
58
|
x86_64-linux
|
57
59
|
|
58
60
|
DEPENDENCIES
|
61
|
+
event_stream_parser
|
59
62
|
openai_ruby!
|
60
63
|
rake (~> 13.0)
|
61
64
|
rspec (~> 3.12)
|
data/README.md
CHANGED
@@ -32,7 +32,7 @@ client = OpenAI::Client.new("your OpenAI key here")
|
|
32
32
|
Or you can pass a hash of options to customize the connection:
|
33
33
|
|
34
34
|
```ruby
|
35
|
-
client = OpenAI::Client.new("your OpenAI key here", { request: { timeout: 20 } })
|
35
|
+
client = OpenAI::Client.new("your OpenAI key here", { base_uri: "https://example.com/", request: { timeout: 20 } })
|
36
36
|
```
|
37
37
|
|
38
38
|
For more options check [here](https://lostisland.github.io/faraday/#/customization/connection-options)
|
@@ -71,15 +71,8 @@ If you set `stream` param to `true`, a block will be called with the chunk data:
|
|
71
71
|
# temperature: 1,
|
72
72
|
# stream: true,
|
73
73
|
# }
|
74
|
-
res = client.create_chat_completion(params) do |
|
75
|
-
data
|
76
|
-
p data # {"id":"chatcmpl-6xcOWMQcilJUwJiosi7Rht6Fvuu3D","object":"chat.completion.chunk","created":1679666960,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"Hello"},"index":0,"finish_reason":null}]}
|
77
|
-
if data == "[DONE]"
|
78
|
-
# the stream is end
|
79
|
-
else
|
80
|
-
response = JSON.parse(data)
|
81
|
-
p response.dig("choices", 0, "text") # "Hello"
|
82
|
-
end
|
74
|
+
res = client.create_chat_completion(params) do |data|
|
75
|
+
p data.dig("choices", 0, "delta", "content") # "Hello"
|
83
76
|
end
|
84
77
|
```
|
85
78
|
|
data/lib/openai_ruby/client.rb
CHANGED
@@ -2,30 +2,40 @@
|
|
2
2
|
|
3
3
|
module OpenAI
|
4
4
|
class Client
|
5
|
-
|
5
|
+
DEFAULT_BASE_URI = "https://api.openai.com"
|
6
6
|
|
7
7
|
attr_reader :api_key, :options
|
8
8
|
|
9
9
|
def initialize(api_key, options = {})
|
10
10
|
@api_key = api_key
|
11
11
|
@options = options
|
12
|
+
@base_uri = options[:base_uri] || DEFAULT_BASE_URI
|
12
13
|
end
|
13
14
|
|
14
15
|
def create_completion(params = {})
|
15
16
|
Faraday.post(
|
16
|
-
"#{
|
17
|
+
"#{base_uri}/v1/completions",
|
17
18
|
params.to_json,
|
18
19
|
headers
|
19
20
|
)
|
20
21
|
end
|
21
22
|
|
22
23
|
def create_chat_completion(params = {})
|
24
|
+
parser = EventStreamParser::Parser.new
|
25
|
+
|
23
26
|
params.deep_stringify_keys!
|
24
27
|
if params["stream"]
|
25
28
|
connection.post("/v1/chat/completions") do |req|
|
26
29
|
req.body = params.to_json
|
27
|
-
req.options.on_data = proc do |chunk,
|
28
|
-
|
30
|
+
req.options.on_data = proc do |chunk, _overall_received_bytes, env|
|
31
|
+
if env && env.status != 200
|
32
|
+
raise_error = Faraday::Response::RaiseError.new
|
33
|
+
raise_error.on_complete(env.merge(body: try_parse_json(chunk)))
|
34
|
+
end
|
35
|
+
|
36
|
+
parser.feed(chunk) do |_type, data|
|
37
|
+
yield(JSON.parse(data)) if block_given? && data != "[DONE]"
|
38
|
+
end
|
29
39
|
end
|
30
40
|
end
|
31
41
|
else
|
@@ -35,7 +45,7 @@ module OpenAI
|
|
35
45
|
|
36
46
|
def create_edit(params = {})
|
37
47
|
Faraday.post(
|
38
|
-
"#{
|
48
|
+
"#{base_uri}/v1/edits",
|
39
49
|
params.to_json,
|
40
50
|
headers
|
41
51
|
)
|
@@ -48,14 +58,20 @@ module OpenAI
|
|
48
58
|
private
|
49
59
|
|
50
60
|
def connection
|
51
|
-
Faraday.new({ url:
|
61
|
+
Faraday.new({ url: base_uri, headers: headers }.merge(options))
|
52
62
|
end
|
53
63
|
|
54
64
|
def headers
|
55
65
|
{
|
56
66
|
"Content-Type" => "application/json",
|
57
|
-
"Authorization" => "Bearer #{api_key}"
|
67
|
+
"Authorization" => "Bearer #{api_key}",
|
58
68
|
}
|
59
69
|
end
|
70
|
+
|
71
|
+
def try_parse_json(maybe_json, default_value = nil)
|
72
|
+
JSON.parse(maybe_json)
|
73
|
+
rescue JSON::ParserError
|
74
|
+
default_value || maybe_json
|
75
|
+
end
|
60
76
|
end
|
61
77
|
end
|
data/lib/openai_ruby/version.rb
CHANGED
data/lib/openai_ruby.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openai_ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Renny Ren
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: event_stream_parser
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: faraday
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -67,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: '0'
|
69
83
|
requirements: []
|
70
|
-
rubygems_version: 3.4.
|
84
|
+
rubygems_version: 3.4.19
|
71
85
|
signing_key:
|
72
86
|
specification_version: 4
|
73
87
|
summary: A Ruby wrapper for OpenAI API
|