This topic was automatically generated from Slack. You can find the original thread here.
I want to build a workflow that calls GPT 56 times with slightly different prompts, each time, adding a cell into Google sheets. What is the easiest way to do this other than making a 114 step work flow (1 trigger, 1 node, 56 GPT calls, 56 insert cells)?
Yes, I’m ready to help you now. To achieve your goal of calling GPT 56 times with slightly different prompts and adding cells to Google Sheets, you can use a loop within a single code step instead of creating multiple steps. Here’s an example of how you can do this: 1. First, use the “Run Node Code” action in your workflow. 2. Add the following code to the action: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { gptApiKey: { type: "string", label: "GPT API Key", }, google: { type: "app", app: "google", }, spreadsheetId: { type: "string", label: "Google Spreadsheet ID", }, }, async run({ $ }) { for (let i = 0; i < 56; i++) { // Call GPT API with slightly different prompts const gptResponse = await axios($, { method: "POST", url: "https://api.openai.com/v1/engines/davinci-codex/completions", headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.gptApiKey}`, }, data: { prompt: `Your prompt with variation ${i}`, // Add other GPT API parameters as needed }, }); // Add the GPT response to Google Sheets await axios($, { method: "POST", url: `https://sheets.googleapis.com/v4/spreadsheets/${this.spreadsheetId}/values/A1:append`, headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, "Content-Type": "application/json", }, data: { range: "A1", majorDimension: "ROWS", values: [[gptResponse]], }, }); } }, }); Replace Your prompt with variation ${i} with your actual prompts, and add any other GPT API parameters as needed. This code will loop 56 times, calling the GPT API with slightly different prompts and appending the results to the specified Google Sheet. Make sure to provide your GPT API key, Google app connection, and Google Spreadsheet ID as props when configuring the action.
To achieve this, you can modify the loop in the code I provided earlier to include the output of the previous GPT call in the next prompt. Here’s an updated version of the code: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ props: { gptApiKey: { type: “string”, label: “GPT API Key”, }, google: { type: “app”, app: “google”, }, spreadsheetId: { type: “string”, label: “Google Spreadsheet ID”, }, }, async run({ $ }) { let previousOutput = “”; for (let i = 0; i < 56; i++) { // Call GPT API with the previous output included in the prompt const gptResponse = await axios($, { method: “POST”, url: “https://api.openai.com/v1/engines/davinci-codex/completions”, headers: { “Content-Type”: “application/json”, Authorization: Bearer ${this.gptApiKey}, }, data: { prompt: Your prompt with variation ${i} and previous output: ${previousOutput}, // Add other GPT API parameters as needed }, }); // Store the GPT response for the next iteration previousOutput = gptResponse.data.choices[0].text; // Add the GPT response to Google Sheets await axios($, { method: “POST”, url: https://sheets.googleapis.com/v4/spreadsheets/${this.spreadsheetId}/values/A1:append, headers: { Authorization: Bearer ${this.google.$auth.oauth_access_token}, “Content-Type”: “application/json”, }, data: { range: “A1”, majorDimension: “ROWS”, values: [[previousOutput]], }, }); } }, });
• Trigger on new row creation in Google Sheets
• Node step that pulls the row number of the created row and outputs 56 cells within that row number that can be referenced.
• GPT Call with a prompt (custom system instructions + user prompt) to create a numbered plan / outline based on the content of the new row
• GPT Call to implement step 1 from the outline GPT call’s output (with the same system instructions).
• GPT Call to implement step 2 from the outline GPT call’s output + the previous step’s output for context
• etc. for 54 more steps
• Then, 56 steps of adding the GPT4 outputs to cells in the trigger row
Here is the Node code for getting the cells:
I understand that you want to modify your workflow to include the output of the previous GPT call in the next prompt. Based on your current workflow, you can achieve this by updating the GPT call steps in your workflow. Here’s a general outline of how to modify your workflow: 1. In your Node step that calculates the row number and cell references, keep the code as it is. 2. For the first GPT call (outline creation), you don’t need to include the previous output since it’s the first step. Keep the code as it is. 3. Starting from the second GPT call (implementing step 1 from the outline), update the prompt to include the output of the previous GPT call. You can access the output of the previous step using steps.previous_step_name.$return_value. For example, if the previous step is named gpt_call_1, you can access its output using steps.gpt_call_1.$return_value. 4. Repeat step 3 for all the remaining GPT calls (steps 2 to 54), making sure to reference the output of the previous GPT call in each prompt. 5. Keep the steps that add the GPT outputs to the cells in the trigger row as they are. Here’s an example of how to update the prompt for the second GPT call (implementing step 1 from the outline): const previousOutput = steps.gpt_call_1.$return_value; const prompt = `Your custom system instructions + user prompt and previous output: ${previousOutput}`; Replace gpt_call_1 with the actual name of the previous GPT call step in your workflow. Update the remaining GPT calls similarly, making sure to reference the output of the previous GPT call in each prompt.
To add all the cells at once from the outputs of Chat, Chat_1, Chat_2, etc., to Cell B, Cell C, Cell D, etc., you can use the Google Sheets API to update the cells in a single request. First, you’ll need to collect the outputs from the GPT calls and store them in an array. Then, you can use the Google Sheets API to update the cells in the row. Here’s an example of how you can modify your workflow: 1. After all the GPT calls, add a new code step to collect the outputs from the GPT calls into an array: const gptOutputs = [ steps.gpt_call_1.$return_value, steps.gpt_call_2.$return_value, // Add all other GPT call outputs here ]; return { gptOutputs }; 2. Add a new Google Sheets action to update the cells in the row. Search for the Google Sheets app in the step menu and select the “Update Cells” action. 3. Connect your Google Sheets account and configure the action: - Spreadsheet ID: The ID of your Google Sheet (you can find it in the URL of the sheet) - Range: The range of cells you want to update, e.g., Sheet1!B${rowNumber}:AN${rowNumber} (assuming your row number is stored in the rowNumber variable) - Values: Use the gptOutputs array from the previous code step, e.g., steps.collect_outputs.gptOutputs 4. Save and test your workflow. This will update the cells in the specified range with the outputs from the GPT calls in a single request. Make sure to replace the step names in the example code with the actual names of the steps in your workflow.
Sure, I can help you modify the code to work with your step variables. Here’s an updated version of the code that collects the outputs from your GPT calls and updates the Google Sheets cells: 1. Add a new code step to collect the outputs from the GPT calls into an array: const gptOutputs = [ steps.chat_54.$return_value.original_messages_with_assistant_response[2].content, steps.chat_53.$return_value.original_messages_with_assistant_response[2].content, steps.chat_52.$return_value.original_messages_with_assistant_response[2].content, steps.chat_51.$return_value.original_messages_with_assistant_response[2].content, // Add all other GPT call outputs here ]; return { gptOutputs }; 2. Add a new Google Sheets action to update the cells in the row. Search for the Google Sheets app in the step menu and select the “Update Cells” action. 3. Connect your Google Sheets account and configure the action: - Spreadsheet ID: The ID of your Google Sheet (you can find it in the URL of the sheet) - Range: The range of cells you want to update, e.g., Sheet1!B${rowNumber}:AN${rowNumber} (assuming your row number is stored in the rowNumber variable) - Values: Use the gptOutputs array from the previous code step, e.g., steps.collect_outputs.gptOutputs 4. Save and test your workflow. Make sure to replace the step names in the example code with the actual names of the steps in your workflow.
is there a way to test the entire workflow at once? It’s taking 1.5 minutes to test every GPT step, and I have 56 steps. I don’t have time to sit here and click “test” one at a time
I understand that testing each step individually can be time-consuming. Instead of testing each step one by one, you can test the entire workflow by triggering it with a real event or a test event, depending on the trigger you’re using. This will execute all the steps in the workflow in sequence, allowing you to see the results of the entire workflow at once. If you’re using an HTTP trigger, you can send an HTTP request to the trigger endpoint. For other triggers, like the Scheduler or an app-based trigger, you can use the “Send Test Event” button in the trigger configuration to simulate an event and run the entire workflow. Please note that if there are any errors in the workflow, you’ll need to debug and fix them before running the test again. Make sure to check the execution logs for any errors or unexpected behavior.
Apologies for the confusion earlier. Instead of “Update Cells,” you can use the “Add Rows” action in the Google Sheets app to add the data to your sheet. Here’s how you can modify the code to work with your step variables and add the data to Google Sheets: 1. Add a new code step to collect the outputs from the GPT calls into an array: const gptOutputs = [ steps.chat_54.$return_value.original_messages_with_assistant_response[2].content, steps.chat_53.$return_value.original_messages_with_assistant_response[2].content, steps.chat_52.$return_value.original_messages_with_assistant_response[2].content, steps.chat_51.$return_value.original_messages_with_assistant_response[2].content, // Add all other GPT call outputs here ]; return { gptOutputs }; 2. Add a new Google Sheets action to add the data to your sheet. Search for the Google Sheets app in the step menu and select the “Add Rows” action. 3. Connect your Google Sheets account and configure the action: - Spreadsheet ID: The ID of your Google Sheet (you can find it in the URL of the sheet) - Worksheet Name or ID: The name or ID of the worksheet you want to add the data to - Rows: Use the gptOutputs array from the previous code step, e.g., steps.collect_outputs.gptOutputs 4. Save and test your workflow. Make sure to replace the step names in the example code with the actual names of the steps in your workflow.
SyntaxError: 'return' outside of function. (58:0)
at _raise (/var/task/node_modules/@babel/parser/src/parser/error.js:147:45)
at raiseWithData (/var/task/node_modules/@babel/parser/src/parser/error.js:142:17)
at raise (/var/task/node_modules/@babel/parser/src/parser/error.js:91:17)
at parseReturnStatement (/var/task/node_modules/@babel/parser/src/parser/statement.js:672:12)
at parseStatementContent (/var/task/node_modules/@babel/parser/src/parser/statement.js:288:21)
at parseStatement (/var/task/node_modules/@babel/parser/src/parser/statement.js:242:17)
at parseStatement (/var/task/node_modules/@babel/parser/src/plugins/flow/index.js:1842:26)
at parseBlockOrModuleBlockBody (/var/task/node_modules/@babel/parser/src/parser/statement.js:980:25)
at parseBlockBody (/var/task/node_modules/@babel/parser/src/parser/statement.js:956:10)
at parseProgram (/var/task/node_modules/@babel/parser/src/parser/statement.js:129:10)
I apologize for the confusion earlier. It seems there’s an issue with the code I provided. Let’s correct the code to avoid the error. Please make sure the return statement is inside the run method of the component: ```
export default defineComponent({ async run({ steps }) { const gptOutputs = [ steps.chat_54.$return_value.original_messages_with_assistant_response[2].content, steps.chat_53.$return_value.original_messages_with_assistant_response[2].content, steps.chat_52.$return_value.original_messages_with_assistant_response[2].content, steps.chat_51.$return_value.original_messages_with_assistant_response[2].content, // Add all other GPT call outputs here ]; return { gptOutputs }; }, });