대화를 메모리 인식으로 만들기
ChargeNet의 ChargeBot 작업을 이어서, 대화의 각 턴을 저장하고 가장 최근 메시지만 Claude에 보내 Amazon Bedrock의 Claude 모델로 대화 문맥을 유지해 보세요. 이 봇은 메시지 기록을 관리하고, 가장 최근 문맥만 사용하여 Claude가 이해할 수 있는 형식으로 대화를 구성합니다.
boto3와 json 라이브러리, 그리고 이전 연습 문제에서 정의한 ConversationManager 클래스가 미리 로드되어 있습니다.
이 연습은 강의의 일부입니다
Amazon Bedrock 입문
연습 안내
add_message()메서드를 사용해 사용자의 입력을 저장하세요.- 올바른 메시지 형식을 사용하여 기록에서 마지막 두 개의 메시지만 Claude에 보내세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
conversation = ConversationManager()
user_input = "What is the charging speed of your Highway Max station?"
# Add the user input
conversation.____
# Send only the last two messages from conversation history
messages = ____
request_body = json.dumps({"anthropic_version": "bedrock-2023-05-31", "max_tokens": 200, "temperature": 0.2, "messages": messages})
response = conversation.bedrock.invoke_model(modelId="us.anthropic.claude-sonnet-4-5-20250929-v1:0", body=request_body)
completion = json.loads(response['body'].read().decode())["content"][0]["text"]
conversation.add_message("assistant", completion)
print(completion)