Skip to content

Commit 407adab

Browse files
committed
Update query reformulator to use direct invoke_model and remove unused PromptManagement construct
1 parent 942e080 commit 407adab

File tree

5 files changed

+29
-80
lines changed

5 files changed

+29
-80
lines changed

packages/cdk/resources/Functions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {TableV2} from "aws-cdk-lib/aws-dynamodb"
77

88
// Claude model for RAG responses
99
const RAG_MODEL_ID = "anthropic.claude-3-sonnet-20240229-v1:0"
10+
// Claude model for query reformulation
11+
const QUERY_REFORMULATION_MODEL_ID = "anthropic.claude-3-haiku-20240307-v1:0"
1012
const BEDROCK_KB_DATA_SOURCE = "eps-assist-kb-ds"
1113
const LAMBDA_MEMORY_SIZE = "265"
1214

@@ -60,6 +62,7 @@ export class Functions extends Construct {
6062
additionalPolicies: [props.slackBotManagedPolicy],
6163
environmentVariables: {
6264
"RAG_MODEL_ID": RAG_MODEL_ID,
65+
"QUERY_REFORMULATION_MODEL_ID": QUERY_REFORMULATION_MODEL_ID,
6366
"KNOWLEDGEBASE_ID": props.knowledgeBaseId || "placeholder",
6467
"BEDROCK_KB_DATA_SOURCE": BEDROCK_KB_DATA_SOURCE,
6568
"LAMBDA_MEMORY_SIZE": LAMBDA_MEMORY_SIZE,

packages/cdk/resources/IamResources.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,9 @@ export class IamResources extends Construct {
152152
resources: [`arn:aws:bedrock:${props.region}:${props.account}:guardrail/*`]
153153
})
154154

155-
const slackBotPromptPolicy = new PolicyStatement({
156-
actions: [
157-
"bedrock:InvokeModel",
158-
"bedrock:GetPrompt"
159-
],
160-
resources: [
161-
`arn:aws:bedrock:${props.region}::foundation-model/${QUERY_REFORMULATION_MODEL_ID}`,
162-
`arn:aws:bedrock:${props.region}:${props.account}:prompt/*`
163-
]
155+
const slackBotQueryReformulationPolicy = new PolicyStatement({
156+
actions: ["bedrock:InvokeModel"],
157+
resources: [`arn:aws:bedrock:${props.region}::foundation-model/${QUERY_REFORMULATION_MODEL_ID}`]
164158
})
165159

166160
const slackBotDynamoDbPolicy = new PolicyStatement({
@@ -196,7 +190,7 @@ export class IamResources extends Construct {
196190
slackBotSSMPolicy,
197191
slackBotLambdaPolicy,
198192
slackBotGuardrailPolicy,
199-
slackBotPromptPolicy,
193+
slackBotQueryReformulationPolicy,
200194
slackBotDynamoDbPolicy,
201195
slackBotKmsPolicy
202196
]

packages/cdk/resources/PromptManagement.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

packages/cdk/stacks/EpsAssistMeStack.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {Storage} from "../resources/Storage"
1111
import {Secrets} from "../resources/Secrets"
1212
import {OpenSearchResources} from "../resources/OpenSearchResources"
1313
import {VectorKnowledgeBaseResources} from "../resources/VectorKnowledgeBaseResources"
14-
import {PromptManagement} from "../resources/PromptManagement"
1514
import {IamResources} from "../resources/IamResources"
1615
import {VectorIndex} from "../resources/VectorIndex"
1716
import {DatabaseTables} from "../resources/DatabaseTables"
@@ -112,11 +111,6 @@ export class EpsAssistMeStack extends Stack {
112111
endpoint
113112
})
114113

115-
// Create Prompt Management resources
116-
const promptManagement = new PromptManagement(this, "PromptManagement", {
117-
stackName: props.stackName
118-
})
119-
120114
// Create VectorKnowledgeBase construct after vector index
121115
const vectorKB = new VectorKnowledgeBaseResources(this, "VectorKB", {
122116
stackName: props.stackName,
@@ -133,10 +127,6 @@ export class EpsAssistMeStack extends Stack {
133127
functions.functions.slackBot.function.addEnvironment("GUARD_RAIL_ID", vectorKB.guardrail.attrGuardrailId)
134128
functions.functions.slackBot.function.addEnvironment("GUARD_RAIL_VERSION", vectorKB.guardrail.attrVersion)
135129
functions.functions.slackBot.function.addEnvironment("KNOWLEDGEBASE_ID", vectorKB.knowledgeBase.attrKnowledgeBaseId)
136-
functions.functions.slackBot.function.addEnvironment(
137-
"QUERY_REFORMULATION_PROMPT_ARN",
138-
promptManagement.queryReformulationPrompt.attrArn
139-
)
140130

141131
// Create Apis and pass the Lambda function
142132
const apis = new Apis(this, "Apis", {

packages/slackBotFunction/query_reformulator.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,35 @@
88

99
def reformulate_query(user_query: str) -> str:
1010
"""
11-
Reformulate user query using Bedrock Prompt Management for better RAG retrieval.
11+
Reformulate user query using Claude Haiku for better RAG retrieval.
1212
"""
1313
try:
1414
client = boto3.client("bedrock-runtime", region_name=os.environ["AWS_REGION"])
15-
prompt_arn = os.environ["QUERY_REFORMULATION_PROMPT_ARN"]
15+
model_id = os.environ["QUERY_REFORMULATION_MODEL_ID"]
16+
17+
prompt = f"""You are a query reformulation assistant for the NHS EPS API documentation system.
18+
19+
Reformulate user queries to improve retrieval from a knowledge base containing FHIR NHS EPS API documentation.
20+
21+
Guidelines:
22+
- Expand abbreviations (EPS = Electronic Prescription Service, FHIR = Fast Healthcare Interoperability Resources)
23+
- Add relevant technical context (API, prescription, dispensing, healthcare)
24+
- Convert casual language to technical terminology
25+
- Include synonyms for better matching
26+
- Keep the core intent intact
27+
- Focus on NHS, healthcare, prescription, and API-related terms
28+
29+
User Query: {user_query}
30+
31+
Reformulated Query:"""
1632

1733
response = client.invoke_model(
18-
modelId="anthropic.claude-3-haiku-20240307-v1:0",
34+
modelId=model_id,
1935
body=json.dumps(
2036
{
2137
"anthropic_version": "bedrock-2023-05-31",
2238
"max_tokens": 200,
23-
"prompt": prompt_arn,
24-
"variables": {"query": user_query},
39+
"messages": [{"role": "user", "content": prompt}],
2540
}
2641
),
2742
)
@@ -30,18 +45,11 @@ def reformulate_query(user_query: str) -> str:
3045
reformulated_query = result["content"][0]["text"].strip()
3146

3247
logger.info(
33-
"Query reformulated",
34-
extra={
35-
"original_query": user_query,
36-
"reformulated_query": reformulated_query,
37-
},
48+
"Query reformulated", extra={"original_query": user_query, "reformulated_query": reformulated_query}
3849
)
3950

4051
return reformulated_query
4152

4253
except Exception as e:
43-
logger.error(
44-
f"Error reformulating query: {e}",
45-
extra={"original_query": user_query, "prompt_arn": os.environ.get("QUERY_REFORMULATION_PROMPT_ARN")},
46-
)
54+
logger.error(f"Error reformulating query: {e}", extra={"original_query": user_query})
4755
return user_query # Fallback to original query

0 commit comments

Comments
 (0)