feat(backend): introduce Bedrock provider and session-based LLM A/B routing#61
Merged
Merged
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
개요 (Overview)
이 PR에서는 LLM 실행 구조를 리팩토링하여 OpenAI뿐 아니라 Amazon Bedrock을 함께 사용할 수 있도록 LLM Provider 추상화 구조를 도입했습니다.
환경 변수 설정을 통해 실행 시점에 사용할 LLM Provider를 선택할 수 있으며, 세션 기반으로 OpenAI와 Bedrock을 분기하는 A/B 실험 라우팅 기능을 추가했습니다.
이를 통해 RAG 파이프라인에서 다음과 같은 비교 실험이 가능해집니다.
본 변경은 향후 멀티 LLM 아키텍처 운영을 위한 기반 구조를 마련하기 위한 작업입니다.
주요 변경 사항 (Key Changes)
1. LLM Provider 추상화 구조 도입
LLM 호출 구조를 다음과 같이 리팩토링했습니다.
llm_service
→ provider 선택
→ chain_builder
→ OpenAI 또는 Bedrock 호출
환경 변수 설정을 통해 실행 시 사용할 LLM Provider를 동적으로 결정할 수 있습니다.
2. Amazon Bedrock 모델 지원 추가
langchain_aws를 사용하여 Bedrock Chat Model을 사용할 수 있도록 지원을 추가했습니다.사용 가능한 모델 예시:
anthropic.claude-3-haiku-20240307-v1:0
Bedrock 관련 설정은 환경 변수로 제어됩니다.
3. 세션 기반 LLM A/B 라우팅 구현
세션 ID 기반의 deterministic routing을 구현하여 OpenAI와 Bedrock 간 A/B 실험이 가능하도록 했습니다.
동작 방식은 다음과 같습니다.
LLM_PROVIDER_MODE=openai → OpenAI만 사용
LLM_PROVIDER_MODE=bedrock → Bedrock만 사용
LLM_PROVIDER_MODE=ab → 세션 기반 A/B 라우팅
A/B 분기 비율은 다음 환경 변수로 제어됩니다.
LLM_AB_RATIO
4. LLM 성능 관측을 위한 로그 확장
LLM 호출 관련 로그에 provider와 model 정보를 추가했습니다.
예시 로그:
METRIC|event=llm_start|provider=openai|model=gpt-4o-mini
METRIC|event=llm_total|provider=bedrock|model=claude-3-haiku
METRIC|event=request|provider=openai|model=gpt-4o-mini
이를 통해 다음 항목을 직접 비교할 수 있습니다.
환경 변수 (Environment Variables)
새로 추가된 환경 변수는 다음과 같습니다.
LLM_PROVIDER_MODE=openai | bedrock | ab
LLM_AB_RATIO=50
BEDROCK_REGION=us-east-1
BEDROCK_MODEL_ID=anthropic.claude-3-haiku-20240307-v1:0
테스트 (Testing)
로컬 환경 (OpenAI 모드)
LLM_PROVIDER_MODE=openai
확인 사항
로컬 환경 (Bedrock 모드)
LLM_PROVIDER_MODE=bedrock
Provider 분기 로직은 정상 동작함을 확인했습니다.
로컬 환경에서는 AWS 인증 토큰이 유효하지 않아 Bedrock 호출이 실패했으며, 이는 로컬 환경 인증 설정에 따른 정상적인 동작입니다.
Dev 환경
Elastic Beanstalk에 다음 환경 변수를 추가했습니다.
LLM_PROVIDER_MODE=ab
LLM_AB_RATIO=50
BEDROCK_REGION=us-east-1
BEDROCK_MODEL_ID=anthropic.claude-3-haiku-20240307-v1:0
Dev 환경에서 다음 항목을 검증할 예정입니다.
기대 효과 (Impact)
이번 변경을 통해 다음과 같은 구조를 갖추게 됩니다.
LLM Execution Layer
│
├─ OpenAI
└─ Bedrock
│
└─ Session 기반 A/B 실험
이를 통해 다음과 같은 운영 전략을 적용할 수 있습니다.
기존 RAG 파이프라인(Pinecone Retrieval)은 변경 없이 그대로 유지됩니다.