Skip to content

[Feature] Gemini API 대신 Bedrock을 사용하도록 변경#19

Merged
hjham0856 merged 17 commits intomainfrom
test-deploy
Nov 12, 2025
Merged

[Feature] Gemini API 대신 Bedrock을 사용하도록 변경#19
hjham0856 merged 17 commits intomainfrom
test-deploy

Conversation

@hjham0856
Copy link
Copy Markdown
Collaborator

관련 이슈

작업사항

  • 기존 Gemini API 대신 Bedrock에서 지원하는 Claude Sonnet 4.5 모델을 사용하도록 수정했습니다.
  • Bedrock 사용을 위한 boto3 패키지가 추가되었습니다.

기타

Claude Haiku 4.5 모델을 아직 학교 AWS 계정에서 지원되지 않고 있어 사용하지 않습니다. 추후 해당 모델 지원 확인 시 변경될 수 있습니다.

Claude Sonnet 4.5 모델의 응답이 기존 gemini-2.5-flash-lite 및 테스트 중 사용한 Claude 3.5 Haiku 모델보다는 다소 느려 AWS Lambda 함수의 실행시간이 1분을 넘어가는 경우가 있어, timeout 시간을 5분으로 연장했습니다. 대략 2분 이내에는 실행이 완료되는 것으로 보이긴 합니다.

hjham0856 and others added 17 commits November 11, 2025 23:44
AWS Bedrock에서 지원하는 Claude API로 이전하기 위함입니다.
[Fix] claude가 불필요한 서론/결론 등을 말하지 못하도록 형식을 구체적으로 수정
[Feature] claude 모델을 3.5 haiku에서 sonnet 4.5로 변경
여는 중괄호로 응답을 시작하고 닫는 중괄호로 응답을 끝마치도록 지시합니다. 이 방법마저 소용이 없는 경우, 중괄호를 기준으로 문자열을 잘라 사용하는 방식을 적용합니다.
claude가 양식을 더 잘 지키도록 프롬프트 개선
Claude 모델의 응답이 명시한 양식임을 신뢰할 수 없으므로, 응답에 포함된 처음 여는 중괄호부터 마지막 닫는 중괄호까지 잘라내 사용합니다.
claude의 응답을 여는 중괄호부터 닫는 중괄호까지 잘라 사용
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/tos_summarize.py
Comment on lines +10 to 13
client = boto3.client(
service_name="bedrock-runtime",
region_name="us-west-2"
)
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread src/tos_summarize.py
Comment on lines +23 to +27
response = client.converse(
modelId=model_id,
system=system_instruction,
messages=messages,
)
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread src/tos_summarize.py
Comment on lines +29 to +30
print("TOS Summarization Response:")
print(response)
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
print("TOS Summarization Response:")
print(response)
# If needed, use logging for debug information:
# import logging
# logging.debug("TOS Summarization Response: %s", response)

Copilot uses AI. Check for mistakes.
Comment thread src/tos_evaluate.py
Comment on lines +43 to +46
client = boto3.client(
service_name="bedrock-runtime",
region_name="us-west-2"
)
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread src/tos_evaluate.py
Comment on lines +56 to 60
response = client.converse(
modelId=model_id,
system=system_instruction,
messages=messages,
)
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread src/tos_evaluate.py
Comment on lines +62 to +63
print("TOS Evaluation Response:")
print(response)
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread src/tos_evaluate.py
return json.loads(response.text)
text = response['output']['message']['content'][0]['text']
start = text.find('{')
end = text.rfind('}') + 1
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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.")

Copilot uses AI. Check for mistakes.
Comment thread src/tos_evaluate.py
json_text = text[start:end]

# response에서 JSON 파싱 후 반환
return json.loads(json_text) No newline at end of file
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread src/tos_evaluate.py
region_name="us-west-2"
)

model_id = "us.anthropic.claude-sonnet-4-5-20250929-v1:0"
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@hjham0856 hjham0856 merged commit 5721e55 into main Nov 12, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Bedrock의 Claude로 LLM 전환

2 participants