Created
May 12, 2025 15:20
-
-
Save koorukuroo/cfad25527d90941947f103adcd16aaf5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import json | |
from botocore.exceptions import ClientError | |
def lambda_handler(event, context): | |
# Bedrock Runtime client | |
client = boto3.client("bedrock-runtime", region_name="us-east-1") | |
# Claude 모델 ID (Haiku는 속도 빠르고 비용 저렴) | |
model_id = "anthropic.claude-3-haiku-20240307-v1:0" | |
model_id = "us.anthropic.claude-3-7-sonnet-20250219-v1:0" | |
# 쿼리 파라미터에서 메시지 추출 | |
input_message = event.get('params', {}).get('querystring', {}).get( | |
'm', "나는 신한투자증권에 입사하려는 개발자야. 클라우드를 공부해야 할 필요가 있을까?") | |
# 모델 요청 페이로드 구성 | |
native_request = { | |
"anthropic_version": "bedrock-2023-05-31", | |
"max_tokens": 512, | |
"temperature": 0.8, | |
"top_k": 250, | |
"top_p": 0.999, | |
"system": "너는 이모티콘을 엄청 많이 쓰는 사회 초년생 직장인이야. 친근하게 말하려구 반말로 해.", | |
"messages": [ | |
{ | |
"role": "user", | |
"content": [ | |
{"type": "text", "text": input_message} | |
] | |
} | |
] | |
} | |
try: | |
# 모델 호출 | |
response = client.invoke_model( | |
modelId=model_id, | |
contentType="application/json", | |
accept="application/json", | |
body=json.dumps(native_request) | |
) | |
# 응답 디코딩 | |
model_response = json.loads(response["body"].read()) | |
# Claude 응답 텍스트 추출 | |
response_text = model_response["content"][0]["text"] | |
return { | |
"statusCode": 200, | |
"body": json.dumps({ | |
"input": input_message, | |
"response": response_text | |
}) | |
} | |
except (ClientError, Exception) as e: | |
return { | |
"statusCode": 500, | |
"body": json.dumps({"error": str(e)}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment