AI 대화 관리

Amazon Bedrock 입문

Nikhil Rangarajan

Data Scientist

Bedrock에서의 대화 상태 관리

  • 대화 관리: 자연스럽고 일관된 AI 상호작용에 필수
  • 대화 컨텍스트: 기록과 메타데이터 포함
  • Claude 모델은 컨텍스트를 반영 가능

대화의 구성 요소(메시지, 기록, 메타데이터)를 원으로 보여주는 그림.

Amazon Bedrock 입문

대화 관리 구현

  • ConversationManager 클래스가 핵심 대화 기능 처리
  • 메시지 기록은 사용자·어시스턴트 메시지를 역할과 함께 저장
  • 구조적 접근으로 확장성과 유지보수성 확보
class ConversationManager:

def __init__(self): self.bedrock = boto3.client('bedrock-runtime')
self.conversation_history = [] self.max_tokens = 8000
def add_message(self, role, content): self.conversation_history .append({"role": role,
"content": content
})
Amazon Bedrock 입문

ConversationManager 활용

  • 새 메시지를 대화 로그에 추가
  • 사용자 입력이 기록되도록 보장
  • 모델이 컨텍스트를 유지하도록 지원

 

conversation_manager = ConversationManager()
conversation_manager.add_message("user", "What's the weather like?")
Amazon Bedrock 입문

ConversationManager 활용

  • 메시지 형태로 포맷
  • 전송 텍스트 길이 제한
  • 포맷된 대화를 모델에 전송
# Send the latest context to the model
conversation_history = conversation_manager.conversation_history
messages = "\n\n".join(
    f"{msg['role']}: {msg['content']}" 
    for msg in conversation_history[-2:]) # Only most recent exchanges


# Send the full message to the model response = conversation_manager.bedrock.invoke_model( modelId="amazon.nova-micro-v1:0", body=messages)
Amazon Bedrock 입문

연습해 봅시다!

Amazon Bedrock 입문

Preparing Video For Download...