Help with Setting Up Speech-to-Text "Thomas Frank Explains” tutorial, pls help

Hi, I am running into some trouble following the written tutorial of Thomas Frank’s “How I Use AI to take perfect notes…without typing” video and I trying to use the heavy code option and am getting stuck on the openai_chat step. I am getting the following error:

Code Error
SyntaxError: The requested module ‘openai’ does not provide an export named ‘Configuration’

The create transcription step worked fine for me.

Down below is the code I have in the openai_chat step:

import { Configuration, OpenAIApi } 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 = 8000

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

// 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)
const result = await sendToChat(stringsArray)
return result

// 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 an 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.createChatCompletion({
          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
});

        resultsArray.push(completion)
        break
      } catch (error) {
        if(error.response && error.response.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
}

},
})

Hi @thomasabraham28,

I’m sorry you’re having trouble with the OpenAI Chat step. The issue seems to be related to the import statement in your code. In Pipedream, you should use the axios constructor from the @pipedream/platform package to make HTTP requests. Here’s an example of how you can make a request to the OpenAI API using Pipedream’s axios:

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

export default defineComponent({
  props: {
    openai: {
      type: "app",
      app: "openai",
    },
  },
  async run({ steps, $ }) {
    const response = await axios($, {
      method: "POST",
      url: "https://api.openai.com/v1/engines/davinci-codex/completions",
      headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${this.openai.$auth.api_key}`,
      },
      data: {
        // Your request data here
      },
    });

    return response;
  },
});

Replace the // Your request data here comment with the appropriate data for your API request. This should resolve the error you’re encountering.
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.

Hi,

OpenAI just released a new version for their npm package with a breaking change, and whenever we deploy a workflow we use the most recently published package. Would you mind going through your Node.js code steps and suffixing the package version like this:

import { Configuration, OpenAIApi } from "openai@3.3.0";

More info here: v3 to v4 Migration Guide

Thanks

1 Like

Thank you @andrew! This resolved this issue, but then it brought a different issue of a 429 error. I searched the community and found this thread to help resolve this new error: