Exploring Conversational Capabilities with GPT-4 and ChatGPT

Introduction

In this lesson, we will explore the benefits of using GPT-4 and ChatGPT, focusing on their ability to maintain context in conversations. We will demonstrate how these advanced language models can remember conversation history and respond accordingly, making them ideal for chat applications. Additionally, we will briefly discuss the improvements in GPT-4, such as longer context length and better generalization. By the end of this lesson, you should be able to understand how GPT-4 and ChatGPT can be used for context-aware chat applications via the API, as opposed to just using the OpenAI ChatGPT webpage.

As mentioned before, OpenAI's GPT-4 represents a significant advancement in the field of large language models. Among its many improvements are enhanced creativity, the ability to process visual input, and an extended contextual understanding. In the realm of conversational AI, both GPT-4 and ChatGPT use the Transformers architecture at their core and are fine-tuned to hold natural dialogue with a user. While the free version of ChatGPT relies on GPT-3, the premium offering, ChatGPT Plus, gives access to the more advanced GPT-4 model.

The benefits of employing ChatGPT and GPT-4 in chat format are numerous. For instance, GPT-4's short-term memory capacity of 64,000 words greatly surpasses GPT-3.5's 8,000-word limit, enabling it to maintain context more effectively in prolonged conversations. Furthermore, GPT-4 is highly multilingual, accurately handling up to 26 languages, and boasts improved steering capabilities, allowing users to tailor responses with a custom "personality."

The new model is considerably safer to use, boasting a 40% increase in factual responses and an 82% reduction in disallowed content responses. It can also interpret images as a foundation for interaction. While this functionality has not yet been incorporated into ChatGPT, its potential to revolutionize context-aware chat applications is immense.

Setting up the API

To use GPT-4 or ChatGPT in your application, you must obtain API keys from OpenAI. You'll need to sign up for an account and submit a request to access the latest model. At the time of writing this lesson, there is a waitlist to get your hands on GPT-4. Then, set the OPENAI_API_KEY key in your environment variables so the LangChain library can access them.

The following example demonstrates how to create a chatbot using the GPT-4 model from OpenAI. After importing the necessary classes, we declare a set of messages. It starts by setting the context for the model (SystemMessage) that it is an assistant, followed by the user’s query (HumanMessage), and finishes by defining a sample response from the AI model (AIMessage). Remember to install the required packages with the following command:

pip install -qU langchain-openai
pip install -qU langchain-community
from langchain.chat_models import ChatOpenAI
from langchain.schema import (
    SystemMessage,
    HumanMessage,
    AIMessage
)

messages = [
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(content="What is the capital of France?"),
    AIMessage(content="The capital of France is Paris.")
]

When the user posed the question about the capital of France, the model confidently answered with "Paris.” Next up, we test if the model can leverage these discussions as a reference to delve further into details about the city without us explicitly mentioning the name (referring to Paris). The code below adds a new message which requires the model to understand and find the “city you just mentioned” reference from previous conversations.

prompt = HumanMessage(
    content="I'd like to know more about the city you just mentioned."
)
# add to messages
messages.append(prompt)

llm = ChatOpenAI(model_name="gpt-4")

response = llm(messages)
AIMessage(content='Paris, the capital of France, is one of the most famous and visited cities in the world. It is located in the north-central part of the country, along the Seine River. With a population of over 2 million people, it is a bustling metropolis known for its art, culture, history, and gastronomy.\n\nParis is often referred to as the "City of Light" (La Ville Lumière) due to its role in the Age of Enlightenment and its early adoption of street lighting. The city is home to numerous iconic landmarks, including the Eiffel Tower, the Louvre Museum, Notre-Dame Cathedral, and the Arc de Triomphe. \n\nParis is also known for its charming neighborhoods, such as Montmartre, Le Marais, and Saint-Germain-des-Prés, each with its own unique character and attractions. The city has an extensive public transportation system, including buses, trams, and the Métro, making it easy to explore its various districts.\n\nAs a global center for art, fashion, and culture, Paris hosts numerous events and exhibitions throughout the year, such as Paris Fashion Week and the Paris Air Show. The city is also renowned for its culinary scene, featuring a wide array of restaurants, cafés, patisseries, and food markets that offer both traditional French cuisine and international flavors.\n\nIn addition to its historical and cultural attractions, Paris is an important international business hub and the headquarters for many multinational corporations and organizations, including UNESCO, the OECD, and the International Chamber of Commerce.', additional_kwargs={}, example=False)

As you can see, the model successfully extracted the information from previous conversations and explained more details about Paris. It shows that the chat models are capable of referring to the chat history and understanding the context.

To recap, the ChatOpenAI class is used to create a chat-based application that can handle user inputs and generate responses using the GPT-4 language model. The conversation is initiated with a series of messages, including system, human, and AI messages. The SystemMessage provides context for the conversation, while HumanMessage and AIMessage represent the user and the AI's messages, respectively.

The LangChain’s Chat API offers several advantages:

  • Context preservation: By maintaining a list of messages in the conversation, the API ensures that the context is preserved throughout the interaction. This allows the GPT-4 model to generate relevant and coherent responses based on the provided information.
  • Memory: The class’s message history acts as a short-term memory for the chatbot, allowing it to refer back to previous messages and provide more accurate and contextual responses.
  • Modularity: The combination of MessageTemplate and ChatOpenAI classes offers a modular approach to designing conversation applications. This makes it easier to develop, maintain, and extend the functionality of the chatbot.
  • Improved performance: GPT-4, as an advanced language model, is more adept at understanding complex prompts and generating better responses than its predecessors. It can handle tasks that require deeper reasoning and context awareness, which leads to a more engaging and useful conversation experience.
  • Flexibility: The Chat API can be adapted to different domains and tasks, making it a versatile solution for various chatbot applications. In this example, the chatbot specializes in French culture but could be easily modified to focus on other subjects or industries. Moreover, as newer and more powerful language models become available, the API can be updated to utilize those models, allowing for continuous improvements in chatbot capabilities.

Conclusion

In this lesson, we learned that GPT-4 boasts remarkable advancements in context length and generalization, paving the way for more sophisticated language processing. By accommodating a more extensive context, GPT-4 can generate lengthier text pieces, analyze more massive documents, and engage in longer conversations without compromising the context's integrity.

ChatGPT and GPT-4 models are tailor-made for conversational interfaces, which require input to be formatted in a specific chat-like transcript format. This format empowers the models to retain conversation history and furnish contextually relevant responses. This attribute is especially advantageous for multi-turn conversations and can also prove useful in non-chat scenarios.

These potent models have diverse applications, including customer support chatbots that manage intricate inquiries and dispense pertinent responses based on previous interactions. They can also function as virtual personal assistants that preserve context across various tasks and requests. They also serve as natural language interfaces for databases and search engines, enabling them to better understand user queries and provide more accurate results.

In the next lesson, you’ll do the first project of the course, that is a news summarizer leveraging LangChain.

You can find the code of this lesson in this online Notebook.