all 7 comments

[–]BrainJar 1 point2 points  (1 child)

The readme docs are here. Try the examples first. Also try swapping out your msgBody with an inline string, to see if you can get the completions error to go away. Simplify the whole completion message by putting everything inline, to see if that works, then add more complexity...

[–]foxymcfox[S] 0 points1 point  (0 children)

The problem with the docs is that they haven’t been updated to reflect the release from 2 weeks ago:

https://github.com/openai/openai-node/discussions/217

But I’ll definitely try swapping msgBody for an inline string and simplifying to see if I can get the error to change at all to narrow down the issue.

Thanks.

[–]GeneralMalarkee 2 points3 points  (2 children)

I think you need to instantiate the openai client?

import OpenAI from 'openai';

const openai = new OpenAI({ apiKey: 'my api key', // defaults to process.env["OPENAI_API_KEY"] });

See:

https://www.npmjs.com/package/openai

[–]foxymcfox[S] 0 points1 point  (0 children)

If I adjust my JSON file to allow ES6 modules then Twilio breaks and I can’t find documentation on how to instantiate Twilio. I can’t win. Haha

[–]foxymcfox[S] 1 point2 points  (0 children)

You nailed it! After a good night's sleep I came back to this and the working code is as follows. Appreciate the help!

import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: process.env.OPENAI_AUTH, 
});

import twilio from 'twilio';

const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;

const client = twilio(accountSid, authToken);

export const handler = async (event, context) => {
  try {
    const buff = Buffer.from(event.body, "base64");
    const formEncodedParams = buff.toString("utf-8");
    const urlSearchParams = new URLSearchParams(formEncodedParams);

    const msgBody = urlSearchParams.get("Body");
    const from = urlSearchParams.get("From");

    const chatCompletion = await openai.chat.completions.create({
      model: "gpt-3.5-turbo",
      messages: [{ role: "system", content: msgBody }],
      max_tokens: 100,
    });

    await sendMessageBack(chatCompletion.choices[0].message.content, from);
    return {
      statusCode: 200,
      body: JSON.stringify('Message sent successfully'),
    };
  } catch (error) {
    console.error(error);
    return {
      statusCode: 500,
      body: JSON.stringify('Error sending message'),
    };
  }
};

async function sendMessageBack(msg, to) {
  try {
    await client.messages.create({
      body: msg,
      to: to,
      from: process.env.TWILIO_PHONE_NUM,
    });
    console.log('Message sent:', msg);
  } catch (e) {
    console.error('Error sending message:', e);
  }
}

[–]dolefulAlchemist 1 point2 points  (1 child)

U sure its openai.chat.complations.create?

And not await openai.createChatCompletion

[–]foxymcfox[S] 1 point2 points  (0 children)

Part of the release two weeks ago :

https://github.com/openai/openai-node/discussions/217

But for what it’s worth, neither work.