Skip to content

Commit 47e04ff

Browse files
committed
Finished migrating to Qwen and Messages API
1 parent f4c06dd commit 47e04ff

File tree

4 files changed

+132
-142
lines changed

4 files changed

+132
-142
lines changed

workshops/fine-tuning-with-sagemakerai-and-bedrock/task_05_fmops/05.01_fine-tuning-pipeline.ipynb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@
485485
"outputs": [],
486486
"source": [
487487
"from steps import pipeline_utils\n",
488-
"guardrail_id, guardrail_version = pipeline_utils.get_or_create_guardrail()"
488+
"guardrail_id, guardrail_version =pipeline_utils.get_or_create_guardrail()"
489489
]
490490
},
491491
{
@@ -523,12 +523,10 @@
523523
")\n",
524524
"run_id=training_step[0]\n",
525525
"model_artifacts_s3_path=training_step[2]\n",
526-
"# output_path=training_step[3]\n",
527526
"\n",
528527
"deploy_step = deploy_step.deploy(\n",
529528
" tracking_server_arn=mlflow_tracking_server_arn,\n",
530529
" model_artifacts_s3_path=model_artifacts_s3_path,\n",
531-
" # output_path=output_path,\n",
532530
" model_id=model_s3_destination,\n",
533531
" experiment_name=pipeline_name,\n",
534532
" run_id=run_id,\n",
@@ -742,6 +740,13 @@
742740
" print(f\"Error during endpoint cleanup: {str(e)}\")\n",
743741
" print(\"You may need to manually delete the endpoint from the SageMaker console\")"
744742
]
743+
},
744+
{
745+
"cell_type": "code",
746+
"execution_count": null,
747+
"metadata": {},
748+
"outputs": [],
749+
"source": []
745750
}
746751
],
747752
"metadata": {

workshops/fine-tuning-with-sagemakerai-and-bedrock/task_05_fmops/steps/pipeline_utils.py

Lines changed: 53 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,6 @@
88
PIPELINE_INSTANCE_TYPE = "ml.m5.xlarge"
99

1010

11-
# PROMPT_TEMPLATE = f"""
12-
# <|begin_of_text|>
13-
# <|start_header_id|>system<|end_header_id|>
14-
# You are a medical expert with advanced knowledge in clinical reasoning, diagnostics, and treatment planning.
15-
# Below is an instruction that describes a task, paired with an input that provides further context.
16-
# Write a response that appropriately completes the request.
17-
# Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.
18-
# <|eot_id|><|start_header_id|>user<|end_header_id|>
19-
# {{question}}<|eot_id|>
20-
# <|start_header_id|>assistant<|end_header_id|>
21-
# {{complex_cot}}
22-
23-
# {{answer}}
24-
# <|eot_id|>
25-
# """
26-
2711
SYSTEM_PROMPT = """You are a medical expert with advanced knowledge in clinical reasoning, diagnostics, and treatment planning.
2812
Below is an instruction that describes a task, paired with an input that provides further context.
2913
Write a response that appropriately completes the request.
@@ -62,6 +46,7 @@ def convert_to_messages(sample, system_prompt=""):
6246

6347
return sample
6448

49+
6550
def invoke_sagemaker_endpoint(payload, endpoint_name):
6651
"""
6752
Invoke a SageMaker endpoint with the given payload.
@@ -90,69 +75,65 @@ def invoke_sagemaker_endpoint(payload, endpoint_name):
9075
return None, -1
9176

9277

93-
def create_guardrail(guardrail_client):
94-
try:
95-
guardrail = guardrail_client.create_guardrail(
96-
name="ExampleMedicalGuardrail",
97-
description='Example of a Guardrail for Medical Use Cases',
98-
topicPolicyConfig={
99-
'topicsConfig': [{
100-
'name': 'Block Pharmaceuticals',
101-
'definition': 'This model cannot recommend one pharmaceutical over another. Generic prescriptions consistent with medical expertise and clinical diagnoses only.',
102-
'type': 'DENY',
103-
'inputAction': 'BLOCK',
104-
'outputAction': 'BLOCK',
105-
}]
106-
},
107-
sensitiveInformationPolicyConfig={
108-
'piiEntitiesConfig': [
109-
{
110-
'type': 'UK_NATIONAL_HEALTH_SERVICE_NUMBER',
111-
'action': 'BLOCK',
112-
'inputAction': 'BLOCK',
113-
'outputAction': 'BLOCK'
114-
},
115-
]
116-
},
117-
contextualGroundingPolicyConfig={
118-
'filtersConfig': [
119-
{
120-
'type': 'RELEVANCE',
121-
'threshold': 0.9,
122-
'action': 'BLOCK',
123-
'enabled': True
124-
},
125-
]
126-
},
127-
blockedInputMessaging="ExampleMedicalGuardrail has blocked this input.",
128-
blockedOutputsMessaging="ExampleMedicalGuardrail has blocked this output."
129-
)
130-
guardrail_id = guardrail['guardrailId']
131-
guardrail_version = guardrail['version']
132-
133-
print(f"Created new guardrail '{guardrail_id}:{guardrail_version}'")
134-
return guardrail_id, guardrail_version
135-
except botocore.exceptions.ClientError as create_error:
136-
print(f"Error creating guardrail: {create_error}")
137-
138-
13978
def get_or_create_guardrail():
14079
guardrail_client = boto3.client('bedrock')
14180
guardrail_name = "ExampleMedicalGuardrail"
14281
try:
14382
# Try to get the guardrail
14483
response = guardrail_client.list_guardrails()
145-
guardrail_id = ""
14684
for guardrail in response.get('guardrails', []):
14785
if guardrail['name'] == guardrail_name:
14886
guardrail_id = guardrail['id']
149-
if guardrail_id != "":
150-
response = guardrail_client.get_guardrail(
151-
guardrailIdentifier=guardrail_id
152-
)
153-
guardrail_version = response["version"]
154-
print(f"Found Guardrail {guardrail_id}:{guardrail_version}")
155-
else:
156-
return create_guardrail(guardrail_client)
87+
response = guardrail_client.get_guardrail(
88+
guardrailIdentifier=guardrail_id
89+
)
90+
guardrail_version = response["version"]
91+
print(f"Found Guardrail {guardrail_id}:{guardrail_version}")
15792
except botocore.exceptions.ClientError as e:
158-
print(f"Error checking guardrail: {e}")
93+
if e.response['Error']['Code'] == 'ResourceNotFoundException':
94+
# Guardrail doesn't exist, create it
95+
try:
96+
guardrail = guardrail_client.create_guardrail(
97+
name="ExampleMedicalGuardrail",
98+
description='Example of a Guardrail for Medical Use Cases',
99+
topicPolicyConfig={
100+
'topicsConfig': [{
101+
'name': 'Block Pharmaceuticals',
102+
'definition': 'This model cannot recommend one pharmaceutical over another. Generic prescriptions consistent with medical expertise and clinical diagnoses only.',
103+
'type': 'DENY',
104+
'inputAction': 'BLOCK',
105+
'outputAction': 'BLOCK',
106+
}]
107+
},
108+
sensitiveInformationPolicyConfig={
109+
'piiEntitiesConfig': [
110+
{
111+
'type': 'UK_NATIONAL_HEALTH_SERVICE_NUMBER',
112+
'action': 'BLOCK',
113+
'inputAction': 'BLOCK',
114+
'outputAction': 'BLOCK'
115+
},
116+
]
117+
},
118+
contextualGroundingPolicyConfig={
119+
'filtersConfig': [
120+
{
121+
'type': 'RELEVANCE',
122+
'threshold': 0.9,
123+
'action': 'BLOCK',
124+
'enabled': True
125+
},
126+
]
127+
},
128+
blockedInputMessaging="ExampleMedicalGuardrail has blocked this input.",
129+
blockedOutputsMessaging="ExampleMedicalGuardrail has blocked this output."
130+
)
131+
guardrail_id = guardrail['guardrailId']
132+
guardrail_version = guardrail['version']
133+
134+
print(f"Created new guardrail '{guardrail_id}:{guardrail_version}'")
135+
except botocore.exceptions.ClientError as create_error:
136+
print(f"Error creating guardrail: {create_error}")
137+
else:
138+
print(f"Error checking guardrail: {e}")
139+
return guardrail_id, guardrail_version

workshops/fine-tuning-with-sagemakerai-and-bedrock/task_05_fmops/steps/qualitative_eval_step.py

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from sagemaker.workflow.function_step import step
66
from .pipeline_utils import PIPELINE_INSTANCE_TYPE
7+
from .pipeline_utils import SYSTEM_PROMPT
8+
from .pipeline_utils import convert_to_messages
79

810

911
@step(
@@ -54,7 +56,7 @@ def invoke_sagemaker_endpoint(payload, endpoint_name):
5456
guardrailIdentifier=guardrail_id,
5557
guardrailVersion=guardrail_version,
5658
source='INPUT',
57-
content=[{'text': {'text': payload["inputs"]}}]
59+
content=[{'text': {'text': payload["messages"][0]["content"]}}]
5860
)
5961
guardrailResult = guardrail_response_input["action"]
6062

@@ -64,15 +66,29 @@ def invoke_sagemaker_endpoint(payload, endpoint_name):
6466

6567
try:
6668
start_time = time.time()
67-
response = sm_client.invoke_endpoint(
68-
EndpointName=endpoint_name,
69-
ContentType='application/json',
70-
Body=json.dumps(payload)
69+
# response = sm_client.invoke_endpoint(
70+
# EndpointName=endpoint_name,
71+
# ContentType='application/json',
72+
# Body=json.dumps(payload)
73+
# )
74+
# inference_time = time.time() - start_time
75+
76+
# response_body = response['Body'].read().decode('utf-8')
77+
# return json.loads(response_body), inference_time
78+
79+
from sagemaker.predictor import Predictor
80+
from sagemaker.serializers import JSONSerializer
81+
from sagemaker.deserializers import JSONDeserializer
82+
83+
predictor = Predictor(
84+
endpoint_name=f"{endpoint_name}",
85+
serializer=JSONSerializer(),
86+
deserializer=JSONDeserializer()
7187
)
72-
inference_time = time.time() - start_time
7388

74-
response_body = response['Body'].read().decode('utf-8')
75-
return json.loads(response_body), inference_time
89+
response = predictor.predict(payload)['choices'][0]['message']['content']
90+
inference_time = time.time() - start_time
91+
return response, inference_time
7692
except Exception as e:
7793
print(f"Error invoking endpoint {endpoint_name}: {str(e)}")
7894
return None, -1
@@ -295,44 +311,31 @@ def evaluate_model_qualitatively(model_config, dataset):
295311
question = example["Question"]
296312
reference = "\n".join([example["Complex_CoT"], example["Response"]])
297313

298-
# Prepare the prompt for the model
299-
prompt = f"""
300-
<|begin_of_text|>
301-
<|start_header_id|>system<|end_header_id|>
302-
You are a medical expert with advanced knowledge in clinical reasoning, diagnostics, and treatment planning.
303-
Below is an instruction that describes a task, paired with an input that provides further context.
304-
Write a response that appropriately completes the request.
305-
Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.
306-
<|eot_id|><|start_header_id|>user<|end_header_id|>
307-
{question}<|eot_id|>
308-
<|start_header_id|>assistant<|end_header_id|>"""
309-
310-
# Payload for SageMaker endpoint
311-
payload = {
312-
"inputs": prompt,
313-
"parameters": {
314-
"max_new_tokens": 512,
315-
"top_p": 0.9,
316-
"temperature": 0.6,
317-
"return_full_text": False
318-
}
314+
payload = {}
315+
messages_prompt = convert_to_messages(example, SYSTEM_PROMPT)
316+
payload["messages"] = messages_prompt["messages"]
317+
payload["parameters"] = {
318+
"max_new_tokens": 512,
319+
"top_p": 0.9,
320+
"temperature": 0.6,
321+
"return_full_text": False
319322
}
320-
323+
321324
# Call the model endpoint
322325
try:
323326
response, inference_time = invoke_sagemaker_endpoint(payload, endpoint_name)
324327

325328
if response is None:
326329
prediction = "Error generating response."
327330
failed_generations += 1
328-
elif isinstance(response, list):
329-
prediction = response[0].get('generated_text', '').strip()
330-
elif isinstance(response, dict):
331-
prediction = response.get('generated_text', '').strip()
331+
# elif isinstance(response, list):
332+
# prediction = response[0].get('generated_text', '').strip()
333+
# elif isinstance(response, dict):
334+
# prediction = response.get('generated_text', '').strip()
332335
else:
333336
prediction = str(response).strip()
334337

335-
prediction = prediction.split("<|eot_id|>")[0] if "<|eot_id|>" in prediction else prediction
338+
# prediction = prediction.split("<|eot_id|>")[0] if "<|eot_id|>" in prediction else prediction
336339
inference_times.append(inference_time)
337340

338341
except Exception as e:
@@ -478,7 +481,7 @@ def evaluate_model_qualitatively(model_config, dataset):
478481
mlflow.log_param("qualitative_evaluation_endpoint", endpoint_name)
479482
mlflow.log_param("qualitative_evaluation_num_samples", num_samples)
480483
mlflow.log_param("qualitative_evaluation_timestamp", datetime.now().isoformat())
481-
mlflow.log_param("llm_judge_model", "bedrock:/us.anthropic.claude-3-haiku-20240307-v1:0")
484+
mlflow.log_param("llm_judge_model", "bedrock:/anthropic.claude-3-haiku-20240307-v1:0")
482485

483486
# Load the test dataset
484487
try:

0 commit comments

Comments
 (0)