Trying to make a call to ChatGPT work using javascript

I have been trying to make a call to ChatGPT work from pipedream. The key should be fine but all i’m getting back is a “no response” message. I am trying to do something much more complicated but my logic was, work out how to do something relatively simple, and then go from there. But this is not working. Thanks for any help!

async function callChatGPT(prompt) {
try {
const response = await fetch(‘https://api.openai.com/v1/completions’, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’,
‘Authorization’: Bearer ${process.env.Chat_GPT_Key} // Replace YOUR_API_KEY with your actual API key
},
body: JSON.stringify({
model: ‘gpt-3.5-turbo’, // or any other model you want to use
prompt: prompt,
max_tokens: 2
})
});

const data = await response.json();
if (data.choices && data.choices.length > 0) {
  return data.choices[0].text.trim();
} else {
  return "No response";
}

} catch (error) {
console.error(‘Error:’, error);
return “Error occurred”;
}
}

// Example usage
const prompt = “Once upon a time”;
callChatGPT(prompt)
.then(response => {
console.log(“ChatGPT:”, response);
})
.catch(error => {
console.error(“Error:”, error);
});

Hi @orders,

It looks like you’re trying to make a call to the OpenAI (ChatGPT) API using a JavaScript fetch function, but you’re encountering issues with receiving a response. To integrate with OpenAI (ChatGPT) on Pipedream, you’ll need to use the Pipedream component model, which simplifies the process of making API calls by handling authentication and request setup for you.

Here’s how you can make a call to the ChatGPT API using a Pipedream component:

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
    const response = await axios($, {
      method: "POST",
      url: `https://api.openai.com/v1/completions`,
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${this.openai.$auth.api_key}`,
      },
      data: {
        model: 'gpt-3.5-turbo', // or any other model you want to use
        prompt: prompt,
        max_tokens: 2,
      },
    });

    if (response.choices && response.choices.length > 0) {
      return response.choices[0].text.trim();
    } else {
      return "No response";
    }
  },
});

This component uses the axios library provided by Pipedream to make HTTP requests. It’s configured to use the OpenAI (ChatGPT) app you’ve connected to your Pipedream account, which simplifies the authentication process. You don’t need to manually manage the API key as it’s automatically handled by the this.openai.$auth.api_key property.

Please replace the prompt variable with your desired input for the ChatGPT model.

If you’re still encountering issues or have further questions, please visit Support - Pipedream for more support options.

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.