How to resolve authentication error when using Langchain with OpenAI Azure

Samirit Saha 75 Reputation points
2023-09-12T17:35:21.8533333+00:00

Even though I'm using the key generated in my keys and endpoint section of my Azure OpenAI instance. I am still getting the following error when I try to integrate it with Langchain:

---------------------------------------------------------------------------
AuthenticationError                       Traceback (most recent call last)
c:\Users\Samirit Saha\OneDrive\Desktop\langchainpdf.ipynb Cell 4 line 7
      4 from langchain.llms import AzureOpenAI
      6 embeddings = OpenAIEmbeddings()
----> 7 doc_search = Chroma.from_documents(texts,embeddings)
      8 chain = RetrievalQA.from_chain_type(llm=AzureOpenAI(model_kwargs={'engine':'gpt-35-turbo-16k'}),chain_type='stuff', retriever = doc_search.as_retriever())

File c:\Users\Samirit Saha\AppData\Local\Programs\Python\Python311\Lib\site-packages\langchain\vectorstores\chroma.py:612, in Chroma.from_documents(cls, documents, embedding, ids, collection_name, persist_directory, client_settings, client, collection_metadata, **kwargs)
    610 texts = [doc.page_content for doc in documents]
    611 metadatas = [doc.metadata for doc in documents]
--> 612 return cls.from_texts(
    613     texts=texts,
    614     embedding=embedding,
    615     metadatas=metadatas,
    616     ids=ids,
    617     collection_name=collection_name,
    618     persist_directory=persist_directory,
    619     client_settings=client_settings,
    620     client=client,
    621     collection_metadata=collection_metadata,
    622     **kwargs,
    623 )

File c:\Users\Samirit Saha\AppData\Local\Programs\Python\Python311\Lib\site-packages\langchain\vectorstores\chroma.py:576, in Chroma.from_texts(cls, texts, embedding, metadatas, ids, collection_name, persist_directory, client_settings, client, collection_metadata, **kwargs)
...
    766         rbody, rcode, resp.data, rheaders, stream_error=stream_error
    767     )
    768 return resp

AuthenticationError: Incorrect API key provided: 8e7248f9********************ba39. You can find your API key at https://platform.openai.com/account/api-keys.
Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
2,880 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Akash Chaudhary 5 Reputation points
    2023-09-13T11:56:18.08+00:00

    from the above error output, I can figure out that you're calling the https://platform.openai.com/ endpoint not the Azure OpenAI endpoint.

    Instead of importing AzureOpenAI from langchain.llms try AzureChatOpenAI as you're using gpt-35-turbo-16k model. Also install openai package if not installed and try to import the same.

    from langchain.chat_models import AzureChatOpenAI
    import openai
    

    Set up the openai type and base

    openai.api_key = "AzureOpenAI key"
    openai.api_base = "https://YOUR_RESOURCE_NAME.openai.azure.com/" 
    openai.api_type = 'azure'
    openai.api_version = '2023-03-15-preview'
    

    Now passing the llm to the chain use AzureChatOpenAI instead of AzureOpenAI

    llm = AzureChatOpenAI(
    deployment_name="gpt-35-turbo-16k",
    openai_api_base="Your base address should look like https://YOUR_RESOURCE_NAME.openai.azure.com/",
    openai_api_key="Your Azure OpenAI Key",
    openai_api_type="azure",
    openai_api_version="2023-03-15-preview")
    
    

    similarly you need to provide detail for OpenAIEmbeddings()

    If this answer has helped you solve your problem, please consider marking it as correct by clicking the "Mark as Answer" button. This will help others with similar questions find the solution more easily.
    Thank you!

    1 person found this answer helpful.
    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.