Conversation
AWS Bedrock에서 지원하는 Claude API로 이전하기 위함입니다.
[Test] 베드락 사용 테스트
test deploy
system instruction 타입 수정
structured output이 지원되지 않습니다...
[Fix] claude가 불필요한 서론/결론 등을 말하지 못하도록 형식을 구체적으로 수정
[Feature] claude 모델을 3.5 haiku에서 sonnet 4.5로 변경
여는 중괄호로 응답을 시작하고 닫는 중괄호로 응답을 끝마치도록 지시합니다. 이 방법마저 소용이 없는 경우, 중괄호를 기준으로 문자열을 잘라 사용하는 방식을 적용합니다.
claude가 양식을 더 잘 지키도록 프롬프트 개선
Claude 모델의 응답이 명시한 양식임을 신뢰할 수 없으므로, 응답에 포함된 처음 여는 중괄호부터 마지막 닫는 중괄호까지 잘라내 사용합니다.
claude의 응답을 여는 중괄호부터 닫는 중괄호까지 잘라 사용
There was a problem hiding this comment.
Pull Request Overview
This PR migrates the TOS (Terms of Service) analysis Lambda function from Google's Gemini API to AWS Bedrock's Claude Sonnet 4.5 model. The change eliminates the need for external API keys and leverages AWS-native services for AI capabilities.
Key Changes:
- Replaced Google Gemini API client with AWS Bedrock boto3 client
- Updated both summarization and evaluation functions to use Claude Sonnet 4.5 via Bedrock's converse API
- Removed GEMINI_API_KEY environment variable dependency
Reviewed Changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| src/tos_summarize.py | Refactored to use boto3 Bedrock client with Claude Sonnet 4.5 for TOS summarization |
| src/tos_evaluate.py | Migrated to Bedrock API with manual JSON extraction from model response |
| src/lambda_function.py | Removed Gemini client initialization and API key handling |
| src/test_local.py | Removed GEMINI_API_KEY environment variable check |
| requirements.txt | Replaced google-genai dependency with boto3 |
| README.md | Removed Gemini-related references from documentation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| client = boto3.client( | ||
| service_name="bedrock-runtime", | ||
| region_name="us-west-2" | ||
| ) |
There was a problem hiding this comment.
The boto3 client is created on every function invocation. In AWS Lambda, clients should be initialized outside the handler function to leverage connection pooling and reduce cold start overhead. Consider moving the client creation outside the tos_summarize function or making it a module-level singleton.
| response = client.converse( | ||
| modelId=model_id, | ||
| system=system_instruction, | ||
| messages=messages, | ||
| ) |
There was a problem hiding this comment.
The Bedrock API call lacks error handling. If the API fails (e.g., throttling, service unavailable, invalid model ID), the Lambda will raise an unhandled exception. Add try-except blocks to catch botocore.exceptions.ClientError and return meaningful error messages.
| print("TOS Summarization Response:") | ||
| print(response) |
There was a problem hiding this comment.
Debug print statements are present in production code. These will clutter CloudWatch Logs with potentially large response objects containing sensitive data. Consider using proper logging with appropriate log levels (e.g., logging.debug()) or remove these statements for production.
| print("TOS Summarization Response:") | |
| print(response) | |
| # If needed, use logging for debug information: | |
| # import logging | |
| # logging.debug("TOS Summarization Response: %s", response) |
| client = boto3.client( | ||
| service_name="bedrock-runtime", | ||
| region_name="us-west-2" | ||
| ) |
There was a problem hiding this comment.
The boto3 client is created on every function invocation. In AWS Lambda, clients should be initialized outside the handler function to leverage connection pooling and reduce cold start overhead. Consider moving the client creation outside the tos_evaluate function or making it a module-level singleton.
| response = client.converse( | ||
| modelId=model_id, | ||
| system=system_instruction, | ||
| messages=messages, | ||
| ) |
There was a problem hiding this comment.
The Bedrock API call lacks error handling. If the API fails (e.g., throttling, service unavailable, invalid model ID), the Lambda will raise an unhandled exception. Add try-except blocks to catch botocore.exceptions.ClientError and return meaningful error messages.
| print("TOS Evaluation Response:") | ||
| print(response) |
There was a problem hiding this comment.
Debug print statements are present in production code. These will clutter CloudWatch Logs with potentially large response objects. Consider using proper logging with appropriate log levels (e.g., logging.debug()) or remove these statements for production.
| return json.loads(response.text) | ||
| text = response['output']['message']['content'][0]['text'] | ||
| start = text.find('{') | ||
| end = text.rfind('}') + 1 |
There was a problem hiding this comment.
The manual JSON extraction is fragile and can fail if the response doesn't contain curly braces or if the JSON is malformed. If find('{') returns -1, the slicing will produce incorrect results. Add validation to check if start is -1 and handle the case where no valid JSON is found, or consider using Bedrock's response format configuration to ensure JSON-only output.
| end = text.rfind('}') + 1 | |
| end = text.rfind('}') + 1 | |
| if start == -1 or end == 0 or end <= start: | |
| raise ValueError("No valid JSON object found in the response text.") |
| json_text = text[start:end] | ||
|
|
||
| # response에서 JSON 파싱 후 반환 | ||
| return json.loads(json_text) No newline at end of file |
There was a problem hiding this comment.
The json.loads() call can raise json.JSONDecodeError if the extracted text is not valid JSON. This exception is not handled and will cause the Lambda function to fail. Wrap this in a try-except block to catch and handle JSON parsing errors gracefully.
| region_name="us-west-2" | ||
| ) | ||
|
|
||
| model_id = "us.anthropic.claude-sonnet-4-5-20250929-v1:0" |
There was a problem hiding this comment.
The model ID is duplicated in both tos_summarize.py and tos_evaluate.py. Consider extracting this to a shared constant or configuration file to maintain consistency and simplify future model updates.
관련 이슈
작업사항
기타
Claude Haiku 4.5 모델을 아직 학교 AWS 계정에서 지원되지 않고 있어 사용하지 않습니다. 추후 해당 모델 지원 확인 시 변경될 수 있습니다.
Claude Sonnet 4.5 모델의 응답이 기존 gemini-2.5-flash-lite 및 테스트 중 사용한 Claude 3.5 Haiku 모델보다는 다소 느려 AWS Lambda 함수의 실행시간이 1분을 넘어가는 경우가 있어, timeout 시간을 5분으로 연장했습니다. 대략 2분 이내에는 실행이 완료되는 것으로 보이긴 합니다.