I have a simple test code to test the OPENAI_API_KEY locally and in docker image.
The application runs fine locally but for docker image run, it gives error as:
"openai.AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: "sk-proj****"
Here is the testcode.py
from openai import Client
from dotenv import load_dotenv
import os
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
print(f"Key is : {OPENAI_API_KEY}")
openai_client = Client()
response = openai_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
)
Here is the Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000
CMD ["python", "testcode.py"]
The .env file in my project
OPENAI_API_KEY="<my_key>"
This is the run command I use to run the docker image:
docker run --env-file .env -it -p 8000:8000 testapp_1:v1
If I run this app locally:
It prints the key value and also gives the response from the llm object.
But if I run the docker image, it prints the api key correctly, but throws an Authentication Error.
On OpenAI site I checked the access permission for the api key. It is set to "all"
What can be the issue when I use the API key from my docker image?
Thanks in adavance!
The issue was that double quotes were used to specify OPEANAI_API_KEY in the .env file. If the key is written in double quotes, e.g.
OPENAI_API_KEY="sk-proj-...................."
it works in the desktop app, but it results in an INVALID_API_KEY error in a docker image.
I removed the double quotes from the .env file and specified the key as follows:
OPENAI_API_KEY=sk-proj-....................
Now it works both in desktop application as well as in docker image.
Thank you!