chatgpt2023 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/chatgpt2023.rb +95 -16
- data.tar.gz.sig +0 -0
- metadata +23 -3
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4728e79c0d12f0dd5a5ce7a7970f507d6fd79471727fb205f7416b3326fe8985
|
4
|
+
data.tar.gz: 7ca136ca869d4908c9fa70827b4a2a549cff92633026c1c70e8843886c026b59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f403b18eec392d2b1958b1168536ad716f02ddcd7445a3c67fa46916193190ee920020a2902e63c00621135392f580a00e12948b6dfa56a40f5316dd42d591d2
|
7
|
+
data.tar.gz: bda567ca1b04f4e4350c475d62cc4576887c36d7762bae951dd918269f149419411eb76e309a6190ad5e34cea71db375dd55a2a0b6cddff86ab0495a74506faf
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/lib/chatgpt2023.rb
CHANGED
@@ -5,6 +5,7 @@
|
|
5
5
|
require 'net/http'
|
6
6
|
require 'uri'
|
7
7
|
require 'json'
|
8
|
+
require 'down'
|
8
9
|
|
9
10
|
|
10
11
|
# description: 1st experiment at playing with the ChatGPT API.
|
@@ -31,42 +32,120 @@ require 'json'
|
|
31
32
|
# chat.completion 'Say this is a test'
|
32
33
|
# #=> This is indeed a test
|
33
34
|
|
35
|
+
class ChatGpt2023Error < Exception
|
36
|
+
end
|
37
|
+
|
34
38
|
class ChatGpt2023
|
35
39
|
|
36
40
|
def initialize(apikey: nil, debug: false)
|
37
41
|
|
38
|
-
@apiurl = "https://api.openai.com/v1
|
42
|
+
@apiurl = "https://api.openai.com/v1"
|
43
|
+
|
39
44
|
raise 'You must supply an API key!' unless apikey
|
40
45
|
@apikey, @debug = apikey, debug
|
41
46
|
|
42
47
|
end
|
43
48
|
|
44
|
-
def completions(s)
|
45
|
-
|
49
|
+
def completions(s, temperature: 0, max_tokens: 7)
|
50
|
+
|
51
|
+
r = go_completions(s, temperature: temperature, max_tokens: max_tokens)
|
52
|
+
raise ChatGpt2023Error, r[:error][:message].inspect if r.has_key? :error
|
53
|
+
|
54
|
+
puts 'completions r: ' + r.inspect if @debug
|
46
55
|
r[:choices]
|
56
|
+
|
47
57
|
end
|
48
58
|
|
49
|
-
def completion(s)
|
50
|
-
|
59
|
+
def completion(s, temperature: 0, max_tokens: 7)
|
60
|
+
go_completions(s, temperature: temperature, max_tokens: max_tokens)\
|
61
|
+
.first[:text].strip
|
51
62
|
end
|
52
63
|
|
53
64
|
alias complete completion
|
65
|
+
alias ask completion
|
66
|
+
|
67
|
+
def edits(s, s2)
|
68
|
+
r = go_edits(s, s2)
|
69
|
+
end
|
70
|
+
|
71
|
+
def images(s)
|
72
|
+
go_images_generations(s)
|
73
|
+
end
|
74
|
+
|
75
|
+
def image(s)
|
76
|
+
r = images(s)
|
77
|
+
Down.download(r[:data].first[:url] )
|
78
|
+
end
|
79
|
+
|
80
|
+
alias imagine image
|
81
|
+
|
82
|
+
def images_edit(s, image, mask: nil)
|
83
|
+
go_images_edits(s, image, mask: mask)
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
54
87
|
|
55
|
-
def
|
88
|
+
def go_completions(s, temperature: 0, max_tokens: 7)
|
89
|
+
|
90
|
+
h = {
|
91
|
+
"model" => 'text-davinci-003',
|
92
|
+
"prompt" => s,
|
93
|
+
"temperature" => temperature,
|
94
|
+
"max_tokens" => max_tokens
|
95
|
+
}
|
96
|
+
|
97
|
+
submit('completions', h)
|
56
98
|
|
57
|
-
|
99
|
+
end
|
100
|
+
|
101
|
+
def go_edits(s, s2)
|
102
|
+
|
103
|
+
h = {
|
104
|
+
"model" => 'text-davinci-edit-001',
|
105
|
+
"input" => s,
|
106
|
+
"instruction" => s2
|
107
|
+
}
|
108
|
+
|
109
|
+
submit('edits', h)
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
def go_images_generations(s, n: 1, size: '1024x1024')
|
114
|
+
|
115
|
+
h = {
|
116
|
+
"prompt" => s,
|
117
|
+
"n" => n,
|
118
|
+
"size" => size
|
119
|
+
}
|
120
|
+
|
121
|
+
submit('images/generations', h)
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
def go_images_edits(s, image, mask: nil, n: 1, size: '1024x1024')
|
126
|
+
|
127
|
+
h = {
|
128
|
+
"image" => image,
|
129
|
+
"prompt" => s,
|
130
|
+
"n" => n,
|
131
|
+
"size" => size
|
132
|
+
}
|
133
|
+
|
134
|
+
h['mask'] = mask if mask
|
135
|
+
|
136
|
+
submit('images/edits', h)
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
def submit(uri2, h)
|
141
|
+
|
142
|
+
uri = URI.parse(@apiurl + '/' + uri2)
|
58
143
|
request = Net::HTTP::Post.new(uri)
|
59
144
|
request.content_type = "application/json"
|
60
145
|
request["Authorization"] = 'Bearer ' + @apikey
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
request.body = JSON.dump({
|
65
|
-
"model" => model[type],
|
66
|
-
"prompt" => prompt,
|
67
|
-
"temperature" => 0,
|
68
|
-
"max_tokens" => 7
|
69
|
-
})
|
146
|
+
|
147
|
+
puts 'h: ' + h.inspect if @debug
|
148
|
+
request.body = JSON.dump(h)
|
70
149
|
|
71
150
|
req_options = {
|
72
151
|
use_ssl: uri.scheme == "https",
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chatgpt2023
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Robertson
|
@@ -36,8 +36,28 @@ cert_chain:
|
|
36
36
|
6FQD1/GISew7VvxUJdptXeuVNIsdNKxvL3RpfLCuFsi1WXyJ4k3odRMTmS0kAfTy
|
37
37
|
J4sZZW9RNfabTMQQY7DIs3tUAn6i+O0r9lo=
|
38
38
|
-----END CERTIFICATE-----
|
39
|
-
date: 2023-
|
40
|
-
dependencies:
|
39
|
+
date: 2023-02-07 00:00:00.000000000 Z
|
40
|
+
dependencies:
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: down
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.4'
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 5.4.0
|
51
|
+
type: :runtime
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '5.4'
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 5.4.0
|
41
61
|
description:
|
42
62
|
email: digital.robertson@gmail.com
|
43
63
|
executables: []
|
metadata.gz.sig
CHANGED
Binary file
|