Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Let's learn Blog Subscribe

with Ahmad Rosid

How to replicate
ChatGPT with
Langchain and GPT-3?
It is well-known that ChatGPT is currently capable of impressive feats. It is
likely that many individuals have ideas for utilizing this technology in their
own projects. However, it should be noted that ChatGPT does not currently
have an official API. Using an unofficial API may result in difficulties.

At present, it is necessary to manually obtain an access token and


cloudflare token in order to use the API. Furthermore, these tokens must
be manually changed every two hours.

ChatGPT
ChatGPT utilizes GPT-3 models in its design, and a new model has been
developed based on this. Therefore, the output of the new model is often
similar to that of GPT-3. At the time of writing, the `text-davinci-002-

render` model was used in the ChatGPT of the new model, but it is not
currently accessible to the public.

While ChatGPT may not be groundbreaking, it offers a new interface for


utilizing existing technologies. By utilizing a strong prompt and an efficient
memory window. So, instead of hacking the unofficial API of ChatGPT, we
can replicate its capabilities using the LLM Chain method.

LangChain
Langchain is new python package it provides a standard interface for
chains, lots of integrations with other tools, and end-to-end chains for
common applications.

LangChain is designed to assist in four main areas, which are listed here in
increasing order of complexity:

1. LLM and Prompts

2. Chains

3. Agents

4. Memory

Learn more about langchain on the official documentation here.

Install
To use langchain package you can install it from pypi.
📋 Copy code

pip install langchain

To get latest update from langchain, you can use this installation method.
📋 Copy code

pip install "git+https://github.com/hwchase17/langchain.git"

More installation option read here.

Example Project
There's a lot you can do with ChatGPT one of the interesting one is to build
Q&A for student homework. So this time we will create AI version of
Brainly.

This is what we'll get from ChatGPT.


Here's the prompt with langchain.
📋 Copy code

from langchain.llms import OpenAI


from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.chains import SimpleSequentialChain

llm = OpenAI(temperature=.7)
template = """You are a teacher in physics for High School student. Given th
e text of question, it is your job to write a answer that question with exam
ple.
Question: {text}
Answer:
"""
prompt_template = PromptTemplate(input_variables=["text"], template=templat
e)
answer_chain = LLMChain(llm=llm, prompt=prompt_template)
answer = answer_chain.run("What is the formula for Gravitational Potential E
nergy (GPE)?")
print(answer)

And here's the result we will get from GPT-3 with langchain.
📋 Copy code

The formula for Gravitational Potential Energy (GPE) is GPE = mgh, where m i
s the mass of an object, g is the acceleration due to gravity, and h is the
height of the object. For example, if an object with a mass of 10 kg is at a
height of 5 meters, then the GPE would be GPE = 10 x 9.8 x 5 = 490 Joules.

Chatbot
If you need to create chatbot like AI you can use memory with langchain.
Here's example how to do it.
📋 Copy code

from langchain.chains.conversation.memory import ConversationBufferMemory


from langchain import OpenAI, LLMChain, PromptTemplate

template = """You are a teacher in physics for High School student. Given th
e text of question, it is your job to write a answer that question with exam
ple.
{chat_history}
Human: {question}
AI:
"""
prompt_template = PromptTemplate(input_variables=["chat_history","questio
n"], template=template)
memory = ConversationBufferMemory(memory_key="chat_history")

llm_chain = LLMChain(
llm=OpenAI(),
prompt=prompt_template,
verbose=True,
memory=memory,
)

llm_chain.predict(question="What is the formula for Gravitational Potential


Energy (GPE)?")

result = llm_chain.predict(question="What is Joules?")


print(result)

And the result would be like this.


📋 Copy code
$ python3 memory.py

> Entering new LLMChain chain...


Prompt after formatting:
You are a teacher in physics for High School student. Given the text of ques
tion, it is your job to write a answer that question with example.

Human: What is the formula for Gravitational Potential Energy (GPE)?


AI:

> Finished LLMChain chain.

> Entering new LLMChain chain...


Prompt after formatting:
You are a teacher in physics for High School student. Given the text of ques
tion, it is your job to write a answer that question with example.

Human: What is the formula for Gravitational Potential Energy (GPE)?


AI:
The formula for Gravitational Potential Energy (GPE) is GPE = mgh, where m i
s the mass of the object, g is the acceleration due to gravity, and h is the
height of the object.

For example, if an object has a mass of 10 kg and is at a height of 5 meter


s, then the gravitational potential energy of the object is GPE = 10 kg x 9.
8 m/s2 x 5 m = 490 Joules.

Conclusion
ChatGPT is a GPT-3 based chatbot and currently does not have an official
API. Using LangChain, developers can replicate the capabilities of
ChatGPT, such as creating chatbots or Q&A systems, without having to use
the unofficial API.

LangChain provides a standard interface, lots of integrations, and end-to-


end chains for common applications. It can be installed from pypi and more
information can be found in the official documentation.

If you have any question let me know, you can contact me on twitter.
Subscribe * indicates required
Stay up-to-date on
the latest web Email Address *

development trends your@mail.com

Join me as we explore the exciting Subscribe


world of web development. In each
I respect your privacy. Unsubscribe at any
issue, I will bring you the latest news, time.
tutorials, and resources to help you
become a top developer.

Github LinkedIn Twitter Cheatsheet Portfolio Wiki

Built with Next.js, Tailwind and Vercel ©2022 All rights reserved.

You might also like