No API KEY provided

I am trying to fine tune a model. I am in the following line:

openai api fine_tunes.create -t <TRAIN_FILE_ID_OR_PATH> -m <BASE_MODEL>

When I try to run it, I get the following message:

No API key provided. You can set your API key in code using 'openai.api_key = ', or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = '. You can generate API keys in the OpenAI web interface.

I am using Replit, which stores the API key in “Secrets”.

How do I provide the API key?

1 Like

Hi @mp ,

You’ll have to use the environment variable for that. More info here

Just type openai.api_key = “{api_key}”

1 Like

I did this. I mean I keep the <API_KEY> in the variable (openai.api_key)… but still it is showing this error.

Error: No API key provided. You can set your API key in code using ‘openai.api_key = ’, or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with ‘openai.api_key_path = ’. You can generate API keys in the OpenAI web interface. See https://onboard.openai.com for details, or email support@openai.com if you have any questions.

Put your API key into a text file named something like this: “api_key.txt”
Then type openai.api_key_path=“api_key.txt”

You can also use base64 encryption to encrypt the api key, but you don’t need to.

2 Likes

If you run openai using command ! openai, you should set api-key on environtment variable of your OS or you can set using this script:

import os
os.environ[‘OPENAI_API_KEY’] = ‘your key’

1 Like

Create a new key and wait a little it will generate whole key and you will be able to copy it only once, if you loose u have to generate new one!

that’s work for my error in python3.10

I have tried both using api_key=
os.environ[“OPENAI_API_KEY”] = api_key

and export OPENAI_API_KEY-
also.
Still i’m getting the same error.

Whether API Key should be used same or can be changed to new every time we run the trained model

1 Like

import openai
openai.api_key = “…”

!export OPENAI_API_KEY=“…”

!openai api fine_tunes.create -t /content/drive/My\ Drive/trainingPromptsAndCompletions1.jsonl -m davinci

I have tried both ways, still getting same error:

Error: No API key provided. You can set your API key in code using ‘openai.api_key = ’, or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with ‘openai.api_key_path = ’. You can generate API keys in the OpenAI web interface. See https://onboard.openai.com for details, or email support@openai.com if you have any questions.

Has anyone solved it yet?

I have never had a single problem using the environmental variable OPENAI_API_KEY, but I don’t use the OpenAI CLI and deleted it off my workstation within minutes after installing it.

HTH

:slight_smile:

I use PyCharm to write code on Windows 11, and I have the same problem.
The problem disappeared after restarting PyCharm.

No @ganga , i written this, it is working fine
os.environ[‘OPENAI_API_KEY’] = key

!openai api fine_tunes.create -t “sport2_prepared_train.jsonl” -v “sport2_prepared_valid.jsonl” --compute_classification_metrics --classification_positive_class “baseball” -m ada