Using LangChain with ChatGPT GPT-3.5: Step-by-Step Guide

Discover the power of LangChain integration with ChatGPT GPT-3.5 in 2023 through our comprehensive step-by-step guide.

Using LangChain with ChatGPT GPT-3.5: Step-by-Step Guide

Welcome to the future of conversational AI! In 2023, ChatGPT GPT-3.5 continues to dazzle with its language capabilities, and LangChain steps into the spotlight as the perfect companion to take your AI-powered conversations to the next level.
If you’re eager to explore the seamless integration of LangChain with ChatGPT GPT-3.5, you’re in the right place. This step-by-step tutorial is your key to unlocking a world of enhanced communication, productivity, and innovation.
In this post, we’ll walk you through the entire process, breaking it down into manageable steps, so you can harness the true potential of these cutting-edge tools. If you are a beginner, and you want to utilize the capabilities of LangChain for your own personal use, then this blog post is for you.
So, let’s embark on this exciting journey together as we delve into the realms of Langchain and ChatGPT GPT-3.5. By the end, you’ll have the confidence to wield these AI giants with knowledge and skills. At the end of this article, we will also create a small Streamlit Application using LangChain and OpenAI.

Are you ready? Let’s dive in!

Using LangChain with ChatGPT GPT-3.5
Using LangChain with ChatGPT GPT-3.5

What is LangChain?

LangChain is a framework for developing applications powered by language models. (Source: LangChain Documentation).

But here’s where it gets exciting: LangChain doesn’t just make ordinary API calls. It’s like the data-savvy sidekick of your language model, connecting it to all sorts of data sources. This means your applications can offer personalized experiences that feel more alive and responsive. In fact, LangChain can even make your language model act like a dynamic problem-solver in its environment.

What is ChatGPT or GPT-3.5?

ChatGPT, sometimes referred to as GPT-3.5, represents a groundbreaking AI Large Language Model created by OpenAI. This innovative technology leads the way in the realm of natural language processing. Essentially, this Large Language Model (LLM) is crafted to comprehend and generate text that closely resembles human language, making it exceptionally versatile for a broad spectrum of applications. Whether you’re delving into chatbots, content generation, language translation, or various other fields, ChatGPT GPT-3.5 excels.

One of its most impressive features is its capacity to engage in coherent and lifelike conversations, providing informative responses to queries. Its knack for grasping context and delivering contextually pertinent answers is a true game-changer. Whether you’re a developer aiming to enhance user interactions or a content creator seeking AI-powered support, ChatGPT GPT-3.5 serves as a potent ally, elevating your projects to new heights of sophistication and engagement. The latest version of ChatGPT in the market is GPT-4 (which is not free for public use).

How to get ChatGPT or GPT-3.5 API Key for Free?

Step 1. Goto this link https://platform.openai.com/account/api-keys

Step 2. Click on Signup (for first-time users) or Login (if already registered)

Step 3. Fill in the required details (Name, Date of Birth, Mobile Number, etc.)

Step 4. In the API Keys section, click on + Create new secret key button

Step 5. Provide any name (Optional) and click on Create secret key

Step 6. Copy the secret key and save it in a Notepad or any Sticky Notes for future use. As you will not be able to view it again.

ChatGPT Secret Key- API Key Free- Using LangChain with ChatGPT
ChatGPT Secret Key

Step-by-Step Guide to Install LangChain, OpenAI, and Streamlit in Python?

Step 1. Open Command Prompt, PyCharm, or VS Code Terminal

Step 2. In the terminal, write the following command and hit the enter key

Install OpenAi, LangChain, and Streamlit in Pythonpip install openai langchain streamlit

How to Integrate LangChain with ChatGPT?

To integrate the LangChain with OpenAI, follow the below steps.

LangChain Integration with OpenAIimport os
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

os.environ["OPENAI_API_KEY"] = "YOUR_API_KEY"

template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate(template=template, input_variables=["question"])

llm = OpenAI()

llm_chain = LLMChain(prompt=prompt, llm=llm,verbose=True)

question = "What is Generative AI?" # ASK_YOUR_QUESTION

response = llm_chain.run(question)

print(response);LangChain Integration with OpenAI Response> Entering new LLMChain chain...
Prompt after formatting:
Question: What is Generative AI?

Answer: Let's think step by step.

> Finished chain.
Generative AI is a type of artificial intelligence (AI) that focuses on creating new data from existing datasets. It works by learning patterns from the data and generating new data with similar properties. Generative AI can be used to create new images, music, text, and more. In some cases, it can even generate completely new ideas or solutions to problems. Its applications range from creating more efficient and effective machine learning algorithms to generating realistic digital artwork and music.

Project- Streamlit Text Summarizer: Create with LangChain and ChatGPT or GPT-3.5

Install Necessary Librariespip install streamlit langchain openai tiktokenBuilding the appimport streamlit as st
from langchain import OpenAI
from langchain.docstore.document import Document
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains.summarize import load_summarize_chain

# Function to generate response
def generate_response(txt):
# Instantiate the LLM model
llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
# Split text
text_splitter = CharacterTextSplitter()
texts = text_splitter.split_text(txt)
# Create multiple documents
docs = [Document(page_content=t) for t in texts]
# Text summarization
chain = load_summarize_chain(llm, chain_type='map_reduce')
return chain.run(docs)

# Page title
st.set_page_config(page_title='Text Summarization App by Pykit.Org')
st.title('Text Summarization App by Pykit.Org')

# Text input
txt_input = st.text_area('Enter your text', '', height=200)

# Form to accept user's text input for summarization
result = []
with st.form('summarize_form', clear_on_submit=True):
openai_api_key = st.text_input('OpenAI API Key', type = 'password', disabled=not txt_input)
submitted = st.form_submit_button('Submit')
if submitted and openai_api_key.startswith('sk-'):
with st.spinner('Calculating...'):
response = generate_response(txt_input)
result.append(response)
del openai_api_key

if len(result):
st.info(response)

Check this out: https://text-summarization-app-pykit.streamlit.app/

Text Summarization Application Using LangChain with ChatGPT and Streamlit
Text Summarization Application Using LangChain with ChatGPT and Streamlit