Building a RAG agent for a large scale project

I have recently created a chatbot using langchain, openAI (for embeddings + LLM) and Pinecone as my vectorDB (serverless). I have not used any advance RAG techniques as of now simple because I don’t know of any.

The problem is that response takes a lot of time. I have used chromaDB in the past and its not that slow, if I am not wrong, Pinecone (serverless) is supposed to be faster. I am new to all this genAI stuff, so I am not even sure if the actual chain is correct or not, but its working.

The idea is that this chatbot will help students in some queries. For now, we are planning to cover the Universities of the US (which are a lot) and right now, I only have the data of like 3 Universities and its this slow. I am afraid what will happen in the future. Is Pinecone even the right option here?
What should I do do decrease the time?

Here’s how the RAG chain works. I am also asking GPT to reformulate the question for better results:

def initialize_chatbot(index_name):
    # Define the Pinecone API key and index name
    pinecone_api_key = os.getenv("PINECONE_API_KEY")
    openai_api_key = os.getenv("OPENAI_API_KEY")

    # Initialize Pinecone
    pinecone = Pinecone(api_key=pinecone_api_key)
    index = pinecone.Index(index_name)
    # Define the embedding model
    embeddings = OpenAIEmbeddings(model="text-embedding-3-small")

    # Load the Pinecone vector store
    vector_store = PineconeVectorStore(index=index, embedding=embeddings)

    # Create a retriever for querying the vector store
    retriever = vector_store.as_retriever(
        search_type="similarity",
        search_kwargs={"k": 2},
    )

    # Create a ChatOpenAI model using gpt-4o
    llm = ChatOpenAI(api_key=openai_api_key, model="gpt-4o")

    # Contextualize question prompt
    contextualize_q_system_prompt = (
        "Given a chat history and the latest user question "
        "which might reference context in the chat history, "
        "formulate a standalone question which can be understood "
        "without the chat history. Do NOT answer the question, just "
        "reformulate it if needed and otherwise return it as is."
    )

    # Create a prompt template for contextualizing questions
    contextualize_q_prompt = ChatPromptTemplate.from_messages(
        [
            ("system", contextualize_q_system_prompt),
            MessagesPlaceholder("chat_history"),
            ("human", "{input}"),
        ]
    )

    # Create a history-aware retriever
    history_aware_retriever = create_history_aware_retriever(
        llm, retriever, contextualize_q_prompt
    )

    # Answer question prompt
    qa_system_prompt = """
        You are a helpful assistant. Use the following pieces of retrieved context to answer the question. 
        Don't use information other than the retrieved context. If the answer is not in the retrieved documents, you are also allowed to ask a follow up question
        or if the query doesn't make any sense then just say that "I did not find any relevant information to your query.

        Tips:

        1- Don't use the words like "mentioned in the provided context" in your answer.
        2- If the answer is not in the retrieved documents, you are allowed to ask a follow-up question. (Only if the question is somewhat related to Universities/Students)
        3- If the question is not related Universities/Students, you can say that "I did not find any relevant information to your query." and don't ask any follow up questions for those queries (which are way off).
        4- You are a helpful assistant that helps students with their questions in the most respectful way possible.
        5- You are someone who was a lot of knowledge about universities and students, you want to help students to not have any confusions regarding their careers.

  
        {context}

        nn===Answer===n[Provide the normal answer here.]n"""

    # Create a prompt template for answering questions
    qa_prompt = ChatPromptTemplate.from_messages(
        [
            ("system", qa_system_prompt),
            MessagesPlaceholder("chat_history"),
            ("human", "{input}"),
        ]
    )

    # Create a chain to combine documents for question answering
    question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)

    # Create a retrieval chain that combines the history-aware retriever and the question answering chain
    rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)

    return rag_chain, retriever

def process_query(query, chat_history, rag_chain):
    result = rag_chain.invoke({"input": query, "chat_history": chat_history})
    return result["answer"]
# Function to simulate a continual chat
def continual_chat(index_name, query, chat_history):
    rag_chain, retriever = initialize_chatbot(index_name)
    while True:
        # Process the user's query through the retrieval chain
        result = process_query(query, chat_history, rag_chain)
        # Display the AI's response
        print(f"AI: {result}")

        # Update the chat history
        chat_history.append(HumanMessage(content=query))
        chat_history.append(SystemMessage(content=result))


        return result, chat_history

Should I use technologies like Groq or maybe something else that is new? I also have to retrieve data from our postgreSQL for each query because we have a lot of data in our database too. So keeping all that in mind, what approach should I use? I only have a week to develop a good chatbot.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật