Issue: Need Help - Implement ChatOpenAI into my LangChain Research
Labels: name: area: embeddings, name: area: vector store, name: auto:question, name: area: models

### Issue you'd like to raise.

Hi there, 

I am doing a research on creating a PDF reader AI which can answer users' questions based on the PDF uploaded and the prompt user entered. I got it so far with using the OpenAI package but now want's to make it more advance by using ChatOpenAI with the LangChain Schema package (SystemMessage, HumanMessage, and AIMessage). I am kinda lost on where I should start and make the adjustments. Could you help me on that? 

Below is my code so far: 
## Imports
import streamlit as st
import os
from apikey import apikey
import pickle
from PyPDF2 import PdfReader
from streamlit_extras.add_vertical_space import add_vertical_space
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.llms import OpenAI
from langchain.chains.question_answering import load_qa_chain
from langchain.callbacks import get_openai_callback
from langchain.chat_models import ChatOpenAI
from langchain.schema import (SystemMessage, HumanMessage, AIMessage)

os.environ['OPENAI_API_KEY'] = apikey

## User Interface
# Side Bar
with st.sidebar:
    st.title('🚀 Zi-GPT Version 2.0')
    st.markdown('''
    ## About
    This app is an LLM-powered chatbot built using:
    - [Streamlit](https://streamlit.io/)
    - [LangChain](https://python.langchain.com/)
    - [OpenAI](https://platform.openai.com/docs/models) LLM model
 
    ''')
    add_vertical_space(5)
    st.write('Made with ❤️ by Zi')

# Main Page
def main():
    st.header("Zi's PDF Helper: Chat with PDF")

    # upload a PDF file
    pdf = st.file_uploader("Please upload your PDF here", type='pdf')
    # st.write(pdf)

    # read PDF
    if pdf is not None: 
        pdf_reader = PdfReader(pdf)
        
        # split document into chunks
        # also can use text split: good for PDFs that do not contains charts and visuals
        sections = []
        for page in pdf_reader.pages:
            # Split the page text by paragraphs (assuming two newlines indicate a new paragraph)
            page_sections = page.extract_text().split('\n\n')
            sections.extend(page_sections)

        chunks = sections
        # st.write(chunks)

        # embeddings
        file_name = pdf.name[:-4]
        # comvert into pickle file
        # wb: open in binary mode
        # rb: read the file
        # Note: only create new vectors for new files updated
        if os.path.exists(f"{file_name}.pkl"):
            with open(f"{file_name}.pkl", "rb") as f:
                VectorStore = pickle.load(f)
            st.write('Embeddings Loaded from the Disk')
        else:
            embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
            VectorStore = FAISS.from_texts(chunks,embedding=embeddings)
            with open(f"{file_name}.pkl", "wb") as f:
                pickle.dump(VectorStore, f)
            st.write('Embeddings Computation Completed')
        
        # Create chat history
        if pdf:
            # generate chat history
            chat_history_file = f"{pdf.name}_chat_history.pkl"

            # load history if exist
            if os.path.exists(chat_history_file):
                with open(chat_history_file, "rb") as f:
                    chat_history  = pickle.load(f)
            else:
                chat_history = []
             
        # Initialize chat_history in session_state if not present
        if 'chat_history' not in st.session_state:
            st.session_state.chat_history = []
        
        # Check if 'prompt' is in session state
        if 'last_input' not in st.session_state:
            st.session_state.last_input = ''
        
        # User Input
        current_prompt = st.session_state.get('user_input', '')

        prompt_placeholder = st.empty()
        prompt = prompt_placeholder.text_area("Ask questions about your PDF:", value=current_prompt, placeholder="Send a message", key="user_input")
        submit_button = st.button("Submit")

        if submit_button and prompt:
            # Update the last input in session state
            st.session_state.last_input = prompt

            docs = VectorStore.similarity_search(query=prompt, k=3)

            #llm = OpenAI(temperature=0.9, model_name='gpt-3.5-turbo')
            chat = ChatOpenAI(model='gpt-3.5-turbo', temperature=0.7)
            chain = load_qa_chain(llm=chat, chain_type="stuff")
            with get_openai_callback() as cb:
                response = chain.run(input_documents=docs, question=prompt)
                print(cb)
            # st.write(response)
            # st.write(docs)

            # Add to chat history
            st.session_state.chat_history.append((prompt, response))

            # Save chat history
            with open(chat_history_file, "wb") as f:
                pickle.dump(st.session_state.chat_history, f)
            
            # Clear the input after processing
            prompt_placeholder.text_area("Ask questions about your PDF:", value='', placeholder="Send a message", key="pdf_prompt") 
            
        # Display the entire chat 
        chat_content = ""
        for user_msg, bot_resp in st.session_state.chat_history:
            chat_content += f"<div style='background-color: #222222; color: white; padding: 10px;'>**You:** {user_msg}</div>"
            chat_content += f"<div style='background-color: #333333; color: white; padding: 10px;'>**Zi GPT:** {bot_resp}</div>"
        st.markdown(chat_content, unsafe_allow_html=True)

if __name__ == '__main__':
    main()

### Suggestion:

_No response_