Class: Zakar::OpenAI
- Inherits:
-
Object
- Object
- Zakar::OpenAI
- Defined in:
- lib/zakar.rb
Instance Method Summary collapse
- #DallE2(filename, description, size: "1024x1024") ⇒ Object
- #GPT3(description) ⇒ Object
-
#initialize(apikey) ⇒ OpenAI
constructor
A new instance of OpenAI.
Constructor Details
#initialize(apikey) ⇒ OpenAI
Returns a new instance of OpenAI.
7 8 9 10 11 12 13 |
# File 'lib/zakar.rb', line 7 def initialize(apikey) @apikey = apikey @headers = { "Content-Type"=> "application/json", "Authorization"=> "Bearer #{apikey}" } end |
Instance Method Details
#DallE2(filename, description, size: "1024x1024") ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/zakar.rb', line 14 def DallE2(filename, description, size:"1024x1024") req = HTTP.post(DALLE2,:headers=>@headers,:body=>JSON({ "prompt"=>description, "n"=>1,"size"=>size })) image = HTTP.get(JSON(req.body)["data"].to_s.match(/"url"=>"(.*)"/m)[1]) a = File.new("#{filename}.jpg", "w") a.write(image.body) a.close() end |
#GPT3(description) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/zakar.rb', line 24 def GPT3(description) req = HTTP.post(GPT3, :headers=>@headers, :body=>JSON({ "prompt"=>description, "stop"=>nil, "n"=>1,"temperature"=>0.5,"max_tokens"=>100 })) puts JSON(req.body)["choices"].to_s.match(/"text"=>"(.*)",/m).to_s.split('"text"=>"')[1].gsub('\n\n', "\n") end |