I’m trying to use the OpenAI API to translate a text using the GPT-4 model, but I’m encountering an error related to the API key configuration. Here’s the code I’m using:
from openai import OpenAI
import os
import openai
# Set your API key
client = OpenAI(
api_key = "sk-****",
)
# Text to translate
text = "I'm feeling intrigued!"
# Make a request to translate the text
response = openai.completions.create(
model="text-davinci-003", # GPT-4 model
prompt=f"What is the feeling?: '{text}'",
max_tokens=60
)
# Print the translation generated by the model
print(response.choices[0].text.strip())
File "C:UsersrAppDataLocalProgramsPythonPython312Libsite-packagesopenai_client.py", line 104, in __init__
raise OpenAIError(
openai.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
I’ve double-checked the API key, and it’s correct. Does anyone know what might be causing this error and how I can fix it?
I attempted to use the OpenAI API to translate a text using the GPT-4 model in Python. I followed the documentation and set up the API key correctly. However, when I ran the code, I encountered an error related to the API key configuration.
I expected the code to successfully translate the text using the specified GPT-4 model and print the translated output. Instead, I received an error message stating that the API key must be set either by passing it to the client or by setting the OPENAI_API_KEY environment variable.
I’ve checked and confirmed that the API key is accurate, so I’m unsure why this error is occurring. Any insights or suggestions on how to resolve this issue would be greatly appreciated.
Raphael Borges is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.