FIX: Chat titler was still using the old code after LLM migration (#314)

This commit is contained in:
Roman Rizzi 2023-11-27 13:03:24 -03:00 committed by GitHub
parent 54a8dd9556
commit 775610b1c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 12 deletions

View File

@ -7,26 +7,34 @@ module DiscourseAi
@thread = thread
end
def suggested_title
return nil if thread_content.blank?
prompt = CompletionPrompt.enabled_by_name("generate_titles")
raise Discourse::InvalidParameters.new(:mode) if !prompt
response =
DiscourseAi::AiHelper::Assistant.new.generate_and_send_prompt(
prompt,
thread_content,
thread.original_message_user,
)
response.dig(:suggestions)&.first
end
private
attr_reader :thread
def thread_content
# Replace me by a proper API call
@thread
thread
.chat_messages
.joins(:user)
.pluck(:username, :message)
.map { |username, message| "#{username}: #{message}" }
.join("\n")
end
def suggested_title
return nil if thread_content.blank?
prompt = CompletionPrompt.enabled_by_name(id: "generate_titles")
raise Discourse::InvalidParameters.new(:mode) if !prompt
response =
DiscourseAi::AiHelper::LlmPrompt.new.generate_and_send_prompt(prompt, thread_content)
response.dig(:suggestions)&.first
end
end
end
end

View File

@ -0,0 +1,21 @@
# frozen_string_literal: true
RSpec.describe DiscourseAi::AiHelper::ChatThreadTitler do
subject(:titler) { described_class.new(thread) }
fab!(:thread) { Fabricate(:chat_thread) }
fab!(:user) { Fabricate(:user) }
describe "#suggested_title" do
it "suggest the first option from the generate_titles prompt" do
titles =
"The solitary horse*The horse etched in gold*A horse's infinite journey*A horse lost in time*A horse's last ride"
expected_title = titles.split("*").first
result =
DiscourseAi::Completions::LLM.with_prepared_responses([titles]) { titler.suggested_title }
expect(result).to eq(expected_title)
end
end
end