-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
51 lines (37 loc) · 1.18 KB
/
lambda_function.py
File metadata and controls
51 lines (37 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import json
import boto3
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# Amazon Titan Text G1 - Premier model
MODEL_ID = "amazon.titan-text-premier-v1:0"
def generate_text(prompt, max_tokens):
bedrock = boto3.client(
service_name="bedrock-runtime",
region_name="us-east-1"
)
body = {
"inputText": prompt,
"textGenerationConfig": {
"maxTokenCount": max_tokens,
"stopSequences": [],
"temperature": 0.7,
"topP": 0.9
}
}
response = bedrock.invoke_model(
modelId=MODEL_ID,
body=json.dumps(body),
accept="application/json",
contentType="application/json"
)
# Retrieve generated text from JSON response and return it
return json.loads(response.get("body").read())["results"][0]["outputText"]
def lambda_handler(event, context):
print(f"Event: {json.dumps(event)}")
# request = json.loads(event["body"])
prompt = event["prompt"]
max_tokens = event["maxTokens"]
# Get generated text from the AWS Bedrock LLM
text_response = generate_text(prompt, max_tokens)
return { "generation": text_response }