2023-12-12 12:28:39 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe Jobs::StreamPostHelper do
|
|
|
|
subject(:job) { described_class.new }
|
|
|
|
|
2024-06-19 17:01:35 -04:00
|
|
|
before { assign_fake_provider_to(:ai_helper_model) }
|
2024-01-29 14:04:25 -05:00
|
|
|
|
2023-12-12 12:28:39 -05:00
|
|
|
describe "#execute" do
|
2024-03-05 10:48:28 -05:00
|
|
|
fab!(:topic)
|
2023-12-12 12:28:39 -05:00
|
|
|
fab!(:post) do
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: topic,
|
|
|
|
raw:
|
|
|
|
"I like to eat pie. It is a very good dessert. Some people are wasteful by throwing pie at others but I do not do that. I always eat the pie.",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
fab!(:user) { Fabricate(:leader) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Group.find(Group::AUTO_GROUPS[:trust_level_3]).add(user)
|
2024-08-12 18:40:23 -04:00
|
|
|
SiteSetting.ai_helper_enabled = true
|
2023-12-12 12:28:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "validates params" do
|
2024-08-08 14:32:39 -04:00
|
|
|
let(:mode) { CompletionPrompt::EXPLAIN }
|
|
|
|
let(:prompt) { CompletionPrompt.find_by(id: mode) }
|
|
|
|
|
2023-12-12 12:28:39 -05:00
|
|
|
it "does nothing if there is no post" do
|
|
|
|
messages =
|
2024-08-08 14:32:39 -04:00
|
|
|
MessageBus.track_publish("/discourse-ai/ai-helper/streamed_suggestion/#{post.id}") do
|
|
|
|
job.execute(post_id: nil, user_id: user.id, text: "pie", prompt: mode)
|
2023-12-12 12:28:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does nothing if there is no user" do
|
|
|
|
messages =
|
|
|
|
MessageBus.track_publish("/discourse-ai/ai-helper/explain/#{post.id}") do
|
2024-08-08 14:32:39 -04:00
|
|
|
job.execute(post_id: post.id, user_id: nil, term_to_explain: "pie", prompt: mode)
|
2023-12-12 12:28:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages).to be_empty
|
|
|
|
end
|
|
|
|
|
2024-08-08 14:32:39 -04:00
|
|
|
it "does nothing if there is no text" do
|
2023-12-12 12:28:39 -05:00
|
|
|
messages =
|
2024-08-08 14:32:39 -04:00
|
|
|
MessageBus.track_publish("/discourse-ai/ai-helper/streamed_suggestion/#{post.id}") do
|
|
|
|
job.execute(post_id: post.id, user_id: user.id, text: nil, prompt: mode)
|
2023-12-12 12:28:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-08-08 14:32:39 -04:00
|
|
|
context "when the prompt is explain" do
|
|
|
|
let(:mode) { CompletionPrompt::EXPLAIN }
|
|
|
|
let(:prompt) { CompletionPrompt.find_by(id: mode) }
|
2023-12-12 12:28:39 -05:00
|
|
|
|
2024-08-08 14:32:39 -04:00
|
|
|
it "publishes updates with a partial result" do
|
|
|
|
explanation =
|
|
|
|
"In this context, \"pie\" refers to a baked dessert typically consisting of a pastry crust and filling."
|
2024-01-12 18:28:06 -05:00
|
|
|
|
2024-08-08 14:32:39 -04:00
|
|
|
partial_explanation = "I"
|
|
|
|
|
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses([explanation]) do
|
|
|
|
messages =
|
|
|
|
MessageBus.track_publish("/discourse-ai/ai-helper/stream_suggestion/#{post.id}") do
|
|
|
|
job.execute(post_id: post.id, user_id: user.id, text: "pie", prompt: prompt.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
partial_result_update = messages.first.data
|
|
|
|
expect(partial_result_update[:done]).to eq(false)
|
|
|
|
expect(partial_result_update[:result]).to eq(partial_explanation)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "publishes a final update to signal we're done" do
|
|
|
|
explanation =
|
|
|
|
"In this context, \"pie\" refers to a baked dessert typically consisting of a pastry crust and filling."
|
2023-12-12 12:28:39 -05:00
|
|
|
|
2024-08-08 14:32:39 -04:00
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses([explanation]) do
|
|
|
|
messages =
|
|
|
|
MessageBus.track_publish("/discourse-ai/ai-helper/stream_suggestion/#{post.id}") do
|
|
|
|
job.execute(post_id: post.id, user_id: user.id, text: "pie", prompt: prompt.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
final_update = messages.last.data
|
|
|
|
expect(final_update[:result]).to eq(explanation)
|
|
|
|
expect(final_update[:done]).to eq(true)
|
|
|
|
end
|
2023-12-12 12:28:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-08-08 14:32:39 -04:00
|
|
|
context "when the prompt is translate" do
|
|
|
|
let(:mode) { CompletionPrompt::TRANSLATE }
|
|
|
|
let(:prompt) { CompletionPrompt.find_by(id: mode) }
|
|
|
|
|
|
|
|
it "publishes updates with a partial result" do
|
|
|
|
sentence = "I like to eat pie."
|
|
|
|
translation = "Me gusta comer pastel."
|
|
|
|
partial_translation = "M"
|
|
|
|
|
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses([translation]) do
|
|
|
|
messages =
|
|
|
|
MessageBus.track_publish("/discourse-ai/ai-helper/stream_suggestion/#{post.id}") do
|
|
|
|
job.execute(post_id: post.id, user_id: user.id, text: sentence, prompt: prompt.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
partial_result_update = messages.first.data
|
|
|
|
expect(partial_result_update[:done]).to eq(false)
|
|
|
|
expect(partial_result_update[:result]).to eq(partial_translation)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "publishes a final update to signal we're done" do
|
|
|
|
sentence = "I like to eat pie."
|
|
|
|
translation = "Me gusta comer pastel."
|
2023-12-12 12:28:39 -05:00
|
|
|
|
2024-08-08 14:32:39 -04:00
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses([translation]) do
|
|
|
|
messages =
|
|
|
|
MessageBus.track_publish("/discourse-ai/ai-helper/stream_suggestion/#{post.id}") do
|
|
|
|
job.execute(post_id: post.id, user_id: user.id, text: sentence, prompt: prompt.name)
|
|
|
|
end
|
2023-12-12 12:28:39 -05:00
|
|
|
|
2024-08-08 14:32:39 -04:00
|
|
|
final_update = messages.last.data
|
|
|
|
expect(final_update[:result]).to eq(translation)
|
|
|
|
expect(final_update[:done]).to eq(true)
|
|
|
|
end
|
2023-12-12 12:28:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|