Files
discourse-ai/lib/modules/ai_bot/commands/image_command.rb
Sam Saffron db11fe391d FEATURE: use triage command to attempt to ground models on GPT 3.5
Previous to this change our results were ungrounded and simpler models like
GPT 3.5 and claude had a real tough time figuring out how to run commands

This splits responses into 2 phases:

Phase 1: figure out if you need to run a command
Phase 2: respond to user

This seems to produce better results on both Claude and GPT 3.5 but still
needs a fair bit of tuning.
2023-05-26 17:07:52 +10:00

56 lines
1.1 KiB
Ruby

#frozen_string_literal: true
module DiscourseAi::AiBot::Commands
class ImageCommand < Command
class << self
def name
"image"
end
def desc
"!image DESC - renders an image from the description (remove all connector words, keep it to 40 words or less, be creative)"
end
end
def result_name
"results"
end
def description_args
{ prompt: @args || 0 }
end
def chain_next_response
false
end
def post_raw_details
"#{super}\n\n#{@last_custom_raw}"
end
def process
prompt = @args
results = DiscourseAi::Inference::StabilityGenerator.perform!(prompt)
uploads = []
results[:artifacts].each_with_index do |image, i|
f = Tempfile.new("v1_txt2img_#{i}.png")
f.binmode
f.write(Base64.decode64(image[:base64]))
f.rewind
uploads << UploadCreator.new(f, "image.png").create_for(bot_user.id)
f.unlink
end
@last_custom_raw =
uploads
.map { |upload| "![#{prompt.gsub(/\|\'\"/, "")}|512x512, 50%](#{upload.short_url})" }
.join(" ")
nil
end
end
end