/docgpt

An AI-Powered Google Docs Add-On

DocGPT: An AI-Powered Google Docs Add-On

DocGPT sends the content of a document to OpenAI's Assistant API and creates a new document with the AI-generated response.

The primary use case is for document-wide transformation, such as translation, copy-editing, or related revisions. But it can do anything that you ask your Assistant to do with your doc!

Demo

Click below for a video demonstrating how it works:

Demo Video

Features

  • Converts the entire Google Doc to markdown.
  • Sends the markdown content to OpenAI's Assistant API.
  • Retrieves AI-generated responses.
  • Creates a new Google Doc with the response from OpenAI.

Requirements

  • An OpenAI API key.
  • An OpenAI Assistant set up in the OpenAI Playground.
  • Access to Google Docs and Google Apps Script.

Step 1: Set Up Your OpenAI API Key

  1. Visit the OpenAI Platform.
  2. Sign in or create an OpenAI account.
  3. Navigate to the API section by clicking on your account in the top-right and selecting "API Keys."
  4. Click “Create New Secret Key” to generate your API key.
  5. Copy the key for use in the script.

Step 2: Set Up an OpenAI Assistant

  1. Go to the OpenAI Playground.
  2. Under the Assistants tab, click Create Assistant.
  3. Configure the Assistant with your desired settings (you can customize how the assistant responds based on your preferences).
  4. Once the Assistant is created, you’ll get an Assistant ID.
  5. Copy the Assistant ID for later use in the script.

Step 3: Create the Google Apps Script as an Add-On

  1. Open Google Docs.
  2. Go to Extensions > Apps Script.
  3. In the Apps Script editor:
    • Replace any existing code with the code from this repository.
    • Replace the placeholder values in the script:
      • OPENAI_API_KEY = "your-key-here" → Add your OpenAI API key.
      • OPENAI_ASSISTANT_ID = "your-assistant-id-here" → Add your OpenAI Assistant ID.
  4. Add necessary Google Drive and API permissions:
    • Click Project Settings (gear icon) and ensure that Google Drive API and OAuth Scopes (like https://www.googleapis.com/auth/drive) are enabled.
    • The line DriveApp.getFiles(); is used to automatically detect the necessary scope for file operations.
  5. Save your project.

Step 4: Set Up and Deploy as an Add-On

  1. In the Apps Script editor, go to Deploy > Test deployments.
  2. Test the add-on in your current Google Doc by clicking Run and verifying if it converts and sends the content successfully.
  3. To deploy the add-on for broader use:
    • Go to Deploy > Manage deployments.
    • Select Deploy as an Add-On.
    • Follow the prompts to authorize and deploy the add-on.

Step 5: Using the Add-On

  1. Open any Google Doc.
  2. Run the add-on script:
    • Go to Extensions > AI-Powered Document Assistant (or the name you’ve chosen).
    • The add-on will convert your document to markdown, send it to OpenAI's API, and generate a new document with the AI response.

How It Works

  1. Convert Google Doc to Markdown:
    The gDocToMarkdown() function fetches the content of the Google Doc using its ID and converts it to markdown.

  2. Send to OpenAI:
    The callAssistant() function sends the markdown content to the OpenAI Assistant using the API.

  3. Retrieve and Write Response:
    The OpenAI Assistant processes the document, generates a response, and the add-on creates a new Google Doc with this AI-generated content.

  4. Polling for Completion:
    The script polls the OpenAI API for the completion of the assistant's response and checks the status. Once completed, the new document is created with the assistant's response.


Permissions

This script requires the following permissions:

  • Google Docs: To access and modify your documents.
  • Google Drive: To create and save new documents.
  • OpenAI API: To send document content to OpenAI and receive responses.

Troubleshooting

  • Authorization Errors: Ensure that you’ve granted all required permissions when setting up the add-on.
  • API Errors: Verify that the OpenAI API key and Assistant ID are correctly inserted and valid.
  • Polling Timeouts: If the script seems to be stuck, it may be taking longer than expected to receive the response from OpenAI. You can adjust the polling time if necessary.

Additional Notes

  • This add-on only supports markdown conversion for now. If your Google Doc contains non-markdown-friendly content (e.g., complex formatting), it may not convert perfectly.
  • Depending on the size of your document and the complexity of the AI response, response times can vary.
  • Ensure that your OpenAI API plan is set up to handle the volume of requests.

License

This script is open-source and can be modified to fit your needs. Please follow OpenAI’s usage policies and ensure that you comply with Google’s Apps Script usage guidelines.