-
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.
Merge pull request #27 from alphagov/bedrock-basic-answer-strat
Bedrock basic answer strategy
- Loading branch information
Showing
17 changed files
with
383 additions
and
13 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
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
99 changes: 99 additions & 0 deletions
99
lib/answer_composition/pipeline/claude/structured_answer_composer.rb
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,99 @@ | ||
module AnswerComposition::Pipeline::Claude | ||
class StructuredAnswerComposer | ||
BEDROCK_MODEL = "eu.anthropic.claude-3-5-sonnet-20240620-v1:0".freeze | ||
|
||
def self.call(...) = new(...).call | ||
|
||
def initialize(context) | ||
@context = context | ||
end | ||
|
||
def call | ||
start_time = Clock.monotonic_time | ||
|
||
response = bedrock_client.converse( | ||
system: [{ text: system_prompt }], | ||
model_id: BEDROCK_MODEL, | ||
messages:, | ||
inference_config:, | ||
tool_config:, | ||
) | ||
|
||
context.answer.assign_llm_response("structured_answer", response.to_h) | ||
message = response.dig("output", "message", "content", 0, "tool_use", "input", "answer") | ||
context.answer.assign_attributes(message:, status: "answered") | ||
context.answer.assign_metrics("structured_answer", build_metrics(start_time, response)) | ||
end | ||
|
||
private | ||
|
||
attr_reader :context | ||
|
||
def messages | ||
[ | ||
{ | ||
role: "user", | ||
content: [{ text: context.question_message }], | ||
}, | ||
] | ||
end | ||
|
||
def inference_config | ||
{ | ||
max_tokens: 1000, | ||
temperature: 0.0, | ||
} | ||
end | ||
|
||
def system_prompt | ||
<<~PROMPT | ||
You are a chat assistant for the UK government, designed to provide helpful and contextually relevant responses to user queries. | ||
Provide concise responses based on the content on the GOV.UK website. | ||
PROMPT | ||
end | ||
|
||
def bedrock_client | ||
@bedrock_client ||= Aws::BedrockRuntime::Client.new | ||
end | ||
|
||
def build_metrics(start_time, response) | ||
{ | ||
duration: Clock.monotonic_time - start_time, | ||
llm_prompt_tokens: response.dig("usage", "input_tokens"), | ||
llm_completion_tokens: response.dig("usage", "output_tokens"), | ||
} | ||
end | ||
|
||
def tool_config | ||
{ | ||
tools: tools, | ||
tool_choice: { | ||
tool: { | ||
name: "answer_confidence", | ||
}, | ||
}, | ||
} | ||
end | ||
|
||
def tools | ||
[ | ||
{ | ||
tool_spec: { | ||
name: "answer_confidence", | ||
description: "Prints the answer of a given question with a confidence score.", | ||
input_schema: { | ||
json: { | ||
type: "object", | ||
properties: { | ||
answer: { description: "Your answer to the question in markdown format", title: "Answer", type: "string" }, | ||
confidence: { description: "Your confidence in the answer provided, ranging from 0.0 to 1.0", title: "Confidence", type: "number" }, | ||
}, | ||
required: %w[answer confidence], | ||
}, | ||
}, | ||
}, | ||
}, | ||
] | ||
end | ||
end | ||
end |
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
52 changes: 52 additions & 0 deletions
52
spec/lib/answer_composition/pipeline/claude/structured_answer_composer_spec.rb
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,52 @@ | ||
RSpec.describe AnswerComposition::Pipeline::Claude::StructuredAnswerComposer, :chunked_content_index do | ||
describe ".call" do | ||
let(:question) { build :question } | ||
let(:context) { build(:answer_pipeline_context, question:) } | ||
|
||
it "uses Bedrock converse endpoint to assign the correct values to the context's answer" do | ||
answer = "VAT (Value Added Tax) is a tax applied to most goods and services in the UK." | ||
|
||
stub_bedrock_converse( | ||
bedrock_claude_structured_answer_response(question.message, answer), | ||
) | ||
|
||
described_class.call(context) | ||
|
||
expect(context.answer.message).to eq(answer) | ||
expect(context.answer.status).to eq("answered") | ||
end | ||
|
||
it "stores the LLM response" do | ||
response = bedrock_claude_tool_response( | ||
{ "answer" => "answer", "confidence" => 0.9 }, | ||
tool_name: "answer_confidence", | ||
) | ||
|
||
stub_bedrock_converse(response) | ||
|
||
described_class.call(context) | ||
expect(context.answer.llm_responses["structured_answer"]).to match(response) | ||
end | ||
|
||
it "assigns metrics to the answer" do | ||
allow(Clock).to receive(:monotonic_time).and_return(100.0, 101.5) | ||
|
||
stub_bedrock_converse( | ||
bedrock_claude_tool_response( | ||
{ "answer" => "answer", "confidence" => 0.9 }, | ||
tool_name: "answer_confidence", | ||
input_tokens: 15, | ||
output_tokens: 25, | ||
), | ||
) | ||
|
||
described_class.call(context) | ||
|
||
expect(context.answer.metrics["structured_answer"]).to eq({ | ||
duration: 1.5, | ||
llm_prompt_tokens: 15, | ||
llm_completion_tokens: 25, | ||
}) | ||
end | ||
end | ||
end |
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
Oops, something went wrong.