chatgpt-ruby 0.6.0 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7aa91296be7cab7d9f1f5f1b2b6e55fa784c4a8760fabebbaac3cf903268da1d
4
- data.tar.gz: cd8a13397002dec2a38da0ad766ce3b9301ca3d6bc5efa2669b30851d3ebc4ca
3
+ metadata.gz: 9ab25c3c1b76e0b3249dbeba1c4b0772c4c6f7d34580005f3888ff09c2829453
4
+ data.tar.gz: 3feffdb2bed24d6464bdd945b6a94abb1a752567fac19d2866b5430dfffda019
5
5
  SHA512:
6
- metadata.gz: adf9e9cdc0e6b3e834d86e388b2c6319a42292c302333cc4a039817c9b4136e1394e00dff859c9f2359674728bf550171a22d64387089accdf48aed2d023658f
7
- data.tar.gz: f1d0932b4ad810440543fa9b538a52190f6355ef892356519373748eb002f94a4d3de35f234fbd120c0bcd959b1f609fd6f35bbd27cd2125009a0b84e11dd8bd
6
+ metadata.gz: d42c42272c0f17cecc040f2796f08181cc858b744a5fbf33d6a26c31aa8990352b6f022832a6b9b61d25cf9b8577939783a93ce6e250a3c4458dfec16d4d43f1
7
+ data.tar.gz: f9a3cd15ff2901c09361ba53dd71d3ea7263669069126672b390444b11a8d660805cb34a7ea71b792afd5208c6971c3d374d58e3bb74fe2cccbc4af3a23baa5d
data/.codeclimate.yml ADDED
@@ -0,0 +1,8 @@
1
+ version: "2"
2
+ checks:
3
+ rubocop:
4
+ enabled: true
5
+ channel: "stable"
6
+ plugins:
7
+ test-reporter:
8
+ enabled: true
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
+ ## [1.0.0] - 2023-04-30
2
+
3
+ ### Added
4
+
5
+ - Initial release of the `chatgpt-ruby` gem.
6
+ - Implements ChatGPT Client with methods for completions, search, classification, summary, and answer generation.
7
+ - Includes unit tests for each method.
8
+
1
9
  ## [Unreleased]
2
10
 
3
11
  ## [0.1.0] - 2023-03-22
4
12
 
5
- - Initial release
13
+ - Initial release
data/Gemfile CHANGED
@@ -11,4 +11,11 @@ gem "minitest", "~> 5.0"
11
11
 
12
12
  gem "rubocop", "~> 1.21"
13
13
 
14
- gem 'rest-client'
14
+ gem 'rest-client'
15
+
16
+ group :test do
17
+ gem 'simplecov', require: false
18
+ gem 'simplecov_json_formatter', require: false
19
+ end
20
+
21
+
data/Gemfile.lock CHANGED
@@ -1,13 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- chatgpt-ruby (0.6.0)
4
+ chatgpt-ruby (1.0.0)
5
5
  rest-client
6
6
 
7
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
- # ChatGPTRuby
1
+ # ChatGPT Ruby
2
2
 
3
- The `chatgpt-ruby` is a Ruby SDK for the OpenAI API, including methods for generating text, completing prompts, and more.
3
+ [![Gem Version](https://badge.fury.io/rb/chatgpt-ruby.svg)](https://badge.fury.io/rb/chatgpt-ruby) [![License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Maintainability](https://api.codeclimate.com/v1/badges/08c7e7b58e9fbe7156eb/maintainability)](https://codeclimate.com/github/nagstler/chatgpt-ruby/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/08c7e7b58e9fbe7156eb/test_coverage)](https://codeclimate.com/github/nagstler/chatgpt-ruby/test_coverage) [![CI](https://github.com/nagstler/chatgpt-ruby/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/nagstler/chatgpt-ruby/actions/workflows/ci.yml) [![GitHub contributors](https://img.shields.io/github/contributors/nagstler/chatgpt-ruby)](https://github.com/nagstler/chatgpt-ruby/graphs/contributors)
4
+
5
+ The `chatgpt-ruby` is a Ruby SDK for the OpenAI API, providing methods for generating text and completing prompts using the ChatGPT model.
4
6
 
5
7
  ## Installation
6
8
 
@@ -10,16 +12,12 @@ Add this line to your application's Gemfile:
10
12
  gem 'chatgpt-ruby'
11
13
  ```
12
14
 
13
-
14
-
15
15
  And then execute:
16
16
 
17
17
  ```ruby
18
18
  $ bundle install
19
19
  ```
20
20
 
21
-
22
-
23
21
  Or install it yourself as:
24
22
 
25
23
  ```ruby
@@ -33,14 +31,13 @@ To use the ChatGPT API SDK, you will need an API key from OpenAI. You can obtain
33
31
  Once you have an API key, you can create a new `ChatGPT::Client` instance with your API key:
34
32
 
35
33
  ```ruby
36
- require 'chatgpt_api'
34
+ require 'chatgpt/client'
37
35
 
38
36
  api_key = 'your-api-key'
39
37
  client = ChatGPT::Client.new(api_key)
40
38
  ```
41
39
 
42
-
43
- ### Completions
40
+ ## Completions
44
41
 
45
42
  To generate completions given a prompt, you can use the `completions` method:
46
43
 
@@ -51,8 +48,6 @@ completions = client.completions(prompt)
51
48
  # Output: an array of completion strings
52
49
  ```
53
50
 
54
-
55
-
56
51
  You can customize the generation process by passing in additional parameters as a hash:
57
52
 
58
53
  ```ruby
@@ -63,109 +58,19 @@ params = {
63
58
  }
64
59
  completions = client.completions(prompt, params)
65
60
 
61
+ puts completions["choices"].map { |c| c["text"] }
66
62
  # Output: an array of completion strings
67
63
  ```
68
64
 
69
-
70
- ### Search
71
-
72
- To perform a search query given a set of documents and a search query, you can use the `search` method:
73
-
74
- ```ruby
75
- documents = ['Document 1', 'Document 2', 'Document 3']
76
- query = 'Search query'
77
- results = client.search(documents, query)
78
-
79
- # Output: an array of search result objects
80
- ```
81
-
82
-
83
-
84
- You can customize the search process by passing in additional parameters as a hash:
85
-
86
- ```ruby
87
- params = {
88
- engine: 'ada',
89
- max_rerank: 100
90
- }
91
- results = client.search(documents, query, params)
92
-
93
- # Output: an array of search result objects
94
- ```
95
-
96
-
97
- ### Classify
98
-
99
- To classify a given text, you can use the `classify` method:
100
-
101
- ```ruby
102
- text = 'This is a sample text'
103
- label = client.classify(text)
104
-
105
- # Output: a string representing the classified label
106
- ```
107
-
108
-
109
-
110
- You can customize the classification process by passing in additional parameters as a hash:
111
-
112
- ```ruby
113
- params = {
114
- model: 'text-davinci-002'
115
- }
116
- label = client.classify(text, params)
117
-
118
- # Output: a string representing the classified label
119
- ```
120
-
121
-
122
- ### Generate Summaries
123
-
124
- To generate summaries given a set of documents, you can use the `generate_summaries` method:
125
-
126
- ```ruby
127
- documents = ['Document 1', 'Document 2', 'Document 3']
128
- summary = client.generate_summaries(documents)
129
-
130
- # Output: a string representing the generated summary
131
- ```
132
-
133
-
134
-
135
- You can customize the summary generation process by passing in additional parameters as a hash:
136
-
137
- ```ruby
138
- params = {
139
- model: 'text-davinci-002',
140
- max_tokens: 100
141
- }
142
- summary = client.generate_summaries(documents, params)
143
-
144
- # Output: a string representing the generated summary
145
- ```
146
-
147
-
148
- ### Generate Answers
149
-
150
- To generate answers given a prompt and a set of documents, you can use the `generate_answers` method:
151
-
152
- ```ruby
153
- prompt = 'What is the capital of France?'
154
- documents = ['France is a country in Europe', 'Paris is the capital of France']
155
- answer = client.generate_answers(prompt, documents)
156
-
157
- # Output
158
- ```
159
-
160
65
  ## Development
161
66
 
162
67
  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
68
 
164
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+ To install this gem onto your local machine, run `bundle exec rake install`.
165
70
 
166
71
  ## Contributing
167
72
 
168
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/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/[USERNAME]/chatgpt-ruby/blob/main/CODE_OF_CONDUCT.md).
73
+ 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
74
 
170
75
  ## License
171
76
 
@@ -173,4 +78,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
173
78
 
174
79
  ## Code of Conduct
175
80
 
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/[USERNAME]/chatgpt-ruby/blob/main/CODE_OF_CONDUCT.md).
81
+ 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
+
@@ -1,27 +1,45 @@
1
- # lib/chatgpt/client.rb
2
-
3
1
  require 'rest-client'
4
2
  require 'json'
5
3
 
6
4
  module ChatGPT
7
5
  class Client
6
+ # Initialize the client with the API key
7
+ #
8
+ # @param api_key [String] The API key for the GPT-3 service
8
9
  def initialize(api_key)
9
10
  @api_key = api_key
11
+ # Base endpoint for the OpenAI API
10
12
  @endpoint = 'https://api.openai.com/v1'
11
13
  end
12
14
 
15
+ # Prepare headers for the API request
16
+ #
17
+ # @return [Hash] The headers for the API request
18
+ def headers
19
+ {
20
+ 'Content-Type' => 'application/json',
21
+ 'Authorization' => "Bearer #{@api_key}"
22
+ }
23
+ end
24
+
25
+ # Generate completions based on a given prompt
26
+ #
27
+ # @param prompt [String] The prompt to be completed
28
+ # @param params [Hash] Additional parameters for the completion request
29
+ #
30
+ # @return [Hash] The completion results from the API
13
31
  def completions(prompt, params = {})
32
+ # Set default parameters
14
33
  engine = params[:engine] || 'text-davinci-002'
15
34
  max_tokens = params[:max_tokens] || 16
16
35
  temperature = params[:temperature] || 0.5
17
36
  top_p = params[:top_p] || 1.0
18
37
  n = params[:n] || 1
19
38
 
39
+ # Construct the URL for the completion request
20
40
  url = "#{@endpoint}/engines/#{engine}/completions"
21
- headers = {
22
- 'Content-Type' => 'application/json',
23
- 'Authorization' => "Bearer #{@api_key}"
24
- }
41
+
42
+ # Prepare the data for the request
25
43
  data = {
26
44
  prompt: prompt,
27
45
  max_tokens: max_tokens,
@@ -29,86 +47,36 @@ module ChatGPT
29
47
  top_p: top_p,
30
48
  n: n
31
49
  }
32
- response = RestClient.post(url, data.to_json, headers)
33
- JSON.parse(response.body)
50
+
51
+ # Make the request to the API
52
+ request_api(url, data)
34
53
  end
35
54
 
36
- def search(documents, query, params = {})
37
- engine = params[:engine] || 'ada'
38
- max_rerank = params[:max_rerank] || 200
39
-
40
- url = "#{@endpoint}/engines/#{engine}/search"
41
- headers = {
42
- 'Content-Type' => 'application/json',
43
- 'Authorization' => "Bearer #{@api_key}"
44
- }
45
- data = {
46
- documents: documents,
47
- query: query,
48
- max_rerank: max_rerank
49
- }
50
- response = RestClient.post(url, data.to_json, headers)
51
- JSON.parse(response.body)['data']
52
- end
53
-
54
- def classify(text, params = {})
55
- model = params[:model] || 'text-davinci-002'
56
-
57
- url = "#{@endpoint}/classifications/#{model}"
58
- headers = {
59
- 'Content-Type' => 'application/json',
60
- 'Authorization' => "Bearer #{@api_key}"
61
- }
62
- data = {
63
- model: model,
64
- input: text
65
- }
66
- response = RestClient.post(url, data.to_json, headers)
67
- JSON.parse(response.body)['data'][0]['label']
68
- end
69
-
70
- def generate_summaries(documents, params = {})
71
- model = params[:model] || 'text-davinci-002'
72
- max_tokens = params[:max_tokens] || 60
73
- temperature = params[:temperature] || 0.5
74
- top_p = params[:top_p] || 1.0
75
- frequency_penalty = params[:frequency_penalty] || 0.0
76
- presence_penalty = params[:presence_penalty] || 0.0
77
-
78
- url = "#{@endpoint}/engines/#{model}/generate"
79
- headers = {
80
- 'Content-Type' => 'application/json',
81
- 'Authorization' => "Bearer #{@api_key}"
82
- }
83
- data = {
84
- prompt: '',
85
- max_tokens: max_tokens,
86
- temperature: temperature,
87
- top_p: top_p,
88
- frequency_penalty: frequency_penalty,
89
- presence_penalty: presence_penalty,
90
- documents: documents
91
- }
92
- response = RestClient.post(url, data.to_json, headers)
93
- JSON.parse(response.body)['choices'][0]['text']
94
- end
95
-
96
- def generate_answers(prompt, documents, params = {})
97
- model = params[:model] || 'text-davinci-002'
98
- max_tokens = params[:max_tokens] || 5
99
-
100
- url = "#{@endpoint}/engines/#{model}/answers"
101
- headers = {
102
- 'Content-Type' => 'application/json',
103
- 'Authorization' => "Bearer #{@api_key}"
104
- }
105
- data = {
106
- prompt: prompt,
107
- documents: documents,
108
- max_tokens: max_tokens
109
- }
110
- response = RestClient.post(url, data.to_json, headers)
111
- JSON.parse(response.body)['data'][0]['answer']
55
+ private
56
+ # Make a request to the API
57
+ #
58
+ # @param url [String] The URL for the request
59
+ # @param data [Hash] The data to be sent in the request
60
+ # @param method [Symbol] The HTTP method for the request (:post by default)
61
+ #
62
+ # @return [Hash] The response from the API
63
+ #
64
+ # @raise [RestClient::ExceptionWithResponse] If the API request fails
65
+ def request_api(url, data, method = :post)
66
+ begin
67
+ # Execute the request
68
+ response = RestClient::Request.execute(method: method, url: url, payload: data.to_json, headers: headers)
69
+
70
+ # Parse and return the response body
71
+ JSON.parse(response.body)
72
+ rescue RestClient::ExceptionWithResponse => e
73
+ # Parse the error message from the API response
74
+ error_msg = JSON.parse(e.response.body)['error']['message']
75
+
76
+ # Raise an exception with the API error message
77
+ raise RestClient::ExceptionWithResponse.new("#{e.message}: #{error_msg} (#{e.http_code})"), nil, e.backtrace
78
+ end
112
79
  end
80
+
113
81
  end
114
82
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Chatgpt
4
4
  module Ruby
5
- VERSION = "0.6.0"
5
+ VERSION = "1.1.0"
6
6
  end
7
7
  end
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.6.0
4
+ version: 1.1.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-03-22 00:00:00.000000000 Z
11
+ date: 2023-06-03 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