How to Resolve OPENAI_API_KEY Error when Setting Up Speech to Text to Notion Using OpenAI Chat?

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

Hi everyone… Not sure if this question has been answered before but I am trying to set up Speech to Text to Notion and is stuck on the open_ai chat. This is my code

import OpenAI from 'openai';
const openai = new OpenAI({
  apiKey: process.env.APIKEY // This is also the default, can be omitted
});

const completion = openai.createChatCompletion({
  model: "gpt-4",
  messages: [
    {role: "user", content: prompt
    },
    {role: "system", content: `You are an assistant that only speaks JSON. Do not write normal text.

Example formatting: {
        "title": "Notion Buttons",
        "summary": "A collection of buttons for Notion",
        "action_items": [
          "item 1",
          "item 2",
          "item 3"
        ],
        "follow_up": [
          "item 1",
          "item 2",
          "item 3"
        ],
        "arguments": [
          "item 1",
          "item 2",
          "item 3"
        ],
        "related_topics": [
          "item 1",
          "item 2",
          "item 3"
        ]
"sentiment": "positive"
      }
  `
    }
  ],
  temperature: 0.2
}).then(function(result) { completion = result });

And this is the error I get
The OPENAI_API_KEY environment variable is missing or empty; either provide it, or instantiate the OpenAI client with an apiKey option, like new OpenAI({ apiKey: 'my apiKey' }).

Hi , I believe the part is incorrect

import OpenAI from 'openai';
const openai = new OpenAI({
  apiKey: ...
});

It should be

import OpenAI from 'openai';
const openai = new OpenAI({
  apiKey: "FThC81oZ..."
});

If the part FThC81oZ… is your real Open AI API Key, PLEASE DELETE YOUR MESSAGE, as it is your sensitive information, others might be able to use your Chat GPT account

Shoots! Thanks for reminding me.

Anyway, tried your fix but it has a new error now

image.png

On your code I see no mention of prompt, I think you can share your whole code with our powerful bot , it can help you with code issue

I noticed that the prompt variable is missing in your code. To help you better, please share the complete code, including the part where you define the prompt variable. This will allow me to understand the context and provide a more accurate solution.

that’s the complete code though

import OpenAI from 'openai';
const openai = new OpenAI({
  apiKey: process.env.APIKEY // This is also the default, can be omitted
});

const completion = openai.createChatCompletion({
  model: "gpt-4",
  messages: [
    {role: "user", content: prompt
    },
    {role: "system", content: `You are an assistant that only speaks JSON. Do not write normal text.

Example formatting: {
        "title": "Notion Buttons",
        "summary": "A collection of buttons for Notion",
        "action_items": [
          "item 1",
          "item 2",
          "item 3"
        ],
        "follow_up": [
          "item 1",
          "item 2",
          "item 3"
        ],
        "arguments": [
          "item 1",
          "item 2",
          "item 3"
        ],
        "related_topics": [
          "item 1",
          "item 2",
          "item 3"
        ]
"sentiment": "positive"
      }
  `
    }
  ],
  temperature: 0.2
}).then(function(result) { completion = result });

that’s the complete code

I see that you’re trying to use the OpenAI API to create a chat completion. However, there are a couple of issues in your code: 1. The gpt-4 model does not exist. You should use the gpt-3.5-turbo model instead. 2. The prompt variable is not defined in your code. You need to define it before using it in the messages array. 3. The OpenAI API has been updated to use the Chat API, so you should use the openai.ChatCompletion.create method instead of openai.createChatCompletion. Here’s an updated version of your code: import OpenAI from 'openai'; const openai = new OpenAI({ apiKey: process.env.APIKEY }); const prompt = "Your prompt text here"; const completion = await openai.ChatCompletion.create({ model: "gpt-3.5-turbo", messages: [ {role: "user", content: prompt}, {role: "system", content: `You are an assistant that only speaks JSON. Do not write normal text. Example formatting: { "title": "Notion Buttons", "summary": "A collection of buttons for Notion", "action_items": [ "item 1", "item 2", "item 3" ], "follow_up": [ "item 1", "item 2", "item 3" ], "arguments": [ "item 1", "item 2", "item 3" ], "related_topics": [ "item 1", "item 2", "item 3" ] "sentiment": "positive" } `} ], temperature: 0.2 }); console.log(completion.choices[0].message.content); Make sure to replace "Your prompt text here" with the actual prompt you want to use.