langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Can not use AzureOpenAI

Open wYasha opened this issue 2 years ago • 16 comments

I want to use from langchain.llms import AzureOpenAI with the following configuration: os.environ["OPENAI_API_KEY"] = api_key_35 os.environ["OPENAI_API_VERSION"] = "2023-03-15-preview" Where api_key_35 is the key for AzureOpenAI.

The code is: llm = AzureOpenAI( max_tokens=1024 ,deployment_name = "gpt-35-turbo" ,openai_api_type = "azure" ,model_name="gpt-35-turbo" ) The returned result is: openai.error.AuthenticationError: Incorrect API key provided: ********************. You can find your API key at https://platform.openai.com/account/api-keys.

I changed the configuration to: os.environ["OPENAI_API_KEY"] = api_key_35 os.environ["OPENAI_API_BASE"] = api_base_35 os.environ["OPENAI_API_VERSION"] = "2023-03-15-preview" Where api_key_35 is the key for AzureOpenAI.

The code is: llm = AzureOpenAI( max_tokens=1024 ,deployment_name = "gpt-35-turbo" ,openai_api_type = "azure" ,model_name="gpt-35-turbo" )

The returned result is: openai.error.InvalidRequestError: Resource not found If I use the key for OpenAI instead of AzureOpenAI, it runs successfully. Why is there an error?

If I use openai.ChatCompletion.create,it's worked

wYasha avatar Apr 25 '23 10:04 wYasha

The following parameters need to be specified os.environ["OPENAI_API_TYPE"] = "azure" os.environ["OPENAI_API_BASE"] = "https://[your-domain].openai.azure.com/"

herove avatar Apr 25 '23 11:04 herove

I changed import,use:from langchain.chat_models import AzureChatOpenAI after specified,return : openai.error.InvalidRequestError: The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.

wYasha avatar Apr 25 '23 11:04 wYasha

I have the same problem for theAzureOpenAI module. Sending requests using it results in a 404 Resource not found as per the previous comments. Posting directly to the same instance without using LangChain does still work. Any help would be appreciated!

mrvoh avatar Apr 25 '23 14:04 mrvoh

Hi, I'm running into the same issue where GPT-3 Model works but 3.5 and 4 is not working. Any help on this would be appreciated.

amith-ajith avatar Apr 26 '23 05:04 amith-ajith

After some debugging, I found that the APIRequestor created in the AzureOpenAI object has an attribute api_type that seems to default to ApiType.OPEN_AI, which should be ApiType.AZURE. This behavior occurs when configuring everything in line with the documentation - specifically: setting os.environ["OPENAI_API_TYPE"] = "azure" does not solve this problem.

For people running into the same issue, I found a somewhat hacky workaround, which is setting the api_type and api_version via the model_kwargs as below:

self.llm = AzureOpenAI( deployment_name=llm_name, model_name=llm_name, temperature=0.75, model_kwargs={"api_type": "azure", "api_version": "2022-12-01"}, )

mrvoh avatar Apr 26 '23 07:04 mrvoh

@mrvoh sorry if this is a silly question but instead of

llm = AzureOpenAI( deployment_name=llm_name, model_name=llm_name, temperature=0.75, model_kwargs={"api_type": "azure", "api_version": "2022-12-01"}, )

you have self.llm

is this because you are utilizing a function call?

sadly this solution did not work for me.

from langchain.llms import AzureOpenAI import openai import os

os.environ["OPENAI_API_TYPE"] = "azure" os.environ["OPENAI_API_KEY"] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" os.environ["OPENAI_API_BASE"] = "https://xxxxxxxxxxxxxx.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", model_kwargs={"api_type": "azure", "api_version": "2022-12-01"})

llm("tell me a joke")

still resulted in

File "C:\Python311\Lib\site-packages\openai\api_requestor.py", line 683, in _interpret_response_line raise self.handle_error_response( openai.error.InvalidRequestError: Unrecognized request argument supplied: openai_api_type

sqlballs avatar Apr 26 '23 12:04 sqlballs

It may have been the first attribute. Took it out, this worked for me. Thank you @mrvoh!!!

llm = AzureOpenAI(deployment_name="text-davinci-003", model_name="text-davinci-003",model_kwargs={"api_type": "azure", "api_version": "2022-12-01"})

sqlballs avatar Apr 26 '23 12:04 sqlballs

upgrade to 148, same issue. here is my code:

  1. use "from langchain.chat_models import AzureChatOpenAI" :

from langchain.document_loaders import DirectoryLoader from langchain.text_splitter import CharacterTextSplitter from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores import Chroma from langchain import VectorDBQA import WechatConfig import os import urllib.request import urllib.parse from langchain.chat_models import AzureChatOpenAI

os.environ["OPENAI_API_KEY"] = Config.api_key_35 os.environ["OPENAI_API_BASE"] = Config.api_base_35 os.environ["OPENAI_API_VERSION"] = "2023-03-15-preview" documents_dir = './documents'

def down_load_file(file_url, file_name): encoded_url = urllib.parse.quote(file_url, safe=':/') local_file_path, _ = urllib.request.urlretrieve(encoded_url, os.path.join(documents_dir, file_name)) read_file(file_name)

def read_file(file_name):
embeddings = OpenAIEmbeddings() loader = DirectoryLoader(documents_dir, glob=file_name)

documents = loader.load()

text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)
texts = text_splitter.split_documents(documents)    

llm=AzureChatOpenAI(
    temperature=0.3,
    model_name="gpt-3.5-turbo",
    max_tokens=2048,
    deployment_name="gpt-35-turbo",
    model_kwargs={"api_type": "azure", "api_version": "2023-03-15-preview"},
)
print(llm)  

docsearch = Chroma.from_documents(texts, embeddings)

qa = VectorDBQA.from_chain_type(llm=llm, chain_type="stuff", vectorstore=docsearch,return_source_documents=True)

result = qa({"query": "hello"})
print(result['result'])

it returns: openai.error.InvalidRequestError: The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.

  1. use "from langchain.llms import AzureOpenAI" :

from langchain.document_loaders import DirectoryLoader from langchain.text_splitter import CharacterTextSplitter from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores import Chroma from langchain.llms import AzureOpenAI from langchain import VectorDBQA import Config import os import urllib.request import urllib.parse

os.environ["OPENAI_API_KEY"] = Config.api_key_35 os.environ["OPENAI_API_BASE"] = Config.api_base_35 os.environ["OPENAI_API_VERSION"] = "2023-03-15-preview" documents_dir = './documents'

def down_load_file(file_url, file_name): encoded_url = urllib.parse.quote(file_url, safe=':/') local_file_path, _ = urllib.request.urlretrieve(encoded_url, os.path.join(documents_dir, file_name)) read_file(file_name)

def read_file(file_name):
embeddings = OpenAIEmbeddings() loader = DirectoryLoader(documents_dir, glob=file_name)

documents = loader.load()

text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)
texts = text_splitter.split_documents(documents)    

llm=AzureOpenAI(
    temperature=0.3,
    model_name="gpt-3.5-turbo",
    max_tokens=2048,
    deployment_name="gpt-35-turbo",
    model_kwargs={"api_type": "azure", "api_version": "2023-03-15-preview"},
)
print(llm)

docsearch = Chroma.from_documents(texts, embeddings)

qa = VectorDBQA.from_chain_type(llm=llm, chain_type="stuff", vectorstore=docsearch,return_source_documents=True)
result = qa({"query": "hello"})
print(result['result'])

it returns : openai.error.InvalidRequestError: Resource not found

  1. use "from langchain.llms import AzureOpenAI" without OPENAI_API_BASE :

from langchain.document_loaders import DirectoryLoader from langchain.text_splitter import CharacterTextSplitter from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores import Chroma from langchain.llms import AzureOpenAI from langchain import VectorDBQA import Config import os import urllib.request import urllib.parse

os.environ["OPENAI_API_KEY"] = Config.api_key_35 os.environ["OPENAI_API_VERSION"] = "2023-03-15-preview" documents_dir = './documents'

def down_load_file(file_url, file_name): encoded_url = urllib.parse.quote(file_url, safe=':/') local_file_path, _ = urllib.request.urlretrieve(encoded_url, os.path.join(documents_dir, file_name)) read_file(file_name)

def read_file(file_name):
embeddings = OpenAIEmbeddings() loader = DirectoryLoader(documents_dir, glob=file_name)

documents = loader.load()

text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)
texts = text_splitter.split_documents(documents)    

llm=AzureOpenAI(
    temperature=0.3,
    model_name="gpt-3.5-turbo",
    max_tokens=2048,
    deployment_name="gpt-35-turbo",
    model_kwargs={"api_type": "azure", "api_version": "2023-03-15-preview"},
)
print(llm)

docsearch = Chroma.from_documents(texts, embeddings)

qa = VectorDBQA.from_chain_type(llm=llm, chain_type="stuff", vectorstore=docsearch,return_source_documents=True)
result = qa({"query": "hello"})
print(result['result'])

it returns : openai.error.AuthenticationError: Incorrect API key provided: ********************. You can find your API key at https://platform.openai.com/account/api-keys.

If I use OpenAI key, not AzureOpenAI key, even I use "from langchain.llms import AzureOpenAI" or "from langchain.chat_models import AzureChatOpenAI", it worked. If I use AzureOpenAI key, and use openai.ChatCompletion.create(), it worked. I don't know why. Please help me, Any help on this would be appreciated.

wYasha avatar Apr 27 '23 00:04 wYasha

I am trying to implement Opensearch using langchain but facing the same issue. Here is my code.

import os
import openai
from langchain.llms import AzureOpenAI
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import OpenSearchVectorSearch
from langchain.document_loaders import TextLoader


openai.api_type = "azure"
openai.api_base = "https://xxxxxx.openai.azure.com/"
openai.api_version = "2022-12-01"
openai.api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"


embeddings = OpenAIEmbeddings()

docsearch = OpenSearchVectorSearch(index_name="address_search", embedding_function=embeddings, opensearch_url="https://admi:[email protected]")
# you can specify custom field names to match the fields you're using to store your embedding, document text value, and metadata
docs = docsearch.similarity_search("Who was asking about getting lunch today?", k=4,search_type="script_scoring", space_type="cosinesimil", vector_field="message_embedding", text_field="message", metadata_field="message_metadata")


But I am unable to connect and getting this error: InvalidRequestError: The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.

Can anyone help me?

ZohaibRamzan avatar May 04 '23 14:05 ZohaibRamzan

Hi team, I am trying to use azureopenAI as llm in RetrievalQA. I am using latest version of langchain (0.0.162). Still i am getting below error. 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.

Here is my sample code.

from langchain.chains import RetrievalQA
from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.chat_models import AzureChatOpenAI
from langchain.schema import HumanMessage

BASE_URL = "BASE_URL_HERE"
API_KEY = "API_KEY_HERE"
DEPLOYMENT_NAME = "gpt-4"
llm = AzureChatOpenAI(
    openai_api_base=BASE_URL,
    openai_api_version="2023-03-15-preview",
    deployment_name=DEPLOYMENT_NAME,
    openai_api_key=API_KEY,
    openai_api_type = "azure",
)
#Proof that LLM is working without Indexing/embedding features.
print("----------------------------")
print(llm([HumanMessage(content="Translate this sentence from English to French. I love programming.")]))
print("----------------------------")

loader = TextLoader('state_of_the_union.txt', encoding='utf8')
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)
embeddings = OpenAIEmbeddings()
db = Chroma.from_documents(texts, embeddings)
retriever = db.as_retriever()
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
query = "What did the president say about Ketanji Brown Jackson"
qa.run(query)

Sample text file path I used :https://github.com/hwchase17/langchain/blob/master/docs/modules/state_of_the_union.txt

With the same llm, I am able to run basic chat completions.

Please help!

ChennaEda avatar May 09 '23 11:05 ChennaEda

Issue got fixed with below embeddings instance . embeddings =OpenAIEmbeddings( model=EMBEDDING_MODEL, deployment=EMBEDDING_MODEL, openai_api_key=API_KEY, openai_api_base=BASE_URL, openai_api_type=API_YPE, openai_api_version=API_VERSION, chunk_size=1 )

ChennaEda avatar May 09 '23 15:05 ChennaEda

Still having the same issue, I'm using the latest version of langchain (0.0.179). Previous fixes do not work because the function now does not permit extra fields:

image

jsandlerus avatar May 24 '23 20:05 jsandlerus

@jsandlerus use like this https://github.com/hwchase17/langchain/issues/4925#issue-1715594468

ZohaibRamzan avatar May 25 '23 18:05 ZohaibRamzan

@ZohaibRamzan Thank you, I already had them set up including the openai variables too. Otherwise, I get an Authentication error. import os config_details = config

os.environ["OPENAI_API_KEY"] = config_details.OPENAI_API_KEY os.environ["OPENAI_API_TYPE"] = "azure" os.environ["OPENAI_API_BASE"] = config_details.OPENAI_API_BASE os.environ["OPENAI_API_VERSION"] = config_details.OPENAI_API_VERSION

#without this I get Authentication error openai.api_key = config_details.OPENAI_API_KEY openai.api_type = "azure" openai.api_base = config_details.OPENAI_API_BASE openai.api_version = config_details.OPENAI_API_VERSION

jsandlerus avatar May 25 '23 19:05 jsandlerus

I am suddenly getting this error with Azure Open AI (it was running fine till yesterday): 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. I have tried the above comments, tried resting the keys and create deployments again, I am getting error for all the functionalities using langchain and Azure OpenAI including general chat and embeddings

shubham184 avatar Jun 06 '23 10:06 shubham184

same issue here, [The API deployment for this resource does not exist.]

insomniacdoll avatar Jun 14 '23 03:06 insomniacdoll

I encountered the same error.

For my code, I was trying to use both embeddings and chat functionality. I had a resource for each under different URLs and Keys. I noticed that when I tried to call the chat function, the base URL for the embedding function was being utilised even though I had specified different key and URL details for both models.

If you find yourself with a similar use case and issue, I'd suggest having both model deployments under the same URL and Key. Once I did this I no longer had issues.

Sntrada avatar Jun 18 '23 13:06 Sntrada

This is what fixed it for me

def get_azure_open_ai_initialized(): openai.api_type = "azure" openai.api_version = "2023-05-15" config = dotenv_values(".env") openai.api_base = config['OPENAI_API_BASE'] # Your Azure OpenAI resource's endpoint value . openai.api_key = config["OPENAI_API_KEY"] openai.Deployment = 'gathering-embedding' return openai

def get_azure_openai_embedding(text: str, model="text-embedding-ada-002"): # new line characters impact performance, remove them text = text.replace("\n", " ")

openai_local_scope = get_azure_open_ai_initialized()
embedding = openai.Embedding.create(input=text, deployment_id="gathering-embedding")
print(embedding)
return embedding

sujitswaroop avatar Jun 28 '23 14:06 sujitswaroop

Hi, @wYasha! I'm Dosu, and I'm helping the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.

Based on my understanding, you were experiencing an error when using AzureOpenAI with the provided configuration and API key. You mentioned that you tried changing the configuration and API key, but the error persisted. Another user found a workaround by setting the api_type and api_version attributes in the model_kwargs parameter. However, it seems that some users are still experiencing the same issue and are seeking further assistance.

Before we close this issue, we wanted to check with you if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself or it will be automatically closed in 7 days.

Thank you for your understanding and contribution to the LangChain community!

dosubot[bot] avatar Sep 27 '23 16:09 dosubot[bot]