chatgpt-ruby 0.3.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -2
- data/Gemfile.lock +20 -8
- data/README.md +131 -16
- data/lib/chatgpt/client.rb +100 -31
- data/lib/chatgpt/ruby/version.rb +1 -1
- metadata +24 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7aa91296be7cab7d9f1f5f1b2b6e55fa784c4a8760fabebbaac3cf903268da1d
|
4
|
+
data.tar.gz: cd8a13397002dec2a38da0ad766ce3b9301ca3d6bc5efa2669b30851d3ebc4ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: adf9e9cdc0e6b3e834d86e388b2c6319a42292c302333cc4a039817c9b4136e1394e00dff859c9f2359674728bf550171a22d64387089accdf48aed2d023658f
|
7
|
+
data.tar.gz: f1d0932b4ad810440543fa9b538a52190f6355ef892356519373748eb002f94a4d3de35f234fbd120c0bcd959b1f609fd6f35bbd27cd2125009a0b84e11dd8bd
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,25 +1,35 @@
|
|
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)
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
domain_name (0.5.20190701)
|
12
|
+
unf (>= 0.0.5, < 1.0.0)
|
13
|
+
http-accept (1.7.0)
|
14
|
+
http-cookie (1.0.5)
|
15
|
+
domain_name (~> 0.5)
|
13
16
|
json (2.6.3)
|
14
|
-
|
17
|
+
mime-types (3.4.1)
|
18
|
+
mime-types-data (~> 3.2015)
|
19
|
+
mime-types-data (3.2023.0218.1)
|
15
20
|
minitest (5.18.0)
|
16
|
-
|
21
|
+
netrc (0.11.0)
|
17
22
|
parallel (1.22.1)
|
18
23
|
parser (3.2.1.1)
|
19
24
|
ast (~> 2.4.1)
|
20
25
|
rainbow (3.1.1)
|
21
26
|
rake (13.0.6)
|
22
27
|
regexp_parser (2.7.0)
|
28
|
+
rest-client (2.1.0)
|
29
|
+
http-accept (>= 1.7.0, < 2.0)
|
30
|
+
http-cookie (>= 1.0.2, < 2.0)
|
31
|
+
mime-types (>= 1.16, < 4.0)
|
32
|
+
netrc (~> 0.8)
|
23
33
|
rexml (3.2.5)
|
24
34
|
rubocop (1.48.1)
|
25
35
|
json (~> 2.3)
|
@@ -34,6 +44,9 @@ GEM
|
|
34
44
|
rubocop-ast (1.27.0)
|
35
45
|
parser (>= 3.2.1.0)
|
36
46
|
ruby-progressbar (1.13.0)
|
47
|
+
unf (0.1.4)
|
48
|
+
unf_ext
|
49
|
+
unf_ext (0.0.8.2)
|
37
50
|
unicode-display_width (2.4.2)
|
38
51
|
|
39
52
|
PLATFORMS
|
@@ -41,10 +54,9 @@ PLATFORMS
|
|
41
54
|
|
42
55
|
DEPENDENCIES
|
43
56
|
chatgpt-ruby!
|
44
|
-
httparty
|
45
|
-
json
|
46
57
|
minitest (~> 5.0)
|
47
58
|
rake (~> 13.0)
|
59
|
+
rest-client
|
48
60
|
rubocop (~> 1.21)
|
49
61
|
|
50
62
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -1,46 +1,161 @@
|
|
1
1
|
# ChatGPTRuby
|
2
2
|
|
3
|
-
The `chatgpt-ruby`
|
3
|
+
The `chatgpt-ruby` is a Ruby SDK for the OpenAI API, including methods for generating text, completing prompts, and more.
|
4
4
|
|
5
|
-
|
5
|
+
## Installation
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem
|
10
|
+
gem 'chatgpt-ruby'
|
11
11
|
```
|
12
12
|
|
13
|
+
|
14
|
+
|
13
15
|
And then execute:
|
14
16
|
|
17
|
+
```ruby
|
15
18
|
$ bundle install
|
19
|
+
```
|
20
|
+
|
16
21
|
|
17
|
-
### Gem install
|
18
22
|
|
19
|
-
Or install
|
23
|
+
Or install it yourself as:
|
20
24
|
|
25
|
+
```ruby
|
21
26
|
$ gem install chatgpt-ruby
|
27
|
+
```
|
22
28
|
|
23
29
|
## Usage
|
24
30
|
|
25
|
-
To use the
|
31
|
+
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/) .
|
32
|
+
|
33
|
+
Once you have an API key, you can create a new `ChatGPT::Client` instance with your API key:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'chatgpt_api'
|
37
|
+
|
38
|
+
api_key = 'your-api-key'
|
39
|
+
client = ChatGPT::Client.new(api_key)
|
40
|
+
```
|
41
|
+
|
42
|
+
|
43
|
+
### Completions
|
44
|
+
|
45
|
+
To generate completions given a prompt, you can use the `completions` method:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
prompt = 'Hello, my name is'
|
49
|
+
completions = client.completions(prompt)
|
50
|
+
|
51
|
+
# Output: an array of completion strings
|
52
|
+
```
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
You can customize the generation process by passing in additional parameters as a hash:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
params = {
|
60
|
+
engine: 'text-davinci-002',
|
61
|
+
max_tokens: 50,
|
62
|
+
temperature: 0.7
|
63
|
+
}
|
64
|
+
completions = client.completions(prompt, params)
|
65
|
+
|
66
|
+
# Output: an array of completion strings
|
67
|
+
```
|
68
|
+
|
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
|
+
|
26
86
|
```ruby
|
27
|
-
|
87
|
+
params = {
|
88
|
+
engine: 'ada',
|
89
|
+
max_rerank: 100
|
90
|
+
}
|
91
|
+
results = client.search(documents, query, params)
|
28
92
|
|
29
|
-
|
30
|
-
response = client.chat('Please translate the following English text to French: "Hello, how are you?"')
|
31
|
-
puts response
|
93
|
+
# Output: an array of search result objects
|
32
94
|
```
|
33
95
|
|
34
|
-
Replace 'your_api_key' with your actual ChatGPT API key.
|
35
96
|
|
36
|
-
###
|
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
|
+
|
37
108
|
|
38
|
-
The `chat` method accepts a mandatory `prompt` parameter and an optional `options` parameter.
|
39
109
|
|
40
|
-
|
110
|
+
You can customize the classification process by passing in additional parameters as a hash:
|
41
111
|
|
42
|
-
|
43
|
-
|
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
|
+
```
|
44
159
|
|
45
160
|
## Development
|
46
161
|
|
data/lib/chatgpt/client.rb
CHANGED
@@ -1,45 +1,114 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# lib/chatgpt/client.rb
|
2
|
+
|
3
|
+
require 'rest-client'
|
3
4
|
require 'json'
|
4
5
|
|
5
|
-
module
|
6
|
+
module ChatGPT
|
6
7
|
class Client
|
7
|
-
include HTTParty
|
8
|
-
base_uri 'https://api.openai.com/v1'
|
9
|
-
|
10
8
|
def initialize(api_key)
|
11
9
|
@api_key = api_key
|
12
|
-
@
|
13
|
-
|
14
|
-
|
10
|
+
@endpoint = 'https://api.openai.com/v1'
|
11
|
+
end
|
12
|
+
|
13
|
+
def completions(prompt, params = {})
|
14
|
+
engine = params[:engine] || 'text-davinci-002'
|
15
|
+
max_tokens = params[:max_tokens] || 16
|
16
|
+
temperature = params[:temperature] || 0.5
|
17
|
+
top_p = params[:top_p] || 1.0
|
18
|
+
n = params[:n] || 1
|
19
|
+
|
20
|
+
url = "#{@endpoint}/engines/#{engine}/completions"
|
21
|
+
headers = {
|
22
|
+
'Content-Type' => 'application/json',
|
23
|
+
'Authorization' => "Bearer #{@api_key}"
|
24
|
+
}
|
25
|
+
data = {
|
26
|
+
prompt: prompt,
|
27
|
+
max_tokens: max_tokens,
|
28
|
+
temperature: temperature,
|
29
|
+
top_p: top_p,
|
30
|
+
n: n
|
15
31
|
}
|
32
|
+
response = RestClient.post(url, data.to_json, headers)
|
33
|
+
JSON.parse(response.body)
|
16
34
|
end
|
17
35
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
'
|
25
|
-
'
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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']
|
33
52
|
end
|
34
53
|
|
35
|
-
|
54
|
+
def classify(text, params = {})
|
55
|
+
model = params[:model] || 'text-davinci-002'
|
36
56
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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']
|
43
112
|
end
|
44
113
|
end
|
45
114
|
end
|
data/lib/chatgpt/ruby/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chatgpt-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nagendra Dhanakeerthi
|
@@ -9,8 +9,23 @@ autorequire:
|
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
11
|
date: 2023-03-22 00:00:00.000000000 Z
|
12
|
-
dependencies:
|
13
|
-
|
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: []
|
@@ -29,10 +44,13 @@ files:
|
|
29
44
|
- lib/chatgpt/ruby.rb
|
30
45
|
- lib/chatgpt/ruby/version.rb
|
31
46
|
- sig/chatgpt/ruby.rbs
|
32
|
-
homepage:
|
47
|
+
homepage: https://github.com/nagstler/chatgpt-ruby.git
|
33
48
|
licenses:
|
34
49
|
- MIT
|
35
|
-
metadata:
|
50
|
+
metadata:
|
51
|
+
homepage_uri: https://github.com/nagstler/chatgpt-ruby.git
|
52
|
+
source_code_uri: https://github.com/nagstler/chatgpt-ruby.git
|
53
|
+
changelog_uri: https://github.com/nagstler/chatgpt-ruby/blob/main/CHANGELOG.md
|
36
54
|
post_install_message:
|
37
55
|
rdoc_options: []
|
38
56
|
require_paths:
|
@@ -51,5 +69,5 @@ requirements: []
|
|
51
69
|
rubygems_version: 3.3.26
|
52
70
|
signing_key:
|
53
71
|
specification_version: 4
|
54
|
-
summary:
|
72
|
+
summary: A Ruby SDK for the OpenAI API
|
55
73
|
test_files: []
|