Unauthorized 401 from Ruby Code

Not sure why I’m getting Unauthorized. It’s Ruby code:

require 'dotenv'
require 'openai'

Dotenv.load('app.env')
puts ENV['GPT_KEY']						# this works fine
client = OpenAI::Client.new(api_key: ENV['GPT_KEY'])

response = client.completions(
  parameters: {
    model: "text-davinci-002",
    prompt: "Hello, world!",
    max_tokens: 5
  }
)

puts response.code, response.body

I literally just made this key. The key shows up fine.

How can I fix this? Why is it returning 401?

Welcome to the dev forum!

I would check the key. Can you use the key from cURL or something to make sure it’s valid? I believe keys are tied to orgs now and have permissions too, so you might take a look at that…

This model was deprecated a long while ago too. Wrong model would likely throw another error, but might be something else to try…

2 Likes

Not sure how to fabricate a url using the key. The key was copied from the interface using their copy button tool. BTW same error using GPT-3.5. There are no restrictions on the key.

Gpt 3.5 is a Chat completions model, so you can’t use the same code

(in which case you want to use client.chat)

1 Like

I meant use cURL from the command line just to check the key. That would mean it’s how you’re including in your env…

2 Likes

Yeah, I’d do what Paul said and try curl. If it works there you may just need to compare the request sent from curl to the request sent from your ruby code.

If it doesn’t work with curl you know it isn’t your Ruby code and there is some other issue.

I build AI apps in Ruby and use the open-ai gem all the time so if you continue to have issues feel free to reach out. I would be happy to pair.

1 Like

OK it seems my quota has closed. But the key works, as it knows who I am. Used cURL and gpt-3.5-turbo. It also seems the API isn’t part of the Plus plan now. I guess that changed.

1 Like

Ah, there we go, getting to the root of the problem.

Weird it didn’t throw you a 429, though.

Anyway, the API and ChatGPT have always been separate products. The API came first, actually.

But yeah, you just need to drop in $5 or so, and you should be good to go.

Stop by if you run into any problems. We’ve got a wealth of information here too.

2 Likes

ChatGPT Plus has never included any API access.

Not sure how I had it before then. I thought 4.0 needed payment and had access to the API. It’s been some time since I used it.

A long, long, time ago OpenAI gave all new user accounts free API credits. First $18, then $5, now none.

They never really made it explicitly clear at the time, but creating any account with OpenAI triggered this free credit grant, so if you made an account for ChatGPT you’d get the credits at that time just as you would if you made an account in the API platform.

Many people actually had an issue where they would make an account on ChatGPT, get the free API credits without knowing it, and eventually find their way to the API dashboard months later only to discover,

  1. They had been granted free API credits, and
  2. Those credit had expired

It was a source of great frustration for many people.

So, my guess would be that when you created your account you received a free credit grant and at some point between them and now those credits had expired which is why you didn’t have API access without adding funds and why you may have been confused about the API access being connected to having a ChatGPT Plus subscription.

1 Like

Sounds on the money. I thought I had more credits.

1 Like