Import Text Articles into ChatGPT Prompts

Hello - Noob here.

I’m trying to import text articles into chatGPT that will interact with a series of prompts I have put together.

I was trying to automate it by uploading a .txt file to my Google Drive and then feeding the information to ChatGPT. But I’m not seeing an obvious no-code way to do it since I can’t see the contents of the .txt file within GDrive.

My next step is to extract the text with Python. But are there any obvious ways that this could be done that I don’t know about?

I’m not sure about a no-code way, but here’s some code that will do it.

I’ve placed this in a Node step, immediately after a Google Drive → Download File step. So the order is:

  1. Trigger (Google Drive)
  2. Google Drive → Download File (storing to /tmp/text.txt)
  3. Node

Here’s the code:

import fs from "fs";
export default defineComponent({
  async run({ steps, $ }) {
    function readAndProcessTextFile(location = "/tmp/text.txt") {
      // Check if the location exists and is a file
      if (!fs.existsSync(location) || !fs.lstatSync(location).isFile()) {
        throw new Error(`File not found at location: ${location}`);
      }

      // Read the file and process the content
      const content = fs.readFileSync(location, 'utf-8');
      const processedContent = splitAndRejoinText(content);

      return processedContent;
    }

    function splitAndRejoinText(text) {
      const sentences = text.match(/[^\.!\?]+[\.!\?]+/g) || [];
      let paragraphs = [];
      let currentParagraph = '';

      sentences.forEach((sentence, index) => {
        currentParagraph += sentence.trim() + ' ';
        if ((index + 1) % 3 === 0 || index === sentences.length - 1) {
          paragraphs.push(currentParagraph.trim());
          currentParagraph = '';
        }
      });

      return paragraphs.join('\n\n');
    }

    // Usage example:
    const processedText = readAndProcessTextFile();
    return processedText
  },
})

I’ll note that ChatGPT wrote this for me. I’ve tested it in a workflow and it worked!

Thanks Thomas! I was actually following your tutorial for transcripts to Notion and with some help from ChatGPT (and your code from the tutorial) I got something working. I’ll compare it to what you have here, I’m sure yours is more complete. Below is what I used. I had the same three steps you mentioned above.

// Import required modules
import fs from 'fs';
import { promisify } from 'util';

// Promisify the readFile function for easier use with async/await
const readFile = promisify(fs.readFile);

// Define the component
export default defineComponent({
  async run({ $ }) {
    // Define the path to the text file
    const path = '/tmp/textfile.txt';

    try {
      // Read the contents of the text file using the promisified readFile function
      const data = await readFile(path, 'utf8');

      // Create a results object containing the text file contents
      const results = {
        textFileContents: data,
      };

      // Return the results object, which will be exported from this step
      return results;
    } catch (err) {
      // Log the error and re-throw it for further handling
      console.error(err);
      throw err;
    }
  },
});

Why did you decide to split it into sentences and then re-join? It makes me wonder if I should be doing that.

I do that for a couple reasons:

  1. This turns the wall-of-text transcript string into paragraphs that have line breaks between them. Makes it much easier to read.
  2. In the code-heavy version, I have to make sure I’m staying within the Notion API’s request limits. Splitting the transcript into paragraphs helps with that.
1 Like

You’re a legend! Thanks dude. I’ve been enjoying your tutorials.

Hi Guys
Following this discussion I’m getting success till I get to “format_chat” stage.
(fercaralt: I tweaked your last bit of code from “tmp/textfile.txt” to “tmp/text.txt” - hope that’s right…)

Does the code for the format_chat stage need to change from the standard (Heavy-Code) instructions ?

Hey matthew, I’m not following that step currently so I won’t be able to help. Sorry! I’m using Thomas’ code and editing it for my own purposes