forked from aws-samples/amazon-redshift-udfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
Purpose: | ||
This sample function demonstrates how use lambda to call the titan embedding model to convert your text to an embedding. | ||
2024-07-11: written by rjvgupta | ||
*/ | ||
CREATE OR REPLACE EXTERNAL FUNCTION f_titan_embedding (varchar) RETURNS varchar STABLE | ||
LAMBDA 'f-titan-embedding-varchar' IAM_ROLE ':RedshiftRole'; |
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 @@ | ||
'With ComfortCut blades, you get a clean shave that's comfortable on your skin. Rounded caps shield 27 self-sharpening blades to gently cut hair just above skin level and help the shaver glide smoothly over your skin.\n4D Flex Heads move independently in 4 directions to automatically adjust to the curves of your face, neck and jaw line.\nPop-up trimmer for mustache and sideburns Finish your look with the built-in trimmer. It’s ideal for maintaining your mustache and trimming your sideburns.\nAt the touch of a button, you can pop the heads open for an easy and thorough clean under the tap.\nUp to 40 minutes of cordless runtime - that's about 13 shaves. Or plug it in for instant, continuous power.' |
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,84 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Resources: | ||
LambdaRole: | ||
Type: AWS::IAM::Role | ||
Properties: | ||
AssumeRolePolicyDocument: | ||
Version: 2012-10-17 | ||
Statement: | ||
- | ||
Effect: Allow | ||
Principal: | ||
Service: | ||
- lambda.amazonaws.com | ||
Action: | ||
- sts:AssumeRole | ||
Path: / | ||
Policies: | ||
- | ||
PolicyName: CloudwatchLogs | ||
PolicyDocument: | ||
Version: 2012-10-17 | ||
Statement: | ||
- | ||
Effect: Allow | ||
Action: | ||
- logs:CreateLogGroup | ||
Resource: | ||
- !Sub "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:*" | ||
- | ||
Effect: Allow | ||
Action: | ||
- logs:CreateLogStream | ||
- logs:PutLogEvents | ||
Resource: | ||
- !Sub "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/*" | ||
- | ||
Effect: Allow | ||
Action: | ||
- bedrock:InvokeModel | ||
Resource: | ||
- !Sub "arn:${AWS::Partition}:bedrock:${AWS::Region}:${AWS::AccountId}:model/*" | ||
LambdaUDFFunction: | ||
Type: "AWS::Lambda::Function" | ||
Properties: | ||
FunctionName: f-titan-embedding-varchar | ||
Role: !GetAtt 'LambdaRole.Arn' | ||
Handler: index.handler | ||
Runtime: python3.9 | ||
Timeout: 300 | ||
Code: | ||
ZipFile: | | ||
import boto3 | ||
import json | ||
bedrock_runtime = boto3.client(service_name="bedrock-runtime") | ||
def generate_embeddings(text=None): | ||
input_data = {"inputText": text} | ||
response = bedrock_runtime.invoke_model( | ||
body=body, | ||
modelId="amazon.titan-embed-image-v1", | ||
accept="application/json", | ||
contentType="application/json" | ||
) | ||
response_body = json.loads(response.get("body").read()) | ||
finish_reason = response_body.get("message") | ||
return response_body.get("embedding") | ||
def handler(event, context): | ||
redshift_response = {"success": False, "num_records": event["num_records"]} | ||
try: | ||
result = [] | ||
for text in event["arguments"]: | ||
try: | ||
result.append(generate_embeddings(text)) | ||
except Exception as e: | ||
print(f"Error: {e}") | ||
result.append(None) | ||
redshift_response["success"] = True | ||
redshift_response["results"] = result | ||
except Exception as e: | ||
redshift_response["error_msg"] = str(e) | ||
return json.dumps(redshift_response) |
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 @@ | ||
'[1.923, 123, 0.3]' |