Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16951

OpenAI API error: "The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY env variable"

$
0
0

I'm a little confuse about using openai in python, and need a little help to make this code work Tryed several solutions found on stackovfw, none of them are working. My goal is to make a python code which ask 2 questions to the user, and then chatgpt 3.5 found the answer and export a multiple choice .csv with it. I will then convert this in xml to import it in a moodle environnement.

Error message : OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

Environnement : MacOS/Thonny.

My level in python : begginer/noob, obviously.

My code :

import pandas as pd
# Configure OpenAI API keyfrom openai import OpenAIclient = OpenAI()openai.api_key = 'my secret key openai'# Prompt user for 2 questionsquestions = []for i in range(2):    question = input("Posez une question: ")    questions.append(question)# Use OpenAI to answer questionsanswers = []for question in questions:    response = client.Completions.create(        engine="gpt-3.5-turbo-instruct",        prompt=f"Question: {question}\nRéponse:",        max_tokens=1024,        n=1,        stop=None,        temperature=0.7,    )    answer = response.choices[0].text.strip()    answers.append(answer)# Create a pandas dataframe with questions and answersdf = pd.DataFrame({"Question": questions, "Réponse": answers})# Export dataframe to CSV filedf.to_csv("questions_reponses.csv", index=False)print("Le fichier CSV a été créé avec succès.")`

I've tried to set env variable OPENAI_API_KEY in my os, didn't work (always the same message), so I'm keep trying to set the key inside the code below. Don't know if my syntax is ok.


Viewing all articles
Browse latest Browse all 16951

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>