Professional Documents
Culture Documents
How To Replicate ChatGPT With Langchain and GPT-3 Ahmad Rosid
How To Replicate ChatGPT With Langchain and GPT-3 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.
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.
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:
2. Chains
3. Agents
4. Memory
Install
To use langchain package you can install it from pypi.
📋 Copy code
To get latest update from langchain, you can use this installation method.
📋 Copy code
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.
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
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,
)
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.
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 *
Built with Next.js, Tailwind and Vercel ©2022 All rights reserved.