failing connection with azure openai with langchain

Riya Chaudhary 20 Reputation points
2024-04-17T13:44:07.64+00:00

Trying to connect to openai through langchain but running into error. All the connection variable match values on portal.

AzureOpenAI(deployment_name=self.deployment_name, temperature=self.temperature, max_tokens=self.max_tokens)

self.handle_error_response( openai.error.AuthenticationError: Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource.

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,072 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel FANG 700 Reputation points
    2024-09-23T01:15:37.4266667+00:00

    Here is an working example of langchain using AzureOpenAI's chat endpoint. There are a few ways to pass in endpoint details. Please try it out, your code snippet does not seem to have these details, it could be api key is not correct.

    import os
    from langchain.chat_models import AzureChatOpenAI
    from langchain.schema import HumanMessage
    
    # Set up your Azure OpenAI credentials
    os.environ["OPENAI_API_TYPE"] = "azure"
    os.environ["OPENAI_API_KEY"] = "ce886d51axxxxxxxx"
    os.environ["OPENAI_API_BASE"] = "https://aoai-xxxxxxxxxxx.openai.azure.com/"
    os.environ["OPENAI_API_VERSION"] = "2023-12-01-preview"  # Update if necessary
    
    # Initialize the Azure OpenAI LLM with LangChain
    llm = AzureChatOpenAI(
        deployment_name="gpt4o",  # Replace with your deployment name
        temperature=0.7,                         # Adjust the temperature as needed
        max_tokens=150                           # Set the maximum number of tokens
    )
    
    msg = HumanMessage(content="Explain step by step. How old is the president of USA?")
    print(llm(messages=[msg]))
    
    
    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.