Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Inspecting Your Chain

00:00 So in the previous lesson, you’ve created and invoked a chain, and we talked about how it runs every step in sequence and passes the output of the previous step as the input to the next step.

00:10 Okay? But let’s take a look to actually see what’s happening behind the scenes. And you can inspect your chain by running these two lines of code: from langchain_core.globals import set_debug.

00:24 And then you need to call this set_debug() function and pass it True as the argument.

00:31 Let’s try that out: from langchain_core.globals import set_debug. And then I will set_debug(True) by calling the set_debug() function and passing True. No visible change, but now when you go ahead and invoke that review chain a second time, you can see that there’s more happening.

00:55 We get a lot of output. Let me scroll up to the beginning of this. Okay, so here we are. First, it’s creating a RunnableSequence, then you can see it creates a ChatPromptTemplate object.

01:07 And eventually, it exits that chain run with the outputs, which is going to be your assembled chat prompt template with the values interpolated into that template.

01:17 And then it starts the next sequence and another runnable sequence, which now is not a prompt. So before it was a prompt, and now it’s an LLM. So this is a chat model that you’re working with. Now it’s the ChatOpenAI class, and it tells you that it’s entering LLM run with the input.

01:34 And here you can see the assembled prompt. This is what LangChain created from your prompt template. It wrote down system, and then the system prompt, which goes until here in that case. And then it also, that’s what it made out of the human prompt Human:, and then “Did anyone have a good experience?” These are the prompts that it’s sending.

01:54 And then again, you can see the end of the LLM run, where it tells you that it exited the LLM run with the following output. And then you get the API response from making an API call to OpenAI in this case.

02:07 And here you can see the text: “Yes, one patient mentioned having a great stay indicating a positive experience.” And a lot of additional information.

02:16 So that’s the raw API response that you get back. And then you can see also, again, it says, okay, that’s the end of the chain. The runnable sequence exits with the following output.

02:27 And then here is the final output, which is back into LangChain, where it creates an AIMessage object and arranges that information from the API response into the structured manner that you can reuse across different LLM providers, right?

02:42 Maybe a different LLM provider, which has a different structure in their API responses. But Langchain handles those and gives you one interface back, which is the AIMessage that you’ve seen before.

02:53 So using the set_debug() method, you can get very verbose output where you can inspect what happened when you ran a chain. In this case, the chain only had two steps involved, but if those are longer chains, your output will be longer.

03:06 And this can be super helpful when you need to debug any piece of the chain when it doesn’t do exactly what you want it to do. If it’s too verbose, you can go ahead and set_debug(False).

03:17 And then again, LangChain doesn’t give you this information and only gives you back the final output. Keep that in your toolbelt when you develop more complex chains, it’s very helpful for debugging.

03:28 Okay, but I mentioned that these chains are quite flexible. And now let’s go ahead and add something to this review_chain object that currently consists of this ChatPromptTemplate and then the ChatOpenAI.

03:41 So it consists of these two runnable elements so far. And now let’s add a third one in the next lesson.

Become a Member to join the conversation.