chatgpt.rake
unknown
ruby
a year ago
7.0 kB
2
Indexable
Never
require 'colorize' # namespace :chatgpt do # desc "Export Categories to Swell" # task clean_adventure_text: :environment do # client = OpenAI::Client.new # Adventure.all.published.each do |adventure| # puts "Cleaning #{adventure.title}...".green # word_count = adventure.content.to_plain_text.split.count # if word_count < 200 # puts "Adventure #{adventure.title} has #{word_count} words, shall I ask ChatGPT? (y/n)".red # begin # input = STDIN.gets.strip.downcase # end until %w(y n).include?(input) # case input # when 'y' # puts "ChatGPT working on #{adventure.title}...".green # full_content = "" # response = client.chat( # parameters: { # model: "gpt-4", # messages: [ # { role: "system", content: "You are writing content for a website selling creative teambuilding related events" }, # { role: "system", content: "This for an event titled: '#{adventure.title}' in the context of the category of: #{adventure.primary_category.title}" }, # { role: "system", content: "The output should be HTML using only starting and ending div tags and double <br><br> at the end of paragraphs" }, # { role: "user", content: "Can you expand this event description into 3 to 4 complete paragraphs?"}, # { role: "user", content: "It should be more between 150 to 180 words in length and without headings"}, # { role: "user", content: adventure.content.to_plain_text}, # ], # max_tokens: 375, # top_p: 0.95, # temperature: 0.7, # stream: proc do |chunk, _bytesize| # content_chunk = chunk.dig("choices", 0, "delta", "content").to_s # print content_chunk # full_content += content_chunk # end # } # ) # adventure.content = full_content # adventure.save! # when 'n' # puts "No worries, I'll leave it alone, NEXT..." # end # end # end # end # end namespace :chatgpt do @about_us = "You are writing content for a website selling creative teambuilding related events. It primarily sells virtual & hybrid teambuilding events to large companies although it also sells in-person events as well in large cities. It does not sell to individuals." desc "Export Categories to Swell" task clean_adventure_text: :environment do client = OpenAI::Client.new adventures = Adventure.all.published adventures.each_with_index do |adventure, index| puts "Cleaning #{adventure.title}...".green word_count = adventure.content.to_plain_text.split.count if word_count < 150 puts "Adventure #{adventure.title} has #{word_count} (#{index + 1} of #{adventures.count})".green full_content = "" response = client.chat( parameters: { model: "gpt-4", messages: [ # Add inperson or virtual # Add number of people # Add brand { role: "system", content: @about_us } #this gives context to the AI, { role: "system", content: "This for an event titled: '#{adventure.title}' in the context of the category of: #{adventure.primary_category.title}" }, { role: "system", content: "The output should be HTML using only starting and ending div tags and double <br><br> at the end of paragraphs" }, { role: "user", content: "Can you expand this event description into 2 to 3 complete paragraphs?"}, { role: "user", content: "It should be more between 120 to 180 words in length and without headings"}, { role: "user", content: adventure.description}, { role: "user", content: adventure.long_description.to_plain_text}, ], max_tokens: 350, top_p: 0.95, temperature: 0.7, stream: proc do |chunk, _bytesize| content_chunk = chunk.dig("choices", 0, "delta", "content").to_s print content_chunk full_content += content_chunk end } ) adventure.content = full_content adventure.save! end end end desc "Export FAQ to Swell" task create_adventure_faqs: :environment do client = OpenAI::Client.new adventures = Adventure.all.published adventures.each_with_index do |adventure, index| if adventure.faq.blank? puts "Adventure #{adventure.title} (#{index + 1} of #{adventures.count})".green response_content = "" response = client.chat( parameters: { model: "gpt-4", messages: [ { role: "system", content: @about_us } { role: "system", content: "Please format the response as 'Q: [Question]' followed by 'A: [Answer]'." }, { role: "system", content: "This is for an event titled: '#{adventure.title}' in the context of the category of: #{adventure.primary_category.title}" }, { role: "user", content: "Generate a minimum of 3 and upto 5 FAQ questions and answers that would be appropriate for this event. Start with the question 'How it works?'"}, { role: "user", content: "Always start with the first question 'How it works?'"}, { role: "user", content: "Ensure questions are between 3 and 12 words in length"}, { role: "user", content: "Ensure answers are between 10 and 30 words in length"}, { role: "user", content: adventure.description}, { role: "user", content: adventure.long_description.to_plain_text}, { role: "user", content: adventure.content.to_plain_text}, ], max_tokens: 250, top_p: 0.95, temperature: 0.7, stream: proc do |chunk, _bytesize| content_chunk = chunk.dig("choices", 0, "delta", "content").to_s print content_chunk response_content += content_chunk end } ) # Split the response content into lines lines = response_content.strip.split("\n") # Initialize an empty hash to store FAQs faqs_hash = {} lines.each_with_index do |line, i| # If the line starts with "Q:" it's a question if line.start_with?("Q:") question = line.gsub("Q:", "").strip # Check if the next line starts with "A:" and is an answer if lines[i + 1] && lines[i + 1].start_with?("A:") answer = lines[i + 1].gsub("A:", "").strip faqs_hash[question] = answer end end end # Save the FAQs hash directly into the faq jsonb column adventure.faq = faqs_hash adventure.save! end end end end