Issue: Embedding with Pinecone
Labels: name: area: embeddings, name: auto:improvement, name: integration: pinecone

### Issue you'd like to raise.

Hi there, 

I am learning how to use Pinecone properly with LangChain and OpenAI Embedding. I built an application which can allow user upload PDFs and ask questions about the PDFs. In the application I used Pinecone as the vector database and store embeddings in Pinecone. However, I want to make change to my code so that whenever an user upload a PDF, the application can check if the PDF already been store as embeddings in Pinecone, and if yes, use the old embeddings; If no, then upload new embeddings. 

Here is my code:
## Imports
import streamlit as st
import os
from apikey import apikey
import pickle
from PyPDF2 import PdfReader

# Streamlit - user interface
from streamlit_extras.add_vertical_space import add_vertical_space

# Langchain
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.openai import ChatOpenAI
from langchain.schema import (SystemMessage, HumanMessage, AIMessage)
from langchain.schema import Document
from langchain.document_loaders import UnstructuredPDFLoader

# Pinecone
from langchain.vectorstores import Pinecone
import pinecone
from apikey import pinecone_api_key
import uuid


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)
        # data = pdf_reader.load()
        
        # 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 = [Document(page_content=section) for section in sections]
        # st.write(chunks)
        
        # text_splitter = RecursiveCharacterTextSplitter(
            # chunk_size = 500,
            # chunk_overlap = 20
        # )
        
        # chunks = text_splitter.split_documents(data)

        ## embeddings
        # Set up embeddings
        embeddings = OpenAIEmbeddings( model="text-embedding-ada-002")

        try:
            # Set up Pinecone
            pinecone.init(api_key=pinecone_api_key, environment='gcp-starter')
            index_name = 'langchainresearch'
            if index_name not in pinecone.list_indexes():
                pinecone.create_index(index_name, dimension=1536, metric="cosine")  # Adjust the dimension as per your embeddings
            index = pinecone.Index(index_name)
    
            docsearch = Pinecone.from_documents(chunks, embeddings, index_name = index_name)

        except Exception as e:
            print(f"An error occurred: {e}")
        
        # Create or Load 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 docsearch is not None and submit_button and prompt:
            # Update the last input in session state
            st.session_state.last_input = prompt

            docs = docsearch.similarity_search(query=prompt, k=3)

            #llm = OpenAI(temperature=0.9, model_name='gpt-3.5-turbo')
            chat = ChatOpenAI(model='gpt-4', temperature=0.7, max_tokens=3000)

            message = [
                SystemMessage(content="You are a helpful assistant"),
                HumanMessage(content=prompt)
            ]
            chain = load_qa_chain(llm=chat, chain_type="stuff")
            with get_openai_callback() as cb:
                response = chain.run(input_documents=docs, question=message)
                print(cb)
            # st.write(response)
            # st.write(docs)

            # Process the response using AIMessage schema
            # ai_message = AIMessage(content="AI message content")
            # ai_message.content = response.generations[0].message.content

            # 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()

Give me some recommedations on what should I do or change. 

### Suggestion:

_No response_