chatgpt-ruby 2.1.0 → 2.1.1
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 +85 -139
- data/lib/chatgpt/client.rb +2 -2
- data/lib/chatgpt/railtie.rb +11 -0
- data/lib/chatgpt/version.rb +1 -1
- data/lib/chatgpt.rb +3 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 538f8eab513f3dba9966108690eb3e744d6381cf401b656c7c5780e3e141b2d4
|
4
|
+
data.tar.gz: 2ba9e1b0a888d03419f40efcef0c4e85aa88d6b8b1a9d7986e6b86475e1defe6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 02ebe81d6af9b9fd3a1b2cfa03508e93917d764d5e873224575ee7d3fc986768c853027c9b1af8a7a6a2280b51a9531792a846b570b7e4eb3a1e114f29b3f32c
|
7
|
+
data.tar.gz: 074ba1aade1f0c6f20afa93e371b411c919afb7fceb38ce5ea898fd4efa5edb5206a0ca09867e803a72aa244c437c92c8562a1359258a5413723c75a159eee34
|
data/README.md
CHANGED
@@ -1,27 +1,20 @@
|
|
1
1
|
# ChatGPT Ruby
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
<a href="https://badge.fury.io/rb/chatgpt-ruby"><img src="https://img.shields.io/gem/v/chatgpt-ruby?style=for-the-badge" alt="Gem Version"></a>
|
4
|
+
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-yellow?style=for-the-badge" alt="License"></a>
|
5
|
+
<a href="https://codeclimate.com/github/nagstler/chatgpt-ruby/test_coverage"><img src="https://img.shields.io/codeclimate/coverage/nagstler/chatgpt-ruby?style=for-the-badge" alt="Test Coverage"></a>
|
6
|
+
<a href="https://github.com/nagstler/chatgpt-ruby/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/nagstler/chatgpt-ruby/ci.yml?branch=main&style=for-the-badge" alt="CI"></a>
|
7
|
+
<a href="https://github.com/nagstler/chatgpt-ruby/stargazers"><img src="https://img.shields.io/github/stars/nagstler/chatgpt-ruby?style=for-the-badge" alt="GitHub stars"></a>
|
8
8
|
|
9
|
-
🤖💎 A
|
10
|
-
|
11
|
-
📚 [Check out the Integration Guide](https://github.com/nagstler/chatgpt-ruby/wiki) to get started!
|
9
|
+
🤖💎 A lightweight Ruby wrapper for the OpenAI API, designed for simplicity and ease of integration.
|
12
10
|
|
13
11
|
## Features
|
14
12
|
|
15
|
-
-
|
16
|
-
-
|
17
|
-
-
|
18
|
-
-
|
19
|
-
-
|
20
|
-
- 📊 Token counting and validation
|
21
|
-
- ⚡ Async operations support
|
22
|
-
- 🛡️ Built-in rate limiting and retries
|
23
|
-
- 🎯 Type-safe responses
|
24
|
-
- 📝 Comprehensive logging
|
13
|
+
- API integration for chat completions and text completions
|
14
|
+
- Streaming capability for handling real-time response chunks
|
15
|
+
- Custom exception classes for different API error types
|
16
|
+
- Configurable timeout, retries and default parameters
|
17
|
+
- Complete test suite with mocked API responses
|
25
18
|
|
26
19
|
## Table of Contents
|
27
20
|
|
@@ -29,17 +22,12 @@
|
|
29
22
|
- [Installation](#installation)
|
30
23
|
- [Quick Start](#quick-start)
|
31
24
|
- [Configuration](#configuration)
|
32
|
-
- [
|
25
|
+
- [Rails Integration](#rails-integration)
|
26
|
+
- [Error Handling](#error-handling)
|
27
|
+
- [Current Capabilities](#current-capabilities)
|
33
28
|
- [Chat Completions](#chat-completions)
|
34
|
-
- [
|
35
|
-
|
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)
|
29
|
+
- [Text Completions](#text-completions)
|
30
|
+
- [Roadmap](#roadmap)
|
43
31
|
- [Development](#development)
|
44
32
|
- [Contributing](#contributing)
|
45
33
|
- [License](#license)
|
@@ -79,155 +67,113 @@ puts response.dig("choices", 0, "text")
|
|
79
67
|
|
80
68
|
```
|
81
69
|
|
82
|
-
##
|
70
|
+
## Rails Integration
|
71
|
+
|
72
|
+
In a Rails application, create an initializer:
|
83
73
|
|
84
74
|
```ruby
|
75
|
+
# config/initializers/chat_gpt.rb
|
76
|
+
require 'chatgpt'
|
77
|
+
|
85
78
|
ChatGPT.configure do |config|
|
86
|
-
config.api_key =
|
87
|
-
config.api_version = 'v1'
|
79
|
+
config.api_key = Rails.application.credentials.openai[:api_key]
|
88
80
|
config.default_engine = 'gpt-3.5-turbo'
|
89
81
|
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
82
|
end
|
98
83
|
```
|
99
|
-
|
100
|
-
## Core Features
|
101
|
-
|
102
|
-
### Chat Completions
|
84
|
+
Then use it in your controllers or services:
|
103
85
|
|
104
86
|
```ruby
|
105
|
-
#
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
87
|
+
# app/services/chat_gpt_service.rb
|
88
|
+
class ChatGPTService
|
89
|
+
def initialize
|
90
|
+
@client = ChatGPT::Client.new
|
91
|
+
end
|
92
|
+
|
93
|
+
def ask_question(question)
|
94
|
+
response = @client.chat([
|
95
|
+
{ role: "user", content: question }
|
96
|
+
])
|
97
|
+
|
98
|
+
response.dig("choices", 0, "message", "content")
|
99
|
+
end
|
100
|
+
end
|
110
101
|
|
111
|
-
#
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
print chunk.dig("choices", 0, "delta", "content")
|
102
|
+
# Usage in controller
|
103
|
+
def show
|
104
|
+
service = ChatGPTService.new
|
105
|
+
@response = service.ask_question("Tell me about Ruby on Rails")
|
116
106
|
end
|
117
107
|
```
|
118
108
|
|
119
|
-
|
109
|
+
## Configuration
|
120
110
|
|
121
111
|
```ruby
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
112
|
+
ChatGPT.configure do |config|
|
113
|
+
config.api_key = ENV['OPENAI_API_KEY']
|
114
|
+
config.api_version = 'v1'
|
115
|
+
config.default_engine = 'gpt-3.5-turbo'
|
116
|
+
config.request_timeout = 30
|
117
|
+
config.max_retries = 3
|
118
|
+
config.default_parameters = {
|
119
|
+
max_tokens: 16,
|
120
|
+
temperature: 0.5,
|
121
|
+
top_p: 1.0,
|
122
|
+
n: 1
|
133
123
|
}
|
134
|
-
|
135
|
-
|
136
|
-
response = client.chat(
|
137
|
-
messages: [{ role: "user", content: "What's the weather in London?" }],
|
138
|
-
functions: functions,
|
139
|
-
function_call: "auto"
|
140
|
-
)
|
141
|
-
```
|
142
|
-
|
143
|
-
### Image Generation (DALL-E)
|
144
|
-
|
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
|
161
|
-
|
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
|
-
)
|
168
|
-
|
169
|
-
# List fine-tuning jobs
|
170
|
-
jobs = client.fine_tunes.list
|
171
|
-
|
172
|
-
# Get job status
|
173
|
-
status = client.fine_tunes.retrieve(job.id)
|
174
|
-
```
|
175
|
-
|
176
|
-
### Token Management
|
177
|
-
|
178
|
-
```ruby
|
179
|
-
# Count tokens
|
180
|
-
count = client.tokens.count("Your text here", model: "gpt-4")
|
181
|
-
|
182
|
-
# Validate token limits
|
183
|
-
client.tokens.validate_messages(messages, max_tokens: 4000)
|
124
|
+
end
|
184
125
|
```
|
185
126
|
|
186
|
-
|
127
|
+
## Error handling
|
187
128
|
|
188
129
|
```ruby
|
189
130
|
begin
|
190
|
-
response = client.chat(
|
131
|
+
response = client.chat([
|
132
|
+
{ role: "user", content: "Hello!" }
|
133
|
+
])
|
134
|
+
rescue ChatGPT::AuthenticationError => e
|
135
|
+
puts "Authentication error: #{e.message}"
|
191
136
|
rescue ChatGPT::RateLimitError => e
|
192
137
|
puts "Rate limit hit: #{e.message}"
|
138
|
+
rescue ChatGPT::InvalidRequestError => e
|
139
|
+
puts "Bad request: #{e.message}"
|
193
140
|
rescue ChatGPT::APIError => e
|
194
141
|
puts "API error: #{e.message}"
|
195
|
-
rescue ChatGPT::TokenLimitError => e
|
196
|
-
puts "Token limit exceeded: #{e.message}"
|
197
142
|
end
|
198
143
|
```
|
199
144
|
|
200
|
-
##
|
201
|
-
|
202
|
-
### Async Operations
|
145
|
+
## Current Capabilities
|
203
146
|
|
147
|
+
### Chat Completions
|
204
148
|
```ruby
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
149
|
+
# Basic chat
|
150
|
+
response = client.chat([
|
151
|
+
{ role: "user", content: "What is Ruby?" }
|
152
|
+
])
|
153
|
+
|
154
|
+
# With streaming
|
155
|
+
client.chat_stream([{ role: "user", content: "Tell me a story" }]) do |chunk|
|
156
|
+
print chunk.dig("choices", 0, "delta", "content")
|
209
157
|
end
|
210
158
|
```
|
211
159
|
|
212
|
-
###
|
213
|
-
|
160
|
+
### Text Completions
|
214
161
|
```ruby
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
end
|
162
|
+
# Basic completion with GPT-3.5-turbo-instruct
|
163
|
+
response = client.completions("What is Ruby?")
|
164
|
+
puts response.dig("choices", 0, "text")
|
219
165
|
```
|
220
166
|
|
221
|
-
|
167
|
+
## Roadmap
|
222
168
|
|
223
|
-
|
224
|
-
response = client.chat(messages: [...])
|
169
|
+
While ChatGPT Ruby is functional, there are several areas planned for improvement:
|
225
170
|
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
171
|
+
- [ ] Response object wrapper & Rails integration with Railtie (v2.2)
|
172
|
+
- [ ] Token counting, function calling, and rate limiting (v2.3)
|
173
|
+
- [ ] Batch operations and async support (v3.0)
|
174
|
+
- [ ] DALL-E image generation and fine-tuning (Future)
|
175
|
+
|
176
|
+
❤️ Contributions in any of these areas are welcome!
|
231
177
|
|
232
178
|
## Development
|
233
179
|
|
data/lib/chatgpt/client.rb
CHANGED
@@ -31,7 +31,7 @@ module ChatGPT
|
|
31
31
|
url = "#{@endpoint}/chat/completions"
|
32
32
|
|
33
33
|
data = @config.default_parameters.merge(
|
34
|
-
model: params[:model] ||
|
34
|
+
model: params[:model] || @config.default_engine, # Use configured default_engine
|
35
35
|
messages: messages,
|
36
36
|
temperature: params[:temperature],
|
37
37
|
top_p: params[:top_p],
|
@@ -47,7 +47,7 @@ module ChatGPT
|
|
47
47
|
|
48
48
|
url = "#{@endpoint}/chat/completions"
|
49
49
|
data = @config.default_parameters.merge(
|
50
|
-
model: params[:model] ||
|
50
|
+
model: params[:model] || @config.default_engine, # Use configured default_engine
|
51
51
|
messages: messages,
|
52
52
|
stream: true
|
53
53
|
).compact
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# lib/chatgpt/railtie.rb
|
2
|
+
module ChatGPT
|
3
|
+
class Railtie < Rails::Railtie
|
4
|
+
initializer "chatgpt.configure_rails_initialization" do
|
5
|
+
# Register inflection for Rails apps
|
6
|
+
ActiveSupport::Inflector.inflections(:en) do |inflect|
|
7
|
+
inflect.acronym 'GPT'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/lib/chatgpt/version.rb
CHANGED
data/lib/chatgpt.rb
CHANGED
@@ -6,6 +6,9 @@ require_relative 'chatgpt/errors'
|
|
6
6
|
require_relative 'chatgpt/configuration'
|
7
7
|
require_relative 'chatgpt/client'
|
8
8
|
|
9
|
+
# Automatically load Railtie when used with Rails
|
10
|
+
require_relative 'chatgpt/railtie' if defined?(Rails)
|
11
|
+
|
9
12
|
module ChatGPT
|
10
13
|
class << self
|
11
14
|
def configuration
|
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: 2.1.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nagendra Dhanakeerthi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -149,6 +149,7 @@ files:
|
|
149
149
|
- lib/chatgpt/client.rb
|
150
150
|
- lib/chatgpt/configuration.rb
|
151
151
|
- lib/chatgpt/errors.rb
|
152
|
+
- lib/chatgpt/railtie.rb
|
152
153
|
- lib/chatgpt/version.rb
|
153
154
|
homepage: https://github.com/nagstler/chatgpt-ruby
|
154
155
|
licenses:
|
@@ -173,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
173
174
|
- !ruby/object:Gem::Version
|
174
175
|
version: '0'
|
175
176
|
requirements: []
|
176
|
-
rubygems_version: 3.
|
177
|
+
rubygems_version: 3.5.2
|
177
178
|
signing_key:
|
178
179
|
specification_version: 4
|
179
180
|
summary: Ruby client for OpenAI's ChatGPT API
|