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!
arjava
8
that’s work for my error in python3.10
ganga
9
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
Anser
10
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

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
i think you don’t understand his question.
his prompt is not python script but native prompt linux
then :
$ openai -h
usage: openai [-h] [-v] [-b API_BASE] [-k API_KEY] [-o ORGANIZATION] {api,tools,wandb} …
positional arguments:
{api,tools,wandb}
api Direct API calls
tools Client side tools for convenience
wandb Logging with Weights & Biases
options:
-h, --help show this help message and exit
-v, --verbose Set verbosity.
-b API_BASE, --api-base API_BASE
What API base url to use.
-k API_KEY, --api-key API_KEY
What API key to use.
-o ORGANIZATION, --organization ORGANIZATION
Which organization to run as (will use your default organization if not specified)
ex: “openai -k my_api_key api fine_tunes.create -t /home/dataset_prepared.jsonl -m gpt-3.5-turbo”
1 Like
You should use it directly in code (without os.getenv() )
1 Like
you can solve this issue simply just by replacing your with code given below:
openai.api_key = “your-key”
1 Like
Try this out in your command line:
openai -k <YOUR_API_KEY> api fine_tunes.create -t “your_path_to_gpt_prepared.jsonl” -m
1 Like
Separating into different cell the execution in kaggle solved for me.
that’s work for my error Thank you
Hello,
I’m Anna and I am new in this area. I have the same error as you @mp .
When I try to finetune a model I get thes message: >
openai api fine_tunes.follow -i ft-xxxxxx
←[91mError:←[0m 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 the onbard page from openai for details, or email support@openai.com if you have any questions.
How did you solve the problem ?
Unfortunately I could not find it in your answers…
I would be very grateful for help
pg3001
25
In my case I basically had to move the
import os
os.environ["OPENAI_API_KEY"] = <OPENAI_API_KEY>
to the very top of the file ABOVE OTHER IMPORTS and it basically works.
2 Likes
That solved it for me - the import of openai itself had to come after the environment setting:
import os
os.environ['OPENAI_API_KEY'] = 'your key here'
import openai
blah blah blah...
I’m getting the error:
Listening on http://127.0.0.1:4370
! OPENAI_API_KEY is not valid. Generate a key at <https://platform.openai.com/account/api-keys>
Set the key in your .Renviron file usethis::edit_r_environ()
Warning: Error in purrr::map_chr: ℹ In index: 1.
Caused by error:
! Result must be length 1, not 0.
Despite having an API Key set
sankar
28
I’m using OpenAI in my Svelte kit web application. Added API key in .env and loads for server-side code.
const _apiKey = import.meta.env.VITE_OPENAI_API_KEY;
import OpenAI from "openai";
const openai = new OpenAI({apiKey:_apiKey });
Everything works fine during development, threads and messages are loading fine.
When I try to do a build for production (npm run build), I get the below error.
Error: 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 API Key’ })
Can someone help to solve this error?