diff --git a/app/models/completion_prompt.rb b/app/models/completion_prompt.rb index 0176d4c2..961941d2 100644 --- a/app/models/completion_prompt.rb +++ b/app/models/completion_prompt.rb @@ -37,7 +37,7 @@ class CompletionPrompt < ActiveRecord::Base prompt = DiscourseAi::Completions::Prompt.new(instructions) - messages_hash[:examples].to_a do |example_pair| + messages_hash[:examples].to_a.each do |example_pair| prompt.push(type: :user, content: example_pair.first) prompt.push(type: :model, content: example_pair.second) end diff --git a/spec/models/completion_prompt_spec.rb b/spec/models/completion_prompt_spec.rb index 7ed4e2d7..dd44c85c 100644 --- a/spec/models/completion_prompt_spec.rb +++ b/spec/models/completion_prompt_spec.rb @@ -22,6 +22,30 @@ RSpec.describe CompletionPrompt do describe "messages_with_input" do let(:user_input) { "A user wrote this." } + context "when mapping to a prompt" do + it "correctly maps everything to the prompt" do + cp = + CompletionPrompt.new( + messages: { + insts: "Instructions", + post_insts: "Post Instructions", + examples: [["Request 1", "Response 1"]], + }, + ) + + prompt = cp.messages_with_input("hello") + + expected = [ + { type: :system, content: "Instructions\nPost Instructions" }, + { type: :user, content: "Request 1" }, + { type: :model, content: "Response 1" }, + { type: :user, content: "hello" }, + ] + + expect(prompt.messages).to eq(expected) + end + end + context "when the record has the custom_prompt type" do let(:custom_prompt) { described_class.find(described_class::CUSTOM_PROMPT) }