How to incorporate context/chat history in OpenAI ChatBot using ChatGPT and langchain in Python? – Python

by
Ali Hasan
chatgpt-api langchain llama-cpp-python openai-api py-langchain

The Problem:

Incorporate context/chat history in an OpenAI ChatBot using ChatGPT and Langchain in Python. The user wants to create a chatbot that can answer questions about a PDF document, but the bot needs to maintain conversational context. Currently, the code is not functioning as intended, and the user needs assistance in troubleshooting the issue.

The Solutions:

Solution 1: Use the latest Assistant APIs

In September 2023, OpenAI released new Assistant APIs that include message history by default. By using these updated APIs, you can incorporate context and chat history into your OpenAI chatbot without additional effort.

Q&A

Does LangChain Memory necessitate a larger context window?

Yes, longer conversations may necessitate a model with a larger context window, e.g. gpt-4-32k.

How can you append the prompt/responses to a running string?

You can simply append the prompt/responses to a running string (or array of strings) which you then append to every prompt.

Are there any new APIs that include History?

Yes, the latest Assistant APIs (released in September 2023) include History with no additional effort.

Video Explanation:

The following video, titled "Using ChatGPT with YOUR OWN Data. This is magical. (LangChain ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

(LangChain OpenAI API). 1.1M views · 7 months ago ...more. TechLead. 1.46M ... EASILY Build Your Custom Data-Trained ChatGPT AI Chatbot in 5 ...