chatgpt2023 0.4.1 → 0.5.0
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 +157 -38
- data.tar.gz.sig +0 -0
- metadata +2 -2
- 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: d76a15a250b67bab6e676ed5c18131cde6310d9c5ace84921e8113c21b4c180f
|
4
|
+
data.tar.gz: bbdce3b4c8ba915e55f3b440da3fc368d90ad6a0555ae1ebf8e6065e44061bd3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb2c05d8493193771ae4480dfa340af843c4c73ae497cc03aa9504c5c9fbb5c30456df7c5c37db26f68bba3419ca41447d4e8436b7aab9b060bcce2160fd6254
|
7
|
+
data.tar.gz: b2c4d4292b30a127935de35156cd48b50ae7c5890e2c5cbe0e6e5f37bf56641db73bb136ee6a2a3d88b8227aacc05f8041281347920d0da7a54d2b9f6a899171
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/lib/chatgpt2023.rb
CHANGED
@@ -39,15 +39,62 @@ end
|
|
39
39
|
|
40
40
|
class ChatGpt2023
|
41
41
|
|
42
|
+
attr_accessor :assistant_recent
|
43
|
+
|
42
44
|
def initialize(apikey: nil, attempts: 1, debug: false)
|
43
45
|
|
44
46
|
@apiurl = "https://api.openai.com/v1"
|
45
47
|
|
46
48
|
raise 'You must supply an API key!' unless apikey
|
47
49
|
@apikey, @attempts, @debug = apikey, attempts, debug
|
50
|
+
@assistant_recent = nil
|
48
51
|
|
49
52
|
end
|
50
53
|
|
54
|
+
|
55
|
+
# CURL example
|
56
|
+
# curl https://api.openai.com/v1/chat/completions \
|
57
|
+
# -H 'Content-Type: application/json' \
|
58
|
+
# -H 'Authorization: Bearer YOUR_API_KEY' \
|
59
|
+
# -d '{
|
60
|
+
# "model": "gpt-3.5-turbo",
|
61
|
+
# "messages": [{"role": "user", "content": "Hello!"}]
|
62
|
+
# }'
|
63
|
+
|
64
|
+
# Ruby example
|
65
|
+
|
66
|
+
# require 'chatgpt2023'
|
67
|
+
|
68
|
+
# c = ChatGpt2023.new(apikey: YOUR_API_KEY)
|
69
|
+
# r = c.chat 'who is Burt Reynolds?'
|
70
|
+
# r2 = c.chat 'what age was he?'
|
71
|
+
# r3 = c.chat 'did he have family?'
|
72
|
+
#
|
73
|
+
|
74
|
+
def chat(s, temperature: 1, max_tokens: 3900)
|
75
|
+
r = chats(s, temperature: temperature, max_tokens: max_tokens)
|
76
|
+
return r if r.is_a?(Hash)
|
77
|
+
return {text: r.first[:message][:content].strip}
|
78
|
+
end
|
79
|
+
|
80
|
+
def chats(s=nil, messages: [], temperature: 1, max_tokens: 3900, n: 1)
|
81
|
+
|
82
|
+
messages << @assistant_recent if @assistant_recent
|
83
|
+
messages << {'role' => 'user', 'content' => s } if s
|
84
|
+
r = go_chat(messages, temperature: temperature,
|
85
|
+
max_tokens: max_tokens, n: n)
|
86
|
+
|
87
|
+
puts 'chat/completions r: ' + r.inspect if @debug
|
88
|
+
|
89
|
+
if r[:error] then r
|
90
|
+
r
|
91
|
+
else
|
92
|
+
@assistant_recent = r[:choices].first[:message]
|
93
|
+
r[:choices]
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
51
98
|
# Example
|
52
99
|
# c = ChatGpt2023.new(apikey: 'yourapikey')
|
53
100
|
# s = '
|
@@ -56,19 +103,20 @@ class ChatGpt2023
|
|
56
103
|
# '
|
57
104
|
# r = c.code_completions s, temperature: 0.2
|
58
105
|
# puts r.first[:text]
|
59
|
-
|
106
|
+
#
|
60
107
|
def code_completions(s, temperature: 1, max_tokens: 32, n: 1)
|
61
108
|
|
62
109
|
r = go_code(s, temperature: temperature,
|
63
110
|
max_tokens: max_tokens, n: n)
|
64
111
|
puts 'code r: ' + r.inspect if @debug
|
65
|
-
r[:choices]
|
112
|
+
r[:error] ? r : r[:choices]
|
66
113
|
|
67
114
|
end
|
68
115
|
|
69
116
|
def code_completion(s, temperature: 1, max_tokens: 32)
|
70
|
-
code_completions(s, temperature: temperature, max_tokens: max_tokens)
|
71
|
-
|
117
|
+
r = code_completions(s, temperature: temperature, max_tokens: max_tokens)
|
118
|
+
return r if r.is_a?(Hash)
|
119
|
+
return {text: r.first[:text].strip}
|
72
120
|
end
|
73
121
|
|
74
122
|
def completions(s, temperature: 1, max_tokens: 32, n: 1)
|
@@ -76,17 +124,18 @@ class ChatGpt2023
|
|
76
124
|
r = go_completions(s, temperature: temperature,
|
77
125
|
max_tokens: max_tokens, n: n)
|
78
126
|
puts 'completions r: ' + r.inspect if @debug
|
79
|
-
r[:choices]
|
127
|
+
r[:error] ? r : r[:choices]
|
80
128
|
|
81
129
|
end
|
82
130
|
|
83
131
|
def completion(s, temperature: 1, max_tokens: 32)
|
84
|
-
completions(s, temperature: temperature, max_tokens: max_tokens)
|
85
|
-
|
132
|
+
r = completions(s, temperature: temperature, max_tokens: max_tokens)
|
133
|
+
return r if r.is_a?(Hash)
|
134
|
+
return {text: r.first[:text].strip}
|
86
135
|
end
|
87
136
|
|
88
137
|
alias complete completion
|
89
|
-
alias ask
|
138
|
+
alias ask chat
|
90
139
|
|
91
140
|
def edits(s, s2)
|
92
141
|
r = go_edits(s, s2)
|
@@ -109,6 +158,20 @@ class ChatGpt2023
|
|
109
158
|
|
110
159
|
private
|
111
160
|
|
161
|
+
def go_chat(messages=[], temperature: 0, max_tokens: 4096, n: 1)
|
162
|
+
|
163
|
+
h = {
|
164
|
+
"model" => 'gpt-3.5-turbo',
|
165
|
+
"messages" => messages,
|
166
|
+
"temperature" => temperature,
|
167
|
+
"max_tokens" => max_tokens,
|
168
|
+
"n" => n
|
169
|
+
}
|
170
|
+
|
171
|
+
submit('chat/completions', h)
|
172
|
+
|
173
|
+
end
|
174
|
+
|
112
175
|
def go_code(s, temperature: 0, max_tokens: 7, n: 1)
|
113
176
|
|
114
177
|
h = {
|
@@ -209,7 +272,7 @@ class ChatGpt2023
|
|
209
272
|
|
210
273
|
end while h.has_key?(:error) and attempts < @attempts
|
211
274
|
|
212
|
-
raise ChatGpt2023Error, h[:error][:message].inspect if h.has_key? :error
|
275
|
+
#raise ChatGpt2023Error, h[:error][:message].inspect if h.has_key? :error
|
213
276
|
|
214
277
|
return h
|
215
278
|
end
|
@@ -222,10 +285,10 @@ class CGRecorder < ChatGpt2023
|
|
222
285
|
|
223
286
|
def initialize(apikey: nil, indexfile: 'cgindex.xml',
|
224
287
|
logfile: 'chatgpt.xml', attempts: 1, debug: false)
|
225
|
-
|
288
|
+
|
226
289
|
super(apikey: apikey, attempts: attempts, debug: debug)
|
227
290
|
@dx = DynarexDaily.new filename: logfile, fields: %i(prompt result),
|
228
|
-
autosave: true, order: 'descending', debug:
|
291
|
+
autosave: true, order: 'descending', debug: debug
|
229
292
|
@index = Dynarex.new(indexfile, schema: 'entries[title]/entry(prompt, ' \
|
230
293
|
+ 'tags)', order: 'descending', autosave: true)
|
231
294
|
title = 'ChatGPT prompt log'
|
@@ -235,9 +298,8 @@ class CGRecorder < ChatGpt2023
|
|
235
298
|
|
236
299
|
def code_completion(s, tags=nil, temperature: 1, max_tokens: 2000)
|
237
300
|
|
238
|
-
r =
|
239
|
-
|
240
|
-
log(s, r, tags)
|
301
|
+
r = super(s, temperature: temperature, max_tokens: max_tokens)
|
302
|
+
log(s, r[:text].strip, tags) unless r[:error]
|
241
303
|
|
242
304
|
return r
|
243
305
|
|
@@ -245,9 +307,9 @@ class CGRecorder < ChatGpt2023
|
|
245
307
|
|
246
308
|
def completion(s, tags=nil, temperature: 1, max_tokens: 1000)
|
247
309
|
|
248
|
-
r =
|
249
|
-
|
250
|
-
log(s, r, tags)
|
310
|
+
r = super(s, temperature: temperature, max_tokens: max_tokens)
|
311
|
+
puts 'CGRecorder inside completion: ' + r.inspect if @debug
|
312
|
+
log(s, r[:text].strip, tags) unless r[:error]
|
251
313
|
|
252
314
|
return r
|
253
315
|
|
@@ -269,7 +331,7 @@ end
|
|
269
331
|
class ChatAway
|
270
332
|
|
271
333
|
# statement below used for debugging
|
272
|
-
|
334
|
+
attr_reader :dx, :prompts
|
273
335
|
|
274
336
|
def initialize(questions, apikey: nil, filepath: '/tmp/chatgpt', debug: false)
|
275
337
|
|
@@ -278,42 +340,99 @@ class ChatAway
|
|
278
340
|
FileUtils.mkdir_p filepath
|
279
341
|
idxfile = File.join(filepath, 'index.xml')
|
280
342
|
cgfile = File.join(filepath, 'chatgpt.xml')
|
281
|
-
|
282
|
-
|
283
|
-
@
|
343
|
+
|
344
|
+
puts 'questions: ' + questions.inspect if @debug
|
345
|
+
@dx = case questions.class.to_s.to_sym
|
346
|
+
when :Dynarex
|
347
|
+
questions
|
348
|
+
when :String
|
349
|
+
questions.lines.length < 2 ? Dynarex.new(questions) : import(questions)
|
350
|
+
end
|
351
|
+
|
352
|
+
@chat = CGRecorder.new(apikey: apikey, indexfile: idxfile,
|
353
|
+
logfile: cgfile, attempts: 5, debug: @debug)
|
284
354
|
@prompts = @chat.index.all.map(&:prompt)
|
285
355
|
|
286
|
-
|
356
|
+
@mode = nil
|
357
|
+
|
358
|
+
end
|
287
359
|
|
288
|
-
def start()
|
360
|
+
def start()
|
289
361
|
|
290
|
-
@dx.all.
|
362
|
+
@dx.all.map do |rx|
|
291
363
|
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
364
|
+
puts 'rx: ' + rx.inspect if @debug
|
365
|
+
|
366
|
+
#if (@prompts.include?(rx.prompt) and rx.redo != 'true') \
|
367
|
+
# or @mode != :import
|
368
|
+
# next
|
369
|
+
#end
|
370
|
+
|
371
|
+
type = rx.type == 'code' ? :code_completion : :completion
|
372
|
+
|
373
|
+
prompt = rx.prompt
|
374
|
+
|
375
|
+
puts 'prompt: ' + prompt
|
376
|
+
|
377
|
+
attempts = 0
|
378
|
+
reply = nil
|
379
|
+
|
380
|
+
begin
|
297
381
|
|
298
|
-
|
382
|
+
r = @chat.method(type).call prompt
|
299
383
|
|
300
|
-
puts '
|
301
|
-
|
302
|
-
attempts = 0
|
384
|
+
puts 'r: ' + r.inspect if @debug
|
303
385
|
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
puts 'Something not working! ' + ($!).inspect
|
386
|
+
if r[:error] then
|
387
|
+
|
388
|
+
puts r[:error][:text]
|
308
389
|
sleep 2
|
309
390
|
attempts += 1
|
310
|
-
|
391
|
+
|
392
|
+
redo if attempts < 4
|
393
|
+
|
394
|
+
else
|
395
|
+
reply = r[:text]
|
311
396
|
end
|
312
397
|
|
398
|
+
rescue
|
399
|
+
|
400
|
+
puts 'Something not working! ' + ($!).inspect
|
313
401
|
sleep 2
|
402
|
+
attempts += 1
|
403
|
+
|
404
|
+
retry if attempts < 4
|
405
|
+
|
406
|
+
ensure
|
407
|
+
|
408
|
+
reply ||= ''
|
409
|
+
|
410
|
+
end
|
411
|
+
|
412
|
+
sleep 2
|
413
|
+
|
414
|
+
reply
|
314
415
|
|
315
416
|
end
|
316
417
|
|
317
418
|
end
|
318
419
|
|
420
|
+
private
|
421
|
+
|
422
|
+
|
423
|
+
def import(s)
|
424
|
+
|
425
|
+
@mode = :import
|
426
|
+
puts 'inside import' if @debug
|
427
|
+
|
428
|
+
header = '<?dynarex schema="prompts/entry(prompt, type, redo)" delimiter=" # "?>
|
429
|
+
--+
|
430
|
+
'
|
431
|
+
|
432
|
+
s2 = header + s.strip.lines.map {|line| 'p: ' + line }.join("\n")
|
433
|
+
|
434
|
+
Dynarex.new(s2)
|
435
|
+
|
436
|
+
end
|
437
|
+
|
319
438
|
end
|
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.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Robertson
|
@@ -36,7 +36,7 @@ cert_chain:
|
|
36
36
|
6FQD1/GISew7VvxUJdptXeuVNIsdNKxvL3RpfLCuFsi1WXyJ4k3odRMTmS0kAfTy
|
37
37
|
J4sZZW9RNfabTMQQY7DIs3tUAn6i+O0r9lo=
|
38
38
|
-----END CERTIFICATE-----
|
39
|
-
date: 2023-
|
39
|
+
date: 2023-03-05 00:00:00.000000000 Z
|
40
40
|
dependencies:
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: down
|
metadata.gz.sig
CHANGED
Binary file
|