-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This applies some ideas introduced in [1] to setup testing for Claude with AWS Bedrock. This replaces the existing Bedrock/Claude helper files with a single StubBedrock set of helpers and in there is a general task to stub the Bedrock converse endpoint and then high and low level tasks for stubbing the Claude tool call responses. I've adapted the code where we test Bedrock to make use of these helpers. [1] https://github.com/alphagov/govuk-chat/pull/27/files#r1935884789
- Loading branch information
Showing
8 changed files
with
138 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module StubBedrock | ||
# Usage scenarios | ||
# | ||
# ## Stub question message provides specific answer | ||
# | ||
# client = stub_bedrock_converse( | ||
# bedrock_claude_structured_answer_response("Expected question", "Expected answer") | ||
# ) | ||
# | ||
# ## Simulate basic tool response | ||
# client = stub_bedrock_converse( | ||
# bedrock_claude_tool_response({ "key" => "value" }, tool_user | ||
# ) | ||
# | ||
# ## | ||
# | ||
# ## Simulate an error | ||
# ``` | ||
# client = stub_bedrock_converse("NotFound") | ||
# client.converse(model_id: "just-generating-an-error") | ||
# => Aws::BedrockRuntime::Errors::ServerError: stubbed-response-error-message | ||
# ``` | ||
def stub_bedrock_converse(*responses) | ||
bedrock_client = Aws::BedrockRuntime::Client.new(stub_responses: true) | ||
allow(Aws::BedrockRuntime::Client).to receive(:new).and_return(bedrock_client) | ||
bedrock_client.stub_responses(:converse, responses) | ||
bedrock_client | ||
end | ||
|
||
def bedrock_claude_structured_answer_response(question, answer) | ||
lambda do |context| | ||
given_question = context.params.dig(:messages, -1, :content, 0, :text) | ||
|
||
if question && given_question != question | ||
raise "Unexpected question received: \"#{given_question}\". Expected \"#{question}\"." | ||
end | ||
|
||
bedrock_claude_tool_response( | ||
{ "answer" => answer, "confidence" => 0.9 }, | ||
tool_name: "answer_confidence", | ||
) | ||
end | ||
end | ||
|
||
def bedrock_claude_tool_response(tool_input, | ||
tool_name:, | ||
tool_use_id: SecureRandom.hex, | ||
input_tokens: 10, | ||
output_tokens: 20) | ||
{ | ||
output: { | ||
message: { | ||
role: "assistant", | ||
content: [ | ||
{ | ||
tool_use: { | ||
input: tool_input, | ||
tool_use_id:, | ||
name: tool_name, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
stop_reason: "end_turn", | ||
usage: { | ||
input_tokens:, | ||
output_tokens:, | ||
total_tokens: input_tokens + output_tokens, | ||
}, | ||
metrics: { | ||
latency_ms: 999, | ||
}, | ||
} | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters