microsoft / PubSec-Info-Assistant

Information Assistant, built with Azure OpenAI Service, Industry Accelerator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

another day another bug, OPENAI_API_KEY is not read from env variable

developer992 opened this issue · comments

  File "/root/.cache/pypoetry/virtualenvs/project-9TtSrW0h-py3.11/lib/python3.11/site-packages/openai/util.py", line 186, in default_api_key
    raise openai.error.AuthenticationError(
openai.error.AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = <API-KEY>', or you can set the environment variable OPENAI_API_KEY=<API-KEY>). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = <PATH>'. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.

oh but it is

# echo $OPENAI_API_KEY
8***************************6
root@59eb15ce51c0:/app#

app/backend/approaches/chatreadretrieveread.py:173:174

This fails with auth error:

        chat_completion = openai.ChatCompletion.create(
            deployment_id=self.chatgpt_deployment,
            model=self.model_name,
            messages=messages,
            temperature=0.0,
            # max_tokens=32, # setting it too low may cause malformed JSON
            max_tokens=100,
            n=1)

and needs this:

        chat_completion = openai.ChatCompletion.create(
            api_key=os.getenv("OPENAI_API_KEY"),
            deployment_id=self.chatgpt_deployment,
            model=self.model_name,
            messages=messages,
            temperature=0.0,
            # max_tokens=32, # setting it too low may cause malformed JSON
            max_tokens=100,
            n=1)

not sure how it works for microsoft guys but it clearly doesn't work without it

The openai class takes the key value on setup not on calls to ChatCompletion()...

In app/backend/app.py you will find

# Used by the OpenAI SDK
openai.api_type = "azure"
openai.api_base = ENV["AZURE_OPENAI_ENDPOINT"]
if ENV["AZURE_OPENAI_AUTHORITY_HOST"] == "AzureUSGovernment":
    AUTHORITY = AzureAuthorityHosts.AZURE_GOVERNMENT
else:
    AUTHORITY = AzureAuthorityHosts.AZURE_PUBLIC_CLOUD
openai.api_version = "2023-12-01-preview"
openai.api_key = ENV["AZURE_OPENAI_SERVICE_KEY"]