Monday, April 22, 2024

Chatbot For Your Google Paperwork Utilizing Langchain And OpenAI

Must read


Introduction

On this article, we’ll create a Chatbot on your Google Paperwork with OpenAI and Langchain. Now why do we’ve to do that within the first place? It will get tedious to repeat and paste your Google Docs contents to OpenAI. OpenAI has a personality token restrict the place you possibly can solely add particular quantities of data. So if you wish to do that at scale otherwise you wish to do it programmatically, you’re going to wish a library that will help you out; with that, Langchain comes into the image. You may create a enterprise impression by connecting Langchain with Google Drive and open AI in an effort to summarize your paperwork and ask associated questions. These paperwork could possibly be your product paperwork, your analysis paperwork, or your inside information base that your organization is utilizing.

Studying Aims

  • You may discover ways to fetch your Google paperwork content material utilizing Langchain.
  • Learn to combine your Google docs content material with OpenAI LLM.
  • You may study to summarize and ask questions on your doc’s content material.
  • You may discover ways to create a Chatbot that solutions questions based mostly in your paperwork.

This text was revealed as part of the Information Science Blogathon.

Load Your Paperwork

Earlier than we get began, we have to arrange our paperwork in google drive.  The crucial half here’s a doc loader that langchain gives referred to as GoogleDriveLoader. Utilizing this, you possibly can initialize this class after which go it an inventory of doc IDs.

from langchain.document_loaders import GoogleDriveLoader
import os
loader = GoogleDriveLoader(document_ids=["YOUR DOCUMENT ID's'"],
                          credentials_path="PATH TO credentials.json FILE")
docs = loader.load()

Yow will discover your doc id out of your doc hyperlink. Yow will discover the id between the ahead slashes after /d/ within the hyperlink.

For instance, in case your doc hyperlink is https://docs.google.com/doc/d/1zqC3_bYM8Jw4NgF then your doc id is “1zqC3_bYM8Jw4NgF”.

You may go the record of those doc IDs to document_ids parameter, and the cool half about that is you too can go a Google Drive folder ID that comprises your paperwork. In case your folder hyperlink is https://drive.google.com/drive/u/0/folders/OuKkeghlPiGgWZdM then the folder ID is “OuKkeghlPiGgWZdM1TzuzM”.

Authorize Google Drive Credentials

Step 1:

Allow the GoogleDrive API by utilizing this hyperlink https://console.cloud.google.com/flows/enableapi?apiid=drive.googleapis.com. Please guarantee you’re logged into the identical Gmail account the place your paperwork are saved within the drive.

Chatbot For Your Google Documents | Langchain | Openai

Step 2: Go to the Google Cloud console by clicking this hyperlink . Choose “OAuth shopper ID”. Give software sort as Desktop app.

Chatbot For Your Google Documents | Langchain | Openai
Chatbot For Your Google Documents | Langchain | Openai

Step 3: After creating the OAuth shopper, obtain the secrets and techniques file by clicking “DOWNLOAD JSON”. You may comply with Google’s steps if in case you have any doubts whereas making a credentials file.

Chatbot For Your Google Documents | Langchain | Openai

Step 4: Improve your Google API Python shopper by operating beneath pip command

pip set up --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

Then we have to go our json file path into GoogleDriveLoader.

Summarizing Your Paperwork

Be sure you have your OpenAI API Keys accessible with you. If not, comply with the beneath steps:

1. Go to ‘https://openai.com/ and create your account.

2. Login into your account and choose ‘API’ in your dashboard.

3. Now click on in your profile icon, then choose ‘View API Keys’.

4. Choose ‘Create new secret key’, copy it, and reserve it.

Subsequent, we have to load our OpenAI LLM. Let’s summarize the loaded docs utilizing OpenAI. Within the beneath code, we used a summarization algorithm referred to as summarize_chain offered by langchain to create a summarization course of which we saved in a variable named chain that takes enter paperwork and produces concise summaries utilizing the map_reduce method. Substitute your API key within the beneath code.

from langchain.llms import OpenAI
from langchain.chains.summarize import load_summarize_chain
llm = OpenAI(temperature=0, openai_api_key=os.environ['OPENAI_API_KEY'])
chain = load_summarize_chain(llm, chain_type="map_reduce", verbose= False)
chain.run(docs)

You’re going to get a abstract of your paperwork should you run this code. If you wish to see what LangChain was doing beneath the covers, change verbose to True, after which you possibly can see the logic that Langchain is utilizing and the way it’s pondering. You may observe that LangChain will robotically insert the question to summarize your doc, and the complete textual content(question+ doc content material) can be handed to OpenAI. Now OpenAI will generate the abstract.

Under is a use case the place I despatched a doc in Google Drive associated to a product SecondaryEquityHub and summarized the doc utilizing the map_reduce chain sort and load_summarize_chain() perform. I’ve set verbose=True to see how Langchain is working internally.

from langchain.document_loaders import GoogleDriveLoader
import os
loader = GoogleDriveLoader(document_ids=["ceHbuZXVTJKe1BT5apJMTUvG9_59-yyknQsz9ZNIEwQ8"],
                          credentials_path="../../desktop_credetnaisl.json")
docs = loader.load()
from langchain.llms import OpenAI
from langchain.chains.summarize import load_summarize_chain
llm = OpenAI(temperature=0, openai_api_key=os.environ['OPENAI_API_KEY'])
chain = load_summarize_chain(llm, chain_type="map_reduce", verbose=True)
chain.run(docs)

Output:

 Source: Author

We are able to observe that Langchain inserted the immediate to generate a abstract for a given doc.

 Source: Author

We are able to see the concise abstract and the product options current within the doc generated by Langchain utilizing OpenAI LLM.

Extra Use Circumstances

1. Analysis: We are able to use this performance whereas doing analysis, As an alternative of intensively studying the complete analysis paper phrase by phrase, we are able to use the summarizing performance to get a look on the paper rapidly.

2. Training: Academic establishments can get curated textbook content material summaries from in depth knowledge, tutorial books, and papers.

3. Enterprise Intelligence: Information analysts should undergo a big set of paperwork to extract insights from paperwork. Utilizing this performance, they’ll cut back the large quantity of effort.

4. Authorized Case Evaluation: Legislation practising professionals can use this performance to rapidly get crucial arguments extra effectively from their huge quantity of earlier comparable case paperwork.

Let’s say we needed to ask questions on content material in a given doc, we have to load in a unique chain named load_qa_chain . Subsequent, we initialise this chain with a chain_type parameter. In our case, we used chain_type as “stuff” This can be a simple chain sort; it takes all of the content material, concatenates, and passes to LLM.

Different chain_types:

  • map_reduce: Originally, the mannequin will individually seems to be into every doc and shops its insights, and on the finish, it combines all these insights and once more seems to be into these mixed insights to get the ultimate response.
  • refine: It iteratively seems to be into every doc given within the document_id record, then it refines the solutions with the latest info it discovered within the doc because it goes.
  • Map re-rank: The mannequin will individually look into every doc and assigns a rating to the insights. Lastly, it would return the one with the very best rating.

Subsequent, we run our chain by passing the enter paperwork and question.

from langchain.chains.question_answering import load_qa_chain
question = "Who's founding father of analytics vidhya?"
chain = load_qa_chain(llm, chain_type="stuff")
chain.run(input_documents=docs, query=question)

If you run this code, langchain robotically inserts the immediate together with your doc content material earlier than sending this to OpenAI LLM. Below the hood, langchain helps us with immediate engineering by offering optimized prompts to extract the required content material from paperwork. If you wish to see what prompts they’re utilizing internally, simply set verbose=True, then you possibly can see the immediate within the output.

from langchain.chains.question_answering import load_qa_chain
question = "Who's founding father of analytics vidhya?"
chain = load_qa_chain(llm, chain_type="stuff", verbose=True)
chain.run(input_documents=docs, query=question)

Construct Your Chatbot

Now we have to discover a strategy to make this mannequin a question-answering Chatbot. Primarily we have to comply with beneath three issues to create a Chatbot.

1. Chatbot ought to bear in mind the chat historical past to grasp the context concerning the continued dialog.

2. Chat historical past ought to be up to date after every immediate the person asks to bot.

2. Chatbot ought to work till the person desires to exit the dialog.

from langchain.chains.question_answering import load_qa_chain

# Operate to load the Langchain question-answering chain
def load_langchain_qa():
    llm = OpenAI(temperature=0, openai_api_key=os.environ['OPENAI_API_KEY'])  
    chain = load_qa_chain(llm, chain_type="stuff", verbose=True)
    return chain

# Operate to deal with person enter and generate responses
def chatbot():
    print("Chatbot: Hello! I am your pleasant chatbot. Ask me something or sort 'exit' to finish the dialog.")
    from langchain.document_loaders import GoogleDriveLoader
    loader = GoogleDriveLoader(document_ids=["YOUR DOCUMENT ID's'"],
                          credentials_path="PATH TO credentials.json FILE")
    docs = loader
    # Initialize the Langchain question-answering chain
    chain = load_langchain_qa()
    
    # Record to retailer chat historical past
    chat_history = []
    
    whereas True:
        user_input = enter("You: ")
        
        if user_input.decrease() == "exit":
            print("Chatbot: Goodbye! Have a terrific day.")
            break

        # Append the person's query to talk historical past
        chat_history.append(user_input)

        # Course of the person's query utilizing the question-answering chain
        response = chain.run(input_documents=chat_history, query=user_input)
        
        # Extract the reply from the response
        reply = response['answers'][0]['answer'] if response['answers'] else "I could not discover a solution to your query."

        # Append the chatbot's response to talk historical past
        chat_history.append("Chatbot: " + reply)

        # Print the chatbot's response
        print("Chatbot:", reply)

if __name__ == "__main__":
    chatbot()

We initialized our google drive paperwork and OpenAI LLM. Subsequent, we created an inventory to retailer the chat historical past, and we up to date the record after each immediate. Then we created an infinite whereas loop that stops when the person provides “exit” as a immediate.

Conclusion

On this article, we’ve seen the best way to create a Chatbot to offer insights about your Google paperwork contents. Integrating Langchain, OpenAI, and Google Drive is likely one of the most useful use instances in any area, whether or not medical, analysis, industrial, or engineering. As an alternative of studying complete knowledge and analyzing the information to get insights which prices numerous human time and effort. We are able to implement this know-how to automate describing, summarizing, analyzing, and extracting insights from our knowledge information.

Key Takeaways

  • Google paperwork may be fetched into Python utilizing Python’s GoogleDriveLoader class and Google Drive API credentials.
  • By integrating OpenAI LLM with Langchain, we are able to summarize our paperwork and ask questions associated to the paperwork.
  • We are able to get insights from a number of paperwork by selecting acceptable chain sorts like map_reduce, stuff, refine, and map rerank.

Steadily Requested Questions

Q1. Methods to construct a wise chatbot with Langchain and ChatGPT?

A. To construct an clever chatbot, it’s essential to have acceptable knowledge, then it’s essential to give entry to ChatGPT for this knowledge. Lastly, it’s essential to present dialog reminiscence to the bot to retailer the chat historical past to grasp the context.

Q2. How do I share a Google Doc with OpenAI’s ChatGPT?

A. One of many options is you need to use Langchain’s GoogleDriveLoader to fetch a Google Doc then, you possibly can initialize the OpenAI LLM utilizing your API keys, then you possibly can share the file to this LLM.

Q3. How do I hyperlink ChatGPT on to a Google Drive file?

A. First, it’s essential to allow Google Drive API, then get your credentials for Google Drive API, then you possibly can go the doc id of your file to the OpenAI ChatGPT mannequin utilizing Langchain GoogleDriveLoader.

This fall. Can ChatGPT entry drive paperwork?

A. ChatGPT can’t entry our paperwork immediately. Nevertheless, we are able to both copy and paste the content material into ChatGPT or immediately fetch the contents of paperwork utilizing Langchain then, we are able to go the contents to ChatGPT by initializing it utilizing secret keys.

The media proven on this article isn’t owned by Analytics Vidhya and is used on the Writer’s discretion.



Supply hyperlink

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article