ChatGPT Chatbot For Google Docs
Published: 2023-06-23 16:48:28

<div class="entry-content">
			<div class="row">
				<div class="col-lg-8 col-xl-8 blog-content-col">
					
<p>ChatGPT has certainly carved a niche for itself in the realm of AI, but its closed-source nature and inability to access external datasets can sometimes limit its use, especially when it comes to personal documents. But what if you could leverage the capabilities of Python, Google Docs, ChatGPT, and LangChain to create your own advanced “Google Docs ChatGPT chatbot”?</p>
<p>Imagine having a Google Docs ChatGPT chatbot that can interact directly with files stored in your Google Drive. </p>



<p>So, whether you’re a <a href="https://blog.nextideatech.com/nearshore-software-development-in-argentina-the-complete-guide-for-2023/" class="ek-link">developer</a>, a tech enthusiast, or someone who just wants to streamline their work on Google Docs, stick around. This guide on creating your own “Google Docs ChatGPT chatbot” might just be the solution you’ve been seeking!</p>



<p>This article will cover:</p>



<ul id="langchain-document-loaders"><li><a href="#what-is-langchain" class="ek-link">What is LangChain?</a></li><li><a href="#openai-gpt-limiation" class="ek-link">Openai / ChatGPT Limitation</a></li><li><a href="#why-google-docs-chatgpt-chatbot" class="ek-link">Why Use a Google Docs ChatGPT Chatbot?</a></li><li><a href="#what-is-langchain-document-loaders" class="ek-link">LangChain Document Loaders</a></li><li><a href="#setting-google-api-credentials" class="ek-link">Setting up Google API Credentials</a></li><li><a href="#step-by-step-google-docs-chatgpt-python" class="ek-link">Step by step: How to build Google Docs ChatGPT Chatbot</a></li></ul>



<h2 id="what-is-langchain">What is LangChain?</h2>



<p><a href="https://github.com/hwchase17/langchain" target="_blank" aria-label="LangChain  (opens in a new tab)" rel="noreferrer noopener" class="ek-link">LangChain </a>is an innovative framework that is designed around the use of Large Language Models (LLMs). It was created by Harrison Chase and first introduced in late 2022. This powerful tool has grown popular due to the rise of generative AI and advancements in LLMs, like Google’s LaMDA chatbot, BLOOM, and OpenAI’s GPT-3.5 models.</p>



<p>What makes LangChain unique is its ability to ‘chain’ together various components, thus enabling advanced applications of LLMs. These applications range from chatbots and generative question-answering (GQA) to summarization tasks and more.</p>



<p>LangChain incorporates multiple key components in its structure. It uses ‘<a href="https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/" target="_blank" aria-label="Prompt templates (opens in a new tab)" rel="noreferrer noopener" class="ek-link">Prompt templates</a>‘ which are predefined templates that can be adjusted to suit various types of prompts. These include prompts for chatbot-like interactions, question-answering in the style of ‘Explain it Like I’m Five’, and more.</p>



<p>Another critical part of LangChain is the LLMs themselves, including models like GPT-3 and BLOOM. These large language models form the core of LangChain’s functionality.</p>



<p>Then we have ‘Agents’, which use LLMs to determine the appropriate actions to take. They leverage tools like web search or calculators and wrap them into a logical operation loop.</p>



<h2 id="openai-gpt-limiation">Openai / ChatGPT Limitation</h2>



<p>OpenAI’s API and ChatGPT have a key drawback: they struggle with large custom datasets. The <a href="https://platform.openai.com/docs/api-reference/chat/create#chat/create-max_tokens" target="_blank" aria-label="max_token  (opens in a new tab)" rel="noreferrer noopener" class="ek-link">max_token </a>limit means you can’t put a lot of data into the context window. Plus, they’re not set up to handle individual document collections well. But LangChain helps to solve this. It uses <a href="https://js.langchain.com/docs/modules/indexes/document_loaders/" target="_blank" aria-label="Document Loaders (opens in a new tab)" rel="noreferrer noopener" class="ek-link">Document Loaders</a> to import and adapt data, so it works well with ChatGPT. This allows <a href="https://www.nextideatech.com/nearshore/software-development-services" class="ek-link">developers</a> to make better use of their unique datasets with large language models.</p>



<h2 id="why-google-docs-chatgpt-chatbot">Why Use a Google Docs ChatGPT Chatbot?</h2>



<p>Why should you build a “Google Docs ChatGPT Chatbot”? The answer is simple – it provides you with the ability to converse directly with your Google Docs documents. By harnessing the capabilities of ChatGPT, langchain, and Python, this chatbot transforms your documents into an interactive platform. Imagine needing information about a company process or having questions about onboarding – with this chatbot, the answers are just a conversation away. It’s not just about accessing information, it’s about streamlining communication and making knowledge sharing more efficient.</p>



<h2 id="what-is-langchain-document-loaders">LangChain Document Loaders</h2>



<p>LangChain’s <a aria-label="document loaders (opens in a new tab)" href="https://python.langchain.com/docs/modules/data_connection/document_loaders/" target="_blank" rel="noreferrer noopener" class="ek-link">document loaders</a> provide a robust solution for importing data from various sources and transforming it into Document objects. A Document object essentially contains a piece of text along with any associated metadata. The power of LangChain lies in its variety of document loaders. These include the <a href="https://python.langchain.com/docs/modules/data_connection/document_loaders/how_to/csv">CSV | 🦜️🔗 Langchain</a> for working with CSV files, PDFLoader for handling PDF documents, a File Directory loader for importing data from a file system, and a JSON loader for processing JSON data.</p>



<pre class="wp-block-code language-python" tabindex="0"><code lang="python" class="language-python">loader <span class="token operator">=</span> GoogleDriveLoader<span class="token punctuation">(</span>
        credentials_path<span class="token operator">=</span>CLIENT_SECRET_FILE<span class="token punctuation">,</span>
        token_path<span class="token operator">=</span>TOKEN_FILE<span class="token punctuation">,</span>
        folder_id<span class="token operator">=</span>GOOGLE_DRIVER_FOLDER_ID<span class="token punctuation">,</span>
        recursive<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span>
        file_types<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">"sheet"</span><span class="token punctuation">,</span> <span class="token string">"document"</span><span class="token punctuation">,</span> <span class="token string">"pdf"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">)</span>
<span class="token keyword">return</span> loader<span class="token punctuation">.</span>load<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>



<p>What’s truly useful about these document loaders is their ‘load’ method. It extracts data from a configured source and turns it into Document objects. For more efficient memory utilization, LangChain also supports ‘lazy load’. This feature progressively loads data into memory as and when required, rather than loading it all at once. This functionality is not just limited to standard text files. For instance, LangChain can load the text contents of a web page or even fetch and process the <a href="https://github.com/hwchase17/langchain/blob/master/langchain/document_loaders/youtube.py" target="_blank" aria-label="transcript of a YouTube video (opens in a new tab)" rel="noreferrer noopener" class="ek-link">transcript of a YouTube video</a>, expanding the scope of data sources you can tap into.</p>



<h2 id="setting-google-api-credentials">Setting up Google API Credentials</h2>



<p>In order to build our Google Docs ChatGPT chatbot, we will use GoogleDriveLoader document loader. </p>



<p>The GoogleDriveLoader is a document loader that allows you to ingest data from Google Docs into your LangChain project. It can load from a list of Google Docs document IDs or a folder ID. </p>



<p>To use the GoogleDriveLoader, you need to have a <a href="https://developers.google.com/workspace/guides/create-credentials" target="_blank" rel="noreferrer noopener">Google Cloud project</a> and enable the Google Drive API. You also need to authorize credentials for a desktop app and install the necessary Python packages. Once you have set up the loader, you can use it to fetch data from Google Docs and integrate it into your LangChain project. </p>



<p><strong>Setting up your Google Drive API credentials</strong> is an essential step when you’re looking to leverage Google Drive functionalities in your application. Let’s break it down step by step.</p>



<ol><li><strong>Creating a new Google Cloud Project</strong>: Start by visiting your Google Cloud Console at <a href="https://console.cloud.google.com/">https://console.cloud.google.com</a>. If you’re not already signed into your Google account, you’ll be prompted to do so. Once in the Console, you can create a new project by clicking on the project dropdown (a triangle next to your Project Name) in the top left corner. A panel will slide open with a list of your projects and a ‘NEW PROJECT’ button at the top. Click on it, name your new project, and create it.</li></ol>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="396" src="https://blog.nextideatech.com/wp-content/uploads/2023/06/image-1024x396.png" alt="" class="wp-image-31101" srcset="https://blog.nextideatech.com/wp-content/uploads/2023/06/image-980x379.png 980w, https://blog.nextideatech.com/wp-content/uploads/2023/06/image-480x186.png 480w" sizes="(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw"><figcaption>Setting up Google Cloud API for ChatGPT Chatbot</figcaption></figure>



<ol><li><strong>Enabling the Google Drive API</strong>: After creating your project, you need to enable the Google Drive API for it. This can be done by visiting the ‘Library’ in the left sidebar. Here, search for ‘Google Drive API’, click on the resulting option, and then click ‘Enable’. If you don’t see the ‘Enable’ button immediately, you may need to refresh the page or navigate back to it once your new project is fully set up.</li></ol>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="249" src="https://blog.nextideatech.com/wp-content/uploads/2023/06/image-1-1024x249.png" alt="" class="wp-image-31102" srcset="https://blog.nextideatech.com/wp-content/uploads/2023/06/image-1-980x239.png 980w, https://blog.nextideatech.com/wp-content/uploads/2023/06/image-1-480x117.png 480w" sizes="(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw"></figure>



<figure class="wp-block-image size-full"><img loading="lazy" width="872" height="432" src="https://blog.nextideatech.com/wp-content/uploads/2023/06/image-2.png" alt="" class="wp-image-31103" srcset="https://blog.nextideatech.com/wp-content/uploads/2023/06/image-2.png 872w, https://blog.nextideatech.com/wp-content/uploads/2023/06/image-2-480x238.png 480w" sizes="(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 872px, 100vw"></figure>



<ol><li><strong>Setting up Google API Credentials</strong>: This process involves four main steps:<ul><li><strong>Authorize credentials for a desktop app</strong>: In the Google Cloud Console, navigate to ‘Credentials’ under ‘APIs &amp; Services’. Click on ‘Create Credentials’ and select ‘OAuth client ID’. In the ‘Application type’, choose ‘Desktop app’. Enter a name for your OAuth client ID and click ‘Create’.</li><li><strong>Download your credentials</strong>: After creating, download the credentials by clicking on the download icon (a down arrow) next to the client ID you’ve just created. This will download a JSON file, such as <code>client_secret_23429870XXXXXXXXXXXXXX.json</code>.</li><li><strong>Configure your consent screen</strong>: Go to ‘OAuth consent screen’ under ‘APIs &amp; Services’ and fill out the necessary details. This includes the app name, user support email, developer contact information, and more. This is the information users will see when your app requests access to their data.</li><li><strong>Add yourself as a test user</strong>: On the same ‘OAuth consent screen’, scroll down to the ‘Test users’ section and click on ‘Add Users’. Enter your email and save. This is especially important when your app is in a testing phase. </li></ul></li><li><strong>Storing your Credentials</strong>: The credentials you downloaded come in a JSON file, which by default, LangChain expects to be stored at <code>~/.credentials/credentials.json</code>. However for the sake of this project, we will place the <em>credentials.json</em> in the project root directory and point it using the <em>GOOGLE_APPLICATION_CREDENTIALS</em> variable.</li></ol>



<h2 id="step-by-step-google-docs-chatgpt-python">Step by step: How to build Google Docs ChatGPT chatbot</h2>



<h4>Installing Python dependencies</h4>



<pre class="wp-block-code language-bash" tabindex="0"><code lang="bash" class="language-bash">pip <span class="token function">install</span> openai
pip <span class="token function">install</span> langchain
pip <span class="token function">install</span> openai
pip <span class="token function">install</span> chromadb
pip <span class="token function">install</span> PyPDF2
pip <span class="token function">install</span> sentence_transformers
pip <span class="token function">install</span> torch
pip <span class="token function">install</span> --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib</code></pre>



<p><strong>Note</strong>: when running the application, you may receive an error message <em>AssertionError: Torch not compiled with CUDA enabled</em>.</p>



<p>This is because the code relies on <em>torch</em> which needs to be installed as follows in order to <a href="https://medium.com/@jjlovesstudying/python-cuda-set-up-on-windows-10-for-gpu-support-78126284b085" target="_blank" aria-label="support CUDA (opens in a new tab)" rel="noreferrer noopener" class="ek-link">support CUDA</a></p>



<pre class="wp-block-code language-bash" tabindex="0"><code lang="bash" class="language-bash">pip uninstall torch
pip cache purge
pip <span class="token function">install</span> torch -f https://download.pytorch.org/whl/torch_stable.html</code></pre>



<h3>Code Walkthrough</h3>



<p><code>load_documents(</code>)</p>



<pre class="wp-block-code language-python" tabindex="0"><code lang="python" class="language-python"><span class="token keyword">def</span> <span class="token function">load_documents</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    loader <span class="token operator">=</span> GoogleDriveLoader<span class="token punctuation">(</span>
        credentials_path<span class="token operator">=</span>CLIENT_SECRET_FILE<span class="token punctuation">,</span>
        token_path<span class="token operator">=</span>TOKEN_FILE<span class="token punctuation">,</span>
        folder_id<span class="token operator">=</span>GOOGLE_DRIVER_FOLDER_ID<span class="token punctuation">,</span>
        recursive<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span>
        file_types<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">"sheet"</span><span class="token punctuation">,</span> <span class="token string">"document"</span><span class="token punctuation">,</span> <span class="token string">"pdf"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">)</span>
    <span class="token keyword">return</span> loader<span class="token punctuation">.</span>load<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre>



<p>The <code>load_documents()</code> function is used to load documents from a specific Google Drive folder. The <code>GoogleDriveLoader</code> class from the <code>langchain.document_loaders</code> module is instantiated with several arguments: a path to the client secret file for authentication (<code>CLIENT_SECRET_FILE</code>), a path to the token file (<code>TOKEN_FILE</code>), and the ID of the Google Drive folder containing the documents (<code>GOOGLE_DRIVER_FOLDER_ID</code>). The <code>recursive</code> argument is set to <code>False</code>, which means the loader will not look for files in sub-folders of the specified folder. The <code>file_types</code> argument lists the types of files to load from the folder (in this case, Google Sheets, Google Docs, and PDF files). The function returns the loaded documents.<br><strong>file_types</strong>: denotes the file types you want to index. “<em>document</em>” corresponds to “Google Docs” file type. GoogleDriveLoader <em>will not</em> index “.doc” or “.docx” files that you manually uploaded to the target folder.</p>



<h5>Getting Google Drive Folder_ID</h5>



<p>Getting the ID of a Google Drive folder is quite straightforward. Follow these steps:</p>



<ol><li>First, you’ll need to open your Google Drive in a web browser. This can be done by visiting the URL: <a href="https://drive.google.com/" class="ek-link">https://drive.google.com</a>.</li><li>Navigate to the folder whose ID you want to retrieve.</li><li>Once you’re inside the folder, look at the URL in your web browser’s address bar. It should look something like this: <code>https://drive.google.com/drive/u/0/folders/1A2B3CDEFGH456I7J89K</code></li><li>The part of the URL after ‘folders/’ is the ID of the Google Drive folder. In the example above, the folder ID is <code>1A2B3CDEFGH456I7J89K</code>.</li><li>Copy this ID and use it for “GOOGLE_DRIVER_FOLDER_ID” variable in your code.</li></ol>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" width="1024" height="249" src="https://blog.nextideatech.com/wp-content/uploads/2023/06/image-3-1024x249.png" alt="" class="wp-image-31106" srcset="https://blog.nextideatech.com/wp-content/uploads/2023/06/image-3-980x239.png 980w, https://blog.nextideatech.com/wp-content/uploads/2023/06/image-3-480x117.png 480w" sizes="(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw"><figcaption>Getting your Google Drive Folder ID</figcaption></figure>



<p><code>split_documents(docs)</code></p>



<pre class="wp-block-code language-python" tabindex="0"><code lang="python" class="language-python"><span class="token keyword">def</span> <span class="token function">split_documents</span><span class="token punctuation">(</span>docs<span class="token punctuation">)</span><span class="token punctuation">:</span>
    text_splitter <span class="token operator">=</span> RecursiveCharacterTextSplitter<span class="token punctuation">(</span>chunk_size<span class="token operator">=</span><span class="token number">4000</span><span class="token punctuation">,</span> chunk_overlap<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> separators<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">" "</span><span class="token punctuation">,</span> <span class="token string">","</span><span class="token punctuation">,</span> <span class="token string">"\n"</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> text_splitter<span class="token punctuation">.</span>split_documents<span class="token punctuation">(</span>docs<span class="token punctuation">)</span>
</code></pre>



<p>The <code>split_documents(docs)</code> function is used to split the loaded documents into chunks. The <code>RecursiveCharacterTextSplitter</code> class from the <code>langchain.text_splitter</code> module is instantiated with specified arguments: <code>chunk_size=4000</code> indicating that each chunk will contain approximately 4000 characters, <code>chunk_overlap=0</code> indicating that there will be no overlapping characters between the chunks, and <code>separators=[" ", ",", "\n"]</code> indicating the preferred points (space, comma, or newline) at which to split the text. The function returns the document text split into chunks.</p>



<p><code>generate_embeddings()</code></p>



<pre class="wp-block-code language-python" tabindex="0"><code lang="python" class="language-python"><span class="token keyword">def</span> <span class="token function">generate_embeddings</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> HuggingFaceEmbeddings<span class="token punctuation">(</span>model_name<span class="token operator">=</span>HUGGINGFACE_MODEL<span class="token punctuation">,</span> model_kwargs<span class="token operator">=</span>MODEL_KWARGS<span class="token punctuation">)</span>
</code></pre>



<p>The <code>generate_embeddings()</code> function creates an instance of the <code>HuggingFaceEmbeddings</code> class from the <code>langchain.embeddings</code> module. The instance is created with the specified model name (<code>HUGGINGFACE_MODEL</code>) and additional keyword arguments (<code>MODEL_KWARGS</code>), which in this case is to specify that the model should run on a CUDA-enabled GPU. The function returns the HuggingFace embeddings generator.</p>



<p><code>create_chroma_db(texts, embeddings)</code></p>



<pre class="wp-block-code language-python" tabindex="0"><code lang="python" class="language-python"><span class="token keyword">def</span> <span class="token function">create_chroma_db</span><span class="token punctuation">(</span>texts<span class="token punctuation">,</span> embeddings<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> <span class="token keyword">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>PERSIST_DIRECTORY<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> Chroma<span class="token punctuation">.</span>from_documents<span class="token punctuation">(</span>texts<span class="token punctuation">,</span> embeddings<span class="token punctuation">,</span> persist_directory<span class="token operator">=</span>PERSIST_DIRECTORY<span class="token punctuation">)</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> Chroma<span class="token punctuation">(</span>embedding_function<span class="token operator">=</span>embeddings<span class="token punctuation">,</span> persist_directory<span class="token operator">=</span>PERSIST_DIRECTORY<span class="token punctuation">)</span>
</code></pre>



<p>The <code>create_chroma_db(texts, embeddings)</code> function creates a Chroma database that stores the embeddings for the chunks of text. The <code>Chroma</code> class is from the <code>langchain.vectorstores</code> module. If the specified persistence directory (<code>PERSIST_DIRECTORY</code>) does not exist, it creates a new Chroma database using the texts and their corresponding embeddings. If the directory already exists, it assumes the Chroma database has already been created, and just loads it using the <code>Chroma()</code> constructor, which needs the embeddings function and the persistence directory as arguments.</p>



<p><code>create_retriever(db)</code></p>



<pre class="wp-block-code"><code class="">def create_retriever(db):
    return db.as_retriever(search_kwargs={"k": RETRIEVER_K_ARG})
</code></pre>



<p>The <code>create_retriever(db)</code> function creates a retriever object from the Chroma database. The retriever will be used to find the most relevant chunks of text based on a given query. The <code>.as_retriever()</code> method of the Chroma database object is used to create the retriever. The <code>search_kwargs</code> parameter is used to configure the search behavior of the retriever; here, it sets the “k” parameter, which determines the number of top results to retrieve.</p>



<p><code>create_index(llm, retriever)</code></p>



<pre class="wp-block-code"><code class="">def create_index(llm, retriever):
    return RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
</code></pre>



<p>The <code>create_index(llm, retriever)</code> function uses the <code>RetrievalQA</code> class from the <code>langchain.chains</code> module to create an index that combines a language model (llm) and a retriever. The <code>from_chain_type()</code> static method is used to create the index, with “default” as the chain type, and the provided language model and retriever as arguments.</p>



<p><code>create_llm()</code></p>



<pre class="wp-block-code"><code class="">def create_llm():
    return ChatOpenAI(temperature=0, model_name=OPENA_AI_MODEL)
</code></pre>



<p>The <code>create_llm()</code> function is used to instantiate an instance of the <code>ChatOpenAI</code> class from the <code>langchain.chat_models</code> module, which represents a language model. The <code>temperature</code> parameter is set to <code>0</code>, indicating that the model’s outputs will be deterministic (i.e., always choosing the most probable next word). The <code>model_name</code> is set to <code>OPENA_AI_MODEL</code>, a constant that refers to a specific version of the OpenAI model to use (in this case, “gpt-4-0314”). The function returns the instantiated language model.</p>



<p>main()</p>



<pre class="wp-block-code language-python" tabindex="0"><code lang="python" class="language-python"><span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    docs <span class="token operator">=</span> load_documents<span class="token punctuation">(</span><span class="token punctuation">)</span>
    texts <span class="token operator">=</span> split_documents<span class="token punctuation">(</span>docs<span class="token punctuation">)</span>
    embeddings <span class="token operator">=</span> generate_embeddings<span class="token punctuation">(</span><span class="token punctuation">)</span>
    db <span class="token operator">=</span> create_chroma_db<span class="token punctuation">(</span>texts<span class="token punctuation">,</span> embeddings<span class="token punctuation">)</span>
    retriever <span class="token operator">=</span> create_retriever<span class="token punctuation">(</span>db<span class="token punctuation">)</span>
    llm <span class="token operator">=</span> create_llm<span class="token punctuation">(</span><span class="token punctuation">)</span>
    qa <span class="token operator">=</span> create_index<span class="token punctuation">(</span>llm<span class="token punctuation">,</span> retriever<span class="token punctuation">)</span>

    <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
        query <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">"&gt; "</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> query<span class="token punctuation">.</span>lower<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string">"exit"</span><span class="token punctuation">:</span>
            exit<span class="token punctuation">(</span><span class="token punctuation">)</span>
        answer <span class="token operator">=</span> qa<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">"query"</span><span class="token punctuation">:</span> <span class="token string-interpolation"><span class="token string">f"### Instructions. </span><span class="token interpolation"><span class="token punctuation">{</span>PRE_PROMPT_INSTRUCTIONS<span class="token punctuation">}</span></span><span class="token string"> ###Prompt </span><span class="token interpolation"><span class="token punctuation">{</span>query<span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>answer<span class="token punctuation">[</span><span class="token string">'result'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">"__main__"</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre>



<p>The <code>main()</code> function orchestrates the entire process of loading, processing, and querying documents.</p>



<ol><li>The function then enters an infinite loop, where it repeatedly asks the user for a query (with <code>input("&gt; ")</code>). The loop continues until the user enters “exit”, at which point the program exits.</li><li>If the input isn’t “exit”, the function treats the input as a query and passes it to the <code>qa</code> object (the index created earlier), which is responsible for producing a response. This response is then printed out.</li></ol>



<p><strong>First run<br></strong>The first time you correctly execute your program, it will automatically open a web browser prompting you to sign in to your Google account. You should log in with the account that you previously set up as a test user during the preparation stage. Once you’ve successfully authenticated your account, the application will quietly generate a <code>token.json</code> file and download it.</p>



<p> Note: the token file name is whatever value you specify in TOKEN_FILE variable. </p>



<p>This file will contain the necessary tokens to access your Google account and it will be used for future sessions, eliminating the need for you to manually log in each time you run the program.</p>



<h2>Conclusion</h2>



<p>This kind of chatbot has a wide variety of use-cases, especially where dealing with large amounts of text data is involved. Here are a few examples:</p>



<ol><li><strong>Research:</strong> If you’re a researcher dealing with a large corpus of articles, papers, or reports, this program can help you find relevant information quickly. Instead of manually reading through each document, you can query the system and get the information you need.</li><li><strong>Legal Work:</strong> In legal professions, there’s often a need to comb through large volumes of legal documents, cases, or laws to find relevant references or precedents. This system could drastically reduce the time spent on such tasks.</li><li><strong>Customer Support:</strong> This could be used in a company’s customer support to quickly find answers to customer queries based on a large database of product manuals, previous support tickets, and other documentation.</li><li><strong>Education:</strong> Teachers and students could use this to quickly find relevant study material from a large corpus of textbooks, articles, and other educational resources.</li><li><strong>Content Creation:</strong> Writers and content creators could use it to quickly find inspiration, references, or background information from a vast collection of documents.</li><li><strong>Business Intelligence:</strong> Companies could use this system to extract insights from a vast amount of business reports, meeting minutes, or emails.</li><li><strong>Healthcare:</strong> Medical practitioners can use it to search through a large collection of medical literature, case studies, research papers, and find treatment options, diagnose diseases, etc.</li></ol>



<p>You can find the code in <a aria-label="this (opens in a new tab)" href="https://github.com/NextIdeaTechUS/LangChain_GDrive_GPT_Python" target="_blank" rel="noreferrer noopener" class="ek-link">this</a> github repository</p>



<h2><br></h2>
<p></p>					<!-- <section class="cta-card">
					  <div class="cta-card__container">
					    <div class="cta-card__header">
					      <p class="cta-card__title">Tell us the skills you need and we'll find the best developer for you in days, not weeks.</p>
					      <a href="https://nextideatech.com/contact/">Hire Developers</a>
					    </div>
					  </div>
					</section> -->

					<!-- subscribe form -->
					<div id="mc_embed_signup" class="subscribe-form">
						<form action="https://nextideatech.us7.list-manage.com/subscribe/post?u=ae45185df605e4c3b1371d09f&amp;id=22c96b1894&amp;f_id=0004c5e4f0" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate="novalidate">
							<div id="mc_embed_signup_scroll">
								<h4>Subscribe to our newsletter and get<br> exclusive content</h4>
								<div class="mc-field-group">
									<input type="email" value="" name="EMAIL" class="required email" placeholder="Email Address" id="mce-EMAIL" aria-required="true">
									<span id="mce-EMAIL-HELPERTEXT" class="helper_text"></span>
									<input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button">
								</div>
								<div id="mce-responses" class="clear foot">
									<div class="response" id="mce-error-response" style="display:none"></div>
									<div class="response" id="mce-success-response" style="display:none"></div>
								</div>
						    <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_ae45185df605e4c3b1371d09f_22c96b1894" tabindex="-1" value=""></div>
						   </div>
						</form>
					</div>
					<script type="text/javascript" src="//s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js"></script>
					<script type="text/javascript">(function($) {window.fnames = new Array(); window.ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='FNAME';ftypes[1]='text';fnames[2]='LNAME';ftypes[2]='text';fnames[3]='ADDRESS';ftypes[3]='address';fnames[4]='PHONE';ftypes[4]='phone';}(jQuery));var $mcj = jQuery.noConflict(true);</script>

				</div>

				<div class="col-lg-4 col-xl-4 blog-sidebar-col">
						<div id="sidebar">
		<div id="block-17" class="et_pb_widget widget_block">
<h5>Related Posts</h5>
</div><div id="block-18" class="et_pb_widget widget_block widget_recent_entries"><ul class="wp-block-latest-posts__list has-dates wp-block-latest-posts"><li><div class="wp-block-latest-posts__featured-image alignleft"><a href="https://blog.nextideatech.com/chatgpt-google-docs-chatbot/" aria-label="ChatGPT Chatbot For Google Docs"><img width="300" height="158" src="https://blog.nextideatech.com/wp-content/uploads/2023/06/Python-300x158.png" class="attachment-medium size-medium wp-post-image" alt="langchain openai google drive" loading="lazy" style=""></a></div><a class="wp-block-latest-posts__post-title" href="https://blog.nextideatech.com/chatgpt-google-docs-chatbot/">ChatGPT Chatbot For Google Docs</a><time datetime="2023-06-21T07:07:00+00:00" class="wp-block-latest-posts__post-date">June 21, 2023</time></li>
<li><div class="wp-block-latest-posts__featured-image alignleft"><a href="https://blog.nextideatech.com/nearshore-software-development-in-argentina-the-complete-guide-for-2023/" aria-label="Nearshore Software Development in Argentina: The Complete Guide for 2023"><img width="300" height="225" src="https://blog.nextideatech.com/wp-content/uploads/2023/06/1280px-Bandera_de_Argentina_a_media_asta-300x225.jpg" class="attachment-medium size-medium wp-post-image" alt="Nearshore Argentina" loading="lazy" style=""></a></div><a class="wp-block-latest-posts__post-title" href="https://blog.nextideatech.com/nearshore-software-development-in-argentina-the-complete-guide-for-2023/">Nearshore Software Development in Argentina: The Complete Guide for 2023</a><time datetime="2023-06-11T19:12:36+00:00" class="wp-block-latest-posts__post-date">June 11, 2023</time></li>
<li><div class="wp-block-latest-posts__featured-image alignleft"><a href="https://blog.nextideatech.com/nearshore-software-development-in-colombia-2023-edition/" aria-label="Nearshore Software Development in Colombia: 2023 Edition"><img width="300" height="200" src="https://blog.nextideatech.com/wp-content/uploads/2023/05/image-16-300x200.png" class="attachment-medium size-medium wp-post-image" alt="software nearshore colombia" loading="lazy" style=""></a></div><a class="wp-block-latest-posts__post-title" href="https://blog.nextideatech.com/nearshore-software-development-in-colombia-2023-edition/">Nearshore Software Development in Colombia: 2023 Edition</a><time datetime="2023-05-30T14:52:32+00:00" class="wp-block-latest-posts__post-date">May 30, 2023</time></li>
</ul></div><div id="block-19" class="et_pb_widget widget_block">
<div class="sticky-block-wrapper sticky-block-wrapper-0"><div class="sticky-block-placeholder sticky-block-placeholder-0" style="width:0; height:0; margin:0; padding:0; visibility:hidden;"></div><div class="wp-block-senff-sticky-block senff-sticky-block-0 sticky-block-original-0 block-is-not-sticky sticky-block-active-0" data-topspace="100" data-cfa="true" data-minwidth="980" data-maxwidth="99999" data-pushup="#main-footer" data-zindex="1" style="">
<h5>Share</h5>


<p></p><div class="addtoany_shortcode"><div class="a2a_kit a2a_kit_size_32 addtoany_list" data-a2a-url="https://blog.nextideatech.com/chatgpt-google-docs-chatbot/" data-a2a-title="ChatGPT Chatbot For Google Docs" style="line-height: 32px;"><a class="a2a_button_facebook" href="/#facebook" title="Facebook" rel="nofollow noopener" target="_blank"><span class="a2a_svg a2a_s__default a2a_s_facebook" style="background-color: rgb(10, 107, 216);"><svg focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path fill="#FFF" d="M17.78 27.5V17.008h3.522l.527-4.09h-4.05v-2.61c0-1.182.33-1.99 2.023-1.99h2.166V4.66c-.375-.05-1.66-.16-3.155-.16-3.123 0-5.26 1.905-5.26 5.405v3.016h-3.53v4.09h3.53V27.5h4.223z"></path></svg></span><span class="a2a_label">Facebook</span></a><a class="a2a_button_twitter" href="/#twitter" title="Twitter" rel="nofollow noopener" target="_blank"><span class="a2a_svg a2a_s__default a2a_s_twitter" style="background-color: rgb(10, 107, 216);"><svg focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path fill="#FFF" d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z"></path></svg></span><span class="a2a_label">Twitter</span></a><a class="a2a_button_pinterest" href="/#pinterest" title="Pinterest" rel="nofollow noopener" target="_blank"><span class="a2a_svg a2a_s__default a2a_s_pinterest" style="background-color: rgb(10, 107, 216);"><svg focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path fill="#FFF" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"></path></svg></span><span class="a2a_label">Pinterest</span></a><a class="a2a_button_linkedin" href="/#linkedin" title="LinkedIn" rel="nofollow noopener" target="_blank"><span class="a2a_svg a2a_s__default a2a_s_linkedin" style="background-color: rgb(10, 107, 216);"><svg focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#FFF"></path></svg></span><span class="a2a_label">LinkedIn</span></a><a class="a2a_button_email" href="/#email" title="Email" rel="nofollow noopener" target="_blank"><span class="a2a_svg a2a_s__default a2a_s_email" style="background-color: rgb(10, 107, 216);"><svg focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path fill="#FFF" d="M26 21.25v-9s-9.1 6.35-9.984 6.68C15.144 18.616 6 12.25 6 12.25v9c0 1.25.266 1.5 1.5 1.5h17c1.266 0 1.5-.22 1.5-1.5zm-.015-10.765c0-.91-.265-1.235-1.485-1.235h-17c-1.255 0-1.5.39-1.5 1.3l.015.14s9.035 6.22 10 6.56c1.02-.395 9.985-6.7 9.985-6.7l-.015-.065z"></path></svg></span><span class="a2a_label">Email</span></a></div></div><p></p>
</div></div>
</div>	</div>
				</div>

			</div>
		</div>