Reading text from Gdoc for feeding to ChatGPT

Here is the workflow I am trying to create.

  1. Read from a google doc file uploaded
  2. Generate text of the doc as a variable (or so called transcript similar to how audio files get converted).
  3. Break the text into chunks of say 1500 tokens
  4. Feed to chatgpt via api for further analysis

I am not able to get my step 2 working.

I have step 3 and 4 working with audio file analysis.

Can someone help me with step 2? How do I generate text of google doc and feed it as variable?

Thanks!

1 Like

Hi @janidb,

To generate the text of a Google Doc and use it as a variable in your workflow, you can use the Google Docs API. Here’s an example of how you can achieve this:

  1. Use the Google Drive API to list the files in your Google Drive and find the ID of the Google Doc you want to read.
  2. Use the Google Docs API to retrieve the content of the Google Doc using the ID you obtained in step 1.
  3. Extract the text from the content and store it in a variable that you can use in your workflow.

Here’s an example Pipedream component that demonstrates how to read the content of a Google Doc and store it in a variable:

import { axios } from "@pipedream/platform";

export default defineComponent({
  props: {
    google: {
      type: "app",
      app: "google",
    },
    fileId: {
      type: "string",
      label: "Google Doc File ID",
    },
    variableName: {
      type: "string",
      label: "Variable Name",
    },
  },
  async run({ steps, $ }) {
    // Step 1: Get the content of the Google Doc
    const response = await axios($, {
      url: `https://docs.googleapis.com/v1/documents/${this.fileId}`,
      headers: {
        Authorization: `Bearer ${this.google.$auth.oauth_access_token}`,
      },
    });

    // Step 2: Extract the text from the content
    const content = response.data.body.content;
    const text = content.map((c) => c.paragraph.elements.map((e) => e.textRun.content).join("")).join("\n");

    // Step 3: Store the text in a variable
    $set(this.variableName, text);

    // Return the text for debugging purposes
    return text;
  },
});

To use this component, you’ll need to provide your Google API credentials in the google prop, the ID of the Google Doc in the fileId prop, and the name of the variable you want to store the text in the variableName prop.

I hope this helps! Let me know if you have any further questions.
I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double check my answers and code! Learn how to build your own.