-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
51 lines (40 loc) · 1.34 KB
/
index.py
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
51
import boto3
import json
import os
import logging
logger = logging.getLogger()
logger.setLevel(logging.getLevelName(os.getenv('LOG_LEVEL', 'INFO')))
bedrock_runtime_client = boto3.client('bedrock-runtime', os.getenv("AWS_DEFAULT_REGION"))
def get_completion(user_prompt):
model_id = 'anthropic.claude-v2'
accept = 'application/json'
content_type = 'application/json'
prompt = "Human: " + user_prompt + "\n\nAssistant:"
body = json.dumps({
"prompt": prompt,
"max_tokens_to_sample": 600,
})
response = bedrock_runtime_client.invoke_model(
modelId=model_id,
accept=accept,
contentType=content_type,
body=body
)
response_body = json.loads(response.get('body').read())
print("Received response_body:" + json.dumps(response_body, ensure_ascii=False))
return response_body.get('completion')
# Lambda のハンドラー関数
def lambda_handler(event, context):
if 'AppsheetBot' not in event['headers']['user-agent']:
return {
'statusCode': 401,
'body': json.dumps('401 Unauthorized')
}
print(event)
# return get_completion(event.get('user_prompt'))
result = {
'statusCode': 200,
'body': {'completion': get_completion(json.loads(event['body'])['user_prompt'])}
}
print(result)
return result