What's Causing My Error in Pipedream and APIs Code for a Simple Test Output?

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

Hey there, I am only starting with using pipedream and APIs. I wanted to do something relatively simple to start with, just to see that I get output. So I wrote the following (with some help): import { axios } from@pipedream/platform”;

export default defineComponent({
props: {
openai: {
type: “app”,
app: “openai”,
},
},
async run({ steps, $ }) {
const prompt = “Once upon a time”; // Your prompt
try {
const response = await axios($, {
method: “POST”,
url: https://api.openai.com/v1/chat/completions,
headers: {
“Content-Type”: “application/json”,
Authorization: Bearer ${process.env.Chat_GPT_Key},
},
data: {
model: ‘gpt-3.5-turbo’, // or any other chat model you want to use
messages: [
{
role: “system”,
content: prompt
}
],
max_tokens: 100,
},
});

  _//console.log(response.data.choices);_
  _//console.log(response.data.choices.length);_

  **if** (response.data.choices && response.data.choices.length > 0) {
    **return** response.data.choices[0].text.trim();
  } **else** {
    **return** "No response";
  }
} **catch** (error) {
  console.error("Error:", error);

  **return** "Error occurred";
}

},
});

But I got the following error message:

Error: at Object.run (file:///tmp/pdg/dist/code/344df51b492aa35465a6b414aacec3fdc0501e9edb90a7adc1bed46392bc674e/component.mjs:35:25)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at null.executeComponent (/var/task/launch_worker.js:287:22)
at MessagePort.messageHandler (/var/task/launch_worker.js:786:28)

Do you know where I have gone wrong?

can you help debug this error?

It seems like there’s no specific error or context provided in your question. Could you please share more details about the error you’re encountering? For example, it would be helpful to know: - What action or operation were you trying to perform when you encountered the error? - Are you working with a specific Pipedream component or API? - Can you share the error message or describe the issue in more detail? With more information, I’ll be better equipped to assist you in debugging the issue.

: Here is the code:

import { axios } from "@pipedream/platform";
export default defineComponent({
  props: {
    openai: {
      type: "app",
      app: "openai",
    },
  },
  async run({ steps, $ }) {
    const prompt = "Once upon a time"; // Your prompt
    try {
      const response = await axios($, {
        method: "POST",
        url: `https://api.openai.com/v1/chat/completions`,
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${process.env.Chat_GPT_Key}`,
        },
        data: {
          model: 'gpt-3.5-turbo', // or any other chat model you want to use
          messages: [
            {
              role: "system",
              content: prompt
            }
          ],
          max_tokens: 100,
        },
      });
      //console.log(response.data.choices);
      //console.log(response.data.choices.length);
      if (response.data.choices && response.data.choices.length > 0) {
        return response.data.choices[0].text.trim();
      } else {
        return "No response";
      }
    } catch (error) {
      console.error("Error:", error);
      return "Error occurred";
    }
  },
});

And here is the error:

Error:     at Object.run (file:///tmp/__pdg__/dist/code/344df51b492aa35465a6b414aacec3fdc0501e9edb90a7adc1bed46392bc674e/component.mjs:35:25)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at null.executeComponent (/var/task/launch_worker.js:287:22)
    at MessagePort.messageHandler (/var/task/launch_worker.js:786:28)

The error you’re encountering seems to be related to how you’re handling the authorization token and possibly the URL formatting in your axios request. Let’s address these issues step by step: 1. Authorization Token: In your code, you’re using process.env.Chat_GPT_Key to access the OpenAI API key. However, within the Pipedream environment, API keys for connected apps are accessed through the $auth object of the app prop. Since you’ve correctly set up the openai app prop, you should use this.openai.$auth.api_key to get the OpenAI API key. 2. URL Formatting: The URL in your axios request is enclosed in angle brackets (< and >). This is unnecessary and could potentially cause issues. You should remove these brackets. 3. Error Handling: While your error handling with try...catch is good practice, the error message logged might not provide enough detail. Consider logging error.response to get more insights if the error is coming from the axios request. Here’s a revised version of your code with these corrections: ``` import { axios } from “@pipedream/platform”; export default defineComponent({ props: { openai: { type: “app”, app: “openai”, }, }, async run({ steps, $ }) { const prompt = “Once upon a time”; // Your prompt try { const response = await axios($, { method: “POST”, url: https://api.openai.com/v1/chat/completions, // Removed angle brackets headers: { “Content-Type”: “application/json”, Authorization: Bearer ${this.openai.$auth.api_key}, // Corrected API key access }, data: { model: ‘gpt-3.5-turbo’, // or any other chat model you want to use messages: [ { role: “system”, content: prompt } ], max_tokens: 100, }, }); if (response.data.choices && response.data.choices.length > 0) { return response.data.choices[0].text.trim(); } else { return “No response”; } } catch (error) { console.error(“Error:”, error.response || error); // Improved error logging return “Error occurred”;