-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbedrock_utils.py
More file actions
149 lines (124 loc) · 4.4 KB
/
bedrock_utils.py
File metadata and controls
149 lines (124 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import boto3
from botocore.exceptions import ClientError
import json
# Initialize AWS Bedrock client
bedrock = boto3.client(
service_name='bedrock-runtime',
region_name='us-east-1' # Replace with your AWS region
)
# Initialize Bedrock Knowledge Base client
bedrock_kb = boto3.client(
service_name='bedrock-agent-runtime',
region_name='us-east-1' # Replace with your AWS region
)
def valid_prompt(prompt: str, model_id: str) -> bool:
"""
Validate the user's prompt by classifying it into one of several categories.
Only prompts classified as Category E (heavy machinery) are allowed.
Returns:
True -> prompt is valid
False -> prompt is invalid
"""
classification_prompt = f"""
Human: Classify the provided user request into ONE of the following categories.
Category A: The request is asking about how the LLM model works or system architecture.
Category B: The request uses profanity, toxic intent, or harmful wording.
Category C: The request is about any subject NOT related to heavy machinery.
Category D: The request is asking about instructions, system behavior, or meta-questions.
Category E: The request is ONLY related to heavy machinery topics.
<user_request>
{prompt}
</user_request>
Respond ONLY with the category letter (Example: "Category C").
Assistant:
"""
try:
messages = [
{
"role": "user",
"content": [{"type": "text", "text": classification_prompt}]
}
]
response = bedrock.invoke_model(
modelId=model_id,
contentType="application/json",
accept="application/json",
body=json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"messages": messages,
"max_tokens": 10,
"temperature": 0,
"top_p": 0.1,
})
)
result_text = json.loads(response["body"].read())["content"][0]["text"]
cleaned = result_text.lower().strip()
print(f"[Prompt Classification] {cleaned}")
return cleaned == "category e"
except ClientError as e:
print(f"Error validating prompt: {e}")
return False
def query_knowledge_base(query, kb_id):
"""
Query the Bedrock Knowledge Base and return results with source metadata.
Returns:
list: List of dicts containing 'content', 'score', and 'source' information
"""
try:
response = bedrock_kb.retrieve(
knowledgeBaseId=kb_id,
retrievalQuery={
'text': query
},
retrievalConfiguration={
'vectorSearchConfiguration': {
'numberOfResults': 3
}
}
)
# Extract and enrich results with source information
enriched_results = []
for result in response['retrievalResults']:
enriched_result = {
'content': result['content']['text'],
'score': result.get('score', 0),
'source': {
'type': result['location']['type'],
's3_uri': result['location'].get('s3Location', {}).get('uri', 'Unknown'),
'metadata': result.get('metadata', {})
}
}
enriched_results.append(enriched_result)
return enriched_results
except ClientError as e:
print(f"Error querying Knowledge Base: {e}")
return []
def generate_response(prompt, model_id, temperature, top_p):
try:
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
}
]
}
]
response = bedrock.invoke_model(
modelId=model_id,
contentType='application/json',
accept='application/json',
body=json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"messages": messages,
"max_tokens": 500,
"temperature": temperature,
"top_p": top_p,
})
)
return json.loads(response['body'].read())['content'][0]["text"]
except ClientError as e:
print(f"Error generating response: {e}")
return ""