chatgpt2023 0.4.2 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 79e85a14caa0d9f7554af7d2df3faf4356d906ffd32eddbeab97a0ce62588aa6
4
- data.tar.gz: 7ed03e8ba40941b21431469711325c9eb20efd5c9374ecfb24e0cdd9d497888b
3
+ metadata.gz: 8f937d13ec5131003c4acf2b9c046cb56e4462bb72e89700cb56fb2c376c3846
4
+ data.tar.gz: e2252feb992e7c3538ae917d63abc6b4f4743e8faa328fff0ce36f1af221508e
5
5
  SHA512:
6
- metadata.gz: '0297c587bab1025db945d899437ca0b19a820c2c172a01ea2129de95b09fd773f02bed19e098d8a3c34881c06b4ddfc301a3e38670bce6c35236300494281099'
7
- data.tar.gz: f80fc62462f62eeee57b2ed50ff81a76de628fa27bd2733c82ac57b108da6ed5a2b14f6afce5a600353c97d3a3fc8ce574117341fec1b70f38bb29b1fa972f14
6
+ metadata.gz: 88b1657db3ac774bfae4df8166a2aa26ef520af5194a7ba1718aea6612a2a75350de457b8dabb150bda4e149a4851648f45eecfdc1dd6276794fdb1358c760a5
7
+ data.tar.gz: dd610445a564e33fde61cdedda227a843d6410457f90b8a85b3255e627462fb51cae5a3767d56e6346f277466df97053fd3f41155fba06d7fbbd34d4e125144c
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/chatgpt2023.rb CHANGED
@@ -39,15 +39,71 @@ 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
+ if @assistant_recent then
83
+
84
+ # We limit what gets passed back because there should enough
85
+ # information in the 1st 140 chars to infer the conversation context
86
+ #
87
+ @assistant_recent[:content].slice!(140..-1)
88
+ messages << @assistant_recent
89
+ max_tokens -= @assistant_recent[:content].split.length
90
+ end
91
+
92
+ messages << {'role' => 'user', 'content' => s } if s
93
+ r = go_chat(messages, temperature: temperature,
94
+ max_tokens: max_tokens, n: n)
95
+
96
+ puts 'chat/completions r: ' + r.inspect if @debug
97
+
98
+ if r[:error] then r
99
+ r
100
+ else
101
+ @assistant_recent = r[:choices].first[:message]
102
+ r[:choices]
103
+ end
104
+
105
+ end
106
+
51
107
  # Example
52
108
  # c = ChatGpt2023.new(apikey: 'yourapikey')
53
109
  # s = '
@@ -56,7 +112,7 @@ class ChatGpt2023
56
112
  # '
57
113
  # r = c.code_completions s, temperature: 0.2
58
114
  # puts r.first[:text]
59
-
115
+ #
60
116
  def code_completions(s, temperature: 1, max_tokens: 32, n: 1)
61
117
 
62
118
  r = go_code(s, temperature: temperature,
@@ -88,7 +144,7 @@ class ChatGpt2023
88
144
  end
89
145
 
90
146
  alias complete completion
91
- alias ask completion
147
+ alias ask chat
92
148
 
93
149
  def edits(s, s2)
94
150
  r = go_edits(s, s2)
@@ -111,6 +167,20 @@ class ChatGpt2023
111
167
 
112
168
  private
113
169
 
170
+ def go_chat(messages=[], temperature: 0, max_tokens: 4096, n: 1)
171
+
172
+ h = {
173
+ "model" => 'gpt-3.5-turbo',
174
+ "messages" => messages,
175
+ "temperature" => temperature,
176
+ "max_tokens" => max_tokens,
177
+ "n" => n
178
+ }
179
+
180
+ submit('chat/completions', h)
181
+
182
+ end
183
+
114
184
  def go_code(s, temperature: 0, max_tokens: 7, n: 1)
115
185
 
116
186
  h = {
@@ -227,7 +297,7 @@ class CGRecorder < ChatGpt2023
227
297
 
228
298
  super(apikey: apikey, attempts: attempts, debug: debug)
229
299
  @dx = DynarexDaily.new filename: logfile, fields: %i(prompt result),
230
- autosave: true, order: 'descending', debug: false
300
+ autosave: true, order: 'descending', debug: debug
231
301
  @index = Dynarex.new(indexfile, schema: 'entries[title]/entry(prompt, ' \
232
302
  + 'tags)', order: 'descending', autosave: true)
233
303
  title = 'ChatGPT prompt log'
@@ -235,10 +305,19 @@ class CGRecorder < ChatGpt2023
235
305
 
236
306
  end
237
307
 
308
+ def chat(s, tags=nil, temperature: 1, max_tokens: 1000)
309
+
310
+ r = super(s, temperature: temperature, max_tokens: max_tokens)
311
+ puts 'CGRecorder inside chat: ' + r.inspect if @debug
312
+ log(s, r[:text].strip, tags) unless r[:error]
313
+
314
+ return r
315
+ end
316
+
238
317
  def code_completion(s, tags=nil, temperature: 1, max_tokens: 2000)
239
318
 
240
319
  r = super(s, temperature: temperature, max_tokens: max_tokens)
241
- log(s, r[:text].strip, tags)
320
+ log(s, r[:text].strip, tags) unless r[:error]
242
321
 
243
322
  return r
244
323
 
@@ -248,11 +327,11 @@ class CGRecorder < ChatGpt2023
248
327
 
249
328
  r = super(s, temperature: temperature, max_tokens: max_tokens)
250
329
  puts 'CGRecorder inside completion: ' + r.inspect if @debug
251
- log(s, r[:text].strip, tags)
330
+ log(s, r[:text].strip, tags) unless r[:error]
252
331
 
253
332
  return r
254
-
255
333
  end
334
+
256
335
 
257
336
  alias complete completion
258
337
  alias ask completion
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.2
4
+ version: 0.5.1
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-02-16 00:00:00.000000000 Z
39
+ date: 2023-03-13 00:00:00.000000000 Z
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: down
metadata.gz.sig CHANGED
Binary file