Closed trungtly closed 6 months ago
π€
Hello @trungtly! I'm Dosu, a friendly bot here to help you with your LlamaIndex issues while we wait for a human maintainer. I'm here to assist with bug solving, answer queries, and guide you on your journey as a contributor. I'm currently looking into your issue and I'll get back to you with a thorough response as soon as possible. Stay tuned!
you probably need to set your embed_model as AzureOpenAIEmbedding(api_key = **) and pass to service_context
That is what I did but it still raises the same error. Below is the full code for replication.
# set up Azure Cognitive Search
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents import SearchClient
from azure.core.credentials import AzureKeyCredential
import openai
# Index name to use
index_name = "test-index"
# Use index client to demonstrate creating an index
index_client = SearchIndexClient(
endpoint=settings["AZUREAI_SEARCH_ENDPOINT"],
credential=AzureKeyCredential(settings["AZUREAI_SEARCH_KEY"]),
)
# Use search client to demonstration using existing index
search_client = SearchClient(
endpoint=settings["AZUREAI_SEARCH_ENDPOINT"],
index_name=index_name,
credential=AzureKeyCredential(settings["AZUREAI_SEARCH_KEY"]),
)
# create index
from azure.search.documents import SearchClient
from llama_index.vector_stores import CognitiveSearchVectorStore
from llama_index.vector_stores.cogsearch import (
IndexManagement,
MetadataIndexFieldType,
CognitiveSearchVectorStore,
)
# Example of a complex mapping, metadata field 'theme' is mapped to a differently name index field 'topic' with its type explicitly set
metadata_fields = {
"author": "author",
}
# A simplified metadata specification is available if all metadata and index fields are similarly named
# metadata_fields = {"author", "theme", "director"}
vector_store = CognitiveSearchVectorStore(
search_or_index_client=index_client,
index_name=index_name,
filterable_metadata_field_keys=metadata_fields,
index_management=IndexManagement.CREATE_IF_NOT_EXISTS,
id_field_key="id",
chunk_field_key="content",
embedding_field_key="embedding",
metadata_string_field_key="li_jsonMetadata",
doc_id_field_key="li_doc_id",
)
# define embedding function
from llama_index.embeddings import AzureOpenAIEmbedding
from llama_index import (
SimpleDirectoryReader,
StorageContext,
ServiceContext,
VectorStoreIndex,
)
openai.api_key = "sk-..." # OpenAI key will work
#openai.api_key = settings["OPENAI_API_KEY_EMBEDDING"] # Azure OpenAI key will NOT work
openai.api_type = "azure"
embed_model = AzureOpenAIEmbedding(
model="text-embedding-ada-002",
deployment_name="text-embedding-ada-002",
api_key=settings["OPENAI_API_KEY_EMBEDDING"],
azure_endpoint=settings["OPENAI_API_BASE_EMBEDDING"],
api_version=settings["OPENAI_API_VERSION"],
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
service_context = ServiceContext.from_defaults(embed_model=embed_model)
# load documents
documents = SimpleDirectoryReader("./test_data/").load_data()
index = VectorStoreIndex.from_documents(
documents, storage_context=storage_context, service_context=service_context)
π€
Based on the error message you're receiving, it seems like the issue is related to the API key you're using for AzureOpenAI. The error message 'Incorrect API key provided: e06832d6****a91a' indicates that the key you're using is not being accepted by AzureOpenAI.
The AuthenticationError
with error code 401 usually indicates an issue with the API key or endpoint. In the context of AzureOpenAIEmbedding
, the error could be due to the following reasons:
The api_key
is not correctly set. This key is used for authenticating your requests to Azure AI. Make sure you have the correct key and it's properly passed to the AzureOpenAIEmbedding
constructor.
The azure_endpoint
is not correctly set. This is the endpoint of your Azure OpenAI service. It should look like https://YOUR_RESOURCE_NAME.openai.azure.com/
. Make sure you have the correct endpoint and it's properly passed to the AzureOpenAIEmbedding
constructor.
The api_version
is not correctly set. This is the version of the Azure OpenAI API you are using. Make sure you have the correct version and it's properly passed to the AzureOpenAIEmbedding
constructor.
The validate_env
method in the AzureOpenAIEmbedding
class checks for these values and raises a ValueError
if they are not set. Make sure you have these values correctly set in your environment or passed to the AzureOpenAIEmbedding
constructor.
Here is the relevant code:
def validate_env(cls, values: Dict[str, Any]) -> Dict[str, Any]:
"""Validate necessary credentials are set."""
if (
values["api_base"] == "https://api.openai.com/v1"
and values["azure_endpoint"] is None
):
raise ValueError(
"You must set OPENAI_API_BASE to your Azure endpoint. "
"It should look like https://YOUR_RESOURCE_NAME.openai.azure.com/"
)
if values["api_version"] is None:
raise ValueError("You must set OPENAI_API_VERSION for Azure OpenAI.")
return values
If you have checked all these and still getting the error, it might be an issue with the Azure service itself or the network connection. You can find this code in the llama_index/embeddings/azure_openai.py file.
As for your second question, the LlamaIndex codebase does not seem to include any specific instructions or methods for handling API key errors when using AzureOpenAI. However, the validate_env
method mentioned above is designed to check the necessary credentials and raise an error if they are not set correctly. This can help in identifying and resolving issues related to the API key and other credentials.
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a π if this is helpful and π if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.
π€
Based on the error message you're receiving, it seems like the issue is related to the API key you're using for AzureOpenAI. The error message 'Incorrect API key provided: e06832d6****a91a' indicates that the key you're using is not being accepted by AzureOpenAI.
The AuthenticationError
with error code 401 usually indicates an issue with the API key or endpoint. In the context of AzureOpenAIEmbedding
, the error could be due to the following reasons:
The api_key
is not correctly set. This key is used for authenticating your requests to Azure AI. Make sure you have the correct key and it's properly passed to the AzureOpenAIEmbedding
constructor.
The azure_endpoint
is not correctly set. This is the endpoint of your Azure OpenAI service. It should look like https://YOUR_RESOURCE_NAME.openai.azure.com/
. Make sure you have the correct endpoint and it's properly passed to the AzureOpenAIEmbedding
constructor.
The api_version
is not correctly set. This is the version of the Azure OpenAI API you are using. Make sure you have the correct version and it's properly passed to the AzureOpenAIEmbedding
constructor.
The validate_env
method in the AzureOpenAIEmbedding
class checks for these values and raises a ValueError
if they are not set. Make sure you have these values correctly set in your environment or passed to the AzureOpenAIEmbedding
constructor.
Here is the relevant code:
def validate_env(cls, values: Dict[str, Any]) -> Dict[str, Any]:
"""Validate necessary credentials are set."""
if (
values["api_base"] == "https://api.openai.com/v1"
and values["azure_endpoint"] is None
):
raise ValueError(
"You must set OPENAI_API_BASE to your Azure endpoint. "
"It should look like https://YOUR_RESOURCE_NAME.openai.azure.com/"
)
if values["api_version"] is None:
raise ValueError("You must set OPENAI_API_VERSION for Azure OpenAI.")
return values
If you have checked all these and still getting the error, it might be an issue with the Azure service itself or the network connection. You can find this code in the llama_index/embeddings/azure_openai.py file.
As for your second question, the LlamaIndex codebase does not seem to include any specific instructions or methods for handling API key errors when using AzureOpenAI. However, the validate_env
method mentioned above is designed to check the necessary credentials and raise an error if they are not set correctly. This can help in identifying and resolving issues related to the API key and other credentials.
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a π if this is helpful and π if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.
Bug Description
Azure AI Search
key error when usingAzureOpenAI
(notopenAI
- which is working well). This is the case whereAzure AI Search
sends the text toAzureOpenAI
foe embedding, which is the typical case when hosting the solution on Azure.Version
latest version
Steps to Reproduce
Run the notebook with AzureOpenAI key https://docs.llamaindex.ai/en/stable/examples/vector_stores/CognitiveSearchIndexDemo.html
Relevant Logs/Tracbacks