Skip to content

Commit

Permalink
try titan embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
rjvgupta committed Jul 11, 2024
1 parent 797bf10 commit 4982a7f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lambda-udfs/f_titan_embedding(varchar)/function.sql
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';
1 change: 1 addition & 0 deletions lambda-udfs/f_titan_embedding(varchar)/input.csv
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.'
84 changes: 84 additions & 0 deletions lambda-udfs/f_titan_embedding(varchar)/lambda.yaml
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)
1 change: 1 addition & 0 deletions lambda-udfs/f_titan_embedding(varchar)/output.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'[1.923, 123, 0.3]'

0 comments on commit 4982a7f

Please sign in to comment.