Class: Wco::AiWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/wco/ai_writer.rb

Overview

OpenAI GPT3 GPT3.5 GPT4

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run_headline(headline) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wco/ai_writer.rb', line 31

def self.run_headline headline
  prompt = "Rephrase the following article title using less than 250 characters: #{headline.name}"
  new_title = self.run_prompt prompt
  new_title = new_title[0..255]
  # 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,
    author: Wco::Profile.ai_writer,
  })

  return report
end

.run_prompt(prompt) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/wco/ai_writer.rb', line 9

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!
  puts! out, 'chatgpt response'
  out = out[:choices][0][:message][:content]
  return out
end

Instance Method Details

#run_headline(h) ⇒ Object



53
# File 'lib/wco/ai_writer.rb', line 53

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

#run_prompt(p) ⇒ Object



28
# File 'lib/wco/ai_writer.rb', line 28

def run_prompt p; self.class.run_prompt p; end