Skip to content

Commit 1658e48

Browse files
authored
Merge pull request #1105 from alphagov/538-retry-cleanup-work
[CHAT-538] Pass less information to logs for BedrockOpenAIOssInvoke retries
2 parents 5d83f96 + dbf0905 commit 1658e48

2 files changed

Lines changed: 3 additions & 53 deletions

File tree

lib/auto_evaluation/bedrock_openai_oss_invoke.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ def call
3535
client = Aws::BedrockRuntime::Client.new
3636

3737
attempts = 0
38-
last_error = nil
3938

4039
while attempts <= MAX_ATTEMPTS
4140
attempts += 1
@@ -70,23 +69,17 @@ def call
7069
metrics: build_metrics(start_time, parsed_response),
7170
)
7271
rescue *RESCUABLE_ERRORS => e
73-
error_string = "#{e.class}, #{e.message}"
7472
full_error_message = "LLM did not return valid JSON that conformed to the schema. " \
75-
"Attempt #{attempts}/#{MAX_ATTEMPTS}. Error: #{error_string}."
76-
77-
if last_error.present? && error_string != last_error
78-
full_error_message += " This error is different from the previous error: #{last_error}."
79-
end
73+
"Attempt #{attempts}/#{MAX_ATTEMPTS}."
8074

8175
logger.warn(full_error_message)
8276

8377
if attempts >= MAX_ATTEMPTS
8478
raise InvalidLlmResponseError, "LLM did not return valid JSON that conformed to the schema " \
85-
"after #{MAX_ATTEMPTS} attempts. Error: #{error_string}"
79+
"after #{MAX_ATTEMPTS} attempts. Error: #{e.class}, #{e.message}"
8680

8781
end
8882

89-
last_error = error_string
9083
next
9184
end
9285
end

spec/lib/auto_evaluation/bedrock_openai_oss_invoke_spec.rb

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
allow(Rails).to receive(:logger).and_return(logger)
4545
(1..described_class::MAX_ATTEMPTS).each do |i|
4646
expected_log_message = "LLM did not return valid JSON that conformed to the schema. " \
47-
"Attempt #{i}/#{described_class::MAX_ATTEMPTS}. " \
48-
"Error: #{error_class}, #{error_message}"
47+
"Attempt #{i}/#{described_class::MAX_ATTEMPTS}."
4948
expect(logger).to receive(:warn)
5049
.with(/#{expected_log_message}/)
5150
.ordered
@@ -55,48 +54,6 @@
5554
described_class.call(user_message:, tool:)
5655
}.to raise_error(described_class::InvalidLlmResponseError)
5756
end
58-
59-
it "logs additional information when the error message (or class) changes between retries" do
60-
logger = instance_double(Logger)
61-
allow(Rails).to receive(:logger).and_return(logger)
62-
allow(logger).to receive(:warn)
63-
64-
call_count = 0
65-
original_response_body = stub.response.body
66-
67-
stub_request(:post, StubBedrock::OPENAI_GPT_OSS_ENDPOINT_REGEX).to_return do |_request|
68-
call_count += 1
69-
if call_count == 1
70-
{ status: 200, headers: { "Content-Type" => "application/json" }, body: original_response_body }
71-
else
72-
{
73-
status: 200,
74-
headers: { "Content-Type" => "application/json" },
75-
body: {
76-
choices: [{
77-
message: {
78-
tool_calls: [{ function: { arguments: "not_json" } }],
79-
},
80-
finish_reason: "stop",
81-
}],
82-
usage: { prompt_tokens: 10, completion_tokens: 10 },
83-
}.to_json,
84-
}
85-
end
86-
end
87-
88-
error = "#{error_class}, #{error_message}"
89-
expect(logger).to receive(:warn)
90-
.with(/#{error}/)
91-
.ordered
92-
expect(logger).to receive(:warn)
93-
.with(/This error is different from the previous error: #{error}/)
94-
.ordered
95-
96-
expect {
97-
described_class.call(user_message: user_message, tool: tool)
98-
}.to raise_error(described_class::InvalidLlmResponseError)
99-
end
10057
end
10158

10259
it "returns a Result object with the evaluation data" do

0 commit comments

Comments
 (0)