Get startedGet started for free

Build a tool that invokes the LLM

The school administration loves your work on the education app with the Wikipedia search agent. They would love you to expand the app even further with some additional tools. Here, you'll build a tool called historical_events() that's able to invoke the LLM within the body of the tool to answer questions about famous dates in history. The Wikipedia tool has already been set up for you and the llm is available in your environment.

This exercise is part of the course

Designing Agentic Systems with LangChain

View Course

Exercise instructions

  • Add a decorator to label the tool and set the input format to string.
  • Within the try block, use the .invoke() method with the llm to query the LLM with the date_input to generate historical events.
  • Return the content of the LLM's response using .content.
  • Add an Exception block as e to catch errors and format the error message to include the error details.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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(____)}"
Edit and Run Code