Skip to content

Commit 098fde9

Browse files
authored
Merge pull request #229 from alphagov/fix-length-bug
Fix JSON parsing error for long question routing responses
2 parents 1082e51 + 0f3c88e commit 098fde9

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

lib/answer_composition/pipeline/openai/question_router.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ def call
1818
if genuine_rag?
1919
answer.assign_attributes(
2020
question_routing_label:,
21-
question_routing_confidence_score: llm_classification_data["confidence"],
21+
question_routing_confidence_score: confidence_score,
2222
)
2323
else
2424
answer.assign_attributes(
2525
message: use_llm_answer? ? llm_answer : Answer::CannedResponses.response_for_question_routing_label(question_routing_label),
2626
status: answer_status,
2727
question_routing_label:,
28-
question_routing_confidence_score: llm_classification_data["confidence"],
28+
question_routing_confidence_score: confidence_score,
2929
)
3030

3131
context.abort_pipeline unless use_llm_answer?
@@ -58,6 +58,12 @@ def openai_token_limit_reached?
5858
openai_response_choice["finish_reason"] == "length"
5959
end
6060

61+
def confidence_score
62+
return if openai_token_limit_reached?
63+
64+
llm_classification_data["confidence"]
65+
end
66+
6167
def llm_answer
6268
llm_classification_data["answer"]
6369
end

spec/lib/answer_composition/pipeline/openai/question_router_spec.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
end
3939

4040
let(:classification_response) do
41-
{ answer: "Hello!", confidence: 0.85 }
41+
{ answer: "Hello!", confidence: 0.85 }.to_json
4242
end
4343

4444
let(:expected_message_history) do
@@ -113,7 +113,7 @@
113113
expected_message_history,
114114
tools:,
115115
function_name: "genuine_rag",
116-
function_arguments: { answer: "Generic answer", confidence: 0.9 },
116+
function_arguments: { answer: "Generic answer", confidence: 0.9 }.to_json,
117117
)
118118
end
119119

@@ -144,10 +144,7 @@
144144
expected_message_history,
145145
tools:,
146146
function_name: "vague_acronym_grammar",
147-
function_arguments: {
148-
answer: "A long answer that is terminated mid senten",
149-
confidence: 0.99,
150-
},
147+
function_arguments: '{"answer": "A long answer that is terminated mid senten',
151148
finish_reason: "length",
152149
)
153150
end
@@ -184,7 +181,7 @@
184181
expected_message_history,
185182
tools:,
186183
function_name: "multi_questions",
187-
function_arguments: { answer: answer_message, confidence: 0.9 },
184+
function_arguments: { answer: answer_message, confidence: 0.9 }.to_json,
188185
)
189186
end
190187

@@ -211,7 +208,7 @@
211208
expected_message_history,
212209
tools:,
213210
function_name: "harmful_vulgar_controversy",
214-
function_arguments: { answer: "Ignored", confidence: 0.9 },
211+
function_arguments: { answer: "Ignored", confidence: 0.9 }.to_json,
215212
)
216213
end
217214

0 commit comments

Comments
 (0)