Using API with node.js and p5

hey, there is a website I’d like to use his API in a P5 sketch I’m doing.
The API looks like that:

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

const response = await openai.createCompletion({
  model: "text-davinci-002",
  prompt: "Decide whether a Tweet's sentiment is positive, neutral, or negative.\n\nTweet: \"I loved the new Batman movie!\"\nSentiment:",
  temperature: 0,
  max_tokens: 60,
  top_p: 1,
  frequency_penalty: 0.5,
  presence_penalty: 0,
});

I tried looking up an answer but couldn’t find one.
does anyone have any idea?
thanks :slight_smile:

Hi,

And why you’re not just installing the The OpenAI Node.js library ?
ie: npm install openai

Cheers
— mnse

1 Like

Thank! I had no idea that exist…
How do I use that in the P5?

Hi,

P5 is client-side and node server-side.
Guess you should implement some server side openai interface which you can call from your p5 scripts.
In general it should work that way, but I’ve not done yet and therefore you’ve to try it on your own … :slight_smile:

Cheers
— mnse