openai_ruby 0.3.8 → 0.4.0
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/Gemfile +1 -0
- data/Gemfile.lock +4 -1
- data/README.md +2 -9
- data/lib/openai_ruby/client.rb +18 -3
- data/lib/openai_ruby/version.rb +1 -1
- data/lib/openai_ruby.rb +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44aa28c19d62f59334f0a7de177963fb8dd7cc25a92b402d8b37a2e89e9cfe00
|
4
|
+
data.tar.gz: 1793600ba3bd77d66efbae21f9af9a9b08a7072c652eaf9dbbd1f1903059f594
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4740f041c58594e3a09a8aaefaa598906123314068253694c96e2d4f092a4fc5d05f9627c5b6093c1aaf656dc98780526b5f2d84c83d356af5d906e5c5bf348
|
7
|
+
data.tar.gz: 72ec3ffe01751c395d689e56cac0947231e2f5022371e7d79dbf113a1abd5df7b9f17072cfbb88171fd79fe499860e3e0e067ba09c7a40e09e6575b80565c886
|
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
@@ -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
@@ -6,7 +6,7 @@ module OpenAI
|
|
6
6
|
|
7
7
|
attr_reader :api_key, :options
|
8
8
|
|
9
|
-
def initialize(api_key, options)
|
9
|
+
def initialize(api_key, options = {})
|
10
10
|
@api_key = api_key
|
11
11
|
@options = options
|
12
12
|
end
|
@@ -20,12 +20,21 @@ module OpenAI
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def create_chat_completion(params = {})
|
23
|
+
parser = EventStreamParser::Parser.new
|
24
|
+
|
23
25
|
params.deep_stringify_keys!
|
24
26
|
if params["stream"]
|
25
27
|
connection.post("/v1/chat/completions") do |req|
|
26
28
|
req.body = params.to_json
|
27
29
|
req.options.on_data = proc do |chunk, overall_received_bytes, env|
|
28
|
-
|
30
|
+
if env && env.status != 200
|
31
|
+
raise_error = Faraday::Response::RaiseError.new
|
32
|
+
raise_error.on_complete(env.merge(body: try_parse_json(chunk)))
|
33
|
+
end
|
34
|
+
|
35
|
+
parser.feed(chunk) do |_type, data|
|
36
|
+
yield(JSON.parse(data)) if block_given? && data != "[DONE]"
|
37
|
+
end
|
29
38
|
end
|
30
39
|
end
|
31
40
|
else
|
@@ -54,8 +63,14 @@ module OpenAI
|
|
54
63
|
def headers
|
55
64
|
{
|
56
65
|
"Content-Type" => "application/json",
|
57
|
-
"Authorization" => "Bearer #{api_key}"
|
66
|
+
"Authorization" => "Bearer #{api_key}"
|
58
67
|
}
|
59
68
|
end
|
69
|
+
|
70
|
+
def try_parse_json(maybe_json, default_value = nil)
|
71
|
+
JSON.parse(maybe_json)
|
72
|
+
rescue JSON::ParserError
|
73
|
+
default_value || maybe_json
|
74
|
+
end
|
60
75
|
end
|
61
76
|
end
|
data/lib/openai_ruby/version.rb
CHANGED
data/lib/openai_ruby.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Renny Ren
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-04-
|
11
|
+
date: 2024-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: event_stream_parser
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
27
41
|
description:
|
28
42
|
email:
|
29
43
|
- rennyrjh@gmail.com
|