The capabilities of large language models (LLMs) such as OpenAI’s GPT-3, Google’s BERT, and Meta’s LLaMA are transforming various industries by enabling the generation of diverse types of text, ranging from marketing content and data science code to poetry. Although ChatGPT has garnered significant attention due to its user-friendly chat interface, numerous untapped possibilities exist for leveraging LLMs by integrating them into different software applications.
If you're captivated by the transformative powers of Generative AI and LLMs, this tutorial is perfect for you. Here, we explore LangChain - An open-source Python framework for building applications based on Large Language Models such as GPT.
Learn more about building AI applications with LangChain in our Building Multimodal AI Applications with LangChain & the OpenAI API AI Code Along where you'll discover how to transcribe YouTube video content with the Whisper speech-to-text AI and then use GPT to ask questions about the content.
What are Large Language Models (LLMs)?
Large Language Models (LLMs) refer to advanced artificial intelligence systems designed to understand and generate human-like text. These models are trained on vast amounts of data, enabling them to grasp complex patterns, comprehend language nuances, and generate coherent responses. LLMs have the capability to perform various language-related tasks, including language translation, text completion, summarization, and even engaging in conversational interactions. GPT is an example of LLM.
LLM is a type of Generative AI. If you would like to learn about Generative AI and how it can boost your creativity, check our blogs on Using Generative AI to Boost Your Creativity and our podcast, Inside the Generative AI Revolution. You can also register for our upcoming course on Large Language Models Concepts.
Introduction to LangChain
LangChain is an open-source framework designed to facilitate the development of applications powered by large language models (LLMs). It offers a suite of tools, components, and interfaces that simplify the construction of LLM-centric applications. With LangChain, it becomes effortless to manage interactions with language models, seamlessly link different components, and incorporate resources such as APIs and databases. You can read more about LangChain For Data Engineering and Data Applications in a separate article.
The LangChain platform comes with a collection of APIs that developers can embed in their applications, empowering them to infuse language processing capabilities without having to build everything from the ground up. Therefore, LangChain efficiently simplifies the process of crafting LLM-based applications, making it suitable for developers across the spectrum of expertise.
Applications like chatbots, virtual assistants, language translation utilities, and sentiment analysis tools are all instances of LLM-powered apps. Developers leverage LangChain to create bespoke language model-based applications that cater to specific needs.
With the continual advancements and broader adoption of natural language processing, the potential applications of this technology are expected to be virtually limitless. Here are several noteworthy characteristics of LangChain:
1. Tailorable prompts to meet your specific requirements
2. Constructing chain link components for advanced usage scenarios
3. Integrating models for data augmentation and accessing top-notch language model capabilities, such as GPT and HuggingFace Hub.
4. Versatile components that allow mixing and matching for specific needs
5. Manipulating context to establish and guide context for enhanced precision and user satisfaction
Setting up LangChain in Python
Installing LangChain in Python is pretty straightforward. You can either install it with pip or conda.
Install using pip
pip install langchain
Install using conda
install langchain -c conda-forge
This will set up the basic necessities of LangChain. Much of LangChain's usefulness is realized when it's integrated with diverse model providers, data stores, and the like.
By default, the dependencies required for these integrations are NOT included in the installation. To install all dependencies, you can run the following command:
pip install langchain[all]
The final option is to build the library from the source. In that case, you can clone the project from its GitHub repo.
Environment setup
Using LangChain usually requires integrations with various model providers, data stores, APIs, and similar components. As with any integration, we must provide appropriate and relevant API keys for LangChain to function. There are two ways to achieve this:
1. Setting up key as an environment variable
OPENAI_API_KEY="..."
If you'd prefer not to set an environment variable, you can pass the key in directly via the openai_api_key named parameter when initiating the OpenAI LLM class:
2. Directly set up the key in the relevant class
pip install langchain
Key Components of LangChain
LangChain stands out due to its emphasis on flexibility and modularity. It disassembles the natural language processing pipeline into separate components, enabling developers to tailor workflows according to their needs. This adaptability makes LangChain ideal for constructing AI applications across various scenarios and sectors.
Components and chains
In LangChain, components are modules performing specific functions in the language processing pipeline. These components can be linked into "chains" for tailored workflows, such as a customer service chatbot chain with sentiment analysis, intent recognition, and response generation modules.
Prompt templates
Prompt templates are reusable predefined prompts across chains. These templates can become dynamic and adaptable by inserting specific "values." For example, a prompt asking for a user's name could be personalized by inserting a specific value. This feature is beneficial for generating prompts based on dynamic resources.
Vector stores
These are used to store and search information via embeddings, essentially analyzing numerical representations of document meanings. VectorStore serves as a storage facility for these embeddings, allowing efficient search based on semantic similarity.
Indexes and retrievers
Indexes act as databases storing details and metadata about the model's training data, while retrievers swiftly search this index for specific information. This improves the model's responses by providing context and related information.
Output parsers
Output parsers come into play to manage and refine the responses generated by the model. They can eliminate undesired content, tailor the output format, or supplement extra data to the response. Thus, output parsers help extract structured results, like JSON objects, from the language model's responses.
Example selectors
Example selectors in LangChain serve to identify appropriate instances from the model's training data, thus improving the precision and pertinence of the generated responses. These selectors can be adjusted to favor certain types of examples or filter out unrelated ones, providing a tailored AI response based on user input.
Agents
Agents are unique LangChain instances, each with specific prompts, memory, and chain for a particular use case. They can be deployed on various platforms, including web, mobile, and chatbots, catering to a wide audience.
How to Build A Language Model Application in LangChain
LangChain provides an LLM class designed for interfacing with various language model providers, such as OpenAI, Cohere, and Hugging Face. The most basic functionality of an LLM is generating text. It is very straightforward to build an application with LangChain that takes a string prompt and returns the output.
pip install langchain
Output:
>>> "What do you get when you tinker with data? A data scientist!"
In the example above, we are using text-ada-001 model from OpenAI. If you would like to swap that for any open-source models from HuggingFace, it’s a simple change:
install langchain -c conda-forge
You can get the Hugging Face hub token id from your HF account.
If you have multiple prompts, you can send a list of prompts at once using the generate method:
pip install langchain[all]
Output:
This is the simplest possible app you can create using LangChain. It takes a prompt, sends it to a language model of your choice, and returns the answer. There are many parameters that you can control, such as `temperature`. The temperature parameter adjusts the randomness of the output, and it is set to 0.7 by default.
Managing Prompt Templates for LLMs in LangChain
LLMs have peculiar APIs. While it may seem intuitive to input prompts in natural language, it actually requires some adjustment of the prompt to achieve the desired output from an LLM. This adjustment process is known as prompt engineering. Once you have a good prompt, you may want to use it as a template for other purposes.
A PromptTemplate in LangChain allows you to use templating to generate a prompt. This is useful when you want to use the same prompt outline in multiple places but with certain values changed.
OPENAI_API_KEY="..."
Output:
1. Climb the Eiffel Tower and take in the breathtaking views of the city
2. Enjoy a romantic cruise along the River Seine and admire the beautiful architecture along the riverbanks
3. Explore the Louvre and admire the world-renowned works of art on display
If you now want to re-use this prompt for a different city, you only have to change the USER_INPUT variable. I have now changed it from Paris to Cancun, Mexico. See how the output was changed:
Output:
1. Relax on the Beach: Enjoy the white sand beaches and crystal-clear waters of the Caribbean Sea.
2. Explore the Mayan Ruins: Visit ancient archaeological sites such as Chichen Itza, Tulum, and Coba to learn about the history and culture of the Mayans.
3. Take a Food Tour: Taste the traditional flavors and learn about the local cuisine by taking a food tour of Cancun.
Combining LLMs and Prompts in Multi-Step Workflows
Chaining within the LangChain context refers to the act of integrating LLMs with other elements to build an application. Several examples include:
- Sequentially combining multiple LLMs by using the output of the first LLM as input for the second LLM (refer to this section)
- Integrating LLMs with prompt templates
- Merging LLMs with external data, such as for question answering
- Incorporating LLMs with long-term memory, like chat history
Let’s see an example of the first scenario where we will use the output from the first LLM as an input to the second LLM.
pip install langchain
Output:
In this particular example, we create a chain with two components. The first component is responsible for identifying the most popular city corresponding to a particular country as input by the user. In contrast, the second component focuses on providing information about the top three activities or attractions available for tourists visiting that specific city.
If you would like to learn more advanced concepts of building applications in LangChain, check out this live course on Building AI Applications with LangChain and GPT on DataCamp.
Conclusion and Further Learning
Only a short while ago, we were all greatly impressed by the impressive capabilities of ChatGPT. However, the landscape has rapidly evolved, and now we have access to new developer tools like LangChain that empower us to create similarly remarkable prototypes on our personal laptops in just a matter of hours.
LangChain, an open-source Python framework, enables individuals to create applications powered by LLMs (Language Model Models). This framework offers a versatile interface to numerous foundational models, facilitating prompt management and serving as a central hub for other components such as prompt templates, additional LLMs, external data, and other tools through agents (at the time of writing).
If you are trying to keep up with all the advancements in Generative AI and LLM, check out our Building AI Applications with LangChain and GPT webinar. Here, you will learn the basics of using LangChain to develop AI applications, as well as how to structure an AI application and how to embed text data for high performance. You can also view our cheat sheet on the generative AI tools landscape to explore the different categories of generative AI tools, their applications, and their influence in various sectors. Finally, check out our list of the top open-source LLMs to learn about other powerful tools.
Earn a Top AI Certification
Demonstrate you can effectively and responsibly use AI.Get Certified, Get HiredThe above is the detailed content of How to Build LLM Applications with LangChain Tutorial. For more information, please follow other related articles on the PHP Chinese website!
![Can't use ChatGPT! Explaining the causes and solutions that can be tested immediately [Latest 2025]](https://img.php.cn/upload/article/001/242/473/174717025174979.jpg?x-oss-process=image/resize,p_40)
ChatGPT is not accessible? This article provides a variety of practical solutions! Many users may encounter problems such as inaccessibility or slow response when using ChatGPT on a daily basis. This article will guide you to solve these problems step by step based on different situations. Causes of ChatGPT's inaccessibility and preliminary troubleshooting First, we need to determine whether the problem lies in the OpenAI server side, or the user's own network or device problems. Please follow the steps below to troubleshoot: Step 1: Check the official status of OpenAI Visit the OpenAI Status page (status.openai.com) to see if the ChatGPT service is running normally. If a red or yellow alarm is displayed, it means Open

On 10 May 2025, MIT physicist Max Tegmark told The Guardian that AI labs should emulate Oppenheimer’s Trinity-test calculus before releasing Artificial Super-Intelligence. “My assessment is that the 'Compton constant', the probability that a race to

AI music creation technology is changing with each passing day. This article will use AI models such as ChatGPT as an example to explain in detail how to use AI to assist music creation, and explain it with actual cases. We will introduce how to create music through SunoAI, AI jukebox on Hugging Face, and Python's Music21 library. Through these technologies, everyone can easily create original music. However, it should be noted that the copyright issue of AI-generated content cannot be ignored, and you must be cautious when using it. Let’s explore the infinite possibilities of AI in the music field together! OpenAI's latest AI agent "OpenAI Deep Research" introduces: [ChatGPT]Ope

The emergence of ChatGPT-4 has greatly expanded the possibility of AI applications. Compared with GPT-3.5, ChatGPT-4 has significantly improved. It has powerful context comprehension capabilities and can also recognize and generate images. It is a universal AI assistant. It has shown great potential in many fields such as improving business efficiency and assisting creation. However, at the same time, we must also pay attention to the precautions in its use. This article will explain the characteristics of ChatGPT-4 in detail and introduce effective usage methods for different scenarios. The article contains skills to make full use of the latest AI technologies, please refer to it. OpenAI's latest AI agent, please click the link below for details of "OpenAI Deep Research"

ChatGPT App: Unleash your creativity with the AI assistant! Beginner's Guide The ChatGPT app is an innovative AI assistant that handles a wide range of tasks, including writing, translation, and question answering. It is a tool with endless possibilities that is useful for creative activities and information gathering. In this article, we will explain in an easy-to-understand way for beginners, from how to install the ChatGPT smartphone app, to the features unique to apps such as voice input functions and plugins, as well as the points to keep in mind when using the app. We'll also be taking a closer look at plugin restrictions and device-to-device configuration synchronization

ChatGPT Chinese version: Unlock new experience of Chinese AI dialogue ChatGPT is popular all over the world, did you know it also offers a Chinese version? This powerful AI tool not only supports daily conversations, but also handles professional content and is compatible with Simplified and Traditional Chinese. Whether it is a user in China or a friend who is learning Chinese, you can benefit from it. This article will introduce in detail how to use ChatGPT Chinese version, including account settings, Chinese prompt word input, filter use, and selection of different packages, and analyze potential risks and response strategies. In addition, we will also compare ChatGPT Chinese version with other Chinese AI tools to help you better understand its advantages and application scenarios. OpenAI's latest AI intelligence

These can be thought of as the next leap forward in the field of generative AI, which gave us ChatGPT and other large-language-model chatbots. Rather than simply answering questions or generating information, they can take action on our behalf, inter

Efficient multiple account management techniques using ChatGPT | A thorough explanation of how to use business and private life! ChatGPT is used in a variety of situations, but some people may be worried about managing multiple accounts. This article will explain in detail how to create multiple accounts for ChatGPT, what to do when using it, and how to operate it safely and efficiently. We also cover important points such as the difference in business and private use, and complying with OpenAI's terms of use, and provide a guide to help you safely utilize multiple accounts. OpenAI


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
