First Steps With LangChain: Setting Up and Assembling Instructions

Real Python · Intermediate ·🧠 Large Language Models ·1y ago

Key Takeaways

This video demonstrates how to set up and use LangChain to interact with large language models, specifically OpenAI's API, and how to build and assemble instructions using LangChain's features such as chains, debug mode, and custom objects.

Full Transcript

Welcome to this course where you'll take your first steps with lang chain to interact with large language models. In this course, you learn how to use chat models with lang chain, how to scaffold and reuse prompts with prompt templates, which is a very powerful feature that makes your LLM applications a lot more maintainable. You'll learn how to build chains, which allow you to connect different steps and pipe outputs from one step into the next one. And this is kind of the heart of lang chain. So this is an important concept to properly grasp and you'll learn how you can inspect the inputs and outputs to chains using the built-in debug mode. Now if that sounds interesting to you, keep going with this course. You should know a couple of things before you get started. You will need to have an installation of Python 310 or newer. I will work with 313 in this course. You should have a basic understanding of object-oriented programming in Python, virtual environments, and how to work with environment variables. You should also have some experience interacting with LLMs because that will make the whole thing make a lot more sense. And that's all. I hope you're ready. Let's ask the chat model whether it's ready. Ah, that's not very motivational. What's all that data here? Well, I guess there is a piece of motivational output here. Absolutely. What would you like to get started with, but seems like the LLM is missing some context. Well, fortunately, you're going to learn how to build chains like this motivational chain. And now, if I'm going to ask it if it's ready to get started, I'll get the output that I actually want. Nicely formatted. I'm ready. Let's do this. So, I hope you're ready, too. If you are then move on to the next lesson where you'll get set up so that you can start interacting with large language models using lang chain. Let's start right away. The first step is to create and activate a virtual environment and then install the dependencies. Create a virtual environment that I'll call VNV using Python 313 in this case. And then I'll go ahead and activate it by running source VNV bin activate. The command is slightly different on Windows. And here I'm working on a Mac OS. So on a Unix machine, you can see that the virtual environment is activated by the prepended name of the virtual environment on the left. So here it says VNV. And now I'll go ahead and install the dependencies by typing python-mip install. And then we wanted lang chain lang chain open AI open AI and Python environment variables. Now you'll have to wait for this dependencies to install. Once it's done, you can type python mpip list to confirm that you have a bunch of dependencies in your virtual environment. And these are the direct dependencies that you installed explicitly and the transitive dependencies for those packages. And if you want to know the exact versions of the direct dependencies that I've used in this course, here's a list of them. And you can also get them from our requirements .txt file when you download the supporting materials. And I'm showing you this exact dependencies here because these packages are in active development. So version changes might happen quite frequently and they might introduce breaking changes. So if you want to make sure to work along exactly how I'm doing it in this course, then make sure to pin your dependencies and use these ones. They might work with newer versions, but there might be some differences. Next, you'll need to get an OpenAI API key and put that key into yourv file. You don't have to use OpenAI. You can use any other LM API provider or even a local LM. Lang chain is able to work with all of those. But in this course, I'll work with OpenAI. So if you want to follow along exactly again, then you should get that API key. Otherwise, there's just some small changes you need to do. Once you have the keys, you should put them in your file. So make a new file, call it, and then paste it in there. This is literally going to be just the name of the environment variable, in this case, OpenAI API key, and an equal sign, and then pasting the key in there. Let me show you mine here. Well, not actually mine. Don't get too excited. So, there's not the real key in here, but it'll really just look like this. The file is called environment variable and followed by an equal sign and your key. With this, you're set up and ready to go. But I'm going to tell you about one more optional dependency, which is PT Python. That's an alternative Python ripple because I'll be using the ripple heavily in this course. So I'll do all the coding in the ripple and ptpython just provides a somewhat nicer experience, better syntax highlighting. So it'll make it easier for you to follow along. You don't have to install PTPython. Feel free to just use the built-in Python ripple or work in scripts if you want to. But I'm going to be using ptypy. Again, if you want to follow along exactly, you can also install that alternative Python ripple. And finally, I want to mention the resources. You can get the sample code that includes all the code that I'm going to run in this course from the downloadable resources on the course overview page or from the supporting material dropdown that you can find on each lesson page under the video. So get that sample code so that you can follow along. All right, with this you're ready with the setup and in the next lesson you start looking at lang chain chat models. Let's start looking at the first conceptual aspect of lang chain that you'll be looking at which are the chat models. Chat models are a representation for API calls to a LLM provider or for interfacing with the API of a local LLM. In this course, you'll use chat open AAI which wraps OpenAI LLM such as GPT. You need to import it from the associated library that you've installed and then also instantiate it. And when you instantiate one of these chat models, you generally need to pass the model as a string. So in this course, you'll be using GPD40 because at the time of recording, that's a relatively cheap but capable LLM that's available. And you also set the temperature to zero just so that you can get mostly reproducible results. And that instantiates a chat model. So this is what you need to do to get access to making API requests basically to an LLM provider. Now, with minimal code, you can already start asking questions in your ripple. You'll just need to make sure that your environment has access to the API key, which you're doing by importing envoing the environment variables, and then also of course importing the class that we just discussed, instantiating it, and then you can use the invoke method on one of these chat models and pass it a string, just a question, any sort of prompt basically that you want to pass. and lang chain is going to make an API request and then return the result to you. Let's go ahead and give this a try in the ripple. So I'll need to import and from lang chain open AI import chat open AI. You need to load the environment variables which you can do by using the load env. So you're set with that and now you can go ahead and instantiate the chat model. If something didn't work with the API key, then you'll get an error when you try to instantiate the class that represents your your chat model. In this case, I'm going to say model equals GPT40 and we'll set the temperature to zero. All right, no errors, which means that it had access to the API key. And now I'm ready to make API calls. Chat model.invoke. Going to ask the healthcare related question. What is blood pressure? Now you can see now the API request is happening. So you need to wait for a moment. And here is the response. It's maybe a little bit hard to see where the actual text response is, but you can see that this is a new class that's an AI message, and it has an attribute called content. And the content is the string that gives you the actual text back. You can see it takes up most of this response in this case, but then there's also some additional keyword arguments that give you information about how many tokens were used, etc. So, model name is in here somewhere. So this might also differ depending on which LLMs you're interfacing with, but you can always go ahead and access content. So let me first do this call once again and assign it to a variable so we can inspect it a little more. So one more time waiting for the response. There it is. Response.content content now gives me the string and you can also print that of course and then you get a response that looks kind of similar to what you might expect when you ask such a question in an online interface or in an app that interfaces with that LLM. All right, you can see it also has formatting here. This is markdown formatting and it's got the nice spacing etc. So you could render this and it would look good right now. Here you just have the text output, but this is just plainly sending a question directly to the invoke method on one of these chat models. And that took us uh maybe like five lines of code to get to this point that you can interface with an LLM like that using lang chain. So I think that's already pretty cool. But of course there's more and you already saw a pointer in the first response that we looked at here. You saw that what gets returned is an AI message. So there's a certain label and a specific class associated with it. And let's look at those a bit more in the next lesson. Now you've seen that you can send simple prompts just plain text to chat models using lang chain and that works pretty straightforwardly. But that's not where the power lies. You've seen that the reply was an AI message. So messages is another thing that you can send to a model and messages can have roles such as a system message, human message and AI message where the system message gives instructions on how the model should behave. The human message is usually a user input and the AI message is then the response of the model. You've already seen the AI message. The cool thing is that you can combine a couple of these labeled classes into one prompt that you then send off. Let's take a look at that in the ripple. Again, I'll need to import the message types. So, I'm going to say from langchains schema messages, import human message and system message. Now, I can use these two classes to construct messages. I'm going to put them all into a list of messages. So, I'm going to say messages equals open up a list. And then here I'll first make a system message not an error hopefully I will make a system message that has as its content you're an assistant knowledgeable about healthcare only answer healthcare related questions. So this system prompt that I'm giving let's stick with that for now and I'll add the human message in a moment. So we have this messages list that currently only contains this one message, but you can see that it's a labeled message. So it's an instance of system message that has a certain content. And you can see that there's also additional quarks and response metadata. So there's additional attributes on this system message class that you'll get to know better throughout this course. Now I want to add another message to this. So I'm just going to call it question because I want to change this out later on. So I'll create a question that is a human message with the content where now I'm going to ask what is blood pressure again. Now let me append that. So now my messages list contains a system message and a human message and I can pass this list of messages directly to the invoke method of the chat model. So I can say chatmodel.invoke and then pass in messages and send it off. And in this case, the first response is not going to be very different as what we got before because uh we told the model that it should answer healthcare related questions. So of course we get a nice answer for what blood pressure is that looks quite similar to before. But I just did this so that I can show you now if I change this question for example to how do I change a tire? Hold on a minute to remove it. and put the new one in there. So you can see now my messages contains the same system message that tells the model that it should only answer healthcare related questions and then the human message that would be an input from a user for example that asks this healthcare chatbot how do I change a tire. Now when we go ahead and send that to the chat chat model by passing it to invoke, you'll see a different response. It shouldn't tell the user how to change the tire. So it also doesn't it it replies with I'm here to help with healthcare related questions. If you have any questions about health or medical topics, feel free to ask. So what you effectively did here is you set up two different types of questions. A system message that gives instructions to the model and a human message that mimics a user a possible user input. And the interface for for sending this API call is the same that you did before with just a plain question. But now you have this objects that you're working with. And what gets sent to the model eventually is text. But in between here, lang chain does a lot of things that allow to build complexity that you will see in the later lessons of this course. And you can remember that you're working here with objects rather than just plain strings, which gives you a lot of flexibility that lang chain builds on top of. So, at this point, you might think, sure, these are custom objects, but I'm still like mostly interested in this content string. Like, I'm I'm not passing anything additional here at this point. And you're right. But in the next lesson, you'll see how having these custom objects that represent prompts can help you in building more maintainable prompts because we'll start looking into prompt templates.

Original Description

This is a preview of the video course, "First Steps With LangChain". You’ve likely interacted with large language models (LLMs), like the ones behind OpenAI’s ChatGPT, and experienced their remarkable ability to answer questions, summarize documents, write code, and much more. While LLMs are remarkable by themselves, with a little programming knowledge, you can leverage libraries like LangChain to create your own LLM-powered applications that can do just about anything. This is a portion of the complete course, which you can find here: https://realpython.com/courses/first-steps-langchain/ The rest of the course covers: - Getting to Know Prompt Templates - Preparing a Chat Template - Linking Runnables in a Chain - Inspecting Your Chain - Extending Your Chain - Asking Questions With Context
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Real Python · Real Python · 0 of 60

← Previous Next →
1 A better Python REPL – bpython vs python interpreter
A better Python REPL – bpython vs python interpreter
Real Python
2 Introducing large-type.com – A Utility Website
Introducing large-type.com – A Utility Website
Real Python
3 Reading Hacker News Without Wasting Tons of Time
Reading Hacker News Without Wasting Tons of Time
Real Python
4 Forward References and Python 3 Type Hints
Forward References and Python 3 Type Hints
Real Python
5 Using Sublime Text as your Git Editor
Using Sublime Text as your Git Editor
Real Python
6 Python Code Linting and Auto-Complete for Sublime Text
Python Code Linting and Auto-Complete for Sublime Text
Real Python
7 Make your Python Code More Readable with Custom Exceptions
Make your Python Code More Readable with Custom Exceptions
Real Python
8 Write Better Tests with Sublime Text's Split Layout Feature
Write Better Tests with Sublime Text's Split Layout Feature
Real Python
9 How to Use Sublime Text from the Command Line
How to Use Sublime Text from the Command Line
Real Python
10 Rename Variables with Multiple Selection in Sublime Text
Rename Variables with Multiple Selection in Sublime Text
Real Python
11 Sublime Text Settings for Writing PEP 8 Python
Sublime Text Settings for Writing PEP 8 Python
Real Python
12 Write Cleaner Python with Sublime Text's Indent Guides
Write Cleaner Python with Sublime Text's Indent Guides
Real Python
13 Sublime Text Whitespace Settings for Python Development
Sublime Text Whitespace Settings for Python Development
Real Python
14 Function Argument Unpacking in Python
Function Argument Unpacking in Python
Real Python
15 Python Code Review: Debugging and Refactoring "Conway's Game of Life" +  Automated Tests
Python Code Review: Debugging and Refactoring "Conway's Game of Life" + Automated Tests
Real Python
16 Using "get()" to Return a Default Value from a Python Dict
Using "get()" to Return a Default Value from a Python Dict
Real Python
17 A Python Shorthand for Swapping Two Variables
A Python Shorthand for Swapping Two Variables
Real Python
18 Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Real Python
19 Click & Jump to Test Failures from the Command Line (iTerm2)
Click & Jump to Test Failures from the Command Line (iTerm2)
Real Python
20 Setting up Sublime Text for Python Developers
Setting up Sublime Text for Python Developers
Real Python
21 Sublime Text + Python Guide Overview
Sublime Text + Python Guide Overview
Real Python
22 Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Real Python
23 Type-Checking Python Programs With Type Hints and mypy
Type-Checking Python Programs With Type Hints and mypy
Real Python
24 A Shorthand for Merging Dictionaries in Python 3.5+
A Shorthand for Merging Dictionaries in Python 3.5+
Real Python
25 Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Real Python
26 My Python Code Looks Ugly and Confusing – Help!
My Python Code Looks Ugly and Confusing – Help!
Real Python
27 Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Real Python
28 Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Real Python
29 Programmer Portfolio – Example and Walkthrough
Programmer Portfolio – Example and Walkthrough
Real Python
30 How to Get Your 1st Speaking Gig at a Tech Conference
How to Get Your 1st Speaking Gig at a Tech Conference
Real Python
31 How to Build Your Public Speaking Skills as a Developer
How to Build Your Public Speaking Skills as a Developer
Real Python
32 The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
Real Python
33 Setting up Sublime Text for Python Developers – Lesson #1
Setting up Sublime Text for Python Developers – Lesson #1
Real Python
34 Cool New Features in Python 3.6
Cool New Features in Python 3.6
Real Python
35 "is" vs "==" in Python – What's the Difference? (And When to Use Each)
"is" vs "==" in Python – What's the Difference? (And When to Use Each)
Real Python
36 Emulating switch/case Statements in Python with Dictionaries
Emulating switch/case Statements in Python with Dictionaries
Real Python
37 Python Function Argument Unpacking Tutorial (* and ** Operators)
Python Function Argument Unpacking Tutorial (* and ** Operators)
Real Python
38 What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
Real Python
39 A Crazy Python Dictionary Expression ?!
A Crazy Python Dictionary Expression ?!
Real Python
40 String Conversion in Python: When to Use __repr__ vs __str__
String Conversion in Python: When to Use __repr__ vs __str__
Real Python
41 Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Real Python
42 Optional Arguments in Python With *args and **kwargs
Optional Arguments in Python With *args and **kwargs
Real Python
43 Python Context Managers and the "with" Statement (__enter__ & __exit__)
Python Context Managers and the "with" Statement (__enter__ & __exit__)
Real Python
44 Installing Python Packages with pip and virtualenv / venv
Installing Python Packages with pip and virtualenv / venv
Real Python
45 "For Each" Loops in Python with enumerate() and range()
"For Each" Loops in Python with enumerate() and range()
Real Python
46 Python Code Review: LibreOffice Automation and the Python Standard Library
Python Code Review: LibreOffice Automation and the Python Standard Library
Real Python
47 Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Real Python
48 Python Tutorial: List Comprehensions Step-By-Step
Python Tutorial: List Comprehensions Step-By-Step
Real Python
49 Leveraging Python's Implicit "return None" Statements
Leveraging Python's Implicit "return None" Statements
Real Python
50 What's the meaning of underscores (_ & __) in Python variable names?
What's the meaning of underscores (_ & __) in Python variable names?
Real Python
51 Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Real Python
52 Writing automated tests for Python command-line apps and scripts
Writing automated tests for Python command-line apps and scripts
Real Python
53 How to find great Python packages on PyPI, the Python Package Repository
How to find great Python packages on PyPI, the Python Package Repository
Real Python
54 Immutable vs Mutable Objects in Python
Immutable vs Mutable Objects in Python
Real Python
55 PyPI vs Warehouse, the Next-Generation Python Package Repository
PyPI vs Warehouse, the Next-Generation Python Package Repository
Real Python
56 pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
Real Python
57 My Experience at PyCon 2017 in Portland
My Experience at PyCon 2017 in Portland
Real Python
58 Pylint Tutorial – How to Write Clean Python
Pylint Tutorial – How to Write Clean Python
Real Python
59 "Reverse a List in Python" Tutorial: Three Methods & How-to Demos
"Reverse a List in Python" Tutorial: Three Methods & How-to Demos
Real Python
60 Python Refactoring: "while True" Infinite Loops & The "input" Function
Python Refactoring: "while True" Infinite Loops & The "input" Function
Real Python

This video teaches how to set up and use LangChain to interact with large language models, and how to build and assemble instructions using LangChain's features. It covers topics such as creating and activating virtual environments, installing dependencies, and using custom objects to build maintainable prompts.

Key Takeaways
  1. Create and activate a virtual environment
  2. Install dependencies using pip
  3. Get an OpenAI API key and put it in a file
  4. Create a new file with the environment variable name and API key
  5. Load environment variables using `load_env`
  6. Import and instantiate the chat model
  7. Pass the model as a string to the `invoke` method
  8. Use the `invoke` method to make API requests to the LLM provider
  9. Instantiate a chat model and set temperature to zero
  10. Make API calls to the model using the invoke method
💡 LangChain allows building complexity by working with objects rather than plain strings, enabling more maintainable and flexible interactions with large language models.

Related Reads

📰
Open-Weight LLM API Integration: A Developer's Guide to Flexible AI Integration
Learn to integrate open-weight LLM APIs for flexible AI integration, enabling fine-grained control and vendor-agnostic solutions
Dev.to AI
📰
Meet GPT-Red: an LLM super-hacker OpenAI built to make its models safer
OpenAI's GPT-Red LLM super-hacker strengthens model defenses against cyberattacks, making GPT-5.6 the most robust release yet
MIT Technology Review
📰
AI Memory Is Not Just a Bigger Context Window
Learn why AI memory is more complex than just increasing the context window and how it impacts LLM performance
Dev.to · Paul Crinigan
📰
Enterprise AI Agency strategy
Learn how to implement a comprehensive Enterprise AI Agency strategy using cloud-native infrastructure and scalable data pipelines to drive AI-driven decision-making
Dev.to AI
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →