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;
}
}
});