開始使用免費開始

建立會呼叫 LLM 的工具

學校行政單位很喜歡你在教育應用程式上所做的 Wikipedia 搜尋代理。現在他們希望你再擴充這個應用,加入更多工具。在這裡,你將建立一個名為 historical_events() 的工具,能在工具本體內呼叫 LLM,回答關於歷史著名日期的問題。Wikipedia 工具已為你設定好,且環境中也已提供 llm

本練習屬於課程

Designing Agentic Systems with LangChain

檢視課程

練習說明

  • 加上裝飾器來標示此工具,並將輸入格式設為字串。
  • try 區塊中,使用 llm.invoke() 方法,將 date_input 提供給 LLM 以產生歷史事件描述。
  • 使用 .content 回傳 LLM 回應的內容。
  • 新增 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(____)}"
編輯並執行程式碼