chatgpt2023 0.1.0 → 0.2.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: 8f614928641b93d4fbc86a6df977e4bc52c42e66a46958cd88b0c086ef0432fa
4
- data.tar.gz: 433beb2a5d8bab7ffe05fd01d615b3c32e0691aac2bebb8c121940f6290fbaff
3
+ metadata.gz: 88c1cfbb0ea81bad341b25450770d3f9cd5142047a76f9650ed165cfc4295941
4
+ data.tar.gz: f155c706875520f4ffb3d25549b7a8cbde8b1b6c88919eb6b9e85f252486b979
5
5
  SHA512:
6
- metadata.gz: 8f21a74de8195a255865f8f60be43291c13170196dbb1c4fb4f6337118da47c3decc4aab4352f74de2ff16ead582b8565703f127c034f6257602e5ad786a7d52
7
- data.tar.gz: 64ea34173dceb5d96f1dc98b2ba22d729c1376839363ebf8b88a957b436780c12c353ad1877c7a26ba6a23735109ffc251ab8079a63b5c37d68213cd92c5d112
6
+ metadata.gz: cb5037feeaeb3181994e6d669bbf0d97c01e3dbaf06ca1f47754e9b9ab136a79c877e5ffb4c477319658fd2bb8594940831278fa02309a0b887ffab8aa0b3692
7
+ data.tar.gz: 2c597fa1448e6552e5ddfe58ff008b42de82b3413f05f020df304e6ff3ace7df88890a3a2d25e687ec98de9c988eaf415515d6fecca2ae4b3f27868c0c9ea3fa
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.
@@ -35,38 +36,108 @@ class ChatGpt2023
35
36
 
36
37
  def initialize(apikey: nil, debug: false)
37
38
 
38
- @apiurl = "https://api.openai.com/v1/completions"
39
+ @apiurl = "https://api.openai.com/v1"
40
+
39
41
  raise 'You must supply an API key!' unless apikey
40
42
  @apikey, @debug = apikey, debug
41
43
 
42
44
  end
43
45
 
44
- def completions(s)
45
- r = self.submit(s)
46
+ def completions(s, temperature: 0, max_tokens: 7)
47
+ r = self.go_completions(s, temperature: temperature,
48
+ max_tokens: max_tokens)
46
49
  r[:choices]
47
50
  end
48
51
 
49
52
  def completion(s)
50
- self.completions(s).first[:text].strip
53
+ self.go_completions(s).first[:text].strip
51
54
  end
52
55
 
53
56
  alias complete completion
57
+
58
+ def edits(s, s2)
59
+ r = self.go_edits(s, s2)
60
+ end
61
+
62
+ def images(s)
63
+ self.go_images_generations(s)
64
+ end
65
+
66
+ def image(s)
67
+ r = self.images(s)
68
+ img = r[:data].first[:url]
69
+ Down.download(img)
70
+ end
71
+
72
+ alias imagine image
73
+
74
+ def images_edit(s, image, mask: nil)
75
+ self.go_images_edits(s, image, mask: mask)
76
+ end
77
+
78
+ private
79
+
80
+ def go_completions(s, temperature: 0, max_tokens: 7)
81
+
82
+ h = {
83
+ "model" => 'text-davinci-003',
84
+ "prompt" => s,
85
+ "temperature" => temperature,
86
+ "max_tokens" => max_tokens
87
+ }
88
+
89
+ submit('completions', h)
90
+
91
+ end
92
+
93
+ def go_edits(s, s2)
54
94
 
55
- def submit(promptx='Say this is a test', prompt: promptx, type: :completions)
95
+ h = {
96
+ "model" => 'text-davinci-edit-001',
97
+ "input" => s,
98
+ "instruction" => s2
99
+ }
56
100
 
57
- uri = URI.parse(@apiurl)
101
+ submit('edits', h)
102
+
103
+ end
104
+
105
+ def go_images_generations(s, n: 1, size: '1024x1024')
106
+
107
+ h = {
108
+ "prompt" => s,
109
+ "n" => n,
110
+ "size" => size
111
+ }
112
+
113
+ submit('images/generations', h)
114
+
115
+ end
116
+
117
+ def go_images_edits(s, image, mask: nil, n: 1, size: '1024x1024')
118
+
119
+ h = {
120
+ "image" => image,
121
+ "prompt" => s,
122
+ "n" => n,
123
+ "size" => size
124
+ }
125
+
126
+ h['mask'] = mask if mask
127
+
128
+ submit('images/edits', h)
129
+
130
+ end
131
+
132
+ def submit(uri, h)
133
+
134
+ uri = URI.parse(@apiurl + '/' + uri)
58
135
  request = Net::HTTP::Post.new(uri)
59
136
  request.content_type = "application/json"
60
137
  request["Authorization"] = 'Bearer ' + @apikey
61
-
62
- model = {completions: 'text-davinci-003'}
63
-
64
- request.body = JSON.dump({
65
- "model" => model[type],
66
- "prompt" => prompt,
67
- "temperature" => 0,
68
- "max_tokens" => 7
69
- })
138
+
139
+ puts 'h: ' + h.inspect if @debug
140
+ request.body = JSON.dump(h)
70
141
 
71
142
  req_options = {
72
143
  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.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -37,7 +37,27 @@ cert_chain:
37
37
  J4sZZW9RNfabTMQQY7DIs3tUAn6i+O0r9lo=
38
38
  -----END CERTIFICATE-----
39
39
  date: 2023-01-25 00:00:00.000000000 Z
40
- dependencies: []
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