DEV Community

Cover image for ChatGPT knowledge is out of date all this time, this is how to keep it to date
Wilbert Misingo
Wilbert Misingo

Posted on • Edited on

ChatGPT knowledge is out of date all this time, this is how to keep it to date

Introduction

I was surprised that most of people didn't knew it, even I didn't knew it from the first place. Despite using the OpenAI's ChatGPT models API for a long time now, but I later came to realize that there are some cases neither ChatGPT nor its API are usefully aware when it came to asking or talking about events that from the year 2021 up to this time, this is due to the models being trained only limited to the data up to the year 2021.

For instance when you ask ChatGPT a question about a current event, e.g. "Who is the English Premier League Champion for 2023?", and here its the answer:-

chatgpt answer

But when I went to google and asked the same question, here its the answer that was given:-

google answer

Now you can see that that's a problem indeed, now this tutorial, we will explore how to create a chatbot using the OpenAI API, even though the knowledge cutoff of the API is limited to the year 2021. We will leverage other tools like Google Search, Wikipedia, and more to ensure that our chatbot remains up-to-date with the latest information. Let's get started!

Step 1: Installing Required Packages

To begin, we need to install the necessary packages. In this example, we will be using the langchain library, which provides convenient wrappers for interacting with various language processing tools. You can install it by running the following command:

pip install langchain
Enter fullscreen mode Exit fullscreen mode

Additionally, make sure you have an OpenAI API key as we will be using the OpenAI API to power our chatbot.

Step 02: Importing Libraries and Modules

To begin, we need to import the necessary libraries and modules that will be used throughout the chatbot creation process. The code snippet below demonstrates the required imports:

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.memory import ConversationBufferMemory
from langchain.chat_models import ChatOpenAl
Enter fullscreen mode Exit fullscreen mode

Step 03: Defining and Initializing the chatbot's memory

Since we a creating a chatbot, its required to have a memory of its own by remembering and managing conversations to provide a realistic experience. By creating an instance of ConversationBufferMemory to store the conversation history.

memory = ConversationBufferMemory()
Enter fullscreen mode Exit fullscreen mode

Step 4: Loading Tools and Models

First, we need to load the required tools and models. In our case, we will load tools like Wikipedia, Google Search, Python REPL, Wolfram Alpha, and more. These tools will enable our chatbot to retrieve up-to-date information from various sources. Here's an example code snippet to load the tools:


llm = ChatOpenAl()

tools = load_tools([
    'wikipedia',
    'llm—math',
    'google—search',
    'python_repl',
    'wolfram—alpha',
    'ternubak',
    'news—api',
    'podcast—api',
    'openweathermap—api'
], llm=llm)

agent = initialize_agent(
    tools,
    llm,
    AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
    verbose=True,
    memory=memory
)

Enter fullscreen mode Exit fullscreen mode

Here, we create an instance of ChatOpenAl to power our chatbot. We then load various tools using the load_tools function, passing a list of tool names. Finally, we initialize the agent by providing the loaded tools, model, agent type, verbosity, and memory.

Step 5: Interacting with the Chatbot

Once we have initialized the chatbot agent, we can start interacting with it. We can provide a prompt or a question to the chatbot and receive a response. Here's an example of how to interact with the chatbot:

response = agent.run(""Who is the English Premier League Champion for 2023?"")
print(response)
Enter fullscreen mode Exit fullscreen mode

In this example, we pass the prompt "Who is the English Premier League Champion for 2023?" to the agent.run function, which triggers the chatbot to generate a response. The response is then printed to the User Interface.

Conclusion

In this tutorial, we have learned how to create a chatbot using the OpenAI API while keeping its knowledge up-to-date using other tools like Wikipedia, Google Search, and more. By leveraging these tools, we can ensure that our chatbot remains relevant and provides accurate information. Feel free to explore further and customize the chatbot based on your specific requirements.

Remember to refer to the official documentation of the langchain library and the respective tools for more details and customization options.

Happy building!

Do you have a project 🚀 that you want me to assist you email me🤝😊: wilbertmisingo@gmail.com
Have a question or wanna be the first to know about my posts:-
Follow ✅ me on Twitter/X 𝕏
Follow ✅ me on LinkedIn 💼

Top comments (0)