Skip to content

Commit dbe3e5f

Browse files
authored
Merge pull request #949 from alphagov/chat-115
2 parents f07f134 + fdd750e commit dbe3e5f

2 files changed

Lines changed: 26 additions & 14 deletions

File tree

lib/answer_composition/pipeline/claude/structured_answer_composer.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,20 @@ def call
3636

3737
context.answer.assign_llm_response("structured_answer", llm_response_with_link_token_mapping)
3838
tool_output = response[:content][0][:input]
39+
answer_completeness = tool_output[:answer_completeness]
40+
answered = %w[complete partial].include?(answer_completeness.downcase)
3941

40-
return abort_cannot_answer(start_time, response) unless tool_output[:answered]
42+
unless answered
43+
return abort_cannot_answer(start_time, response, answer_completeness)
44+
end
4145

4246
set_context_sources(tool_output[:sources_used])
43-
return abort_cannot_answer(start_time, response) if context.answer.sources.none?(&:used)
47+
if context.answer.sources.none?(&:used)
48+
return abort_cannot_answer(start_time, response, answer_completeness)
49+
end
4450

4551
message = link_token_mapper.replace_tokens_with_links(tool_output[:answer])
46-
context.answer.assign_attributes(message:, status: "answered", completeness: tool_output[:answer_completeness])
52+
context.answer.assign_attributes(message:, status: "answered", completeness: answer_completeness)
4753
context.answer.assign_metrics("structured_answer", build_metrics(start_time, response))
4854
end
4955

@@ -119,10 +125,11 @@ def tools
119125
[prompt_config[:tool_spec]]
120126
end
121127

122-
def abort_cannot_answer(start_time, response)
128+
def abort_cannot_answer(start_time, response, answer_completeness)
123129
context.abort_pipeline!(
124130
message: Answer::CannedResponses::LLM_CANNOT_ANSWER_MESSAGE,
125131
status: "unanswerable_llm_cannot_answer",
132+
completeness: answer_completeness,
126133
metrics: { "structured_answer" => build_metrics(start_time, response) },
127134
)
128135
end

spec/lib/answer_composition/pipeline/claude/structured_answer_composer_spec.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
before { context.search_results = [search_result] }
2525

2626
shared_examples "llm cannot answer the question" do |options|
27-
it "aborts the pipeline and sets the answer's status and message correctly" do
27+
it "aborts the pipeline and sets the answer's attributes correctly" do
2828
stub_claude_structured_answer(
2929
question.message,
3030
"Sorry I cannot answer that question.",
@@ -34,6 +34,7 @@
3434
expect { described_class.call(context) }.to throw_symbol(:abort)
3535
.and change { context.answer.status }.to("unanswerable_llm_cannot_answer")
3636
.and change { context.answer.message }.to(Answer::CannedResponses::LLM_CANNOT_ANSWER_MESSAGE)
37+
.and change { context.answer.completeness }.to(options[:answer_completeness])
3738
end
3839

3940
it "sets sources used to false for all sources" do
@@ -110,15 +111,19 @@
110111
end
111112
end
112113

113-
it "uses Claude via Anthropic to assign the correct values to the context's answer" do
114-
answer = "VAT (Value Added Tax) is a tax applied to most goods and services in the UK."
115-
stub_claude_structured_answer(question.message, answer)
114+
describe "with valid answer_completeness values" do
115+
%w[complete partial].each do |answer_completeness|
116+
it "uses Claude via Anthropic to assign the correct values to the context's answer" do
117+
answer = "VAT (Value Added Tax) is a tax applied to most goods and services in the UK."
118+
stub_claude_structured_answer(question.message, answer, answer_completeness: answer_completeness)
116119

117-
described_class.call(context)
120+
described_class.call(context)
118121

119-
expect(context.answer.message.squish).to eq(answer)
120-
expect(context.answer)
121-
.to have_attributes(status: "answered", completeness: "complete")
122+
expect(context.answer.message.squish).to eq(answer)
123+
expect(context.answer)
124+
.to have_attributes(status: "answered", completeness: answer_completeness)
125+
end
126+
end
122127
end
123128

124129
it "stores the LLM response" do
@@ -194,11 +199,11 @@
194199
.and change { context.answer.sources.first.used }.to(false)
195200
end
196201

197-
context "when answered is false" do
202+
context "when answer_completeness is not a successful value" do
198203
include_examples "llm cannot answer the question", {
199204
answered: false,
200205
sources_used: [],
201-
answer_completeness: "incomplete",
206+
answer_completeness: "no_information",
202207
}
203208
end
204209

0 commit comments

Comments
 (0)