chatgpt2023 0.4.2 → 0.5.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 79e85a14caa0d9f7554af7d2df3faf4356d906ffd32eddbeab97a0ce62588aa6
4
- data.tar.gz: 7ed03e8ba40941b21431469711325c9eb20efd5c9374ecfb24e0cdd9d497888b
3
+ metadata.gz: d76a15a250b67bab6e676ed5c18131cde6310d9c5ace84921e8113c21b4c180f
4
+ data.tar.gz: bbdce3b4c8ba915e55f3b440da3fc368d90ad6a0555ae1ebf8e6065e44061bd3
5
5
  SHA512:
6
- metadata.gz: '0297c587bab1025db945d899437ca0b19a820c2c172a01ea2129de95b09fd773f02bed19e098d8a3c34881c06b4ddfc301a3e38670bce6c35236300494281099'
7
- data.tar.gz: f80fc62462f62eeee57b2ed50ff81a76de628fa27bd2733c82ac57b108da6ed5a2b14f6afce5a600353c97d3a3fc8ce574117341fec1b70f38bb29b1fa972f14
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,7 +103,7 @@ 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,
@@ -88,7 +135,7 @@ class ChatGpt2023
88
135
  end
89
136
 
90
137
  alias complete completion
91
- alias ask completion
138
+ alias ask chat
92
139
 
93
140
  def edits(s, s2)
94
141
  r = go_edits(s, s2)
@@ -111,6 +158,20 @@ class ChatGpt2023
111
158
 
112
159
  private
113
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
+
114
175
  def go_code(s, temperature: 0, max_tokens: 7, n: 1)
115
176
 
116
177
  h = {
@@ -227,7 +288,7 @@ class CGRecorder < ChatGpt2023
227
288
 
228
289
  super(apikey: apikey, attempts: attempts, debug: debug)
229
290
  @dx = DynarexDaily.new filename: logfile, fields: %i(prompt result),
230
- autosave: true, order: 'descending', debug: false
291
+ autosave: true, order: 'descending', debug: debug
231
292
  @index = Dynarex.new(indexfile, schema: 'entries[title]/entry(prompt, ' \
232
293
  + 'tags)', order: 'descending', autosave: true)
233
294
  title = 'ChatGPT prompt log'
@@ -238,7 +299,7 @@ class CGRecorder < ChatGpt2023
238
299
  def code_completion(s, tags=nil, temperature: 1, max_tokens: 2000)
239
300
 
240
301
  r = super(s, temperature: temperature, max_tokens: max_tokens)
241
- log(s, r[:text].strip, tags)
302
+ log(s, r[:text].strip, tags) unless r[:error]
242
303
 
243
304
  return r
244
305
 
@@ -248,7 +309,7 @@ class CGRecorder < ChatGpt2023
248
309
 
249
310
  r = super(s, temperature: temperature, max_tokens: max_tokens)
250
311
  puts 'CGRecorder inside completion: ' + r.inspect if @debug
251
- log(s, r[:text].strip, tags)
312
+ log(s, r[:text].strip, tags) unless r[:error]
252
313
 
253
314
  return r
254
315
 
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.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-02-16 00:00:00.000000000 Z
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