chatgpt-ruby 0.6.0 → 1.0.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/.codeclimate.yml +8 -0
- data/Gemfile +8 -1
- data/Gemfile.lock +9 -0
- data/README.md +8 -6
- data/Rakefile +3 -0
- data/lib/chatgpt/client.rb +35 -30
- data/lib/chatgpt/ruby/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6a246b757af45ec2b58fd7fad4cda501d6408f273f62c697705a4b85b824c07e
|
4
|
+
data.tar.gz: b314a13e482bf33e88d2dd7adec626850612cb1da247ed0a247492dc40bd09af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cd02878303337251add7d71e82bd9d29504313217dfeb6bcc2228a9d61f14abe0306523e55374465d431e65f438106b8054936b22d9d2a6de9aaa7b1d1e8a29
|
7
|
+
data.tar.gz: b30e1470736ccfd7ebd26c380792eb1759707bb630bbb3d4ad2dc446968d6e414c3ace6ac3164e7681ff57f07445a50d9025e72b5c497f77e6b3de949a537b79
|
data/.codeclimate.yml
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -8,6 +8,7 @@ GEM
|
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
10
|
ast (2.4.2)
|
11
|
+
docile (1.4.0)
|
11
12
|
domain_name (0.5.20190701)
|
12
13
|
unf (>= 0.0.5, < 1.0.0)
|
13
14
|
http-accept (1.7.0)
|
@@ -44,6 +45,12 @@ GEM
|
|
44
45
|
rubocop-ast (1.27.0)
|
45
46
|
parser (>= 3.2.1.0)
|
46
47
|
ruby-progressbar (1.13.0)
|
48
|
+
simplecov (0.22.0)
|
49
|
+
docile (~> 1.1)
|
50
|
+
simplecov-html (~> 0.11)
|
51
|
+
simplecov_json_formatter (~> 0.1)
|
52
|
+
simplecov-html (0.12.3)
|
53
|
+
simplecov_json_formatter (0.1.4)
|
47
54
|
unf (0.1.4)
|
48
55
|
unf_ext
|
49
56
|
unf_ext (0.0.8.2)
|
@@ -58,6 +65,8 @@ DEPENDENCIES
|
|
58
65
|
rake (~> 13.0)
|
59
66
|
rest-client
|
60
67
|
rubocop (~> 1.21)
|
68
|
+
simplecov
|
69
|
+
simplecov_json_formatter
|
61
70
|
|
62
71
|
BUNDLED WITH
|
63
72
|
2.3.26
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
#
|
1
|
+
# ChatGPT Ruby
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/chatgpt-ruby) [](https://opensource.org/licenses/MIT) [](https://codeclimate.com/github/nagstler/chatgpt-ruby/maintainability) [](https://codeclimate.com/github/nagstler/chatgpt-ruby/test_coverage) [](https://github.com/nagstler/chatgpt-ruby/actions/workflows/ci.yml) [](https://github.com/nagstler/chatgpt-ruby/graphs/contributors)
|
4
|
+
|
5
|
+
The `chatgpt-ruby` is a Ruby SDK for the OpenAI API, including methods for generating text, completing prompts, and more ❤️
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -33,7 +35,7 @@ To use the ChatGPT API SDK, you will need an API key from OpenAI. You can obtain
|
|
33
35
|
Once you have an API key, you can create a new `ChatGPT::Client` instance with your API key:
|
34
36
|
|
35
37
|
```ruby
|
36
|
-
require '
|
38
|
+
require 'chatgpt/client'
|
37
39
|
|
38
40
|
api_key = 'your-api-key'
|
39
41
|
client = ChatGPT::Client.new(api_key)
|
@@ -161,11 +163,11 @@ answer = client.generate_answers(prompt, documents)
|
|
161
163
|
|
162
164
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
163
165
|
|
164
|
-
To install this gem onto your local machine, run `bundle exec rake install`.
|
166
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
165
167
|
|
166
168
|
## Contributing
|
167
169
|
|
168
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
170
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nagstler/chatgpt-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nagstler/chatgpt-ruby/blob/main/CODE_OF_CONDUCT.md).
|
169
171
|
|
170
172
|
## License
|
171
173
|
|
@@ -173,4 +175,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
173
175
|
|
174
176
|
## Code of Conduct
|
175
177
|
|
176
|
-
Everyone interacting in the Chatgpt::Ruby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
178
|
+
Everyone interacting in the Chatgpt::Ruby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nagstler/chatgpt-ruby/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
CHANGED
@@ -3,8 +3,10 @@
|
|
3
3
|
require "bundler/gem_tasks"
|
4
4
|
require "rake/testtask"
|
5
5
|
|
6
|
+
|
6
7
|
Rake::TestTask.new(:test) do |t|
|
7
8
|
t.libs << "test"
|
9
|
+
t.pattern = 'test/**/*_test.rb'
|
8
10
|
t.libs << "lib"
|
9
11
|
t.test_files = FileList["test/**/test_*.rb"]
|
10
12
|
end
|
@@ -14,3 +16,4 @@ require "rubocop/rake_task"
|
|
14
16
|
RuboCop::RakeTask.new
|
15
17
|
|
16
18
|
task default: %i[test rubocop]
|
19
|
+
|
data/lib/chatgpt/client.rb
CHANGED
@@ -10,6 +10,15 @@ module ChatGPT
|
|
10
10
|
@endpoint = 'https://api.openai.com/v1'
|
11
11
|
end
|
12
12
|
|
13
|
+
# Helper method to prepare headers
|
14
|
+
def headers
|
15
|
+
{
|
16
|
+
'Content-Type' => 'application/json',
|
17
|
+
'Authorization' => "Bearer #{@api_key}"
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
# Completion-related methods
|
13
22
|
def completions(prompt, params = {})
|
14
23
|
engine = params[:engine] || 'text-davinci-002'
|
15
24
|
max_tokens = params[:max_tokens] || 16
|
@@ -18,10 +27,6 @@ module ChatGPT
|
|
18
27
|
n = params[:n] || 1
|
19
28
|
|
20
29
|
url = "#{@endpoint}/engines/#{engine}/completions"
|
21
|
-
headers = {
|
22
|
-
'Content-Type' => 'application/json',
|
23
|
-
'Authorization' => "Bearer #{@api_key}"
|
24
|
-
}
|
25
30
|
data = {
|
26
31
|
prompt: prompt,
|
27
32
|
max_tokens: max_tokens,
|
@@ -29,44 +34,38 @@ module ChatGPT
|
|
29
34
|
top_p: top_p,
|
30
35
|
n: n
|
31
36
|
}
|
32
|
-
|
33
|
-
JSON.parse(response.body)
|
37
|
+
request_api(url, data)
|
34
38
|
end
|
35
39
|
|
40
|
+
# Search-related methods
|
36
41
|
def search(documents, query, params = {})
|
37
42
|
engine = params[:engine] || 'ada'
|
38
43
|
max_rerank = params[:max_rerank] || 200
|
39
44
|
|
40
45
|
url = "#{@endpoint}/engines/#{engine}/search"
|
41
|
-
headers = {
|
42
|
-
'Content-Type' => 'application/json',
|
43
|
-
'Authorization' => "Bearer #{@api_key}"
|
44
|
-
}
|
45
46
|
data = {
|
46
47
|
documents: documents,
|
47
48
|
query: query,
|
48
49
|
max_rerank: max_rerank
|
49
50
|
}
|
50
|
-
response =
|
51
|
-
|
51
|
+
response = request_api(url, data)
|
52
|
+
response['data']
|
52
53
|
end
|
53
54
|
|
55
|
+
# Classification-related methods
|
54
56
|
def classify(text, params = {})
|
55
57
|
model = params[:model] || 'text-davinci-002'
|
56
58
|
|
57
59
|
url = "#{@endpoint}/classifications/#{model}"
|
58
|
-
headers = {
|
59
|
-
'Content-Type' => 'application/json',
|
60
|
-
'Authorization' => "Bearer #{@api_key}"
|
61
|
-
}
|
62
60
|
data = {
|
63
61
|
model: model,
|
64
62
|
input: text
|
65
63
|
}
|
66
|
-
response =
|
67
|
-
|
64
|
+
response = request_api(url, data)
|
65
|
+
response['data'][0]['label']
|
68
66
|
end
|
69
67
|
|
68
|
+
# Summary-related methods
|
70
69
|
def generate_summaries(documents, params = {})
|
71
70
|
model = params[:model] || 'text-davinci-002'
|
72
71
|
max_tokens = params[:max_tokens] || 60
|
@@ -76,10 +75,6 @@ module ChatGPT
|
|
76
75
|
presence_penalty = params[:presence_penalty] || 0.0
|
77
76
|
|
78
77
|
url = "#{@endpoint}/engines/#{model}/generate"
|
79
|
-
headers = {
|
80
|
-
'Content-Type' => 'application/json',
|
81
|
-
'Authorization' => "Bearer #{@api_key}"
|
82
|
-
}
|
83
78
|
data = {
|
84
79
|
prompt: '',
|
85
80
|
max_tokens: max_tokens,
|
@@ -89,26 +84,36 @@ module ChatGPT
|
|
89
84
|
presence_penalty: presence_penalty,
|
90
85
|
documents: documents
|
91
86
|
}
|
92
|
-
response =
|
93
|
-
|
87
|
+
response = request_api(url, data)
|
88
|
+
response['choices'][0]['text']
|
94
89
|
end
|
95
90
|
|
91
|
+
# Answer-generation-related methods
|
96
92
|
def generate_answers(prompt, documents, params = {})
|
97
93
|
model = params[:model] || 'text-davinci-002'
|
98
94
|
max_tokens = params[:max_tokens] || 5
|
99
95
|
|
100
96
|
url = "#{@endpoint}/engines/#{model}/answers"
|
101
|
-
headers = {
|
102
|
-
'Content-Type' => 'application/json',
|
103
|
-
'Authorization' => "Bearer #{@api_key}"
|
104
|
-
}
|
105
97
|
data = {
|
106
98
|
prompt: prompt,
|
107
99
|
documents: documents,
|
108
100
|
max_tokens: max_tokens
|
109
101
|
}
|
110
|
-
response =
|
111
|
-
|
102
|
+
response = request_api(url, data)
|
103
|
+
response['data'][0]['answer']
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
108
|
+
# Helper method to make API requests
|
109
|
+
def request_api(url, data)
|
110
|
+
begin
|
111
|
+
response = RestClient.post(url, data.to_json, headers)
|
112
|
+
JSON.parse(response.body)
|
113
|
+
rescue RestClient::ExceptionWithResponse => e
|
114
|
+
error_msg = JSON.parse(e.response.body)['error']['message']
|
115
|
+
raise RestClient::ExceptionWithResponse.new("#{e.message}: #{error_msg} (#{e.http_code})"), nil, e.backtrace
|
116
|
+
end
|
112
117
|
end
|
113
118
|
end
|
114
119
|
end
|
data/lib/chatgpt/ruby/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chatgpt-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nagendra Dhanakeerthi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-04-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -32,6 +32,7 @@ executables: []
|
|
32
32
|
extensions: []
|
33
33
|
extra_rdoc_files: []
|
34
34
|
files:
|
35
|
+
- ".codeclimate.yml"
|
35
36
|
- ".rubocop.yml"
|
36
37
|
- CHANGELOG.md
|
37
38
|
- CODE_OF_CONDUCT.md
|