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(____)}"