gpt 0.0.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 +7 -0
- data/CHANGELOG.md +6 -0
- data/LICENSE +21 -0
- data/README.md +43 -0
- data/lib/gpt/client.rb +121 -0
- data/lib/gpt/error.rb +12 -0
- data/lib/gpt/responses.rb +48 -0
- data/lib/gpt.rb +19 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 63a792b425c4cbce7e97eea48da74efc7d1fab57bb88f8623c683b19d567a55c
|
4
|
+
data.tar.gz: d359d30ae5e9d184330a7922d39416ab80de88a03871a5f6009d2e9d9c65b562
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 87a5832356464d22b9e0b10a346e8e441593ca6a54ba81a76f135b48cb94fe01951fe89954bcff891eba3d2bfae5a0755c99a974900b90fb4615dfe0459ae410
|
7
|
+
data.tar.gz: 4ad05661b2149c090d5e2546d3952b9410cd9d6c0332cca32cfed5499b21972a0953a153768ebff59ddc66458b1122cadf2312ce45fff6a292c0d7b5a43a6da6
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Gedean Dias
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# gpt
|
2
|
+
|
3
|
+
Cliente Ruby simples para a API Responses.
|
4
|
+
|
5
|
+
Instalação
|
6
|
+
```
|
7
|
+
gem build gpt.gemspec && gem install ./gpt-0.0.1.gem
|
8
|
+
```
|
9
|
+
|
10
|
+
Uso básico
|
11
|
+
```ruby
|
12
|
+
require 'gpt'
|
13
|
+
|
14
|
+
client = GPT.client
|
15
|
+
|
16
|
+
res = client.responses.create({
|
17
|
+
'model' => 'gpt-4.1',
|
18
|
+
'input' => 'Diga olá em uma frase.'
|
19
|
+
})
|
20
|
+
|
21
|
+
puts res['id']
|
22
|
+
```
|
23
|
+
|
24
|
+
Streaming SSE
|
25
|
+
```ruby
|
26
|
+
require 'gpt'
|
27
|
+
|
28
|
+
GPT.responses.stream({
|
29
|
+
'model' => 'gpt-4.1',
|
30
|
+
'input' => 'Conte uma história curta.'
|
31
|
+
}) do |chunk|
|
32
|
+
print chunk
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
Outras operações
|
37
|
+
```ruby
|
38
|
+
id = res['id']
|
39
|
+
GPT.responses.get(id)
|
40
|
+
GPT.responses.input_items(id)
|
41
|
+
GPT.responses.cancel(id)
|
42
|
+
GPT.responses.delete(id)
|
43
|
+
```
|
data/lib/gpt/client.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
module GPT
|
2
|
+
class Client
|
3
|
+
DEFAULT_BASE_URL = 'https://api.openai.com'.freeze
|
4
|
+
DEFAULT_TIMEOUT = 120
|
5
|
+
|
6
|
+
attr_reader :api_key, :base_url, :timeout, :organization, :project
|
7
|
+
|
8
|
+
def initialize(api_key: ENV['OPENAI_API_KEY'], base_url: nil, timeout: nil, organization: ENV['OPENAI_ORG_ID'], project: ENV['OPENAI_PROJECT_ID'])
|
9
|
+
@api_key = api_key
|
10
|
+
@base_url = base_url || DEFAULT_BASE_URL
|
11
|
+
@timeout = (timeout || DEFAULT_TIMEOUT).to_i
|
12
|
+
@organization = organization
|
13
|
+
@project = project
|
14
|
+
end
|
15
|
+
|
16
|
+
def json_get(path, query: nil)
|
17
|
+
request(:get, path, query: query)
|
18
|
+
end
|
19
|
+
|
20
|
+
def json_post(path, body: nil)
|
21
|
+
request(:post, path, body: body)
|
22
|
+
end
|
23
|
+
|
24
|
+
def json_delete(path)
|
25
|
+
request(:delete, path)
|
26
|
+
end
|
27
|
+
|
28
|
+
def sse_stream(path, body: nil, query: nil)
|
29
|
+
uri = build_uri(path, query: query)
|
30
|
+
http = build_http(uri)
|
31
|
+
req = Net::HTTP::Post.new(uri)
|
32
|
+
apply_headers(req)
|
33
|
+
req['Accept'] = 'text/event-stream'
|
34
|
+
req.content_type = 'application/json'
|
35
|
+
req.body = body ? Oj.dump(body) : nil
|
36
|
+
|
37
|
+
http.request(req) do |res|
|
38
|
+
raise_http_error(res) unless res.is_a?(Net::HTTPSuccess)
|
39
|
+
res.read_body do |chunk|
|
40
|
+
yield chunk if block_given?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
true
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def request(method, path, body: nil, query: nil)
|
49
|
+
uri = build_uri(path, query: query)
|
50
|
+
http = build_http(uri)
|
51
|
+
req = build_request(method, uri)
|
52
|
+
apply_headers(req)
|
53
|
+
if body
|
54
|
+
req.content_type = 'application/json'
|
55
|
+
req.body = Oj.dump(body)
|
56
|
+
end
|
57
|
+
res = http.request(req)
|
58
|
+
parse_response(res)
|
59
|
+
end
|
60
|
+
|
61
|
+
def build_uri(path, query: nil)
|
62
|
+
uri = URI.join(base_url, path)
|
63
|
+
if query && query.any?
|
64
|
+
uri.query = URI.encode_www_form(query)
|
65
|
+
end
|
66
|
+
uri
|
67
|
+
end
|
68
|
+
|
69
|
+
def build_http(uri)
|
70
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
71
|
+
http.use_ssl = uri.scheme == 'https'
|
72
|
+
http.read_timeout = timeout
|
73
|
+
http.open_timeout = timeout
|
74
|
+
http.write_timeout = timeout
|
75
|
+
http
|
76
|
+
end
|
77
|
+
|
78
|
+
def build_request(method, uri)
|
79
|
+
case method
|
80
|
+
when :get then Net::HTTP::Get.new(uri)
|
81
|
+
when :post then Net::HTTP::Post.new(uri)
|
82
|
+
when :delete then Net::HTTP::Delete.new(uri)
|
83
|
+
else
|
84
|
+
raise ArgumentError, 'unsupported method'
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def apply_headers(req)
|
89
|
+
req['Authorization'] = "Bearer #{api_key}"
|
90
|
+
req['OpenAI-Organization'] = organization if organization && !organization.empty?
|
91
|
+
req['OpenAI-Project'] = project if project && !project.empty?
|
92
|
+
req['User-Agent'] = 'gpt-ruby/0.0.1'
|
93
|
+
end
|
94
|
+
|
95
|
+
def parse_response(res)
|
96
|
+
body = res.body
|
97
|
+
if res['Content-Type']&.include?('application/json')
|
98
|
+
parsed = body && !body.empty? ? Oj.load(body) : nil
|
99
|
+
else
|
100
|
+
parsed = body
|
101
|
+
end
|
102
|
+
|
103
|
+
unless res.is_a?(Net::HTTPSuccess)
|
104
|
+
message = if parsed.is_a?(Hash) && parsed['error']
|
105
|
+
parsed['error']['message'] || parsed['error'].to_s
|
106
|
+
else
|
107
|
+
body.to_s
|
108
|
+
end
|
109
|
+
raise GPT::Error.new(message, status: res.code.to_i, headers: res.each_header.to_h, response: parsed)
|
110
|
+
end
|
111
|
+
|
112
|
+
parsed
|
113
|
+
end
|
114
|
+
|
115
|
+
def raise_http_error(res)
|
116
|
+
raise GPT::Error.new("HTTP #{res.code}", status: res.code.to_i, headers: res.each_header.to_h, response: res.body)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
|
data/lib/gpt/error.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module GPT
|
2
|
+
class Responses
|
3
|
+
def initialize(client)
|
4
|
+
@client = client
|
5
|
+
end
|
6
|
+
|
7
|
+
def create(payload)
|
8
|
+
@client.json_post('/v1/responses', body: payload)
|
9
|
+
end
|
10
|
+
|
11
|
+
def get(response_id, include: nil, include_obfuscation: nil, starting_after: nil, stream: nil)
|
12
|
+
query = {}
|
13
|
+
query['include[]'] = include if include
|
14
|
+
query['include_obfuscation'] = include_obfuscation unless include_obfuscation.nil?
|
15
|
+
query['starting_after'] = starting_after if starting_after
|
16
|
+
query['stream'] = stream unless stream.nil?
|
17
|
+
@client.json_get("/v1/responses/#{response_id}", query: query)
|
18
|
+
end
|
19
|
+
|
20
|
+
def delete(response_id)
|
21
|
+
@client.json_delete("/v1/responses/#{response_id}")
|
22
|
+
end
|
23
|
+
|
24
|
+
def cancel(response_id)
|
25
|
+
@client.json_post("/v1/responses/#{response_id}/cancel")
|
26
|
+
end
|
27
|
+
|
28
|
+
def input_items(response_id, after: nil, before: nil, include: nil, limit: nil, order: nil)
|
29
|
+
query = {}
|
30
|
+
query['after'] = after if after
|
31
|
+
query['before'] = before if before
|
32
|
+
query['include[]'] = include if include
|
33
|
+
query['limit'] = limit if limit
|
34
|
+
query['order'] = order if order
|
35
|
+
@client.json_get("/v1/responses/#{response_id}/input_items", query: query)
|
36
|
+
end
|
37
|
+
|
38
|
+
def stream(payload)
|
39
|
+
payload = payload.dup
|
40
|
+
payload['stream'] = true
|
41
|
+
@client.sse_stream('/v1/responses', body: payload) do |chunk|
|
42
|
+
yield chunk if block_given?
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
data/lib/gpt.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'oj'
|
2
|
+
require 'net/http'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
require_relative 'gpt/error'
|
6
|
+
require_relative 'gpt/client'
|
7
|
+
require_relative 'gpt/responses'
|
8
|
+
|
9
|
+
module GPT
|
10
|
+
def self.client
|
11
|
+
@client ||= Client.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.responses
|
15
|
+
@responses ||= Responses.new(client)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gpt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gedean Dias
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-08-14 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: oj
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '3'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '3'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '3'
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: webmock
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '3'
|
47
|
+
type: :development
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3'
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: rubocop
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1'
|
61
|
+
type: :development
|
62
|
+
prerelease: false
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '1'
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: simplecov
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0.22'
|
75
|
+
type: :development
|
76
|
+
prerelease: false
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0.22'
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: yard
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0.9'
|
89
|
+
type: :development
|
90
|
+
prerelease: false
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0.9'
|
96
|
+
description: Based on ruby-openai, adds some extra features for working with OpenAI
|
97
|
+
APIs
|
98
|
+
email: gedean.dias@gmail.com
|
99
|
+
executables: []
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files: []
|
102
|
+
files:
|
103
|
+
- CHANGELOG.md
|
104
|
+
- LICENSE
|
105
|
+
- README.md
|
106
|
+
- lib/gpt.rb
|
107
|
+
- lib/gpt/client.rb
|
108
|
+
- lib/gpt/error.rb
|
109
|
+
- lib/gpt/responses.rb
|
110
|
+
homepage: https://github.com/gedean/openaiext
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata: {}
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '3'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubygems_version: 3.7.1
|
129
|
+
specification_version: 4
|
130
|
+
summary: GPT >= 5
|
131
|
+
test_files: []
|