질의와 응답
에이전트 그래프가 설정되어 챗봇을 바로 실행할 수 있어요! 이제 ChatGPT를 사용해 질의에 답할 수 있도록 함수를 정의해 봅시다. 이 함수는 그래프 이벤트를 실시간으로 스트리밍하고, 마지막 메시지를 사용자 질의에 대한 응답으로 반환합니다.
이 연습은 강의의 일부입니다
LangChain으로 에이전트형 시스템 설계하기
연습 안내
- 챗봇 실행을 위해 문자열 매개변수
user_input을 받는stream_graph_updates()함수를 정의하세요. graph에.stream()메서드를 적용하여"messages"에"user"메시지로user_input을 넣어 이벤트를 스트리밍하세요.- 각
event.values()의item에 대해item의"messages"키를 사용해 응답을 가져와 출력하세요. - 챗봇을 테스트하기 위해
stream_graph_updates()함수에user_query를 전달하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Define a function to execute the chatbot based on user input
def stream_graph_updates(____: ____):
# Start streaming events from the graph with the user's input
for event in graph.____({"____": [("____", ____)]}):
# Retrieve and print the chatbot node responses
for ____ in event.values():
print("Agent:", ____["____"])
# Define the user query and run the chatbot
____ = "Who is Ada Lovelace?"
stream_graph_updates(____)