chatgpt-ruby 1.0.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 +4 -4
- data/CHANGELOG.md +9 -1
- data/Gemfile.lock +1 -1
- data/README.md +3 -100
- data/lib/chatgpt/client.rb +37 -74
- data/lib/chatgpt/ruby/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ab25c3c1b76e0b3249dbeba1c4b0772c4c6f7d34580005f3888ff09c2829453
|
4
|
+
data.tar.gz: 3feffdb2bed24d6464bdd945b6a94abb1a752567fac19d2866b5430dfffda019
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d42c42272c0f17cecc040f2796f08181cc858b744a5fbf33d6a26c31aa8990352b6f022832a6b9b61d25cf9b8577939783a93ce6e250a3c4458dfec16d4d43f1
|
7
|
+
data.tar.gz: f9a3cd15ff2901c09361ba53dd71d3ea7263669069126672b390444b11a8d660805cb34a7ea71b792afd5208c6971c3d374d58e3bb74fe2cccbc4af3a23baa5d
|
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.lock
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
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
|
-
The `chatgpt-ruby` is a Ruby SDK for the OpenAI API,
|
5
|
+
The `chatgpt-ruby` is a Ruby SDK for the OpenAI API, providing methods for generating text and completing prompts using the ChatGPT model.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -12,16 +12,12 @@ Add this line to your application's Gemfile:
|
|
12
12
|
gem 'chatgpt-ruby'
|
13
13
|
```
|
14
14
|
|
15
|
-
|
16
|
-
|
17
15
|
And then execute:
|
18
16
|
|
19
17
|
```ruby
|
20
18
|
$ bundle install
|
21
19
|
```
|
22
20
|
|
23
|
-
|
24
|
-
|
25
21
|
Or install it yourself as:
|
26
22
|
|
27
23
|
```ruby
|
@@ -41,8 +37,7 @@ api_key = 'your-api-key'
|
|
41
37
|
client = ChatGPT::Client.new(api_key)
|
42
38
|
```
|
43
39
|
|
44
|
-
|
45
|
-
### Completions
|
40
|
+
## Completions
|
46
41
|
|
47
42
|
To generate completions given a prompt, you can use the `completions` method:
|
48
43
|
|
@@ -53,8 +48,6 @@ completions = client.completions(prompt)
|
|
53
48
|
# Output: an array of completion strings
|
54
49
|
```
|
55
50
|
|
56
|
-
|
57
|
-
|
58
51
|
You can customize the generation process by passing in additional parameters as a hash:
|
59
52
|
|
60
53
|
```ruby
|
@@ -65,100 +58,10 @@ params = {
|
|
65
58
|
}
|
66
59
|
completions = client.completions(prompt, params)
|
67
60
|
|
61
|
+
puts completions["choices"].map { |c| c["text"] }
|
68
62
|
# Output: an array of completion strings
|
69
63
|
```
|
70
64
|
|
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
|
-
```
|
97
|
-
|
98
|
-
|
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
|
-
```
|
161
|
-
|
162
65
|
## Development
|
163
66
|
|
164
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.
|
data/lib/chatgpt/client.rb
CHANGED
@@ -1,16 +1,20 @@
|
|
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
|
|
13
|
-
#
|
15
|
+
# Prepare headers for the API request
|
16
|
+
#
|
17
|
+
# @return [Hash] The headers for the API request
|
14
18
|
def headers
|
15
19
|
{
|
16
20
|
'Content-Type' => 'application/json',
|
@@ -18,15 +22,24 @@ module ChatGPT
|
|
18
22
|
}
|
19
23
|
end
|
20
24
|
|
21
|
-
#
|
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
|
22
31
|
def completions(prompt, params = {})
|
32
|
+
# Set default parameters
|
23
33
|
engine = params[:engine] || 'text-davinci-002'
|
24
34
|
max_tokens = params[:max_tokens] || 16
|
25
35
|
temperature = params[:temperature] || 0.5
|
26
36
|
top_p = params[:top_p] || 1.0
|
27
37
|
n = params[:n] || 1
|
28
38
|
|
39
|
+
# Construct the URL for the completion request
|
29
40
|
url = "#{@endpoint}/engines/#{engine}/completions"
|
41
|
+
|
42
|
+
# Prepare the data for the request
|
30
43
|
data = {
|
31
44
|
prompt: prompt,
|
32
45
|
max_tokens: max_tokens,
|
@@ -34,86 +47,36 @@ module ChatGPT
|
|
34
47
|
top_p: top_p,
|
35
48
|
n: n
|
36
49
|
}
|
50
|
+
|
51
|
+
# Make the request to the API
|
37
52
|
request_api(url, data)
|
38
53
|
end
|
39
54
|
|
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 = {
|
98
|
-
prompt: prompt,
|
99
|
-
documents: documents,
|
100
|
-
max_tokens: max_tokens
|
101
|
-
}
|
102
|
-
response = request_api(url, data)
|
103
|
-
response['data'][0]['answer']
|
104
|
-
end
|
105
|
-
|
106
55
|
private
|
107
|
-
|
108
|
-
#
|
109
|
-
|
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)
|
110
66
|
begin
|
111
|
-
|
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
|
112
71
|
JSON.parse(response.body)
|
113
72
|
rescue RestClient::ExceptionWithResponse => e
|
73
|
+
# Parse the error message from the API response
|
114
74
|
error_msg = JSON.parse(e.response.body)['error']['message']
|
75
|
+
|
76
|
+
# Raise an exception with the API error message
|
115
77
|
raise RestClient::ExceptionWithResponse.new("#{e.message}: #{error_msg} (#{e.http_code})"), nil, e.backtrace
|
116
78
|
end
|
117
79
|
end
|
80
|
+
|
118
81
|
end
|
119
82
|
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: 1.
|
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-
|
11
|
+
date: 2023-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|