How to make a Chatbot responde based on custom data and from the internet?
Labels: name: area: memory, name: area: embeddings, name: area: doc loader, name: area: vector store, name: auto:question

### Issue you'd like to raise.

I've updated the code but very strange it doesn't find a good response. When I print(response["answer"]) I get that there is no text to give to the query I put. Even if it gets information from the internet and the Document on the list seems good structured. Here the code:

`

        from googlesearch import search
        from langchain.chains import ConversationalRetrievalChain
        from langchain.memory import ConversationBufferMemory
        from langchain.vectorstores import DocArrayInMemorySearch
        from langchain.embeddings.openai import OpenAIEmbeddings
        from langchain.document_loaders import (
            UnstructuredWordDocumentLoader,
            TextLoader,
            UnstructuredPowerPointLoader,
        )
        from langchain.tools import Tool
        from langchain.utilities import GoogleSearchAPIWrapper
        from langchain.chat_models import ChatOpenAI
        from langchain.docstore.document import Document
        import os
        import openai
        import sys
        from dotenv import load_dotenv, find_dotenv
        
        sys.path.append('../..')
        
        _ = load_dotenv(find_dotenv())  
        
        google_api_key = os.environ.get("GOOGLE_API_KEY")
        google_cse_id = os.environ.get("GOOGLE_CSE_ID")
        
        openai.api_key = os.environ['OPENAI_API_KEY']
        
        os.environ["LANGCHAIN_TRACING_V2"] = "true"
        os.environ["LANGCHAIN_ENDPOINT"] = "https://api.langchain.plus"
        os.environ["LANGCHAIN_API_KEY"] = os.environ['LANGCHAIN_API_KEY']
        
        os.environ["GOOGLE_API_KEY"] = google_api_key
        os.environ["GOOGLE_CSE_ID"] = google_cse_id
        
        folder_path_docx = "DB\\DB VARIADO\\DOCS"
        folder_path_txt = "DB\\BLOG-POSTS"
        folder_path_pptx_1 = "DB\\PPT JUNIO"
        folder_path_pptx_2 = "DB\\DB VARIADO\\PPTX"
        
        loaded_content = []
        
        for file in os.listdir(folder_path_docx):
            if file.endswith(".docx"):
                file_path = os.path.join(folder_path_docx, file)
                loader = UnstructuredWordDocumentLoader(file_path)
                docx = loader.load()
                loaded_content.extend(docx)
        
        for file in os.listdir(folder_path_txt):
            if file.endswith(".txt"):
                file_path = os.path.join(folder_path_txt, file)
                loader = TextLoader(file_path, encoding='utf-8')
                text = loader.load()
                loaded_content.extend(text)
        
        for file in os.listdir(folder_path_pptx_1):
            if file.endswith(".pptx"):
                file_path = os.path.join(folder_path_pptx_1, file)
                loader = UnstructuredPowerPointLoader(file_path)
                slides_1 = loader.load()
                loaded_content.extend(slides_1)
        
        for file in os.listdir(folder_path_pptx_2):
            if file.endswith(".pptx"):
                file_path = os.path.join(folder_path_pptx_2, file)
                loader = UnstructuredPowerPointLoader(file_path)
                slides_2 = loader.load()
                loaded_content.extend(slides_2)
        
        embedding = OpenAIEmbeddings()
        
        embeddings_content = []
        for one_loaded_content in loaded_content:
            embedding_content = embedding.embed_query(one_loaded_content.page_content)
            embeddings_content.append(embedding_content)
        
        db = DocArrayInMemorySearch.from_documents(loaded_content, embedding)
        retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 3})
        
        search = GoogleSearchAPIWrapper()
        
        
        def custom_search(query):
            max_results = 3
        
            internet_results = search.results(query, max_results)
            internet_documents = [Document(page_content=result["snippet"], metadata={
                                           "source": result["link"]}) for result in internet_results
                                  ]
            return internet_documents
        
        
        chain = ConversationalRetrievalChain.from_llm(
            llm=ChatOpenAI(model_name="gpt-4", temperature=0),
            chain_type="map_reduce",
            retriever=retriever,
            return_source_documents=True,
            return_generated_question=True,
        )
        
        history = []
        
        
        
        while True:
            query = input("Hola, soy Chatbot. ¿Qué te gustaría saber? ")
        
            internet_documents = custom_search(query)
        
            small = loaded_content[:3]
        
            combined_results = small + internet_documents
            print(combined_results)
        
            response = chain(
                {"question": query, "chat_history": history, "documents": combined_results})
        
            print(response["answer"])
        
            history.append(("system", query))
            history.append(("assistant", response["answer"]))
`

Can anyone help me to make it work? Appreciate!

### Suggestion:

I would like that the Chatbot gives a response not just from the custom data also from what it gets from the internet. But of what I've done so far it doesn't work.