DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • LLMops: The Future of AI Model Management
  • Developing Intelligent and Relevant Software Applications Through the Utilization of AI and ML Technologies
  • Mastering Synthetic Data Generation: Applications and Best Practices
  • ChatGPT Applications: Unleashing the Potential Across Industries

Trending

  • Power BI Embedded Analytics — Part 2: Power BI Embedded Overview
  • Java Virtual Threads and Scaling
  • Contextual AI Integration for Agile Product Teams
  • How to Format Articles for DZone
  1. DZone
  2. Data Engineering
  3. Databases
  4. What Is Langchain And Large Language Models?

What Is Langchain And Large Language Models?

In this comprehensive article, we explore both LangChain and Large Language Models. We will go through a simple tutorial to understand both.

By 
Pavan Belagatti user avatar
Pavan Belagatti
DZone Core CORE ·
Sep. 05, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
3.8K Views

Join the DZone community and get the full member experience.

Join For Free

If you're a developer or simply someone passionate about technology, you've likely encountered AI tools such as ChatGPT. These utilities are powered by advanced large language models (LLMs). Interested in taking it up a notch by crafting your own LLM-based applications? If so, LangChain is the platform for you.

Let's keep everything aside and understand about LLMs first. Then, we can go over LangChain with a simple tutorial. Sounds interesting enough? Let's get going.

What Are Large Language Models (LlMS)?

Large Language Models (LLMs) like GPT-3 and GPT-4 from OpenAI are machine learning algorithms designed to understand and generate human-like text based on the data they've been trained on. These models are built using neural networks with millions or even billions of parameters, making them capable of complex tasks such as translation, summarization, question-answering, and even creative writing.

Trained on diverse and extensive datasets, often encompassing parts of the internet, books, and other texts, LLMs analyze the patterns and relationships between words and phrases to generate coherent and contextually relevant output. While they can perform a wide range of linguistic tasks, they are not conscious and don't possess understanding or emotions, despite their ability to mimic such qualities in the text they generate.

LlMS

Source Credits: NVIDIA


Large language models primarily belong to a category of deep learning structures known as transformer networks. A transformer model is a type of neural network that gains an understanding of context and significance by identifying the connections between elements in a sequence, such as the words in a given sentence.

What Is LangChain?

Developed by Harrison Chase, and debuted in October 2022, LangChain serves as an open-source platform designed for constructing sturdy applications powered by Large Language Models, such as chatbots like ChatGPT and various tailor-made applications.

Langchain seeks to equip data engineers with an all-encompassing toolkit for utilizing LLMs in diverse use cases, such as chatbots, automated question-answering, text summarization, and beyond.

LangChain is composed of 6 modules explained below:

LlMS

Image credits: ByteByteGo


  • Large Language Models:
    LangChain serves as a standard interface that allows for interactions with a wide range of Large Language Models (LLMs).

  • Prompt Construction:
    LangChain offers a variety of classes and functions designed to simplify the process of creating and handling prompts.

  • Conversational Memory:
    LangChain incorporates memory modules that enable the management and alteration of past chat conversations, a key feature for chatbots that need to recall previous interactions.

  • Intelligent Agents:
    LangChain equips agents with a comprehensive toolkit. These agents can choose which tools to utilize based on user input.

  • Indexes:
    Indexes in LangChain are methods for organizing documents in a manner that facilitates effective interaction with LLMs.

  • Chains:
    While using a single LLM may be sufficient for simpler tasks, LangChain provides a standard interface and some commonly used implementations for chaining LLMs together for more complex applications, either among themselves or with other specialized modules.

How Does LangChain Work?

How Does LangChain Work?


LangChain is composed of large amounts of data and it breaks down that data into smaller chunks which can be easily embedded into vector store. Now, with the help of LLMs, we can retrieve the only information that is needed.

When a user inserts a prompt, LangChain will query the Vector Store for relevant information. When exact or almost matching information is found, we feed that information to LLM to complete or generate the answer that the user is looking for.

Get Started With LangChain

Let's use SingleStore's Notebooks feature (it is FREE to use) as our development environment for this tutorial.

Get Started With LangChain


The SingleStore Notebook extends the capabilities of Jupyter Notebook to enable data professionals to easily work and play around.

What Is A Single Store?

SingleStore is a distributed, in-memory, SQL database management system designed for high-performance, high-velocity applications. It offers real-time analytics and mixes the capabilities of a traditional operational database with that of an analytical database to allow for transactions and analytics to be performed in a single system.

Sign up for SingleStore to use the Notebooks.

SingleStore

Once you sign up to SingleStore, you will also receive $600 worth of free computing resources. So why not use this opportunity?

Click on 'Notebooks' and start with a blank Notebook.

blank Notebook

Name it something like 'LangChain-Tutorial' or as per your wish.

LangChain-Tutorial

Let's start working with our Notebook that we just created.
Follow this step-by-step guide and keep adding the code shown in each step in your Notebook and execute it. Let's start!

Now, to use Langchain, let’s first install it with the pip command.

Shell
 
!pip install -q langchain


To work with LangChain, you need integrations with one or more model providers like OpenAI or Hugging Face. In this example, let's leverage OpenAI’s APIs, so let's install it.

Shell
 
!pip install -q openai


LangChain-Tutorial


Next, we need to set up the environment variable for the playground.
Let's do that.

Shell
 
import os
os.environ["OPENAI_API_KEY"] = "Your-API-Key"


Hope you know how to get Your-API-Key, if not, go to this link to get your OpenAI API key.

[Note: Make sure you still have the quota to use your API Key]

Next, let’s get an LLM like OpenAI and predict with this model.

Let's ask our model the top 5 most populated cities in the world.

Shell
 
from langchain.llms import OpenAI
llm = OpenAI(temperature=0.7)
text = "what are the 5 most populated cities in the world?"
print(llm(text))


tutorial

As you can see, our model made a prediction and printed the 5 most populated cities in the world.

Prompt Templates

Let’s first define the prompt template.

Shell
 
from langchain.prompts import PromptTemplate

# Creating a prompt
prompt = PromptTemplate(
    input_variables=["input"],
    template="what are the 5 most {input} cities in the world?",
)


Exececute query

We created our prompt. To get a prediction, let’s now call the format method and pass it an input.

print data

Creating Chains

So far, we’ve seen how to initialize a LLM model, and how to get a prediction with this model. Now, let’s take a step forward and chain these steps using the LLMChain class.

Shell
 
from langchain.chains import LLMChain
# Instancing a LLM model
llm = OpenAI(temperature=0.7)
# Creating a prompt
prompt = PromptTemplate(
  input_variables=["attribute"],
  template= "What is the largest {attribute} in the world?",
)


LangChain Tutorial class

You can see the prediction of the model.

Developing An Application Using LangChain LLM

Again use SingleStore's Notebooks as the development environment.

Let's develop a very simple chat application.

Start with a blank Notebook and name it as per your wish.

  • First, install the dependencies.
Shell
 
pip install langchain openai


  • Next, import the installed dependencies.
Shell
 
from langchain import ConversationChain, OpenAI, PromptTemplate, LLMChain
from langchain.memory import ConversationBufferWindowMemory


  • Get your OpenAI API key and save it safely.
  • Add and customize the LLM template

Shell
 
# Customize the LLM template 
template = """Assistant is a large language model trained by OpenAI.

{history}
Human: {human_input}
Assistant:"""

prompt = PromptTemplate(input_variables=["history", "human_input"], template=template)


  • Load the ChatGPT chain with the API key you saved safely. Add the human input as 'What is SingleStore?'. You can change your input to whatever you want.
Shell
 
chatgpt_chain = LLMChain(

           llm=OpenAI(openai_api_key="YOUR-API-KEY",temperature=0),
           prompt=prompt,
           verbose=True,
           memory=ConversationBufferWindowMemory(k=2),

           )
# Predict a sentence using the chatgpt chain
output = chatgpt_chain.predict(
       human_input="What is SingleStore?"
       )
# Display the model's response
print(output)


The script initializes the LLM chain using the OpenAI API key and a preset prompt. It then takes user input and shows the resulting output.

The expected output is as shown below,

 expected output

Play with this by changing the human input text/content.

The complete execution steps in code format are available on GitHub.

LangChain emerges as an indispensable framework for data engineers and developers striving to build cutting-edge applications powered by large language models. Unlike traditional tools and platforms, LangChain offers a more robust and versatile framework tailored for complex AI applications. LangChain is not just another tool in a developer's arsenal; it's a transformative framework that redefines what is possible in the realm of AI-powered applications.

In the realm of GenAI applications and LLMs, it is highly recommended to know about vector databases. I recently wrote a complete overview of vector databases, you might like to go through that article.

Vector Database: A Beginner's Guide!

AI API Data structure Machine learning applications Data (computing) jupyter notebook Template SingleStore ChatGPT

Published at DZone with permission of Pavan Belagatti, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • LLMops: The Future of AI Model Management
  • Developing Intelligent and Relevant Software Applications Through the Utilization of AI and ML Technologies
  • Mastering Synthetic Data Generation: Applications and Best Practices
  • ChatGPT Applications: Unleashing the Potential Across Industries

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!