|
30 | 30 | end |
31 | 31 | end |
32 | 32 |
|
33 | | - context "when the question is the beginning of the conversation" do |
34 | | - let(:context) { build(:answer_pipeline_context) } |
| 33 | + it "includes the current question in the user prompt" do |
| 34 | + described_class.call(context) |
| 35 | + expect(stub).to have_been_requested |
| 36 | + end |
35 | 37 |
|
36 | | - it "returns nil" do |
37 | | - expect(described_class.call(context)).to be_nil |
38 | | - end |
| 38 | + it "includes the message_history in the user prompt" do |
| 39 | + message_history = <<~HISTORY.strip |
| 40 | + user: |
| 41 | + """ |
| 42 | + How do I pay my tax |
| 43 | + """ |
| 44 | + assistant: |
| 45 | + """ |
| 46 | + What type of tax |
| 47 | + """ |
| 48 | + user: |
| 49 | + """ |
| 50 | + What types are there |
| 51 | + """ |
| 52 | + assistant: |
| 53 | + """ |
| 54 | + Self-assessment, PAYE, Corporation tax |
| 55 | + """ |
| 56 | + HISTORY |
| 57 | + |
| 58 | + anthropic_request = stub_claude_question_rephrasing( |
| 59 | + Regexp.new(message_history), |
| 60 | + rephrased, |
| 61 | + ) |
| 62 | + |
| 63 | + described_class.call(context) |
| 64 | + |
| 65 | + expect(anthropic_request).to have_been_made |
39 | 66 | end |
40 | 67 |
|
41 | | - context "when all other recent answers have statuses in Answer::STATUSES_EXCLUDED_FROM_REPHRASING" do |
42 | | - it "returns nil" do |
43 | | - conversation = create(:conversation) |
44 | | - create(:question, conversation:) |
45 | | - Answer::STATUSES_EXCLUDED_FROM_REPHRASING.sample(4) do |status| |
46 | | - question = create(:question, conversation:) |
47 | | - create(:answer, question:, status:) |
48 | | - end |
49 | | - latest_question = create(:question, conversation:) |
50 | | - context = build(:answer_pipeline_context, question: latest_question) |
| 68 | + it "updates the context's question_message with the rephrased question" do |
| 69 | + described_class.call(context) |
| 70 | + expect(context.question_message).to eq(rephrased) |
| 71 | + end |
51 | 72 |
|
52 | | - expect(described_class.call(context)).to be_nil |
53 | | - end |
| 73 | + it "assigns metrics to the answer" do |
| 74 | + allow(Clock).to receive(:monotonic_time).and_return(100.0, 101.5) |
| 75 | + |
| 76 | + described_class.call(context) |
| 77 | + |
| 78 | + expect(context.answer.metrics["question_rephrasing"]) |
| 79 | + .to eq({ |
| 80 | + duration: 1.5, |
| 81 | + llm_prompt_tokens: 10, |
| 82 | + llm_completion_tokens: 20, |
| 83 | + llm_cached_tokens: nil, |
| 84 | + model: BedrockModels.model_id(described_class::DEFAULT_MODEL), |
| 85 | + }) |
54 | 86 | end |
55 | 87 |
|
56 | | - context "when the question is part of an ongoing chat" do |
57 | | - it "includes the current question in the user prompt" do |
| 88 | + it "assigns the llm response to the answer" do |
| 89 | + described_class.call(context) |
| 90 | + |
| 91 | + expected_llm_response = claude_messages_response( |
| 92 | + content: [claude_messages_text_block(rephrased)], |
| 93 | + usage: claude_messages_usage_block(input_tokens: 10, output_tokens: 20), |
| 94 | + bedrock_model: described_class::DEFAULT_MODEL, |
| 95 | + ).to_h |
| 96 | + |
| 97 | + expect(context.answer.llm_responses["question_rephrasing"]) |
| 98 | + .to eq(expected_llm_response) |
| 99 | + end |
| 100 | + |
| 101 | + context "when the question is the first in the conversation" do |
| 102 | + let(:question) { create(:question) } |
| 103 | + let(:context) { build(:answer_pipeline_context, question:) } |
| 104 | + let!(:stub) { stub_claude_question_rephrasing(question.message, rephrased) } |
| 105 | + |
| 106 | + it "calls the llm and rephrases the question" do |
58 | 107 | described_class.call(context) |
59 | 108 | expect(stub).to have_been_requested |
60 | 109 | end |
61 | 110 |
|
62 | | - it "includes the message_history in the user prompt" do |
63 | | - message_history = <<~HISTORY.strip |
64 | | - user: |
65 | | - """ |
66 | | - How do I pay my tax |
67 | | - """ |
68 | | - assistant: |
69 | | - """ |
70 | | - What type of tax |
71 | | - """ |
72 | | - user: |
73 | | - """ |
74 | | - What types are there |
75 | | - """ |
76 | | - assistant: |
77 | | - """ |
78 | | - Self-assessment, PAYE, Corporation tax |
79 | | - """ |
80 | | - HISTORY |
| 111 | + it "doesn't include a message history in the prompt" do |
| 112 | + expected_prompt = AnswerComposition::Pipeline::Prompts.config( |
| 113 | + :question_rephraser, described_class::DEFAULT_MODEL |
| 114 | + )[:user_prompt_without_history] |
| 115 | + .sub("{question}", question.message) |
81 | 116 |
|
82 | 117 | anthropic_request = stub_claude_question_rephrasing( |
83 | | - Regexp.new(message_history), |
| 118 | + expected_prompt, |
84 | 119 | rephrased, |
85 | 120 | ) |
86 | 121 |
|
87 | 122 | described_class.call(context) |
88 | 123 |
|
89 | 124 | expect(anthropic_request).to have_been_made |
90 | 125 | end |
91 | | - |
92 | | - it "updates the context's question_message with the rephrased question" do |
93 | | - described_class.call(context) |
94 | | - expect(context.question_message).to eq(rephrased) |
95 | | - end |
96 | | - |
97 | | - it "assigns metrics to the answer" do |
98 | | - allow(Clock).to receive(:monotonic_time).and_return(100.0, 101.5) |
99 | | - |
100 | | - described_class.call(context) |
101 | | - |
102 | | - expect(context.answer.metrics["question_rephrasing"]) |
103 | | - .to eq({ |
104 | | - duration: 1.5, |
105 | | - llm_prompt_tokens: 10, |
106 | | - llm_completion_tokens: 20, |
107 | | - llm_cached_tokens: nil, |
108 | | - model: BedrockModels.model_id(described_class::DEFAULT_MODEL), |
109 | | - }) |
110 | | - end |
111 | | - |
112 | | - it "assigns the llm response to the answer" do |
113 | | - described_class.call(context) |
114 | | - |
115 | | - expected_llm_response = claude_messages_response( |
116 | | - content: [claude_messages_text_block(rephrased)], |
117 | | - usage: claude_messages_usage_block(input_tokens: 10, output_tokens: 20), |
118 | | - bedrock_model: described_class::DEFAULT_MODEL, |
119 | | - ).to_h |
120 | | - |
121 | | - expect(context.answer.llm_responses["question_rephrasing"]) |
122 | | - .to eq(expected_llm_response) |
123 | | - end |
124 | 126 | end |
125 | 127 |
|
126 | 128 | context "with a long history" do |
|
225 | 227 | expect(anthropic_request).to have_been_made |
226 | 228 | end |
227 | 229 | end |
| 230 | + |
| 231 | + context "when the model is claude_sonnet_4_0" do |
| 232 | + let!(:stub) do |
| 233 | + stub_claude_question_rephrasing( |
| 234 | + question.message, rephrased, chat_options: { bedrock_model: :claude_sonnet_4_0 } |
| 235 | + ) |
| 236 | + end |
| 237 | + |
| 238 | + before { stub_const("#{described_class}::DEFAULT_MODEL", :claude_sonnet_4_0) } |
| 239 | + |
| 240 | + it "uses the system prompt configured for claude_sonnet_4_0" do |
| 241 | + allow(AnswerComposition::Pipeline::Prompts.config(:question_rephraser, :claude_sonnet_4_0)) |
| 242 | + .to receive(:[]).and_call_original |
| 243 | + |
| 244 | + described_class.call(context) |
| 245 | + |
| 246 | + expect(AnswerComposition::Pipeline::Prompts.config(:question_rephraser, :claude_sonnet_4_0)) |
| 247 | + .to have_received(:[]).with(:system_prompt) |
| 248 | + end |
| 249 | + |
| 250 | + it "calls the llm when there is message history" do |
| 251 | + described_class.call(context) |
| 252 | + |
| 253 | + expect(stub).to have_been_requested |
| 254 | + expect(context.question_message).to eq(rephrased) |
| 255 | + end |
| 256 | + |
| 257 | + context "when all other recent answers have statuses in Answer::STATUSES_EXCLUDED_FROM_REPHRASING" do |
| 258 | + it "returns nil" do |
| 259 | + conversation = create(:conversation) |
| 260 | + create(:question, conversation:) |
| 261 | + Answer::STATUSES_EXCLUDED_FROM_REPHRASING.sample(4) do |status| |
| 262 | + question = create(:question, conversation:) |
| 263 | + create(:answer, question:, status:) |
| 264 | + end |
| 265 | + latest_question = create(:question, conversation:) |
| 266 | + context = build(:answer_pipeline_context, question: latest_question) |
| 267 | + |
| 268 | + expect(described_class.call(context)).to be_nil |
| 269 | + expect(stub).not_to have_been_requested |
| 270 | + end |
| 271 | + end |
| 272 | + |
| 273 | + context "when there is no message history" do |
| 274 | + let(:conversation) { create(:conversation) } |
| 275 | + let(:question) { create(:question, conversation:) } |
| 276 | + let(:context) { build(:answer_pipeline_context, question:) } |
| 277 | + |
| 278 | + it "returns nil" do |
| 279 | + result = described_class.call(context) |
| 280 | + |
| 281 | + expect(stub).not_to have_been_requested |
| 282 | + expect(result).to be_nil |
| 283 | + end |
| 284 | + end |
| 285 | + end |
228 | 286 | end |
0 commit comments