-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding base function for bedrock (#365)
- Loading branch information
1 parent
0aaeaca
commit 7f32491
Showing
5 changed files
with
193 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def handler(event, context): | ||
pass |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import boto3 | ||
import json | ||
import logging | ||
import os | ||
from decimal import Decimal | ||
|
||
db = boto3.resource("dynamodb", region_name="ap-northeast-1") | ||
table = db.Table(os.getenv('TABLE_NAME')) | ||
|
||
s3_client = boto3.client('s3') | ||
bucket = os.getenv('BUCKET_NAME') | ||
|
||
bedrock_client = boto3.client('bedrock-runtime', region_name='ap-northeast-1') | ||
|
||
|
||
class DecimalEncoder(json.JSONEncoder): | ||
def default(self, obj): | ||
if isinstance(obj, Decimal): | ||
return float(obj) | ||
return json.JSONEncoder.default(self, obj) | ||
|
||
|
||
class ExtendedEncoder(json.JSONEncoder): | ||
def default(self, obj): | ||
if isinstance(obj, Decimal): | ||
return float(obj) | ||
if isinstance(obj, set): | ||
return list(obj) | ||
return super(ExtendedEncoder, self).default(obj) | ||
|
||
|
||
class JsonPayloadBuilder: | ||
payload = {} | ||
|
||
def add_status(self, success): | ||
self.payload['success'] = success | ||
return self | ||
|
||
def add_data(self, data): | ||
self.payload['data'] = data | ||
return self | ||
|
||
def add_message(self, msg): | ||
self.payload['message'] = msg | ||
return self | ||
|
||
def compile(self): | ||
return json.dumps(self.payload, cls=ExtendedEncoder, ensure_ascii=False).encode('utf8') | ||
|
||
|
||
def api_response(code, body): | ||
return { | ||
"isBase64Encoded": False, | ||
"statusCode": code, | ||
'headers': { | ||
"Access-Control-Allow-Origin": '*', | ||
"Content-Type": "application/json", | ||
"Referrer-Policy": "origin" | ||
}, | ||
"multiValueHeaders": {"Access-Control-Allow-Methods": ["POST", "OPTIONS", "GET", "PATCH", "DELETE"]}, | ||
"body": body | ||
} | ||
|
||
|
||
def resp_handler(func): | ||
def handle(*args, **kwargs): | ||
try: | ||
resp = func(*args, **kwargs) | ||
return api_response(200, resp) | ||
except LookupError: | ||
resp = JsonPayloadBuilder().add_status(False).add_data(None) \ | ||
.add_message("Not found").compile() | ||
return api_response(404, resp) | ||
except Exception as e: | ||
logging.error(str(e)) | ||
resp = JsonPayloadBuilder().add_status(False).add_data(None) \ | ||
.add_message("Internal error, please contact [email protected].").compile() | ||
return api_response(500, resp) | ||
|
||
return handle | ||
|
||
|
||
def generate_url(bucket_name, object_key, expiration=3600): | ||
try: | ||
response = s3_client.generate_presigned_url('get_object', | ||
Params={'Bucket': bucket_name, | ||
'Key': object_key}, | ||
ExpiresIn=expiration) | ||
except Exception as e: | ||
logging.error(str(e)) | ||
return None | ||
|
||
return response |