I got help from ChatGPT to integrate Alexa Skill in a mini marathon
Tags: chatgpt, lambdafunction, aws, alexa
Published: 2023-02-24 04:25:11

Ash Wednesday, 10:12 PM:
— "Do you think I can integrate ChatGPT with Alexa by midnight?" I asked.
— "Yes. Let me help you there," my friend replied, sending me instructions he asked from the AI.

I laughed and started.

## Mini marathon
I dusted off my AWS account, "drafted" (of course, it was ChatGPT) a code and uploaded it to the Amazon Lambda Function.

> I didn't like that I had to upload the *node modules* too, but there are pros and cons. I won't elaborate.

I created the Skill on Amazon Developer Console.

I created the key to access the OpenAI API there. It's free too, by the way!

I added the endpoint to the Lambda Function, and in the Lambda Function, I added the skill ID as a trigger. Everything went smoothly.

```typescript
const ChatGPTCommandIntentHandler = {
  canHandle(handlerInput) {
    return (
      Alexa.getRequestType(handlerInput.requestEnvelope) === "IntentRequest" &&
      Alexa.getIntentName(handlerInput.requestEnvelope) ===
        "ChatGPTCommandIntent"
    );
  },
  async handle(handlerInput) {
    const command =
      handlerInput.requestEnvelope.request.intent.slots.command.value;

    const speechOutput = await queryChatGPT(command);

    console.log(speechOutput);

    return handlerInput.responseBuilder.speak(speechOutput).getResponse();
  },
};

const skillBuilder = Alexa.SkillBuilders.custom();

const handler = skillBuilder
  .addRequestHandlers(
    ChatGPTCommandIntentHandler,
    Alexa.CancelIntentHandler,
    Alexa.HelpIntentHandler,
    Alexa.StopIntentHandler,
    Alexa.NavigateHomeIntentHandler
  )
  .lambda();

export { handler };
```

In theory, I managed to do everything by midnight. It was a bit messy, but I did it.

Tonight, I tried to understand why everything is working fine, but Alexa can't understand voice commands. It doesn't detect the intent. It works very well via text, but I have to finish this job before I think about publishing the Skill or making the code available in full. If you're interested, talk to me.

It wasn't my intention to delve into the technical part here, but rather to demonstrate that you can use ChatGPT to produce more. Can you? Yes! But only up to page three.

## Reality
The reality is that I finished 80% of the work in 20% of the time, but there was a lot going wrong. 80% of the time was spent debugging and refining the part that I thought would be the easiest, the Interaction Model creation. Is it the Alexa Developer Console's fault? I don't know, but it's not as simple as it seems, and there's still a lot to adjust.

### Early opinion on the API
I had the impression that the OpenAI API is still quite weak compared to the ChatGPT itself, and this seems to be more about design choices than capacity. It could also be due to parameter adjustments. There is still a lot to learn about the API and I just made some quick adjustments to test it out.

```typescript
export async function queryChatGPT(query) {
  const prompt = query;
  const headers = {
    "Content-Type": "application/json",
    Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
  };
  const data = JSON.stringify({ // These parameters here control
    prompt: prompt,             // the characteristics of the responses.
    max_tokens: 50,             // Read the docs.
    temperature: 0.3,
    n: 1,
  });

  const response = await axios.post(endpoint, data, { headers: headers });
  const answer = response.data.choices[0].text.trim();
  const speechOutput = answer.split("\n")[0];

  return speechOutput;
}
```

> I'm using the endpoint `https://api.openai.com/v1/engines/davinci/completions`, not the `davinci-codex`.

## About the experience
Developing with complete AI assistance and using Google by choice was very interesting and dynamic.

However, there were moments when the ChatGPT misled me and made me delete functional code to try to implement something that didn't make sense.

For example, it wrote a Code Snippet that had something like `const chatGPT = new AWS.ChatGPT()`. I was happy thinking that Amazon had integrated Chat into the SDK, but later I felt stupid when I researched it.

It is an excellent liar, and if you do not proceed with caution, it can literally tell you that a pig's nose is a socket and you can believe it, and the pig will pay the price.

In summary, for now, the developer remains highly relevant. Architecture decisions need to be made, errors need to be debugged, parameters refined, and UX defined. There is still a world of things that cannot be delegated to AI.