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

Linking Runnables in a Chain

00:00 In this lesson, you’ll make it to the core of LangChain, which are the chains as also exemplified in the naming of the library. In LangChain, you can build chains to connect multiple runnable components.

00:12 You’ve already encountered a couple of these runnable components. For example, the prompt templates that you’ve built in the previous lesson are runnable components and chat models are runnable components.

00:22 There’s things like output parsers, and many more.

00:27 In LangChain, you can create a chain using the pipe operator. So using the two objects that you’ve worked with before, the review_prompt_template and the chat_model, you can create a chain by placing one after another, and then separate them with a pipe character.

00:43 And with just that, you’ve already created a chain that you can now invoke. During one call to invoke runs several steps in sequence. In this case, it would be two steps like creating, setting up the prompt template, interpolating any values that you need to stick in there, and then passing the outputs of that first runnable component on to the next one.

01:06 In this case, the chat_model that then makes the API request. So this gives you a very flexible way to arrange runnable components together to add something at the end, or swap out a component somewhere in the middle of that chain, and then still just run it by using the invoke() method and then passing the inputs that you need.

01:25 So the inputs in this case for the chain that you’ve built here would be the context and the question that you’ve previously defined as strings and then passed to chat_model.invoke().

01:35 Here, you’ll move towards running invoke() on a review chain. Let’s try that out. So you’ll still need to have access to the review_prompt_template that you’ve defined previously and the chat_model.

01:52 And then you can create a review_chain by passing this review_prompt_template, then the | operator, and then the chat_model.

02:06 So this is a declarative syntax for creating chains in LangChain. This case, the outputs of the review_prompt_template object are going to be passed on to the chat_model.

02:17 And you can now go ahead and invoke this review chain. So I’m going to say review_chain.invoke(),

02:25 and now I need to pass it the inputs that the review_prompt_template needs to operate. In that case, that was a string for context and a string for question.

02:35 And you’ll pass that as a dictionary. So where you use the replacement field variables as the keys. The first one was context, or one of them was context, is a dictionary that order doesn’t matter.

02:50 And the context would be, “I had a great stay!”

02:56 And then we also need a question, so I’ll write a question key. And as a value to that key, I’m going to pass the user question, which is, “Did anyone have a good experience?”

03:12 Or something like that. Alright. And then when I press Enter, then LangChain sets up the review_prompt_template, interpolates the values, passes it on to the chat model, and then makes the API request.

03:26 And as a response, again, you get the AIMessage object with the content that has the answer that we’re looking for. ‘Yes, one patient mentioned having a great stay, indicating a positive experience.’

03:38 Alright, so maybe you can see that this makes your chains quite flexible. You can swap out components of that chain and just chain more stuff to it, which you’ll do in just a moment.

03:47 But you may be interested to know what exactly happened behind the scenes here, right? Like I’ve talked you through it, but is there a way to also see that?

03:56 And of course, LangChain provides something like that for you, which is called the debug mode. Let’s look at that in the next lesson.

Become a Member to join the conversation.