LLM을 호출하는 도구 만들기
학교 행정팀이 Wikipedia 검색 에이전트를 활용한 교육 앱 작업을 매우 높게 평가했어요. 이제 여기에 몇 가지 도구를 더 추가해 앱을 확장해 달라고 요청했어요. 이번에는 도구 본문 안에서 LLM을 호출해 역사적 유명 날짜에 대한 질문에 답할 수 있는 historical_events()라는 도구를 만들어 볼 거예요. Wikipedia 도구는 이미 설정되어 있고, llm도 환경에 준비되어 있어요.
이 연습은 강의의 일부입니다
LangChain으로 에이전트형 시스템 설계하기
연습 안내
- 데코레이터를 추가해 도구에 레이블을 지정하고 입력 형식을 문자열로 설정하세요.
try블록 안에서llm의.invoke()메서드를 사용해date_input으로 LLM에 질의하고 역사적 사건을 생성하세요.- LLM 응답의 내용을
.content로 반환하세요. - 오류를 포착하기 위해
Exception을e로 받아 오류 세부 정보가 포함되도록 메시지 형식을 지정하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Use a decorator to label the tool and set the input format to string
@____
def historical_events(date_input: ____) -> ____:
"""Provide a list of important historical events for a given date in any format."""
try:
# Invoke the LLM to interpret the date and generate historical events
response = ____.____(f"List important historical events that occurred on {____}.")
# Return the response
return ____.____
# Set an exception block for errors in retrieval
except ____ as ____:
return f"Error retrieving events: {str(____)}"