How to connect a Chatbot that has its own data but has also access to internet for search?
Labels: name: area: agent, name: auto:question

### Issue you'd like to raise.

Hi!

How to make a Chatbot that uses its own data access to the internet to get more info (like new updated)? I've tried and searched everywhere but can't make it work. 

Here the code: 

`
                  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
                  import os
                  import openai
                  import sys
                  from dotenv import load_dotenv, find_dotenv
        
        sys.path.append('../..')
        
        _ = load_dotenv(find_dotenv())  # read local .env file
        
        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.run(query)[:max_results]
            return internet_results
        
        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_results = custom_search(query)
        
            combined_results = loaded_content + [internet_results]
        
            response = chain(
                {"question": query, "chat_history": history, "documents": combined_results})
        
            print(response["answer"])
        
            history.append(("system", query))
            history.append(("assistant", response["answer"])) 
`

This is the error message I get: "The document does not provide information on... ". So it seems it doesn't access to the internet or something else (?)

Really appreciate your suggestion or your help!

### Suggestion:

_No response_