skip to Main Content

I’m trying to use LangChain’s AzureOpenAI as below but getting this error.
Do you know how can I fix this?

openai.error.InvalidRequestError: Resource not found

# Import Azure OpenAI
from langchain.llms import AzureOpenAI
import openai
import os

os.environ["OPENAI_API_TYPE"] = "azure"
os.environ["OPENAI_API_KEY"] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
os.environ["OPENAI_API_BASE"] = "https://XXXXXX-openai.openai.azure.com/"
os.environ["OPENAI_API_VERSION"] = "2022-12-01"

llm = AzureOpenAI(
    openai_api_type="azure",
    deployment_name="text-davinci-003", 
    model_name="text-davinci-003") 

print(llm("hi"))

Use Azure OpenAI with LangChain framework

2

Answers


  1. I tried using below code with a sample Azure OpenAPI and it worked successfully.

    Code:-

    # Import Azure OpenAI
    from langchain.llms import AzureOpenAI
    import openai
    import os
    
    # os.environ["OPENAI_API_TYPE"] = "azure"
    os.environ["OPENAI_API_KEY"] = "<open-api-key>"
    os.environ["OPENAI_API_BASE"] = "https://xxxxx.openai.azure.com/"
    os.environ["OPENAI_API_VERSION"] = "2022-12-01"
    
    llm = AzureOpenAI(
        # openai_api_type="azure",
        deployment_name="text-davinci-003", 
        model_name="text-davinci-003") 
    
    print(llm("hi"))
    

    Output:-

    enter image description here

    Another output:-

    enter image description here

    Double check if your OpenAPI key and Azure Open AI Endpoint that you have entered in the os.env code is missing any string or characters. Make sure the endpoint you are using for Azure is correct and not invalid.

    You can verify the endpoint by visiting :-
    Azure OpenAI Studio > Playground > Code view or by visiting your OpenAI resource on azure in the Resource management section

    Refer this Github MS document for the details mentioned above and run the code of
    this github repo:- azure-docs/python.md at main ·
    MicrosoftDocs/azure-docs ·
    GitHub

    Get the deployment model from the model you deployed while creating
    the resource- Refer this MS document here-
    https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal Make sure the model name is correct as that also causes resource not found error like below:-
    Error:-

    openai.error.InvalidRequestError: The model `text-davin-043` does not exist
    

    enter image description here

    Additionally check if there’s any space or character after your api
    base key try deleting it and run the code again and refer other
    solutions mentioned in this MS forum regarding the same error code :-

    Open AI error: "InvalidRequestError: Resource not found". Please help
    to fix. – Microsoft
    Q&A

    Login or Signup to reply.
  2. What worked for me was removing the import of openai when using the langchain.llms.AzureOpenAI module.

    In you example, try removing line 3 import openai

    In my code, I also did not include openai_api_type="azure" since it is already set as an environment variable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search