Make your own ChatGPT

Feb 11, 20235 min read

Recommendation: Use NSDT Scene Designer to quickly build 3D scenes.

ChatGPT is known to be capable of impressive feats at the moment. It is likely that many people have ideas for using this technique in their own projects. However, it should be noted that ChatGPT currently does not have an official API. Using unofficial APIs can cause difficulties.

Currently, access token and cloudflare token need to be obtained manually to use the API. Additionally, these tokens must be changed manually every two hours.

1、ChatGPT

ChatGPT used the GPT-3 model in its design and developed a new model based on it. Therefore, the output of the new model tends to be similar to GPT-3. As of this writing, the text-davinci-002-render model is used in ChatGPT for the new model, but it is not currently available to the public.

While ChatGPT may not be groundbreaking, it offers a new interface that leverages existing technology. By utilizing powerful hints and efficient memory windows. Therefore, instead of hacking ChatGPT's unofficial API, we can replicate its functionality using the LLM Chain approach.

2、LangChain

Langchain is a new python package that provides a standard interface for LLM chains, extensive integrations with other tools, and end-to-end chains for common applications.

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

  • LLM and Tips

  • chain

  • acting

  • memory

Learn more about langchain in the official documentation here .

3. Installation

To use the langchain package, you can install it from pypi.

pip install langchain

To get the latest updates from langchain you can use this installation method.

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

Read more about installation options here .

4. Example project

There are many things you can do with ChatGPT, one of the fun things is building Q&A for student assignments. So this time we're going to create an AI version of Brainly.

Here's what we'll get from ChatGPT.

Here are the langchain tips:

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 the text of question, it is your job to write a answer that question with example.
Question: {text}
Answer:
"""
prompt_template = PromptTemplate(input_variables=["text"], template=template)
answer_chain = LLMChain(llm=llm, prompt=prompt_template)
answer = answer_chain.run("What is the formula for Gravitational Potential Energy (GPE)?")
print(answer)

Here are the results we get from GPT-3 using langchain:

The formula for Gravitational Potential Energy (GPE) is GPE = mgh, where m is 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.

5. Chatbots

If you need to create chatbots like AI, you can use langchain's memory. Here's an example of how to do it.

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 the text of question, it is your job to write a answer that question with example.
{chat_history}
Human: {question}
AI:
"""
prompt_template = PromptTemplate(input_variables=["chat_history","question"], 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)

The result will be something like this:

$ python3 memory.py

> Entering new LLMChain chain...
Prompt after formatting:
You are a teacher in physics for High School student. Given the text of question, 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 question, 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 is 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 meters, then the gravitational potential energy of the object is GPE = 10 kg x 9.8 m/s2 x 5 m = 490 Joules.
Human: What is Joules?
AI:

> Finished LLMChain chain.
Joules (J) is the SI unit of energy. It is defined as the amount of energy required to move an object of one kilogram at a speed of one meter per second. It is also equal to the work done when a force of one Newton is applied to an object and moved one meter in the direction of the force.

6. Conclusion

ChatGPT is a chatbot based on GPT-3 and currently has no official API. Using LangChain, developers can replicate the functionality of ChatGPT, such as creating chatbots or question answering systems, without using unofficial APIs.

LangChain provides standard interfaces, numerous 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.

Guess you like

Origin blog.csdn.net/tianqiquan/article/details/129018505