openai_ruby 0.3.1 → 0.3.3
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 +38 -13
- data/lib/openai_ruby/client.rb +19 -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: 33013deb6a09941565ac13e9b3ee5de96d220b54c5c18f691201312237625ed1
|
|
4
|
+
data.tar.gz: f0e635b8af085b9111f83e3575e125a6de7a931793df2f75cca27f84abb6ccac
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e8b07aafb4694d054618c56808fd8e9fd12d246f24003c5d8ad1672dff02a75e9c91c2d656f0e537e9f25dfe2d1402a84ec3f0d0f8491a1310b06c936b483b09
|
|
7
|
+
data.tar.gz: f32b7a3a048742c1d2ec478ede1082176ad00a7351ee7b9de094ab2e2315e7153b2de3c641d5474ba85109b21eddbe334209c4800060dd87bb89e8433e553765
|
data/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[](https://travis-ci.org/renny-ren/openai_ruby)
|
|
5
5
|
[](https://github.com/renny-ren/openai_ruby/blob/main/LICENSE)
|
|
6
6
|
|
|
7
|
-
This is a Ruby wrapper for [OpenAI API](https://openai.com/api
|
|
7
|
+
This is a Ruby wrapper for [OpenAI API](https://platform.openai.com/docs/api-reference), which provides convenient access to the OpenAI API from applications written in Ruby.
|
|
8
8
|
|
|
9
9
|
Let's build next-gen apps with OpenAI’s powerful models.
|
|
10
10
|
|
|
@@ -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,21 @@ module OpenAI
|
|
|
18
18
|
)
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
+
def create_chat_completion(params = {})
|
|
22
|
+
if params[:stream]
|
|
23
|
+
connection.post("/v1/chat/completions") do |req|
|
|
24
|
+
req.body = params.to_json
|
|
25
|
+
req.options.on_data = proc do |chunk, overall_received_bytes, env|
|
|
26
|
+
if block_given?
|
|
27
|
+
yield(chunk, overall_received_bytes, env)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
else
|
|
32
|
+
connection.post("/v1/chat/completions", params.to_json, headers)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
21
36
|
def create_edit(params = {})
|
|
22
37
|
Faraday.post(
|
|
23
38
|
"#{BASE_URL}/v1/edits",
|
|
@@ -28,6 +43,10 @@ module OpenAI
|
|
|
28
43
|
|
|
29
44
|
private
|
|
30
45
|
|
|
46
|
+
def connection
|
|
47
|
+
Faraday.new(url: BASE_URL, headers: headers)
|
|
48
|
+
end
|
|
49
|
+
|
|
31
50
|
def headers
|
|
32
51
|
{
|
|
33
52
|
"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.3
|
|
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-27 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
|