Sha256: efc60ff9e1c78bfd5e8269e5dd3694a78545ca163335a06b2e69d0bad3ce0875

Contents?: true

Size: 1.39 KB

Versions: 46

Compression:

Stored size: 1.39 KB

Contents

# require 'httparty'

##
## OpenAI GPT3 GPT3.5 GPT4
##
class Wco::AiWriter

  def self.run_prompt prompt
    out = HTTParty.post("https://api.openai.com/v1/chat/completions", {
      headers: {
        "Content-Type": "application/json",
        "Authorization": "Bearer #{OPENAI_API_KEY}",
      },
      body: {
        model: 'gpt-3.5-turbo',
        messages: [
          { role: "system", content: "You are a knowledgable office assistant." },
          { role: 'user', content: prompt },
        ] }.to_json
    })
    out = JSON.parse out.response.body
    out.deep_symbolize_keys!
    out = out[:choices][0][:message][:content]
    return out
  end
  def run_prompt p; self.class.run_prompt p; end


  def self.run_headline headline
    prompt = "Rephrase the following article title using one sentence: #{headline.name}"
    new_title = self.run_prompt prompt
    puts! new_title, 'new_title'

    prompt = "Write an article about the following topic: #{headline.name}"
    new_body = self.run_prompt prompt
    new_body.gsub!("\r", '')
    new_body = new_body.split("\n\n").map { |ppp| "<p>#{ppp}</p>" }.join
    new_body = new_body.gsub("\n", "<br />")
    puts! new_body[0...200], 'new_body'

    report = Wco::Report.create!({
      title: new_title,
      slug: new_title,
      body:  new_body,
    })

    # headline.delete

    return report
  end
  def run_headline h; self.class.run_headline h; end


end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
wco_models-3.1.0.85 lib/wco/ai_writer.rb
wco_models-3.1.0.83 lib/wco/ai_writer.rb
wco_models-3.1.0.82 lib/wco/ai_writer.rb
wco_models-3.1.0.81 lib/wco/ai_writer.rb
wco_models-3.1.0.80 lib/wco/ai_writer.rb
wco_models-3.1.0.79 lib/wco/ai_writer.rb
wco_models-3.1.0.78 lib/wco/ai_writer.rb
wco_models-3.1.0.77 lib/wco/ai_writer.rb
wco_models-3.1.0.76 lib/wco/ai_writer.rb
wco_models-3.1.0.75 lib/wco/ai_writer.rb
wco_models-3.1.0.74 lib/wco/ai_writer.rb
wco_models-3.1.0.73 lib/wco/ai_writer.rb
wco_models-3.1.0.72 lib/wco/ai_writer.rb
wco_models-3.1.0.71 lib/wco/ai_writer.rb
wco_models-3.1.0.70 lib/wco/ai_writer.rb
wco_models-3.1.0.69 lib/wco/ai_writer.rb
wco_models-3.1.0.67 lib/wco/ai_writer.rb
wco_models-3.1.0.66 lib/wco/ai_writer.rb
wco_models-3.1.0.65 lib/wco/ai_writer.rb
wco_models-3.1.0.64 lib/wco/ai_writer.rb