openAI authentication error

Tharun Viswanathan 0 Reputation points
2024-09-27T19:33:46.5133333+00:00

I have a a streamlit model running (code attached). it was initially working but then today it gave an error

openai.AuthenticationError: Error code: 401 - {'statusCode': 401, 'message': 'Unauthorized. Access token is missing, invalid, audience is incorrect (https://cognitiveservices.azure.com), or have expired.'}

i regenerated the tokens, redeployed the models ( gpt-35-turbo-16k and text-embedding-3-large). But that still didn't fix the problem. I clicked on the uri endpoints from the openai studio and it seemed to have returned a 404, resource not found. Im unsure on what to do to rectify the issue

import os
from dotenv import load_dotenv
from llama_index.llms.azure_openai import AzureOpenAI
from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding
import pickle
from llama_index.core import Settings
import streamlit as st
import time
# Load env variables
@st.cache_resource
def load_env_vars():
    load_dotenv()
    api_key = os.getenv("OPENAI_API_KEY")
    azure_endpoint = os.getenv("OPENAI_API_ENDPOINT")
    api_version = os.getenv("OPENAI_API_VERSION")
    print("env variables successfully loaded")
    return api_key, azure_endpoint, api_version
api_key, azure_endpoint, api_version = load_env_vars()
# Load pickle file
@st.cache_resource
def load_index():
    with open("./bystander_index.pkl", 'rb') as file:
        index = pickle.load(file)
    print("Pickle successfully loaded")
    query_engine = index.as_query_engine()
    return query_engine
query_engine = load_index()
st.header("Chat with the Bystander")
if "messages" not in st.session_state.keys(): # Initialize the chat message history
    st.session_state.messages = [
        {"role": "assistant", "content": "Ask me a question about the Bystander!"}
    ]
if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})
for message in st.session_state.messages: # Display the prior chat messages
    with st.chat_message(message["role"]):
        st.write(message["content"])
# If last message is not from assistant, generate a new response
if st.session_state.messages[-1]["role"] != "assistant":
    with st.chat_message("assistant"):
        with st.spinner("Thinking..."):
            t_0 = time.time()
            answer = query_engine.query(st.session_state.messages[-1]["content"])
            print(f'Time taken for that is {time.time() - t_0}')
            st.write(answer.response)
            message = {"role": "assistant", "content": answer.response}
            st.session_state.messages.append(message) # Add response to message history
Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,061 questions
{count} votes

1 answer

Sort by: Most helpful
  1. YutongTie-MSFT 51,726 Reputation points
    2024-09-29T08:54:57.2466667+00:00

    The openai.AuthenticationError: Error code: 401 indicates that there’s an issue with your authentication when trying to access the OpenAI API.

    Please make sure you have check API Key if correct as env variables/ if expires, check usage and rate limits, also check on API version (not model version), check the endpoint which should be something like - xxxxxx.openai.azure.com/

    If leverage third party service, please make sure everything has been input correct.

    Feel free to open a new thread if you see the same issue.

    Regards,

    Yutong

    -Please kindly accept the answer if you feel helpful to support the community, thanks a lot.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.