Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}} encountered when running langchain program with Azure AI

Peter 0 Reputation points
2024-09-23T12:03:04.6666667+00:00

I encountered below NotFoundError when running langchain tutorial program (code: model.invoke(messages). Please help and advise, thanks.

NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}

LangChain tutorial program:


import os
os.environ["AZURE_OPENAI_API_KEY"] = "d957dxxxxxxxxxxxxxxxxxxxxxxxxx18438ba99f4b7"
os.environ["AZURE_OPENAI_ENDPOINT"] = "https://xxxxxxxxxx.cognitiveservices.azure.com/"
os.environ["AZURE_OPENAI_DEPLOYMENT"] = "Microsoft.CognitiveServicesAllInOne-20240775798778"
os.environ["AZURE_OPENAI_API_VERSION"] = "2021-04-30"

from langchain_openai import AzureChatOpenAI

model = AzureChatOpenAI(
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
    azure_deployment=os.environ["AZURE_OPENAI_DEPLOYMENT"],
    openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"]
)

from langchain_core.messages import HumanMessage, SystemMessage
messages = [
    SystemMessage(content="Translate the following from English into Italian"),
    HumanMessage(content="hi!"),
]
model.invoke(messages)

Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,831 questions
{count} votes

1 answer

Sort by: Most helpful
  1. santoshkc 8,690 Reputation points Microsoft Vendor
    2024-09-23T15:15:00.8+00:00

    Hi @Peter,

    Thank you for reaching out to Microsoft Q&A forum!

    I tried to repro the issue with the given code below and able to run the program:

    import os
    from langchain_openai import AzureChatOpenAI
    from langchain_core.messages import HumanMessage, SystemMessage
    
    # Set your Azure OpenAI credentials in environment variables
    os.environ["AZURE_OPENAI_API_KEY"] = "<KEY>"
    os.environ["AZURE_OPENAI_ENDPOINT"] = "https://<ENDPOINT>.openai.azure.com/" # Ensure this is correct
    os.environ["AZURE_OPENAI_DEPLOYMENT"] = "gpt-4o-mini" # Use your actual deployment name
    os.environ["AZURE_OPENAI_API_VERSION"] = "2023-05-15" # Check if this version is supported
    
    # Initialize the AzureChatOpenAI model
    model = AzureChatOpenAI(
        azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
        azure_deployment=os.environ["AZURE_OPENAI_DEPLOYMENT"],
        openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"]
    )
    
    # Create system and human messages
    messages = [
        SystemMessage(content="Translate the following from English into Italian"),
        HumanMessage(content="hi!")
    ]
    response = model.invoke(messages)
    
    # Print the translation
    print("Translation:", response)
    

    Output:
    User's image

    Hope this helps. Do let us know if you any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful.


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.