Skip to content

Commit

Permalink
Merge branch 'develop' v0.1.10
Browse files Browse the repository at this point in the history
  • Loading branch information
rstrahan committed Oct 27, 2023
2 parents 4d15e04 + c11b047 commit 36da0a0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lambdas/bedrock-embeddings-and-llm/src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Default prompt templates
AMAZON_GENERATE_QUERY_PROMPT_TEMPLATE = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.<br>Chat History: <br>{history}<br>Follow up question: {input}<br>Standalone question:"""
AMAZON_QA_PROMPT_TEMPLATE = """<br><br>Human: You are a friendly AI assistant. You provide answers only based on the provided reference passages. Here are reference passages in <references> tags:<br><references><br>{context}<br></references><br>If the references contain the information needed to respond, then write a confident response in under 50 words, quoting the relevant references. <br>Otherwise, if you can make an informed guess based on the reference passages, then write a less confident response in under 50 words, stating your assumptions.<br>Finally, if the references do not have any relevant information, then respond saying \\"Sorry, I don't know\\".<br><br>{query}<br><br>Assistant: According to the reference passages, in under 50 words:"""
AMAZON_QA_PROMPT_TEMPLATE = """<br><br>Human: You are a friendly AI assistant. Answer the question in <question> tags only based on the provided reference passages. Here are reference passages in <references> tags:<br><references><br>{context}<br></references><br>If the references contain the information needed to respond, then write a confident response in under 50 words, quoting the relevant references. <br>Otherwise, if you can make an informed guess based on the reference passages, then write a less confident response in under 50 words, stating your assumptions.<br>Finally, if the references do not have any relevant information, then respond saying \\"Sorry, I don't know\\".<br><question><br>{query}<br></question><br>Assistant: According to the reference passages, in under 50 words:"""
ANTHROPIC_GENERATE_QUERY_PROMPT_TEMPLATE = """<br><br>Human: Here is a chat history in <chatHistory> tags:<br><chatHistory><br>{history}<br></chatHistory><br>Human: And here is a follow up question or statement from the human in <followUpMessage> tags:<br><followUpMessage><br>{input}<br></followUpMessage><br>Human: Rephrase the follow up question or statement as a standalone question or statement that makes sense without reading the chat history.<br><br>Assistant: Here is the rephrased follow up question or statement:"""
ANTHROPIC_QA_PROMPT_TEMPLATE = AMAZON_QA_PROMPT_TEMPLATE
AI21_GENERATE_QUERY_PROMPT_TEMPATE = ANTHROPIC_GENERATE_QUERY_PROMPT_TEMPLATE
Expand Down
42 changes: 32 additions & 10 deletions lambdas/bedrock-embeddings-and-llm/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ Resources:
Role: !GetAtt 'BedrockBoto3ZipFunctionRole.Arn'
Timeout: 60
MemorySize: 512
Environment:
Variables:
BOTO3_BUCKET: !Ref BedrockBoto3Bucket
Code:
ZipFile: |
Type: AWS::Lambda::Function
Properties:
Handler: index.handler
Runtime: python3.11
Role: !GetAtt 'BedrockBoto3ZipFunctionRole.Arn'
Timeout: 60
MemorySize: 512
Environment:
Variables:
BOTO3_BUCKET: !Ref BedrockBoto3Bucket
Expand All @@ -85,6 +97,7 @@ Resources:
import boto3
import zipfile
import urllib3
import json
from datetime import datetime
import cfnresponse
boto3_bucket = os.environ['BOTO3_BUCKET']
Expand All @@ -93,7 +106,7 @@ Resources:
s3 = boto3.client('s3')
s3.upload_file(file_path, bucket, key)
print(f"Upload successful. {file_path} uploaded to {bucket}/{key}")
def make_zip_filename():
now = datetime.now()
timestamp = now.strftime('%Y%m%d_%H%M%S')
Expand All @@ -109,16 +122,21 @@ Resources:
os.path.join(path, '..')))
zipf.close()
def empty_bucket(bucket_name):
def deleteObject(existingZip):
print(f'Deleting Existing Zip: {existingZip}')
s3_client = boto3.client('s3')
response = s3_client.list_objects_v2(Bucket=bucket_name)
if 'Contents' in response:
keys = [{'Key': obj['Key']} for obj in response['Contents']]
s3_client.delete_objects(Bucket=bucket_name, Delete={'Objects': keys})
s3_client.delete_object(Bucket=existingZip["Bucket"], Key=existingZip["Key"])
return
def handler(event, context):
print("Event: ", event)
print("Event: ", json.dumps(event))
physicalResourceId = event.get("PhysicalResourceId", None)
existingZip = None
if physicalResourceId:
try:
existingZip = json.loads(physicalResourceId)
except:
existingZip = ""
responseData={}
reason=""
status = cfnresponse.SUCCESS
Expand All @@ -132,14 +150,17 @@ Resources:
print(f"uploading {boto3_zip_name} to s3 bucket {boto3_bucket}")
upload_file_to_s3(boto3_zip_name, boto3_bucket, boto3_zip_name)
responseData = {"Bucket": boto3_bucket, "Key": boto3_zip_name}
physicalResourceId = json.dumps(responseData)
else:
# delete - empty the bucket so it can be deleted by the stack.
empty_bucket(boto3_bucket)
print(f"RequestType is: {event['RequestType']}")
except Exception as e:
print(e)
status = cfnresponse.FAILED
reason = f"Exception thrown: {e}"
cfnresponse.send(event, context, status, responseData, reason=reason)
# delete any previously created zip file (during update or delete), so that bucket can be deleted
if existingZip:
deleteObject(existingZip)
cfnresponse.send(event, context, status, responseData, reason=reason, physicalResourceId=physicalResourceId)
Metadata:
cfn_nag:
rules_to_suppress:
Expand All @@ -154,6 +175,7 @@ Resources:
ServiceToken: !GetAtt BedrockBoto3ZipFunction.Arn
# Rerun BedrockBoto3ZipFunction if any of the following parameters change
BOTO3_BUCKET: !Ref BedrockBoto3Bucket
VERSION: 1

BedrockBoto3Layer:
Type: "AWS::Lambda::LayerVersion"
Expand Down

0 comments on commit 36da0a0

Please sign in to comment.