Langchain's AzureOpenAI error: Resource not found
Asked Answered
C

5

8

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

Cassady answered 19/4, 2023 at 1:13 Comment(0)
F
7

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.

Favourable answered 19/4, 2023 at 23:54 Comment(0)
R
1

Switching this line from

os.environ["OPENAI_API_VERSION"] = "2023-03-15-preview"

to

os.environ["OPENAI_API_VERSION"] = "2022-12-01"

fixed the error for me

Rabbinical answered 8/6, 2023 at 17:1 Comment(0)
E
0

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

Eskridge answered 19/4, 2023 at 7:14 Comment(0)
P
0

As devon mentioned, just remove the openai import

Polard answered 16/5, 2023 at 2:14 Comment(0)
R
0

Need to restart OS when set env in windows important

Richardricharda answered 16/6, 2023 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.