openai_ruby 0.3.1 → 0.3.2
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/README.md +37 -12
- data/lib/openai_ruby/client.rb +21 -0
- data/lib/openai_ruby/version.rb +1 -1
- metadata +2 -3
- data/openai_ruby.gemspec +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 973ae690504a531c6b3b95415d622346a7fedca7e1fe7d2b509fe689fa733f7a
|
4
|
+
data.tar.gz: ec6b40954dccffbed531aad98b79458061eb1cba7e88b370d30a639c891352c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 379578031e02141ee8ce4b7949cd08c84a98806cfdeb277a12e1a46f4a0394d59a402007b566ab4247bafda0daa646869866b6fc1bd29acebf338d312bbce76d
|
7
|
+
data.tar.gz: e8a1fc7ee1822493e11b1e1b4eb318491af75252b2873040379f102ce997961d0afa23e1a9abff40459d89a7af6a81c29418f22487287c379bcf23aae8b83d3a
|
data/README.md
CHANGED
@@ -30,27 +30,52 @@ create a client like this:
|
|
30
30
|
client = OpenAI::Client.new("your OpenAI key here")
|
31
31
|
```
|
32
32
|
|
33
|
-
### Completion
|
33
|
+
### Completion
|
34
|
+
https://platform.openai.com/docs/api-reference/completions
|
34
35
|
|
35
36
|
```ruby
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
)
|
46
|
-
p res.status # 200
|
37
|
+
# params = {
|
38
|
+
# model: "text-davinci-003",
|
39
|
+
# prompt: "Hello, who are you?",
|
40
|
+
# max_tokens: 100,
|
41
|
+
# temperature: 0.5,
|
42
|
+
# frequency_penalty: 0,
|
43
|
+
# presence_penalty: 0,
|
44
|
+
# }
|
45
|
+
res = client.create_completion(params)
|
47
46
|
response = JSON.parse(res.body)
|
48
47
|
|
49
48
|
p response.dig("choices", 0, "text") # "I am an AI created by OpenAI."
|
50
49
|
p response.dig('usage', 'total_tokens') # 18
|
51
50
|
```
|
52
51
|
|
52
|
+
### Chat Completion
|
53
|
+
https://platform.openai.com/docs/api-reference/chat/create
|
54
|
+
|
55
|
+
If you set `stream` param to `true`, a block will be called with the chunk data:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
# params = {
|
59
|
+
# model: "gpt-3.5-turbo",
|
60
|
+
# prompt: "Hello",
|
61
|
+
# max_tokens: 100,
|
62
|
+
# temperature: 1,
|
63
|
+
# stream: true,
|
64
|
+
# }
|
65
|
+
res = client.create_chat_completion(params) do |chunk, overall_received_bytes, env|
|
66
|
+
data = chunk[/data: (.*)\n\n$/, 1]
|
67
|
+
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}]}
|
68
|
+
if data == "[DONE]"
|
69
|
+
# the stream is end
|
70
|
+
else
|
71
|
+
response = JSON.parse(data)
|
72
|
+
p response.dig("choices", 0, "text") # "Hello"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
53
77
|
### Edit
|
78
|
+
https://platform.openai.com/docs/api-reference/edits
|
54
79
|
|
55
80
|
```ruby
|
56
81
|
res = client.create_edit(
|
data/lib/openai_ruby/client.rb
CHANGED
@@ -18,6 +18,23 @@ module OpenAI
|
|
18
18
|
)
|
19
19
|
end
|
20
20
|
|
21
|
+
def create_chat_completion(params = {}, &block)
|
22
|
+
if params[:stream]
|
23
|
+
connection.post("/v1/chat/completions") do |req|
|
24
|
+
req.body = params.to_json
|
25
|
+
req.options.on_data = Proc.new do |chunk, overall_received_bytes, env|
|
26
|
+
block.call(chunk, overall_received_bytes, env)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
else
|
30
|
+
connection.post(
|
31
|
+
"/v1/chat/completions",
|
32
|
+
params.to_json,
|
33
|
+
headers
|
34
|
+
)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
21
38
|
def create_edit(params = {})
|
22
39
|
Faraday.post(
|
23
40
|
"#{BASE_URL}/v1/edits",
|
@@ -28,6 +45,10 @@ module OpenAI
|
|
28
45
|
|
29
46
|
private
|
30
47
|
|
48
|
+
def connection
|
49
|
+
Faraday.new(url: BASE_URL, headers: headers)
|
50
|
+
end
|
51
|
+
|
31
52
|
def headers
|
32
53
|
{
|
33
54
|
"Content-Type" => "application/json",
|
data/lib/openai_ruby/version.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.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Renny Ren
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -42,7 +42,6 @@ files:
|
|
42
42
|
- lib/openai_ruby.rb
|
43
43
|
- lib/openai_ruby/client.rb
|
44
44
|
- lib/openai_ruby/version.rb
|
45
|
-
- openai_ruby.gemspec
|
46
45
|
- sig/openai_ruby/ruby.rbs
|
47
46
|
homepage: https://github.com/renny-ren/openai_ruby
|
48
47
|
licenses:
|
data/openai_ruby.gemspec
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "lib/openai_ruby/version"
|
4
|
-
|
5
|
-
Gem::Specification.new do |spec|
|
6
|
-
spec.name = "openai_ruby"
|
7
|
-
spec.version = OpenAI::VERSION
|
8
|
-
spec.authors = ["Renny Ren"]
|
9
|
-
spec.email = ["rennyrjh@gmail.com"]
|
10
|
-
|
11
|
-
spec.summary = "A Ruby wrapper for OpenAI API"
|
12
|
-
spec.homepage = "https://github.com/renny-ren/openai_ruby"
|
13
|
-
spec.license = "MIT"
|
14
|
-
spec.required_ruby_version = ">= 2.6.0"
|
15
|
-
|
16
|
-
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
-
spec.metadata["source_code_uri"] = "https://github.com/renny-ren/openai_ruby"
|
18
|
-
spec.metadata["changelog_uri"] = "https://github.com/renny-ren/openai_ruby/blob/main/CHANGELOG.md"
|
19
|
-
|
20
|
-
# Specify which files should be added to the gem when it is released.
|
21
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
-
spec.files = Dir.chdir(__dir__) do
|
23
|
-
`git ls-files -z`.split("\x0").reject do |f|
|
24
|
-
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
spec.bindir = "exe"
|
29
|
-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
-
spec.require_paths = ["lib"]
|
31
|
-
|
32
|
-
spec.add_dependency "faraday", "~> 2.7"
|
33
|
-
|
34
|
-
# For more information and examples about making a new gem, check out our
|
35
|
-
# guide at: https://bundler.io/guides/creating_gem.html
|
36
|
-
end
|