Created
December 30, 2024 05:38
-
-
Save ensean/cec469b704a8de4876fc51279a3cc778 to your computer and use it in GitHub Desktop.
bedrock for comments analysis
This file contains hidden or 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
import json | |
import base64 | |
import boto3 | |
import os | |
tools = [ | |
{ | |
"toolSpec": { | |
"name": "analysis_product_review", | |
"description": "Analysis the product reviews, determine its category, sentiment.", | |
"inputSchema": { | |
"json": { | |
"type": "object", | |
"properties": { | |
"category": { | |
"type": "string", | |
"description": "The category of the review", | |
"enum": ["Sizing", "Style", "Color", "Materials", "Others"] | |
}, | |
"intention": { | |
"type": "string", | |
"description": "The intention of this review", | |
"enum": ["Praise", "Suggest", "Complain", "Spam", "Others"] | |
}, | |
"tags": { | |
"type": "array", | |
"description": "An array of summaried tags of this review", | |
"items": {"type": "string"} | |
}, | |
"sentiment": { | |
"type": "string", | |
"description": "The sentiment of the review", | |
"enum": ["Positive", "Neutral", "Negative"] | |
}, | |
"confidence": { | |
"type": "number", | |
"description": "The confidence of the analysis, 0.0-1.0 (inclusive)", | |
} | |
}, | |
"required": ["category", "sentiment", "intention", "tags", "confidence"] | |
} | |
} | |
} | |
} | |
] | |
def get_review_enrichment(br_runtime, o_review): | |
prompt = """ | |
You are a helpful assistant that analyzes the product review and determine its category and sentiment. | |
Use the analysis_product_review tool to provide the extracted information. | |
Here's the original review message: | |
""" | |
# https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference-supported-models-features.html | |
# https://community.aws/content/2hWA16FSt2bIzKs0Z1fgJBwu589/generating-json-with-the-amazon-bedrock-converse-api | |
resp = br_runtime.converse( | |
# change at your wish | |
# anthropic.claude-3-5-sonnet-20241022-v2:0 | |
# anthropic.claude-3-haiku-20240307-v1:0 | |
# anthropic.claude-3-5-haiku-20241022-v1:0 | |
# us.meta.llama3-2-11b-instruct-v1:0, should use CRI | |
# amazon.nova-pro-v1:0, us.amazon.nova-lite-v1:0 | |
modelId=os.environ.get( | |
'MODEL_ID', 'us.amazon.nova-lite-v1:0'), | |
messages=[ | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"text": prompt, | |
}, | |
{ | |
"text": o_review, | |
} | |
] | |
} | |
], | |
inferenceConfig={ | |
"maxTokens": 4096, | |
"temperature": 0 | |
}, | |
toolConfig={ | |
"tools": tools, | |
# only Claude supports force use of the tool | |
# "toolChoice": { | |
# "tool": { | |
# "name": "analysis_product_review" | |
# } | |
# } | |
} | |
) | |
for content in resp['output']['message']['content']: | |
if content is not None and 'toolUse' in content.keys(): | |
return content['toolUse']['input'] | |
# no msg extracted | |
return None | |
br_rt = boto3.client('bedrock-runtime', region_name='us-west-2') | |
def lambda_handler(event, context): | |
b6 = base64.b64decode(event[0]['data']).decode('utf-8') | |
payload = json.loads(b6) | |
text = payload.get('text', '').strip() | |
if text: | |
review_info = get_review_enrichment(br_rt, text) | |
if review_info is not None: | |
payload.update(review_info) | |
else: | |
payload.update({"category": "unknown", "sentiment": "unknown"}) | |
return payload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment