chatgpt-ruby 2.0.0 → 2.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/README.md +196 -70
- data/lib/chatgpt/client.rb +99 -85
- data/lib/chatgpt/configuration.rb +22 -0
- data/lib/chatgpt/errors.rb +21 -0
- data/lib/chatgpt/version.rb +6 -0
- data/lib/chatgpt.rb +23 -0
- metadata +130 -24
- data/.codeclimate.yml +0 -8
- data/.rubocop.yml +0 -13
- data/CHANGELOG.md +0 -19
- data/CODE_OF_CONDUCT.md +0 -84
- data/Gemfile +0 -21
- data/Gemfile.lock +0 -72
- data/Rakefile +0 -19
- data/lib/chatgpt/ruby/version.rb +0 -7
- data/lib/chatgpt/ruby.rb +0 -10
- data/sig/chatgpt/ruby.rbs +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fb132d30e599a50c8d7f9772fec8fed575883f5cfd3d3f42f91f0b9db9d345e
|
4
|
+
data.tar.gz: 68790d6783d57b4cbfaae685e0f8e6048f531959852bc91df4b62a8e61712532
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9729ae2cea19ff37e8b7fe0c810bc4a2c05d273078aad4702effe989c8fa393690605114a59c4022b4e758aad5e0451fe6667b68ee688bfe8e6f5594a31574bb
|
7
|
+
data.tar.gz: 4d42361f982bb0448bc9cceba09646ef14fd83b03b1940e6fed650aaf695e677e2da5e3caefbec57253c9adf38d9fae4384c311fae5f5d1d8c4a764343769c51
|
data/README.md
CHANGED
@@ -1,131 +1,257 @@
|
|
1
1
|
# ChatGPT Ruby
|
2
2
|
|
3
|
-
[](https://badge.fury.io/rb/chatgpt-ruby)
|
4
|
-
|
5
|
-
|
3
|
+
[](https://badge.fury.io/rb/chatgpt-ruby)
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
5
|
+
[](https://codeclimate.com/github/nagstler/chatgpt-ruby/maintainability)
|
6
|
+
[](https://codeclimate.com/github/nagstler/chatgpt-ruby/test_coverage)
|
7
|
+
[](https://github.com/nagstler/chatgpt-ruby/actions/workflows/ci.yml)
|
8
|
+
|
9
|
+
🤖💎 A comprehensive Ruby SDK for OpenAI's GPT APIs, providing a robust, feature-rich interface for AI-powered applications.
|
10
|
+
|
11
|
+
📚 [Check out the Integration Guide](https://github.com/nagstler/chatgpt-ruby/wiki) to get started!
|
12
|
+
|
13
|
+
## Features
|
14
|
+
|
15
|
+
- 🚀 Full support for GPT-3.5-Turbo and GPT-4 models
|
16
|
+
- 📡 Streaming responses support
|
17
|
+
- 🔧 Function calling and JSON mode
|
18
|
+
- 🎨 DALL-E image generation
|
19
|
+
- 🔄 Fine-tuning capabilities
|
20
|
+
- 📊 Token counting and validation
|
21
|
+
- ⚡ Async operations support
|
22
|
+
- 🛡️ Built-in rate limiting and retries
|
23
|
+
- 🎯 Type-safe responses
|
24
|
+
- 📝 Comprehensive logging
|
25
|
+
|
26
|
+
## Table of Contents
|
27
|
+
|
28
|
+
- [Features](#features)
|
29
|
+
- [Installation](#installation)
|
30
|
+
- [Quick Start](#quick-start)
|
31
|
+
- [Configuration](#configuration)
|
32
|
+
- [Core Features](#core-features)
|
33
|
+
- [Chat Completions](#chat-completions)
|
34
|
+
- [Function Calling](#function-calling)
|
35
|
+
- [Image Generation (DALL-E)](#image-generation-dall-e)
|
36
|
+
- [Fine-tuning](#fine-tuning)
|
37
|
+
- [Token Management](#token-management)
|
38
|
+
- [Error Handling](#error-handling)
|
39
|
+
- [Advanced Usage](#advanced-usage)
|
40
|
+
- [Async Operations](#async-operations)
|
41
|
+
- [Batch Operations](#batch-operations)
|
42
|
+
- [Response Objects](#response-objects)
|
43
|
+
- [Development](#development)
|
44
|
+
- [Contributing](#contributing)
|
45
|
+
- [License](#license)
|
6
46
|
|
7
47
|
## Installation
|
8
48
|
|
9
|
-
Add
|
49
|
+
Add to your Gemfile:
|
10
50
|
|
11
51
|
```ruby
|
12
52
|
gem 'chatgpt-ruby'
|
13
53
|
```
|
14
54
|
|
15
|
-
|
55
|
+
Or install directly:
|
16
56
|
|
17
|
-
```
|
18
|
-
$
|
57
|
+
```bash
|
58
|
+
$ gem install chatgpt-ruby
|
19
59
|
```
|
20
60
|
|
21
|
-
|
61
|
+
## Quick Start
|
22
62
|
|
23
63
|
```ruby
|
24
|
-
|
25
|
-
```
|
64
|
+
require 'chatgpt'
|
26
65
|
|
27
|
-
|
66
|
+
# Initialize with API key
|
67
|
+
client = ChatGPT::Client.new(ENV['OPENAI_API_KEY'])
|
28
68
|
|
29
|
-
|
69
|
+
# Chat API (Recommended for GPT-3.5-turbo, GPT-4)
|
70
|
+
response = client.chat([
|
71
|
+
{ role: "user", content: "What is Ruby?" }
|
72
|
+
])
|
30
73
|
|
31
|
-
|
74
|
+
puts response.dig("choices", 0, "message", "content")
|
32
75
|
|
33
|
-
|
34
|
-
|
76
|
+
# Completions API (For GPT-3.5-turbo-instruct)
|
77
|
+
response = client.completions("What is Ruby?")
|
78
|
+
puts response.dig("choices", 0, "text")
|
35
79
|
|
36
|
-
api_key = 'your-api-key'
|
37
|
-
client = ChatGPT::Client.new(api_key)
|
38
80
|
```
|
39
81
|
|
40
|
-
##
|
41
|
-
|
42
|
-
To generate completions given a prompt, you can use the `completions` method:
|
82
|
+
## Configuration
|
43
83
|
|
44
84
|
```ruby
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
85
|
+
ChatGPT.configure do |config|
|
86
|
+
config.api_key = ENV['OPENAI_API_KEY']
|
87
|
+
config.api_version = 'v1'
|
88
|
+
config.default_engine = 'gpt-3.5-turbo'
|
89
|
+
config.request_timeout = 30
|
90
|
+
config.max_retries = 3
|
91
|
+
config.default_parameters = {
|
92
|
+
max_tokens: 16,
|
93
|
+
temperature: 0.5,
|
94
|
+
top_p: 1.0,
|
95
|
+
n: 1
|
96
|
+
}
|
97
|
+
end
|
49
98
|
```
|
50
99
|
|
51
|
-
|
100
|
+
## Core Features
|
101
|
+
|
102
|
+
### Chat Completions
|
52
103
|
|
53
104
|
```ruby
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
105
|
+
# Chat with system message
|
106
|
+
response = client.chat([
|
107
|
+
{ role: "system", content: "You are a helpful assistant." },
|
108
|
+
{ role: "user", content: "Hello!" }
|
109
|
+
])
|
110
|
+
|
111
|
+
# With streaming
|
112
|
+
client.chat_stream([
|
113
|
+
{ role: "user", content: "Tell me a story" }
|
114
|
+
]) do |chunk|
|
115
|
+
print chunk.dig("choices", 0, "delta", "content")
|
116
|
+
end
|
63
117
|
```
|
64
118
|
|
65
|
-
|
119
|
+
### Function Calling
|
66
120
|
|
67
|
-
|
121
|
+
```ruby
|
122
|
+
functions = [
|
123
|
+
{
|
124
|
+
name: "get_weather",
|
125
|
+
description: "Get current weather",
|
126
|
+
parameters: {
|
127
|
+
type: "object",
|
128
|
+
properties: {
|
129
|
+
location: { type: "string" },
|
130
|
+
unit: { type: "string", enum: ["celsius", "fahrenheit"] }
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
134
|
+
]
|
68
135
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
136
|
+
response = client.chat(
|
137
|
+
messages: [{ role: "user", content: "What's the weather in London?" }],
|
138
|
+
functions: functions,
|
139
|
+
function_call: "auto"
|
140
|
+
)
|
141
|
+
```
|
73
142
|
|
74
|
-
|
143
|
+
### Image Generation (DALL-E)
|
75
144
|
|
76
|
-
|
145
|
+
```ruby
|
146
|
+
# Generate image
|
147
|
+
image = client.images.generate(
|
148
|
+
prompt: "A sunset over mountains",
|
149
|
+
size: "1024x1024",
|
150
|
+
quality: "hd"
|
151
|
+
)
|
152
|
+
|
153
|
+
# Create variations
|
154
|
+
variation = client.images.create_variation(
|
155
|
+
image: File.read("input.png"),
|
156
|
+
n: 1
|
157
|
+
)
|
158
|
+
```
|
159
|
+
|
160
|
+
### Fine-tuning
|
77
161
|
|
78
162
|
```ruby
|
163
|
+
# Create fine-tuning job
|
164
|
+
job = client.fine_tunes.create(
|
165
|
+
training_file: "file-abc123",
|
166
|
+
model: "gpt-3.5-turbo"
|
167
|
+
)
|
79
168
|
|
80
|
-
#
|
81
|
-
|
82
|
-
{
|
83
|
-
role: "system",
|
84
|
-
content: "You are a helpful assistant."
|
85
|
-
},
|
86
|
-
{
|
87
|
-
role: "user",
|
88
|
-
content: "Who won the world series in 2020?"
|
89
|
-
}
|
90
|
-
]
|
169
|
+
# List fine-tuning jobs
|
170
|
+
jobs = client.fine_tunes.list
|
91
171
|
|
92
|
-
#
|
93
|
-
|
172
|
+
# Get job status
|
173
|
+
status = client.fine_tunes.retrieve(job.id)
|
94
174
|
```
|
95
175
|
|
96
|
-
|
176
|
+
### Token Management
|
97
177
|
|
98
178
|
```ruby
|
179
|
+
# Count tokens
|
180
|
+
count = client.tokens.count("Your text here", model: "gpt-4")
|
99
181
|
|
100
|
-
|
182
|
+
# Validate token limits
|
183
|
+
client.tokens.validate_messages(messages, max_tokens: 4000)
|
101
184
|
```
|
102
185
|
|
103
|
-
|
186
|
+
### Error Handling
|
104
187
|
|
105
188
|
```ruby
|
189
|
+
begin
|
190
|
+
response = client.chat(messages: [...])
|
191
|
+
rescue ChatGPT::RateLimitError => e
|
192
|
+
puts "Rate limit hit: #{e.message}"
|
193
|
+
rescue ChatGPT::APIError => e
|
194
|
+
puts "API error: #{e.message}"
|
195
|
+
rescue ChatGPT::TokenLimitError => e
|
196
|
+
puts "Token limit exceeded: #{e.message}"
|
197
|
+
end
|
198
|
+
```
|
199
|
+
|
200
|
+
## Advanced Usage
|
201
|
+
|
202
|
+
### Async Operations
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
client.async do
|
206
|
+
response1 = client.chat(messages: [...])
|
207
|
+
response2 = client.chat(messages: [...])
|
208
|
+
[response1, response2]
|
209
|
+
end
|
210
|
+
```
|
106
211
|
|
107
|
-
|
212
|
+
### Batch Operations
|
108
213
|
|
109
|
-
|
110
|
-
|
214
|
+
```ruby
|
215
|
+
responses = client.batch do |batch|
|
216
|
+
batch.add_chat(messages: [...])
|
217
|
+
batch.add_chat(messages: [...])
|
218
|
+
end
|
111
219
|
```
|
112
220
|
|
113
|
-
|
221
|
+
### Response Objects
|
222
|
+
|
223
|
+
```ruby
|
224
|
+
response = client.chat(messages: [...])
|
225
|
+
|
226
|
+
response.content # Main response content
|
227
|
+
response.usage # Token usage information
|
228
|
+
response.finish_reason # Why the response ended
|
229
|
+
response.model # Model used
|
230
|
+
```
|
114
231
|
|
115
232
|
## Development
|
116
233
|
|
117
|
-
|
234
|
+
```bash
|
235
|
+
# Run tests
|
236
|
+
bundle exec rake test
|
118
237
|
|
119
|
-
|
238
|
+
# Run linter
|
239
|
+
bundle exec rubocop
|
240
|
+
|
241
|
+
# Generate documentation
|
242
|
+
bundle exec yard doc
|
243
|
+
```
|
120
244
|
|
121
245
|
## Contributing
|
122
246
|
|
123
|
-
|
247
|
+
1. Fork it
|
248
|
+
2. Create your feature branch (`git checkout -b feature/my-new-feature`)
|
249
|
+
3. Add tests for your feature
|
250
|
+
4. Make your changes
|
251
|
+
5. Commit your changes (`git commit -am 'Add some feature'`)
|
252
|
+
6. Push to the branch (`git push origin feature/my-new-feature`)
|
253
|
+
7. Create a new Pull Request
|
124
254
|
|
125
255
|
## License
|
126
256
|
|
127
|
-
|
128
|
-
|
129
|
-
## Code of Conduct
|
130
|
-
|
131
|
-
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).
|
257
|
+
Released under the MIT License. See [LICENSE](LICENSE.txt) for details.
|
data/lib/chatgpt/client.rb
CHANGED
@@ -1,111 +1,125 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/chatgpt/client.rb
|
1
4
|
require 'rest-client'
|
2
5
|
require 'json'
|
3
6
|
|
4
7
|
module ChatGPT
|
5
8
|
class Client
|
6
|
-
|
7
|
-
|
8
|
-
# @param api_key [String] The API key for the GPT-3 service
|
9
|
-
def initialize(api_key)
|
10
|
-
@api_key = api_key
|
11
|
-
# Base endpoint for the OpenAI API
|
9
|
+
def initialize(api_key = nil)
|
10
|
+
@api_key = api_key || ChatGPT.configuration.api_key
|
12
11
|
@endpoint = 'https://api.openai.com/v1'
|
12
|
+
@config = ChatGPT.configuration
|
13
13
|
end
|
14
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
|
31
15
|
def completions(prompt, params = {})
|
32
|
-
|
33
|
-
engine = params[:engine] || 'text-davinci-002'
|
34
|
-
max_tokens = params[:max_tokens] || 16
|
35
|
-
temperature = params[:temperature] || 0.5
|
36
|
-
top_p = params[:top_p] || 1.0
|
37
|
-
n = params[:n] || 1
|
38
|
-
|
39
|
-
# Construct the URL for the completion request
|
16
|
+
engine = params[:engine] || @config.default_engine
|
40
17
|
url = "#{@endpoint}/engines/#{engine}/completions"
|
41
|
-
|
42
|
-
|
43
|
-
data = {
|
18
|
+
|
19
|
+
data = @config.default_parameters.merge(
|
44
20
|
prompt: prompt,
|
45
|
-
max_tokens: max_tokens,
|
46
|
-
temperature: temperature,
|
47
|
-
top_p: top_p,
|
48
|
-
n: n
|
49
|
-
|
50
|
-
|
51
|
-
# Make the request to the API
|
21
|
+
max_tokens: params[:max_tokens],
|
22
|
+
temperature: params[:temperature],
|
23
|
+
top_p: params[:top_p],
|
24
|
+
n: params[:n]
|
25
|
+
).compact
|
26
|
+
|
52
27
|
request_api(url, data)
|
53
28
|
end
|
54
29
|
|
55
|
-
|
56
|
-
# This method sends a chat message to the API
|
57
|
-
#
|
58
|
-
# @param messages [Array<Hash>] The array of messages for the conversation.
|
59
|
-
# Each message is a hash with a `role` and `content` key. The `role` key can be 'system', 'user', or 'assistant',
|
60
|
-
# and the `content` key contains the text of the message.
|
61
|
-
#
|
62
|
-
# @param params [Hash] Optional parameters for the chat request. This can include the 'model' key to specify
|
63
|
-
# the model to be used for the chat. If no 'model' key is provided, 'gpt-3.5-turbo' is used by default.
|
64
|
-
#
|
65
|
-
# @return [Hash] The response from the API.
|
66
30
|
def chat(messages, params = {})
|
67
|
-
# Set default parameters
|
68
|
-
model = params[:model] || 'gpt-3.5-turbo'
|
69
|
-
|
70
|
-
# Construct the URL for the chat request
|
71
31
|
url = "#{@endpoint}/chat/completions"
|
72
32
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
33
|
+
data = @config.default_parameters.merge(
|
34
|
+
model: params[:model] || 'gpt-3.5-turbo',
|
35
|
+
messages: messages,
|
36
|
+
temperature: params[:temperature],
|
37
|
+
top_p: params[:top_p],
|
38
|
+
n: params[:n],
|
39
|
+
stream: params[:stream] || false
|
40
|
+
).compact
|
41
|
+
|
80
42
|
request_api(url, data)
|
81
43
|
end
|
82
44
|
|
45
|
+
def chat_stream(messages, params = {}, &block)
|
46
|
+
raise ArgumentError, 'Block is required for streaming' unless block_given?
|
47
|
+
|
48
|
+
url = "#{@endpoint}/chat/completions"
|
49
|
+
data = @config.default_parameters.merge(
|
50
|
+
model: params[:model] || 'gpt-3.5-turbo',
|
51
|
+
messages: messages,
|
52
|
+
stream: true
|
53
|
+
).compact
|
54
|
+
|
55
|
+
request_streaming(url, data, &block)
|
56
|
+
end
|
83
57
|
|
84
58
|
private
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
59
|
+
|
60
|
+
def request_api(url, data)
|
61
|
+
response = RestClient::Request.execute(
|
62
|
+
method: :post,
|
63
|
+
url: url,
|
64
|
+
payload: data.to_json,
|
65
|
+
headers: {
|
66
|
+
'Authorization' => "Bearer #{@api_key}",
|
67
|
+
'Content-Type' => 'application/json'
|
68
|
+
},
|
69
|
+
timeout: @config.request_timeout
|
70
|
+
)
|
71
|
+
JSON.parse(response.body)
|
72
|
+
rescue RestClient::ExceptionWithResponse => e
|
73
|
+
handle_error(e)
|
74
|
+
end
|
75
|
+
|
76
|
+
def handle_error(error)
|
77
|
+
error_response = JSON.parse(error.response.body)
|
78
|
+
error_message = error_response['error']['message']
|
79
|
+
status_code = error.response.code
|
80
|
+
|
81
|
+
case status_code
|
82
|
+
when 401
|
83
|
+
raise ChatGPT::AuthenticationError.new(error_message, status_code)
|
84
|
+
when 429
|
85
|
+
raise ChatGPT::RateLimitError.new(error_message, status_code)
|
86
|
+
when 400
|
87
|
+
raise ChatGPT::InvalidRequestError.new(error_message, status_code)
|
88
|
+
else
|
89
|
+
raise ChatGPT::APIError.new(error_message, status_code)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def request_streaming(url, data)
|
94
|
+
RestClient::Request.execute( # Remove the response = assignment
|
95
|
+
method: :post,
|
96
|
+
url: url,
|
97
|
+
payload: data.to_json,
|
98
|
+
headers: {
|
99
|
+
'Authorization' => "Bearer #{@api_key}",
|
100
|
+
'Content-Type' => 'application/json'
|
101
|
+
},
|
102
|
+
timeout: @config.request_timeout,
|
103
|
+
stream_to_buffer: true
|
104
|
+
) do |chunk, _x, _z|
|
105
|
+
if chunk.include?('data: ')
|
106
|
+
chunk.split("\n").each do |line|
|
107
|
+
next unless line.start_with?('data: ')
|
108
|
+
|
109
|
+
data = line.sub(/^data: /, '')
|
110
|
+
next if data.strip == '[DONE]'
|
111
|
+
|
112
|
+
begin
|
113
|
+
parsed = JSON.parse(data)
|
114
|
+
yield parsed if block_given?
|
115
|
+
rescue JSON::ParserError
|
116
|
+
next
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
107
120
|
end
|
121
|
+
rescue RestClient::ExceptionWithResponse => e
|
122
|
+
handle_error(e)
|
108
123
|
end
|
109
|
-
|
110
124
|
end
|
111
125
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/chatgpt/configuration.rb
|
4
|
+
module ChatGPT
|
5
|
+
class Configuration
|
6
|
+
attr_accessor :api_key, :api_version, :default_engine,
|
7
|
+
:request_timeout, :max_retries, :default_parameters
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@api_version = 'v1'
|
11
|
+
@default_engine = 'text-davinci-002'
|
12
|
+
@request_timeout = 30
|
13
|
+
@max_retries = 3
|
14
|
+
@default_parameters = {
|
15
|
+
max_tokens: 16,
|
16
|
+
temperature: 0.5,
|
17
|
+
top_p: 1.0,
|
18
|
+
n: 1
|
19
|
+
}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/chatgpt/errors.rb
|
4
|
+
module ChatGPT
|
5
|
+
class Error < StandardError; end
|
6
|
+
|
7
|
+
class APIError < Error
|
8
|
+
attr_reader :status_code, :error_type
|
9
|
+
|
10
|
+
def initialize(message = nil, status_code = nil, error_type = nil)
|
11
|
+
@status_code = status_code
|
12
|
+
@error_type = error_type
|
13
|
+
super(message)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class AuthenticationError < APIError; end
|
18
|
+
class RateLimitError < APIError; end
|
19
|
+
class InvalidRequestError < APIError; end
|
20
|
+
class TokenLimitError < APIError; end
|
21
|
+
end
|
data/lib/chatgpt.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# lib/chatgpt.rb
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative 'chatgpt/version'
|
5
|
+
require_relative 'chatgpt/errors'
|
6
|
+
require_relative 'chatgpt/configuration'
|
7
|
+
require_relative 'chatgpt/client'
|
8
|
+
|
9
|
+
module ChatGPT
|
10
|
+
class << self
|
11
|
+
def configuration
|
12
|
+
@configuration ||= Configuration.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def configure
|
16
|
+
yield(configuration)
|
17
|
+
end
|
18
|
+
|
19
|
+
def reset_configuration!
|
20
|
+
@configuration = Configuration.new
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,57 +1,163 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chatgpt-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.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:
|
11
|
+
date: 2025-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
27
|
-
|
28
|
-
|
26
|
+
version: '2.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: brakeman
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.2'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler-audit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '13.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '13.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.21'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.21'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.21'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.21'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: simplecov_json_formatter
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.1'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.1'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: webmock
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '3.18'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '3.18'
|
139
|
+
description: A Ruby SDK for OpenAI's ChatGPT API
|
29
140
|
email:
|
30
141
|
- nagendra.dhanakeerthi@gmail.com
|
31
142
|
executables: []
|
32
143
|
extensions: []
|
33
144
|
extra_rdoc_files: []
|
34
145
|
files:
|
35
|
-
- ".codeclimate.yml"
|
36
|
-
- ".rubocop.yml"
|
37
|
-
- CHANGELOG.md
|
38
|
-
- CODE_OF_CONDUCT.md
|
39
|
-
- Gemfile
|
40
|
-
- Gemfile.lock
|
41
146
|
- LICENSE.txt
|
42
147
|
- README.md
|
43
|
-
-
|
148
|
+
- lib/chatgpt.rb
|
44
149
|
- lib/chatgpt/client.rb
|
45
|
-
- lib/chatgpt/
|
46
|
-
- lib/chatgpt/
|
47
|
-
-
|
48
|
-
homepage: https://github.com/nagstler/chatgpt-ruby
|
150
|
+
- lib/chatgpt/configuration.rb
|
151
|
+
- lib/chatgpt/errors.rb
|
152
|
+
- lib/chatgpt/version.rb
|
153
|
+
homepage: https://github.com/nagstler/chatgpt-ruby
|
49
154
|
licenses:
|
50
155
|
- MIT
|
51
156
|
metadata:
|
52
|
-
homepage_uri: https://github.com/nagstler/chatgpt-ruby
|
53
|
-
source_code_uri: https://github.com/nagstler/chatgpt-ruby
|
157
|
+
homepage_uri: https://github.com/nagstler/chatgpt-ruby
|
158
|
+
source_code_uri: https://github.com/nagstler/chatgpt-ruby
|
54
159
|
changelog_uri: https://github.com/nagstler/chatgpt-ruby/blob/main/CHANGELOG.md
|
160
|
+
rubygems_mfa_required: 'true'
|
55
161
|
post_install_message:
|
56
162
|
rdoc_options: []
|
57
163
|
require_paths:
|
@@ -60,15 +166,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
60
166
|
requirements:
|
61
167
|
- - ">="
|
62
168
|
- !ruby/object:Gem::Version
|
63
|
-
version:
|
169
|
+
version: 3.0.0
|
64
170
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
171
|
requirements:
|
66
172
|
- - ">="
|
67
173
|
- !ruby/object:Gem::Version
|
68
174
|
version: '0'
|
69
175
|
requirements: []
|
70
|
-
rubygems_version: 3.
|
176
|
+
rubygems_version: 3.4.19
|
71
177
|
signing_key:
|
72
178
|
specification_version: 4
|
73
|
-
summary:
|
179
|
+
summary: Ruby client for OpenAI's ChatGPT API
|
74
180
|
test_files: []
|
data/.codeclimate.yml
DELETED
data/.rubocop.yml
DELETED
data/CHANGELOG.md
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
## [2.0.0] - 2023-07-08
|
2
|
-
|
3
|
-
### Added
|
4
|
-
|
5
|
-
- Added support for chat interactions with the GPT-3.5-turbo model.
|
6
|
-
- Introduced a new `chat` method to the `ChatGPT::Client` class, which sends an array of chat messages to the API and receives the generated messages in return.
|
7
|
-
- Updated the README to provide usage instructions for the new `chat` method.
|
8
|
-
|
9
|
-
## [1.0.0] - 2023-04-30
|
10
|
-
|
11
|
-
### Added
|
12
|
-
|
13
|
-
- Initial release of the `chatgpt-ruby` gem.
|
14
|
-
- Implements ChatGPT Client with methods for completions, search, classification, summary, and answer generation.
|
15
|
-
- Includes unit tests for each method.
|
16
|
-
|
17
|
-
## [0.1.0] - 2023-03-22
|
18
|
-
|
19
|
-
- Initial release
|
data/CODE_OF_CONDUCT.md
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
# Contributor Covenant Code of Conduct
|
2
|
-
|
3
|
-
## Our Pledge
|
4
|
-
|
5
|
-
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
-
|
7
|
-
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
-
|
9
|
-
## Our Standards
|
10
|
-
|
11
|
-
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
-
|
13
|
-
* Demonstrating empathy and kindness toward other people
|
14
|
-
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
-
* Giving and gracefully accepting constructive feedback
|
16
|
-
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
-
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
-
|
19
|
-
Examples of unacceptable behavior include:
|
20
|
-
|
21
|
-
* The use of sexualized language or imagery, and sexual attention or
|
22
|
-
advances of any kind
|
23
|
-
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
-
* Public or private harassment
|
25
|
-
* Publishing others' private information, such as a physical or email
|
26
|
-
address, without their explicit permission
|
27
|
-
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
-
professional setting
|
29
|
-
|
30
|
-
## Enforcement Responsibilities
|
31
|
-
|
32
|
-
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
-
|
34
|
-
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
-
|
36
|
-
## Scope
|
37
|
-
|
38
|
-
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
-
|
40
|
-
## Enforcement
|
41
|
-
|
42
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at nagendra.dhanakeerthi@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
-
|
44
|
-
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
-
|
46
|
-
## Enforcement Guidelines
|
47
|
-
|
48
|
-
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
-
|
50
|
-
### 1. Correction
|
51
|
-
|
52
|
-
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
-
|
54
|
-
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
-
|
56
|
-
### 2. Warning
|
57
|
-
|
58
|
-
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
-
|
60
|
-
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
-
|
62
|
-
### 3. Temporary Ban
|
63
|
-
|
64
|
-
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
-
|
66
|
-
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
-
|
68
|
-
### 4. Permanent Ban
|
69
|
-
|
70
|
-
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
-
|
72
|
-
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
-
|
74
|
-
## Attribution
|
75
|
-
|
76
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
-
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
-
|
79
|
-
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
-
|
81
|
-
[homepage]: https://www.contributor-covenant.org
|
82
|
-
|
83
|
-
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
-
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
# Specify your gem's dependencies in chatgpt-ruby.gemspec
|
6
|
-
gemspec
|
7
|
-
|
8
|
-
gem "rake", "~> 13.0"
|
9
|
-
|
10
|
-
gem "minitest", "~> 5.0"
|
11
|
-
|
12
|
-
gem "rubocop", "~> 1.21"
|
13
|
-
|
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
DELETED
@@ -1,72 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
chatgpt-ruby (1.0.0)
|
5
|
-
rest-client
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: https://rubygems.org/
|
9
|
-
specs:
|
10
|
-
ast (2.4.2)
|
11
|
-
docile (1.4.0)
|
12
|
-
domain_name (0.5.20190701)
|
13
|
-
unf (>= 0.0.5, < 1.0.0)
|
14
|
-
http-accept (1.7.0)
|
15
|
-
http-cookie (1.0.5)
|
16
|
-
domain_name (~> 0.5)
|
17
|
-
json (2.6.3)
|
18
|
-
mime-types (3.4.1)
|
19
|
-
mime-types-data (~> 3.2015)
|
20
|
-
mime-types-data (3.2023.0218.1)
|
21
|
-
minitest (5.18.0)
|
22
|
-
netrc (0.11.0)
|
23
|
-
parallel (1.22.1)
|
24
|
-
parser (3.2.1.1)
|
25
|
-
ast (~> 2.4.1)
|
26
|
-
rainbow (3.1.1)
|
27
|
-
rake (13.0.6)
|
28
|
-
regexp_parser (2.7.0)
|
29
|
-
rest-client (2.1.0)
|
30
|
-
http-accept (>= 1.7.0, < 2.0)
|
31
|
-
http-cookie (>= 1.0.2, < 2.0)
|
32
|
-
mime-types (>= 1.16, < 4.0)
|
33
|
-
netrc (~> 0.8)
|
34
|
-
rexml (3.2.5)
|
35
|
-
rubocop (1.48.1)
|
36
|
-
json (~> 2.3)
|
37
|
-
parallel (~> 1.10)
|
38
|
-
parser (>= 3.2.0.0)
|
39
|
-
rainbow (>= 2.2.2, < 4.0)
|
40
|
-
regexp_parser (>= 1.8, < 3.0)
|
41
|
-
rexml (>= 3.2.5, < 4.0)
|
42
|
-
rubocop-ast (>= 1.26.0, < 2.0)
|
43
|
-
ruby-progressbar (~> 1.7)
|
44
|
-
unicode-display_width (>= 2.4.0, < 3.0)
|
45
|
-
rubocop-ast (1.27.0)
|
46
|
-
parser (>= 3.2.1.0)
|
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)
|
54
|
-
unf (0.1.4)
|
55
|
-
unf_ext
|
56
|
-
unf_ext (0.0.8.2)
|
57
|
-
unicode-display_width (2.4.2)
|
58
|
-
|
59
|
-
PLATFORMS
|
60
|
-
arm64-darwin-21
|
61
|
-
|
62
|
-
DEPENDENCIES
|
63
|
-
chatgpt-ruby!
|
64
|
-
minitest (~> 5.0)
|
65
|
-
rake (~> 13.0)
|
66
|
-
rest-client
|
67
|
-
rubocop (~> 1.21)
|
68
|
-
simplecov
|
69
|
-
simplecov_json_formatter
|
70
|
-
|
71
|
-
BUNDLED WITH
|
72
|
-
2.3.26
|
data/Rakefile
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "bundler/gem_tasks"
|
4
|
-
require "rake/testtask"
|
5
|
-
|
6
|
-
|
7
|
-
Rake::TestTask.new(:test) do |t|
|
8
|
-
t.libs << "test"
|
9
|
-
t.pattern = 'test/**/*_test.rb'
|
10
|
-
t.libs << "lib"
|
11
|
-
t.test_files = FileList["test/**/test_*.rb"]
|
12
|
-
end
|
13
|
-
|
14
|
-
require "rubocop/rake_task"
|
15
|
-
|
16
|
-
RuboCop::RakeTask.new
|
17
|
-
|
18
|
-
task default: %i[test rubocop]
|
19
|
-
|
data/lib/chatgpt/ruby/version.rb
DELETED
data/lib/chatgpt/ruby.rb
DELETED