|
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) } |
35 | | - |
36 | | - it "returns nil" do |
37 | | - expect(described_class.call(context)).to be_nil |
38 | | - end |
| 33 | + it "includes the current question in the user prompt" do |
| 34 | + described_class.call(context) |
| 35 | + expect(stub).to have_been_requested |
39 | 36 | end |
40 | 37 |
|
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) |
| 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 |
51 | 57 |
|
52 | | - expect(described_class.call(context)).to be_nil |
53 | | - end |
| 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 |
54 | 66 | end |
55 | 67 |
|
56 | | - context "when the question is part of an ongoing chat" do |
57 | | - it "includes the current question in the user prompt" do |
58 | | - described_class.call(context) |
59 | | - expect(stub).to have_been_requested |
60 | | - end |
| 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 |
61 | 72 |
|
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 |
| 73 | + it "assigns metrics to the answer" do |
| 74 | + allow(Clock).to receive(:monotonic_time).and_return(100.0, 101.5) |
81 | 75 |
|
82 | | - anthropic_request = stub_claude_question_rephrasing( |
83 | | - Regexp.new(message_history), |
84 | | - rephrased, |
85 | | - ) |
| 76 | + described_class.call(context) |
86 | 77 |
|
87 | | - described_class.call(context) |
| 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 | + }) |
| 86 | + end |
88 | 87 |
|
89 | | - expect(anthropic_request).to have_been_made |
90 | | - end |
| 88 | + it "assigns the llm response to the answer" do |
| 89 | + described_class.call(context) |
91 | 90 |
|
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 |
| 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 | 96 |
|
97 | | - it "assigns metrics to the answer" do |
98 | | - allow(Clock).to receive(:monotonic_time).and_return(100.0, 101.5) |
| 97 | + expect(context.answer.llm_responses["question_rephrasing"]) |
| 98 | + .to eq(expected_llm_response) |
| 99 | + end |
99 | 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 |
100 | 107 | described_class.call(context) |
101 | 108 |
|
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 | | - }) |
| 109 | + expect(stub).to have_been_requested |
| 110 | + expect(context.question_message).to eq(rephrased) |
110 | 111 | end |
111 | 112 |
|
112 | | - it "assigns the llm response to the answer" do |
113 | | - described_class.call(context) |
| 113 | + it "uses the user_prompt_without_history prompt" do |
| 114 | + expected_prompt = AnswerComposition::Pipeline::Prompts.config( |
| 115 | + :question_rephraser, described_class::DEFAULT_MODEL |
| 116 | + )[:user_prompt_without_history] |
| 117 | + .sub("{question}", question.message) |
| 118 | + |
| 119 | + anthropic_request = stub_claude_question_rephrasing( |
| 120 | + expected_prompt, |
| 121 | + rephrased, |
| 122 | + ) |
114 | 123 |
|
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 |
| 124 | + described_class.call(context) |
120 | 125 |
|
121 | | - expect(context.answer.llm_responses["question_rephrasing"]) |
122 | | - .to eq(expected_llm_response) |
| 126 | + expect(anthropic_request).to have_been_made |
123 | 127 | end |
124 | 128 | end |
125 | 129 |
|
|
225 | 229 | expect(anthropic_request).to have_been_made |
226 | 230 | end |
227 | 231 | end |
| 232 | + |
| 233 | + context "when the model is claude_sonnet_4_0" do |
| 234 | + let!(:stub) do |
| 235 | + stub_claude_question_rephrasing( |
| 236 | + question.message, rephrased, chat_options: { bedrock_model: :claude_sonnet_4_0 } |
| 237 | + ) |
| 238 | + end |
| 239 | + |
| 240 | + before { stub_const("#{described_class}::DEFAULT_MODEL", :claude_sonnet_4_0) } |
| 241 | + |
| 242 | + it "uses the system prompt configured for claude_sonnet_4_0" do |
| 243 | + allow(AnswerComposition::Pipeline::Prompts.config(:question_rephraser, :claude_sonnet_4_0)) |
| 244 | + .to receive(:[]).and_call_original |
| 245 | + |
| 246 | + described_class.call(context) |
| 247 | + |
| 248 | + expect(AnswerComposition::Pipeline::Prompts.config(:question_rephraser, :claude_sonnet_4_0)) |
| 249 | + .to have_received(:[]).with(:system_prompt) |
| 250 | + end |
| 251 | + |
| 252 | + it "calls the llm when there is message history" do |
| 253 | + described_class.call(context) |
| 254 | + |
| 255 | + expect(stub).to have_been_requested |
| 256 | + expect(context.question_message).to eq(rephrased) |
| 257 | + end |
| 258 | + |
| 259 | + context "and all other recent answers have statuses in Answer::STATUSES_EXCLUDED_FROM_REPHRASING" do |
| 260 | + it "returns nil" do |
| 261 | + conversation = create(:conversation) |
| 262 | + create(:question, conversation:) |
| 263 | + Answer::STATUSES_EXCLUDED_FROM_REPHRASING.sample(4) do |status| |
| 264 | + question = create(:question, conversation:) |
| 265 | + create(:answer, question:, status:) |
| 266 | + end |
| 267 | + latest_question = create(:question, conversation:) |
| 268 | + context = build(:answer_pipeline_context, question: latest_question) |
| 269 | + |
| 270 | + expect(described_class.call(context)).to be_nil |
| 271 | + expect(stub).not_to have_been_requested |
| 272 | + end |
| 273 | + end |
| 274 | + |
| 275 | + context "and there is no message history" do |
| 276 | + let(:conversation) { create(:conversation) } |
| 277 | + let(:question) { create(:question, conversation:) } |
| 278 | + let(:context) { build(:answer_pipeline_context, question:) } |
| 279 | + |
| 280 | + it "returns nil" do |
| 281 | + result = described_class.call(context) |
| 282 | + |
| 283 | + expect(stub).not_to have_been_requested |
| 284 | + expect(result).to be_nil |
| 285 | + end |
| 286 | + end |
| 287 | + end |
228 | 288 | end |
0 commit comments