How to resolve authentication error when using Langchain with OpenAI Azure

Samirit Saha 25 Reputation points

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\, 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\, in Chroma.from_texts(cls, texts, embedding, metadatas, ids, collection_name, persist_directory, client_settings, client, collection_metadata, **kwargs)
    766         rbody, rcode,, rheaders, stream_error=stream_error
    767     )
    768 return resp

AuthenticationError: Incorrect API key provided: 8e7248f9********************ba39. You can find your API key at
Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
1,038 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Akash Chaudhary 5 Reputation points

    from the above error output, I can figure out that you're calling the 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 = "" 
    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(
    openai_api_base="Your base address should look like",
    openai_api_key="Your Azure OpenAI Key",

    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