Why am I Receiving a Syntax Error for My Workflow Code with OpenAI's GPT-3 Encoder?

This topic was automatically generated from Slack. You can find the original thread here.

Hi I received a ‘syntax’ error for this code in a workflow. Most of the time it works, but recently it has stopped.

import OpenAI from “openai”;
import { encode, decode } from “gpt-3-encoder”;

export default defineComponent({
props: {
openai: {
type: “app”,
app: “openai”,
}
},

**async** run({ steps, $ }) {
    _// Import the transcript from the previous step_
    **const** transcript = steps.create_transcription.$return_value.transcription;

    _// Set the max number of input tokens_
    **const** maxTokens = 2000;

    _// Initialize OpenAI_
    **const** openAIkey = **this**.openai.$auth.api_key;
    **const** openai = **new** OpenAI({ apiKey: openAIkey });

    _// Split the transcript into shorter strings if needed, based on GPT token limit_
    **function** splitTranscript(encodedTranscript, maxTokens) {
        **const** stringsArray = [];
        **let** currentIndex = 0;

        **while** (currentIndex < encodedTranscript.length) {
            **let** endIndex = Math.min(currentIndex + maxTokens, encodedTranscript.length);

            _// Find the next period_
            **while** (endIndex < encodedTranscript.length && decode([encodedTranscript[endIndex]]) !== ".") {
                endIndex++;
            }

            _// Include the period in the current string_
            **if** (endIndex < encodedTranscript.length) {
                endIndex++;
            }

            _// Add the current chunk to the stringsArray_
            **const** chunk = encodedTranscript.slice(currentIndex, endIndex);
            stringsArray.push(decode(chunk));

            currentIndex = endIndex;
        }

        **return** stringsArray;
    }

    **const** encoded = encode(transcript);
    **const** stringsArray = splitTranscript(encoded, maxTokens);
    **return** **await** sendToChat(stringsArray);

    _// Function to send transcript string(s) to Chat API_
    **async** **function** sendToChat(stringsArray) {
        **const** resultsArray = [];

        **for** (**let** arr **of** stringsArray) {
            _// Define the prompt_
            **const** prompt = `Analyze the transcript provided below, then provide the following:

Key “title:” - add a title.
Key “summary” - create a summary.
Key “main_points” - add an array of the main points. Limit each item to 100 words, and limit the list to 10 items.
Key “action_items:” - add an array of action items. Limit each item to 100 words, and limit the list to 5 items.
Key “follow_up:” - add an array of follow-up questions. Limit each item to 100 words, and limit the list to 5 items.
Key “stories:” - add an array of stories, examples, or cited works found in the transcript. Limit each item to 200 words, and limit the list to 5 items.
Key “arguments:” - add an array of potential arguments against the transcript. Limit each item to 100 words, and limit the list to 5 items.
Key “related_topics:” - add an array of topics related to the transcript. Limit each item to 100 words, and limit the list to 5 items.
Key “sentiment” - add a sentiment analysis

Ensure that the final element of any array within the JSON object is not followed by a comma.

Transcript:

${arr}`;

            **let** retries = 3;
            **while** (retries > 0) {
                **try** {
                    **const** completion = **await** openai.chat.completions.create({
                        model: "gpt-3.5-turbo",
                        messages: [
                            { role: "user", content: prompt },
                            {
                                role: "system", content: `You are an assistant that only speaks JSON. Do not write normal text.

Example formatting:
{
“title”: “Notion Buttons”,
“summary”: “A collection of buttons for Notion”,
“action_items”: [
“item 1”,
“item 2”,
“item 3”
],
“follow_up”: [
“item 1”,
“item 2”,
“item 3”
],
“arguments”: [
“item 1”,
“item 2”,
“item 3”
],
“related_topics”: [
“item 1”,
“item 2”,
“item 3”
],
“sentiment”: “positive”
}`
}
],
temperature: 0.2
});

                    **const** messageContent = completion.choices[0].message.content;
                    **const** parsedContent = JSON.parse(messageContent);

                    resultsArray.push({
                        title: parsedContent.title,
                        summary: parsedContent.summary,
                        main_points: parsedContent.main_points,
                        action_items: parsedContent.action_items,
                        follow_up: parsedContent.follow_up,
                        stories: parsedContent.stories,
                        arguments: parsedContent.arguments,
                        related_topics: parsedContent.related_topics,
                        sentiment: parsedContent.sentiment
                    });

                    **break**;
                } **catch** (error) {
                    **if** (error **instanceof** OpenAI.APIError && error.status === 500) {
                        retries--;
                        **if** (retries === 0) {
                            **throw** **new** Error("Failed to get a response from OpenAI Chat API after 3 attempts.");
                        }
                        console.log("OpenAI Chat API returned a 500 error. Retrying...");
                    } **else** {
                        **throw** error;
                    }
                }
            }
        }

        **return** resultsArray;
    }
}

});

I’m sorry to hear that you’re experiencing issues with your code. It’s difficult to pinpoint the exact cause of the syntax error without more information. Can you provide any additional details about the error message or the specific part of the code that’s causing the issue? This will help me better understand the problem and provide a solution.

SyntaxError
Unexpected end of JSON input

Hi , the error is: SyntaxError Unexpected end of JSON input

I’m sorry to hear you’re encountering a “SyntaxError: Unexpected end of JSON input” error. This error typically occurs when you’re trying to parse an incomplete or improperly formatted JSON string using JSON.parse(). To help you resolve this issue, I’ll need more information about the code you’re working with. Can you please provide the relevant code snippet where the error is occurring? This will help me better understand the problem and provide a solution.

the code is provided in the first message of this thread

I apologize for the confusion earlier. I don’t see any code provided in the previous messages. Can you please share the code snippet where you’re encountering the “SyntaxError: Unexpected end of JSON input” error? This will help me better understand the issue and provide a solution.

of course, here’s the code:

import OpenAI from "openai";
import { encode, decode } from "gpt-3-encoder";

export default defineComponent({
    props: {
        openai: {
            type: "app",
            app: "openai",
        }
    },

    async run({ steps, $ }) {
        // Import the transcript from the previous step
        const transcript = steps.create_transcription.$return_value.transcription;

        // Set the max number of input tokens
        const maxTokens = 2000;

        // Initialize OpenAI
        const openAIkey = this.openai.$auth.api_key;
        const openai = new OpenAI({ apiKey: openAIkey });

        // Split the transcript into shorter strings if needed, based on GPT token limit
        function splitTranscript(encodedTranscript, maxTokens) {
            const stringsArray = [];
            let currentIndex = 0;

            while (currentIndex < encodedTranscript.length) {
                let endIndex = Math.min(currentIndex + maxTokens, encodedTranscript.length);

                // Find the next period
                while (endIndex < encodedTranscript.length && decode([encodedTranscript[endIndex]]) !== ".") {
                    endIndex++;
                }

                // Include the period in the current string
                if (endIndex < encodedTranscript.length) {
                    endIndex++;
                }

                // Add the current chunk to the stringsArray
                const chunk = encodedTranscript.slice(currentIndex, endIndex);
                stringsArray.push(decode(chunk));

                currentIndex = endIndex;
            }

            return stringsArray;
        }

        const encoded = encode(transcript);
        const stringsArray = splitTranscript(encoded, maxTokens);
        return await sendToChat(stringsArray);

        // Function to send transcript string(s) to Chat API
        async function sendToChat(stringsArray) {
            const resultsArray = [];

            for (let arr of stringsArray) {
                // Define the prompt
                const prompt = `Analyze the transcript provided below, then provide the following:
Key "title:" - add a title.
Key "summary" - create a summary.
Key "main_points" - add an array of the main points. Limit each item to 100 words, and limit the list to 10 items.
Key "action_items:" - add an array of action items. Limit each item to 100 words, and limit the list to 5 items.
Key "follow_up:" - add an array of follow-up questions. Limit each item to 100 words, and limit the list to 5 items.
Key "stories:" - add an array of stories, examples, or cited works found in the transcript. Limit each item to 200 words, and limit the list to 5 items.
Key "arguments:" - add an array of potential arguments against the transcript. Limit each item to 100 words, and limit the list to 5 items.
Key "related_topics:" - add an array of topics related to the transcript. Limit each item to 100 words, and limit the list to 5 items.
Key "sentiment" - add a sentiment analysis

Ensure that the final element of any array within the JSON object is not followed by a comma.

Transcript:

${arr}`;

                let retries = 3;
                while (retries > 0) {
                    try {
                        const completion = await openai.chat.completions.create({
                            model: "gpt-3.5-turbo",
                            messages: [
                                { role: "user", content: prompt },
                                {
                                    role: "system", content: `You are an assistant that only speaks JSON. Do not write normal text.
Example formatting:
{
    "title": "Notion Buttons",
    "summary": "A collection of buttons for Notion",
    "action_items": [
        "item 1",
        "item 2",
        "item 3"
    ],
    "follow_up": [
        "item 1",
        "item 2",
        "item 3"
    ],
    "arguments": [
        "item 1",
        "item 2",
        "item 3"
    ],
    "related_topics": [
        "item 1",
        "item 2",
        "item 3"
    ],
    "sentiment": "positive"
}`
                                }
                            ],
                            temperature: 0.2
                        });

                        const messageContent = completion.choices[0].message.content;
                        const parsedContent = JSON.parse(messageContent);

                        resultsArray.push({
                            title: parsedContent.title,
                            summary: parsedContent.summary,
                            main_points: parsedContent.main_points,
                            action_items: parsedContent.action_items,
                            follow_up: parsedContent.follow_up,
                            stories: parsedContent.stories,
                            arguments: parsedContent.arguments,
                            related_topics: parsedContent.related_topics,
                            sentiment: parsedContent.sentiment
                        });

                        break;
                    } catch (error) {
                        if (error instanceof OpenAI.APIError && error.status === 500) {
                            retries--;
                            if (retries === 0) {
                                throw new Error("Failed to get a response from OpenAI Chat API after 3 attempts.");
                            }
                            console.log("OpenAI Chat API returned a 500 error. Retrying...");
                        } else {
                            throw error;
                        }
                    }
                }
            }

            return resultsArray;
        }
    }
});

The provided code is a Pipedream component that uses the OpenAI API to analyze a transcript and generate a JSON object containing various information like title, summary, main points, action items, follow-up questions, stories, arguments, related topics, and sentiment analysis. The code imports the OpenAI package, GPT-3 encoder, and defines a Pipedream component with the OpenAI app as a prop. It then splits the transcript into smaller chunks if needed, sends each chunk to the OpenAI Chat API, and processes the results. Please note that the OpenAI package and GPT-3 encoder are not available in Pipedream’s environment. You should use the @pipedream/platform package with axios for making API requests. You can find an example of using OpenAI with Pipedream in the sources provided: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ props: { openai: { type: “app”, app: “openai”, }, }, async run({ steps, $ }) { return await axios($, { url: https://api.openai.com/v1/models, headers: { Authorization: Bearer ${this.openai.$auth.api_key}, }, }); }, });