chatgpt-ruby 0.4.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 +11 -1
- data/README.md +137 -20
- data/Rakefile +3 -0
- data/lib/chatgpt/client.rb +106 -10
- data/lib/chatgpt/ruby/version.rb +1 -1
- metadata +26 -7
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
@@ -1,12 +1,14 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
chatgpt-ruby (0.
|
4
|
+
chatgpt-ruby (0.6.0)
|
5
|
+
rest-client
|
5
6
|
|
6
7
|
GEM
|
7
8
|
remote: https://rubygems.org/
|
8
9
|
specs:
|
9
10
|
ast (2.4.2)
|
11
|
+
docile (1.4.0)
|
10
12
|
domain_name (0.5.20190701)
|
11
13
|
unf (>= 0.0.5, < 1.0.0)
|
12
14
|
http-accept (1.7.0)
|
@@ -43,6 +45,12 @@ GEM
|
|
43
45
|
rubocop-ast (1.27.0)
|
44
46
|
parser (>= 3.2.1.0)
|
45
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)
|
46
54
|
unf (0.1.4)
|
47
55
|
unf_ext
|
48
56
|
unf_ext (0.0.8.2)
|
@@ -57,6 +65,8 @@ DEPENDENCIES
|
|
57
65
|
rake (~> 13.0)
|
58
66
|
rest-client
|
59
67
|
rubocop (~> 1.21)
|
68
|
+
simplecov
|
69
|
+
simplecov_json_formatter
|
60
70
|
|
61
71
|
BUNDLED WITH
|
62
72
|
2.3.26
|
data/README.md
CHANGED
@@ -1,56 +1,173 @@
|
|
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
4
|
|
5
|
-
|
5
|
+
The `chatgpt-ruby` is a Ruby SDK for the OpenAI API, including methods for generating text, completing prompts, and more ❤️
|
6
|
+
|
7
|
+
## Installation
|
6
8
|
|
7
9
|
Add this line to your application's Gemfile:
|
8
10
|
|
9
11
|
```ruby
|
10
|
-
gem
|
12
|
+
gem 'chatgpt-ruby'
|
11
13
|
```
|
12
14
|
|
15
|
+
|
16
|
+
|
13
17
|
And then execute:
|
14
18
|
|
19
|
+
```ruby
|
15
20
|
$ bundle install
|
21
|
+
```
|
16
22
|
|
17
|
-
### Gem install
|
18
23
|
|
19
|
-
Or install with:
|
20
24
|
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
```ruby
|
21
28
|
$ gem install chatgpt-ruby
|
29
|
+
```
|
22
30
|
|
23
31
|
## Usage
|
24
32
|
|
25
|
-
To use the
|
33
|
+
To use the ChatGPT API SDK, you will need an API key from OpenAI. You can obtain an API key by signing up for the [OpenAI API beta program](https://beta.openai.com/signup/) .
|
34
|
+
|
35
|
+
Once you have an API key, you can create a new `ChatGPT::Client` instance with your API key:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'chatgpt/client'
|
39
|
+
|
40
|
+
api_key = 'your-api-key'
|
41
|
+
client = ChatGPT::Client.new(api_key)
|
42
|
+
```
|
43
|
+
|
44
|
+
|
45
|
+
### Completions
|
46
|
+
|
47
|
+
To generate completions given a prompt, you can use the `completions` method:
|
48
|
+
|
26
49
|
```ruby
|
27
|
-
|
50
|
+
prompt = 'Hello, my name is'
|
51
|
+
completions = client.completions(prompt)
|
28
52
|
|
29
|
-
|
30
|
-
response = client.chat('Please translate the following English text to French: "Hello, how are you?"')
|
31
|
-
puts response
|
53
|
+
# Output: an array of completion strings
|
32
54
|
```
|
33
55
|
|
34
|
-
Replace 'your_api_key' with your actual ChatGPT API key.
|
35
56
|
|
36
|
-
### Parameters
|
37
57
|
|
38
|
-
|
58
|
+
You can customize the generation process by passing in additional parameters as a hash:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
params = {
|
62
|
+
engine: 'text-davinci-002',
|
63
|
+
max_tokens: 50,
|
64
|
+
temperature: 0.7
|
65
|
+
}
|
66
|
+
completions = client.completions(prompt, params)
|
67
|
+
|
68
|
+
# Output: an array of completion strings
|
69
|
+
```
|
70
|
+
|
71
|
+
|
72
|
+
### Search
|
73
|
+
|
74
|
+
To perform a search query given a set of documents and a search query, you can use the `search` method:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
documents = ['Document 1', 'Document 2', 'Document 3']
|
78
|
+
query = 'Search query'
|
79
|
+
results = client.search(documents, query)
|
80
|
+
|
81
|
+
# Output: an array of search result objects
|
82
|
+
```
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
You can customize the search process by passing in additional parameters as a hash:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
params = {
|
90
|
+
engine: 'ada',
|
91
|
+
max_rerank: 100
|
92
|
+
}
|
93
|
+
results = client.search(documents, query, params)
|
94
|
+
|
95
|
+
# Output: an array of search result objects
|
96
|
+
```
|
39
97
|
|
40
|
-
The `prompt` parameter is a string representing the input you want to send to the ChatGPT API.
|
41
98
|
|
42
|
-
|
43
|
-
|
99
|
+
### Classify
|
100
|
+
|
101
|
+
To classify a given text, you can use the `classify` method:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
text = 'This is a sample text'
|
105
|
+
label = client.classify(text)
|
106
|
+
|
107
|
+
# Output: a string representing the classified label
|
108
|
+
```
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
You can customize the classification process by passing in additional parameters as a hash:
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
params = {
|
116
|
+
model: 'text-davinci-002'
|
117
|
+
}
|
118
|
+
label = client.classify(text, params)
|
119
|
+
|
120
|
+
# Output: a string representing the classified label
|
121
|
+
```
|
122
|
+
|
123
|
+
|
124
|
+
### Generate Summaries
|
125
|
+
|
126
|
+
To generate summaries given a set of documents, you can use the `generate_summaries` method:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
documents = ['Document 1', 'Document 2', 'Document 3']
|
130
|
+
summary = client.generate_summaries(documents)
|
131
|
+
|
132
|
+
# Output: a string representing the generated summary
|
133
|
+
```
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
You can customize the summary generation process by passing in additional parameters as a hash:
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
params = {
|
141
|
+
model: 'text-davinci-002',
|
142
|
+
max_tokens: 100
|
143
|
+
}
|
144
|
+
summary = client.generate_summaries(documents, params)
|
145
|
+
|
146
|
+
# Output: a string representing the generated summary
|
147
|
+
```
|
148
|
+
|
149
|
+
|
150
|
+
### Generate Answers
|
151
|
+
|
152
|
+
To generate answers given a prompt and a set of documents, you can use the `generate_answers` method:
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
prompt = 'What is the capital of France?'
|
156
|
+
documents = ['France is a country in Europe', 'Paris is the capital of France']
|
157
|
+
answer = client.generate_answers(prompt, documents)
|
158
|
+
|
159
|
+
# Output
|
160
|
+
```
|
44
161
|
|
45
162
|
## Development
|
46
163
|
|
47
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.
|
48
165
|
|
49
|
-
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`.
|
50
167
|
|
51
168
|
## Contributing
|
52
169
|
|
53
|
-
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).
|
54
171
|
|
55
172
|
## License
|
56
173
|
|
@@ -58,4 +175,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
58
175
|
|
59
176
|
## Code of Conduct
|
60
177
|
|
61
|
-
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
@@ -1,23 +1,119 @@
|
|
1
|
-
# lib/
|
1
|
+
# lib/chatgpt/client.rb
|
2
2
|
|
3
3
|
require 'rest-client'
|
4
|
+
require 'json'
|
4
5
|
|
5
|
-
module
|
6
|
+
module ChatGPT
|
6
7
|
class Client
|
7
8
|
def initialize(api_key)
|
8
9
|
@api_key = api_key
|
9
|
-
@endpoint = 'https://api.
|
10
|
+
@endpoint = 'https://api.openai.com/v1'
|
10
11
|
end
|
11
12
|
|
12
|
-
#
|
13
|
-
def
|
14
|
-
|
15
|
-
|
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
|
22
|
+
def completions(prompt, params = {})
|
23
|
+
engine = params[:engine] || 'text-davinci-002'
|
24
|
+
max_tokens = params[:max_tokens] || 16
|
25
|
+
temperature = params[:temperature] || 0.5
|
26
|
+
top_p = params[:top_p] || 1.0
|
27
|
+
n = params[:n] || 1
|
28
|
+
|
29
|
+
url = "#{@endpoint}/engines/#{engine}/completions"
|
30
|
+
data = {
|
31
|
+
prompt: prompt,
|
32
|
+
max_tokens: max_tokens,
|
33
|
+
temperature: temperature,
|
34
|
+
top_p: top_p,
|
35
|
+
n: n
|
36
|
+
}
|
37
|
+
request_api(url, data)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Search-related methods
|
41
|
+
def search(documents, query, params = {})
|
42
|
+
engine = params[:engine] || 'ada'
|
43
|
+
max_rerank = params[:max_rerank] || 200
|
44
|
+
|
45
|
+
url = "#{@endpoint}/engines/#{engine}/search"
|
46
|
+
data = {
|
47
|
+
documents: documents,
|
48
|
+
query: query,
|
49
|
+
max_rerank: max_rerank
|
50
|
+
}
|
51
|
+
response = request_api(url, data)
|
52
|
+
response['data']
|
53
|
+
end
|
54
|
+
|
55
|
+
# Classification-related methods
|
56
|
+
def classify(text, params = {})
|
57
|
+
model = params[:model] || 'text-davinci-002'
|
58
|
+
|
59
|
+
url = "#{@endpoint}/classifications/#{model}"
|
60
|
+
data = {
|
61
|
+
model: model,
|
62
|
+
input: text
|
63
|
+
}
|
64
|
+
response = request_api(url, data)
|
65
|
+
response['data'][0]['label']
|
66
|
+
end
|
67
|
+
|
68
|
+
# Summary-related methods
|
69
|
+
def generate_summaries(documents, params = {})
|
70
|
+
model = params[:model] || 'text-davinci-002'
|
71
|
+
max_tokens = params[:max_tokens] || 60
|
72
|
+
temperature = params[:temperature] || 0.5
|
73
|
+
top_p = params[:top_p] || 1.0
|
74
|
+
frequency_penalty = params[:frequency_penalty] || 0.0
|
75
|
+
presence_penalty = params[:presence_penalty] || 0.0
|
76
|
+
|
77
|
+
url = "#{@endpoint}/engines/#{model}/generate"
|
78
|
+
data = {
|
79
|
+
prompt: '',
|
80
|
+
max_tokens: max_tokens,
|
81
|
+
temperature: temperature,
|
82
|
+
top_p: top_p,
|
83
|
+
frequency_penalty: frequency_penalty,
|
84
|
+
presence_penalty: presence_penalty,
|
85
|
+
documents: documents
|
86
|
+
}
|
87
|
+
response = request_api(url, data)
|
88
|
+
response['choices'][0]['text']
|
89
|
+
end
|
90
|
+
|
91
|
+
# Answer-generation-related methods
|
92
|
+
def generate_answers(prompt, documents, params = {})
|
93
|
+
model = params[:model] || 'text-davinci-002'
|
94
|
+
max_tokens = params[:max_tokens] || 5
|
95
|
+
|
96
|
+
url = "#{@endpoint}/engines/#{model}/answers"
|
97
|
+
data = {
|
16
98
|
prompt: prompt,
|
17
|
-
|
99
|
+
documents: documents,
|
100
|
+
max_tokens: max_tokens
|
18
101
|
}
|
19
|
-
response =
|
20
|
-
|
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
|
21
117
|
end
|
22
118
|
end
|
23
119
|
end
|
data/lib/chatgpt/ruby/version.rb
CHANGED
metadata
CHANGED
@@ -1,22 +1,38 @@
|
|
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-
|
12
|
-
dependencies:
|
13
|
-
|
11
|
+
date: 2023-04-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: This gem provides a Ruby SDK for interacting with the OpenAI API, including
|
28
|
+
methods for generating text, completing prompts, and more.
|
14
29
|
email:
|
15
30
|
- nagendra.dhanakeerthi@gmail.com
|
16
31
|
executables: []
|
17
32
|
extensions: []
|
18
33
|
extra_rdoc_files: []
|
19
34
|
files:
|
35
|
+
- ".codeclimate.yml"
|
20
36
|
- ".rubocop.yml"
|
21
37
|
- CHANGELOG.md
|
22
38
|
- CODE_OF_CONDUCT.md
|
@@ -29,10 +45,13 @@ files:
|
|
29
45
|
- lib/chatgpt/ruby.rb
|
30
46
|
- lib/chatgpt/ruby/version.rb
|
31
47
|
- sig/chatgpt/ruby.rbs
|
32
|
-
homepage:
|
48
|
+
homepage: https://github.com/nagstler/chatgpt-ruby.git
|
33
49
|
licenses:
|
34
50
|
- MIT
|
35
|
-
metadata:
|
51
|
+
metadata:
|
52
|
+
homepage_uri: https://github.com/nagstler/chatgpt-ruby.git
|
53
|
+
source_code_uri: https://github.com/nagstler/chatgpt-ruby.git
|
54
|
+
changelog_uri: https://github.com/nagstler/chatgpt-ruby/blob/main/CHANGELOG.md
|
36
55
|
post_install_message:
|
37
56
|
rdoc_options: []
|
38
57
|
require_paths:
|
@@ -51,5 +70,5 @@ requirements: []
|
|
51
70
|
rubygems_version: 3.3.26
|
52
71
|
signing_key:
|
53
72
|
specification_version: 4
|
54
|
-
summary:
|
73
|
+
summary: A Ruby SDK for the OpenAI API
|
55
74
|
test_files: []
|