Skip to content

Commit

Permalink
Update lambda.tf and index.py files
Browse files Browse the repository at this point in the history
  • Loading branch information
FloRul committed Mar 21, 2024
1 parent 5318895 commit 45e4d76
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
12 changes: 4 additions & 8 deletions lambdas/inference/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,13 @@ module "lambda_function_container_image" {
"logs:PutLogEvents",
]
}

bedrock_usage = {
effect = "Allow"

resources = [
"*"
]

actions = [
"bedrock:*"
]
resources = ["*"]
actions = ["bedrock:*"]
}

rds_connect_readwrite = {
effect = "Allow"

Expand Down
12 changes: 9 additions & 3 deletions lambdas/inference/src/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from retrieval import Retrieval
from history import History
import uuid
from aws_lambda_powertools import Logger, Metrics, Tracer

tracer = Tracer()
logger = Logger()
metrics = Metrics()

HEADERS = {
"Access-Control-Allow-Origin": "*",
Expand Down Expand Up @@ -78,8 +82,6 @@ def invoke_model(
if source == "call":
maxtokens //= 2

messages.append({"role": "user", "content": user_message})

body = json.dumps(
{
"anthropic_version": "bedrock-2023-05-31",
Expand All @@ -104,6 +106,9 @@ def invoke_model(
raise e


@metrics.log_metrics
@logger.inject_lambda_context
@tracer.capture_lambda_handler
def lambda_handler(event, context):
response = "this is a dummy response"
source = event.get("queryStringParameters", {}).get("source", "message")
Expand All @@ -127,13 +132,14 @@ def lambda_handler(event, context):
)
docs = retrieval.fetch_documents(query=query, top_k=ENV_VARS["top_k"])


# prepare the prompt
system_prompt = prepare_system_prompt(docs, source)

chat_history = history.get(limit=5)
chat_history.append({"role": "user", "content": query})

logger.info(f"Chat history: {chat_history}")

response = invoke_model(
system_prompt=system_prompt,
source=source,
Expand Down
3 changes: 2 additions & 1 deletion lambdas/inference/src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ langchain-community
langchain
psycopg2-binary
pgvector
boto3
boto3
aws-lambda-powertools

0 comments on commit 45e4d76

Please sign in to comment.