대화 기록에 대해 질문하기
LangChain의 기본 ReAct 에이전트에서는 에이전트의 대화 기록을 유지해 후속 질문을 할 수 있어요. LLM이 이전 메시지 전체에 접근할 수 있으므로 이제 새로운 질문을 해도 에이전트가 전체 메시지 맥락을 활용해 답할 수 있습니다.
이제 다른 삼각형의 변에 대해 후속 질문을 해 보겠습니다.
HumanMessage와 AIMessage 기능을 사용할 수 있도록 다음 모듈이 이미 임포트되어 있어요: HumanMessage, AIMessage.
이 연습은 강의의 일부입니다
LangChain으로 에이전트형 시스템 설계하기
연습 안내
- 주어진 자연어 질문을
new_query에 할당하세요. app객체를 호출할 때message_history와new_query를 포함한 모든 메시지를 함께 전달하세요.- 리스트 컴프리헨션을 사용해
response["messages"]에서HumanMessage또는AIMessage로 라벨된 메시지를 추출하세요. - 새 쿼리를 입력으로 전달하고, 메시지 클래스를
"agent_output"에 전달해 추출된 메시지를 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
message_history = response["messages"]
____ = "What about one with sides 12 and 14?"
# Invoke the app with the full message history
response = app.____({"messages": ____ + [("human", ____)]})
# Extract the human and AI messages from the result
filtered_messages = [msg for msg in ____["____"] if isinstance(msg, (____, ____)) and msg.content.strip()]
# Pass the new query as input and print the final outputs
print({
"user_input": ____,
"agent_output": [f"{msg.____.____}: {msg.content}" for msg in ____]
})