Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Exploring a LangChain Chain
00:00
In this lesson, you’ll briefly explore the notice parser chain. So please go to the notice_extraction.py file in the chains subfolder.
00:10
Now you’re going to need to feed a structured prompt into an LLM and define the structure of the output you’re looking for. So starting with the output on line 8, you define the NoticeEmailExtract class.
00:24
Now this class inherits from a Pydantic BaseModel, and its purpose is to describe the structure of the output. You might remember you have come across this NoticeEmailExtract object type before, when one of the agent’s tools was expected to return this type of object.
00:40 And that was, of course, a tool that uses this chain you’re looking at right now. Now scrolling through that class, you can see it’s defining output and data types for that output.
00:51 In other words, it gives structure to the output. If you then scroll down to line 83, you can see that you define your prompt there with instructions on how the model should behave.
01:04
Now the message placeholder on line 95, that is where your email notice will flow in. Line 101 is where the LLM you’ll be using is defined. And then the chain is on line 103.
01:20
So line 103, it just feeds the prompt into the model, that’s what a chain does. So notice the .with_structured_output() method, which takes your Pydantic BaseModel class as an input.
01:34
So what happens in the background here is that LangChain converts your NoticeEmailExtract base model to a prompt that tells the LLM to conform its output to the schema defined by NoticeEmailExtract.
01:48 Now that is very, very powerful. We’ll have to leave it here as far as LangChain is concerned. In the next lesson, you’ll see the agent in action.
Become a Member to join the conversation.