AI Agents with LangGraph

Nov 12 2024 · Python 3.12, LangGraph 0.2.x, JupyterLab 4.2.4

Lesson 05: Evaluating & Optimizing AI Agents

Monitoring & Debugging Demo

Episode complete

Play next episode

Next
Transcript

Monitoring & Debugging Demo

Open the localizer.ipynb notebook in this lesson’s Starter folder. It should be in the same state as you left it at the end of Lesson 4.

You’ll start by replacing the print statements with the logging library. Add a new code cell near the top of the notebook. Then import and initialize the library:

import logging

logging.basicConfig(
  level=logging.DEBUG,
  format="%(asctime)s - %(levelname)s - %(message)s"
)

Setting the level tells the logger how much data to output. DEBUG is the most verbose level. The default is WARNING, so if you didn’t specify the level here, it’d only show things like warnings and errors. format tells the logger what information to show when it outputs a line. In this case, you show the time, the logging level and the message.

Now, go through your notebook and replace every print statement with a logging.info call. Rerun the cells. Wow, you’ve got a lot more debugging output than before. If that’s too much, go back to the basicConfig and change DEBUG to a higher level. Set it to INFO, restart the kernel, and try again. This time, there’s a lot less output.

Now, tell the logger to send its output to a file:

logging.basicConfig(
  level=logging.INFO,
  format="%(asctime)s - %(levelname)s - %(message)s",
  filename="app.log"
)

The logs will be stored in a file called app.log. Restart the kernel and run through the cells again. If you open the file, you can see the output there. The problem is that you can’t see it in the notebook anymore. There are solutions for that, but for now, just disable the file logging. Restart the kernel.

Next, you’ll see how to stream the output as the graph executes. Run the cells until you get to the one with app.invoke. Don’t run that cell yet. Comment out the app.invoke line and replace it with:

for output in app.stream(state, thread, stream_mode="values"):
  print(output)

Setting the stream_mode to values gives you the entire state at every node. Run that and see what you get. That’s a lot of output.

Your image is still stored in the message list, and since the whole list gets printed at every node, you keep seeing it again and again. How about only printing what’s new each time? Change values to updates and run the cell again. This time, it’s much shorter.

You could stream the output in the final cell as well, but for now, just leave it as invoke.

The last thing you’ll learn in this demo is how to use LangSmith. Go to the LangSmith website at smith.langchain.com. Then sign in.

Go to Settings and API Keys and click Create API Key. Give the key a description. Choose Personal Access Token for the type and click Create API Key. Copy your key. Then open your .env file and add two lines:

LANGCHAIN_API_KEY=<your LangSmith API key>
LANGCHAIN_TRACING_V2=true

This demo assumes you’ve previously installed LangChain with pip install langchain. LangSmith is part of that package.

Restart the kernel. Load the keys and run through the whole notebook as usual.

Now, go back to the LangSmith website. Go back to the home screen. Choose Projects. Click default. Choose the bottom item in the list. That’s the oldest one.

You can see the start of your graph, the contextualizer, the translator and the checker. If you scroll through the Run output, you can see it’s all in an easy-to-read format. Nice, isn’t it? LangSmith is a paid service, but you can see it might be worth your while to use it. If you prefer not to, you can always stick with logging and streaming.

See forum comments
Cinema mode Download course materials from Github
Previous: Making Improvements to AI Agents Next: Conclusion