Skip to content

Commit 7d4d3a1

Browse files
author
Dinesh Sajwan
committed
feat(visualqa): added review comments
1 parent 34a5ba1 commit 7d4d3a1

File tree

5 files changed

+21
-34
lines changed

5 files changed

+21
-34
lines changed

lambda/aws-qa-appsync-opensearch/question_answering/src/qa_agent/image_qa.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,14 @@ def process_visual_qa(input_params,status_variables,filename):
198198
local_file_path= download_file(bucket_name,filename)
199199
base64_images=encode_image_to_base64(local_file_path,filename)
200200
status_variables['answer']= generate_vision_answer_bedrock(_qa_llm,base64_images, qa_modelId,decoded_question)
201-
status_variables['jobstatus'] = JobStatus.DONE.status
202-
streaming = input_params.get("streaming", False)
201+
if(status_variables['answer'] is None):
202+
status_variables['answer'] = JobStatus.ERROR_PREDICTION.status
203+
error = JobStatus.ERROR_PREDICTION.get_message()
204+
status_variables['answer'] = error.decode("utf-8")
205+
status_variables['jobstatus'] = JobStatus.ERROR_PREDICTION.status
206+
else:
207+
status_variables['jobstatus'] = JobStatus.DONE.status
208+
streaming = input_params.get("streaming", False)
203209

204210
else:
205211
logger.error('Invalid Model , cannot load LLM , returning..')
@@ -253,7 +259,7 @@ def generate_vision_answer_sagemaker(_qa_llm,input_params,decoded_question,statu
253259

254260
return status_variables
255261

256-
def generate_vision_answer_bedrock(bedrock_client,base64_images, model_id,decoded_question):
262+
def generate_vision_answer_bedrock(bedrock_client,base64_images,model_id,decoded_question):
257263
system_prompt=""
258264
# use system prompt for fine tuning the performamce
259265
# system_prompt= """
@@ -293,10 +299,15 @@ def generate_vision_answer_bedrock(bedrock_client,base64_images, model_id,decode
293299
}
294300

295301
body=json.dumps({'messages': [messages],**claude_config, "system": system_prompt})
296-
response = bedrock_client.invoke_model(
297-
body=body, modelId=model_id, accept="application/json",
298-
contentType="application/json"
299-
)
302+
try:
303+
response = bedrock_client.invoke_model(
304+
body=body, modelId=model_id, accept="application/json",
305+
contentType="application/json"
306+
)
307+
except Exception as err:
308+
logger.exception(f'Error occurred , Reason :{err}')
309+
return None
310+
300311
response = json.loads(response['body'].read().decode('utf-8'))
301312

302313
formated_response= response['content'][0]['text']

lambda/aws-qa-appsync-opensearch/question_answering/src/qa_agent/sagemaker_endpoint.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
logger = Logger(service="QUESTION_ANSWERING")
1818

1919

20-
sageMakerEndpoint= os.environ['SAGEMAKER_ENDPOINT']
2120

2221
class ContentHandler(LLMContentHandler):
2322
content_type = "application/json"
@@ -48,7 +47,6 @@ class MultiModal():
4847

4948
@classmethod
5049
def sagemakerendpoint_llm(self,model_id):
51-
if(sageMakerEndpoint ==model_id):
5250
try:
5351
endpoint= SagemakerEndpoint(
5452
endpoint_name=model_id,
@@ -60,9 +58,7 @@ def sagemakerendpoint_llm(self,model_id):
6058
except Exception as err:
6159
logger.error(f' Error when accessing sagemaker endpoint :: {model_id} , returning...')
6260
return None
63-
else:
64-
logger.error(f" The sagemaker model Id {model_id} do not match a sagemaker endpoint name {sageMakerEndpoint}")
65-
return None
61+
6662

6763

6864

src/patterns/gen-ai/aws-qa-appsync-opensearch/index.ts

+2-16
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,7 @@ export interface QaAppsyncOpensearchProps {
154154
*/
155155
readonly customDockerLambdaProps?: DockerLambdaCustomProps | undefined;
156156

157-
/**
158-
* Optional. Allows to provide custom lambda code
159-
* and settings instead of the existing
160-
*/
161-
readonly sagemakerEndpointName?: string;
157+
162158
}
163159

164160
/**
@@ -466,15 +462,7 @@ export class QaAppsyncOpensearch extends BaseClass {
466462
}),
467463
);
468464

469-
if (props.sagemakerEndpointName) {
470-
question_answering_function_role.addToPolicy(
471-
new iam.PolicyStatement({
472-
effect: iam.Effect.ALLOW,
473-
actions: ['sagemaker:InvokeEndpoint'],
474-
resources: ['arn:'+ Aws.PARTITION +':sagemaker:' + Aws.ACCOUNT_ID + ':endpoint/*'],
475-
}),
476-
);
477-
}
465+
478466
// The lambda will access the opensearch credentials
479467
if (props.openSearchSecret) {
480468
props.openSearchSecret.grantRead(question_answering_function_role);
@@ -555,7 +543,6 @@ export class QaAppsyncOpensearch extends BaseClass {
555543
true,
556544
);
557545

558-
const sagemakerEndpointNamestr = props.sagemakerEndpointName || '';
559546
const construct_docker_lambda_props = {
560547
code: lambda.DockerImageCode.fromImageAsset(
561548
path.join(
@@ -579,7 +566,6 @@ export class QaAppsyncOpensearch extends BaseClass {
579566
OPENSEARCH_DOMAIN_ENDPOINT: opensearch_helper.getOpenSearchEndpoint(props),
580567
OPENSEARCH_INDEX: props.openSearchIndexName,
581568
OPENSEARCH_SECRET_ID: SecretId,
582-
SAGEMAKER_ENDPOINT: sagemakerEndpointNamestr,
583569
},
584570
...(props.lambdaProvisionedConcurrency && {
585571
currentVersionOptions: {

test/patterns/gen-ai/aws-qa-appsync-opensearch/aws-qa-appsync-opensearch-serverless.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ describe('QA Appsync Open search construct', () => {
5555
openSearchIndexName: 'demoindex',
5656
openSearchSecret: osSecret,
5757
cognitoUserPool: userPoolLoaded,
58-
sagemakerEndpointName: 'sageMakerEndpoint',
5958
};
6059

6160
qaTestConstruct = new QaAppsyncOpensearch(qaTestStack, 'test', qaTestProps);
@@ -90,7 +89,6 @@ describe('QA Appsync Open search construct', () => {
9089
},
9190
OPENSEARCH_INDEX: 'demoindex',
9291
OPENSEARCH_SECRET_ID: 'OSSecretId',
93-
SAGEMAKER_ENDPOINT: 'sageMakerEndpoint',
9492
},
9593
},
9694
});

test/patterns/gen-ai/aws-qa-appsync-opensearch/aws-qa-appsync-opensearch.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ describe('QA Appsync Open search construct', () => {
5858
openSearchIndexName: 'demoindex',
5959
openSearchSecret: osSecret,
6060
cognitoUserPool: userPoolLoaded,
61-
sagemakerEndpointName: 'sageMakerEndpoint',
6261
};
6362

6463
qaTestConstruct = new QaAppsyncOpensearch(qaTestStack, 'test', qaTestProps);
@@ -88,7 +87,6 @@ describe('QA Appsync Open search construct', () => {
8887
OPENSEARCH_DOMAIN_ENDPOINT: 'osendppint.amazon.aws.com',
8988
OPENSEARCH_INDEX: 'demoindex',
9089
OPENSEARCH_SECRET_ID: 'OSSecretId',
91-
SAGEMAKER_ENDPOINT: 'sageMakerEndpoint',
9290
},
9391
},
9492
});
@@ -225,7 +223,6 @@ describe('QA Appsync Open search construct custom lambda', () => {
225223
openSearchSecret: osSecret,
226224
cognitoUserPool: userPoolLoaded,
227225
customDockerLambdaProps: customDockerLambdaProps,
228-
sagemakerEndpointName: 'sageMakerEndpoint',
229226
};
230227

231228
qaTestConstruct = new QaAppsyncOpensearch(qaTestStack, 'test', qaTestProps);
@@ -255,7 +252,6 @@ describe('QA Appsync Open search construct custom lambda', () => {
255252
OPENSEARCH_DOMAIN_ENDPOINT: 'osendppint.amazon.aws.com',
256253
OPENSEARCH_INDEX: 'demoindex',
257254
OPENSEARCH_SECRET_ID: 'OSSecretId',
258-
SAGEMAKER_ENDPOINT: 'sageMakerEndpoint',
259255
TEST_VAR: 'hello',
260256
},
261257
},

0 commit comments

Comments
 (0)