
Have you ever wondered how to build applications that not only utilize large language models (LLMs) but are also capable of interacting with their environment and connecting to other data sources? If so, then LangChain is the answer! In this blog, we will learn about what is LangChain, what are its key aspects, how does it work. We will the learn about creating a ‘Hello World’ Python program using LangChain and OpenAI’s Language Learning Model (LLM).
What is LangChain Framework?
LangChain is a dynamic framework specifically designed for the development of such applications. The unique aspect of LangChain is that it encourages the creation of applications that are “data-aware” and “agentic”.
- Data-aware apps: “Data-aware” applications have the ability to connect and interact with various data sources. This feature allows them to incorporate and process external data, thereby enhancing their functionality and adaptability.
- Agent-based apps: On the other hand, “agentic” applications are designed to interact with their environment. They can respond to changes, make decisions, and take actions based on the context they operate in, making them more dynamic and interactive.
LangChain is built around several core modules, each offering standard, extendable interfaces. These modules include Models, Prompts, Memory, Indexes, Chains, Agents, and Callbacks, which are considered the building blocks of any LLM-powered application.
LangChain is versatile and can be used in a wide range of scenarios, including autonomous agents, agent simulations, personal assistants, question answering, chatbots, querying tabular data, code understanding, interacting with APIs, extraction, summarization, and evaluation.
Setting up the Environment for LangChain Apps
To get started with LangChain on Google Colab, there are several key steps you’ll need to follow:
- Generate API Keys: You first need to create API key for OpenAI. To do this, visit OpenAI and follow their instructions to generate your key.
- Store API Keys: Once you have your keys, you’ll need to store them in a file named .env using the following format:
OPENAI_API_KEY="xxxxxxxxxxx"
Replace the ‘xxxxxxxxxxx’ with your actual API keys. Once your .env file is ready, upload it to the file folder in your Google Colab environment.
- Install Necessary Python Libraries: Next, you need to install the necessary Python libraries. You can do this by creating a requirements.txt file with the following content. On the day of writing the blog, the latest version of Langchain found is 0.0.174
python-dotenv==1.0.0
langchain==0.0.174
openai
azure-core
Once you’ve created this file, upload it to your Google Colab file folder. Then, run the following command in a code cell to install the libraries. Note that we need to install azure-core library for working with langchain version 0.0.174. Otherwise, you will get a warning message or error.
!pip install -r requirements.txt
- Check Your Environment: After installing the necessary libraries, you can check if your environment is set up correctly by executing the following commands:
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
If your environment is set up correctly, this should output True
Executing ‘Hello World’ Program using LangChain
Now that your environment is ready, you can run your first LangChain command. Create a new code cell and enter/execute the following code:
from langchain.llms import OpenAI
llm=OpenAI(model_name="text-davinci-003")
llm("Explain machine learning in one paragraph")
The above Python code is using the LangChain library to interact with an OpenAI model, specifically the “text-davinci-003” model. The line, llm=OpenAI(model_name=”text-davinci-003″), is creating an instance of the OpenAI class, called llm, and specifying “text-davinci-003” as the model to be used. “text-davinci-003” is the name of a specific model provided by OpenAI. You can access information about other libraries from this page on OpenAI GPT-3 models.
If everything is set up correctly, you should see a response from the LangChain model to your prompt. Here is the screenshot representing the output:

Another Method for LangChain Hello World
You can also quickly get started based on the following instructions:
- Set up the environment
!pip install langchain==0.0.174
!pip install openai
!pip install azure-core
- Execute the following code to get your response from ChatGPT
import os
os.environ["OPENAI_API_KEY"] = "abc-k9le9xyzabFGWAbcdEfgHijKTdAPmFnlaD123BvNO" #paste your key here
from langchain.llms import OpenAI
llm = OpenAI(temperature=0.9)
text = "Explain the concept of machine learning in one paragraph"
print(llm(text))
Conclusion
We have set up your Google Colab environment, installed and configured LangChain, and executed our first LangChain program using OpenAI’s language model. This ‘Hello World’ example is just the tip of the iceberg when it comes to the powerful capabilities of LangChain. By now, you should have a solid foundation to explore more complex tasks and dive deeper into the world of LangChain. Experiment with different prompts, play around with various settings, and continue learning.
- How to Access GPT-4 using OpenAI Playground? - May 30, 2023
- Online US Degree Courses & Programs in AI / Machine Learning - May 29, 2023
- AIC & BIC for Selecting Regression Models: Formula, Examples - May 28, 2023
Leave a Reply