|
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 |
| 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 |
| 66 | + end |
51 | 67 |
|
52 | | - expect(described_class.call(context)).to be_nil |
53 | | - 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) |
54 | 71 | end |
55 | 72 |
|
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 |
| 73 | + it "assigns metrics to the answer" do |
| 74 | + allow(Clock).to receive(:monotonic_time).and_return(100.0, 101.5) |
61 | 75 |
|
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 |
81 | | - |
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(:claude_sonnet_4_0), |
| 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 | + ).to_h |
96 | 95 |
|
97 | | - it "assigns metrics to the answer" do |
98 | | - allow(Clock).to receive(:monotonic_time).and_return(100.0, 101.5) |
| 96 | + expect(context.answer.llm_responses["question_rephrasing"]) |
| 97 | + .to eq(expected_llm_response) |
| 98 | + end |
99 | 99 |
|
100 | | - described_class.call(context) |
| 100 | + context "when the question is the first in the conversation" do |
| 101 | + let(:question) { create(:question) } |
| 102 | + let(:context) { build(:answer_pipeline_context, question:) } |
| 103 | + let!(:stub) { stub_claude_question_rephrasing(question.message, rephrased) } |
101 | 104 |
|
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(:claude_sonnet_4_0), |
109 | | - }) |
110 | | - end |
111 | 105 |
|
112 | | - it "assigns the llm response to the answer" do |
| 106 | + it "calls the llm and rephrases the question" do |
113 | 107 | 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 | | - ).to_h |
119 | | - |
120 | | - expect(context.answer.llm_responses["question_rephrasing"]) |
121 | | - .to eq(expected_llm_response) |
| 108 | + expect(stub).to have_been_requested |
122 | 109 | end |
123 | 110 | end |
124 | 111 |
|
|
0 commit comments