run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
36.14k stars 5.15k forks source link

[Bug]: VectorStoreIndex with azureopenai AuthenticationError: Error code: 401 #16565

Open AlexJJJChen opened 1 day ago

AlexJJJChen commented 1 day ago

Bug Description

when I use VectorStoreIndex and azureopenai, it shows the following error:

AuthenticationError Traceback (most recent call last) Cell In[54], line 6 2 from llama_index.core import VectorStoreIndex 3 from llama_index.core.retrievers import AutoMergingRetriever ----> 6 base_index = VectorStoreIndex( 7 leaf_nodes, 8 storage_context=storage_context 9 ) 10 base_retriever = base_index.as_retriever(similarity_top_k=6) 11 retriever = AutoMergingRetriever(base_retriever, storage_context, verbose=True)

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/llama_index/core/indices/vector_store/base.py:78, in VectorStoreIndex.init(self, nodes, use_async, store_nodes_override, embed_model, insert_batch_size, objects, index_struct, storage_context, callback_manager, transformations, show_progress, service_context, kwargs) 71 self._embed_model = ( 72 resolve_embed_model(embed_model, callback_manager=callback_manager) 73 if embed_model 74 else embed_model_from_settings_or_context(Settings, service_context) 75 ) 77 self._insert_batch_size = insert_batch_size ---> 78 super().init( 79 nodes=nodes, 80 index_struct=index_struct, 81 service_context=service_context, 82 storage_context=storage_context, 83 show_progress=show_progress, 84 objects=objects, 85 callback_manager=callback_manager, 86 transformations=transformations, 87 kwargs, 88 )

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/llama_index/core/indices/base.py:94, in BaseIndex.init(self, nodes, objects, index_struct, storage_context, callback_manager, transformations, show_progress, service_context, kwargs) 92 if index_struct is None: 93 nodes = nodes or [] ---> 94 index_struct = self.build_index_from_nodes( 95 nodes + objects, kwargs # type: ignore 96 ) 97 self._index_struct = index_struct 98 self._storage_context.index_store.add_index_struct(self._index_struct)

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/llama_index/core/indices/vector_store/base.py:314, in VectorStoreIndex.build_index_from_nodes(self, nodes, insert_kwargs) 306 if any( 307 node.get_content(metadata_mode=MetadataMode.EMBED) == "" for node in nodes 308 ): 309 raise ValueError( 310 "Cannot build index from nodes with no content. " 311 "Please ensure all nodes have content." 312 ) --> 314 return self._build_index_from_nodes(nodes, insert_kwargs)

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/llama_index/core/indices/vector_store/base.py:285, in VectorStoreIndex._build_index_from_nodes(self, nodes, insert_kwargs) 283 run_async_tasks(tasks) 284 else: --> 285 self._add_nodes_to_index( 286 index_struct, 287 nodes, 288 show_progress=self._show_progress, 289 insert_kwargs, 290 ) 291 return index_struct

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/llama_index/core/indices/vector_store/base.py:238, in VectorStoreIndex._add_nodes_to_index(self, index_struct, nodes, show_progress, insert_kwargs) 235 return 237 for nodes_batch in iter_batch(nodes, self._insert_batch_size): --> 238 nodes_batch = self._get_node_with_embedding(nodes_batch, show_progress) 239 new_ids = self._vector_store.add(nodes_batch, insert_kwargs) 241 if not self._vector_store.stores_text or self._store_nodes_override: 242 # NOTE: if the vector store doesn't store text, 243 # we need to add the nodes to the index struct and document store

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/llama_index/core/indices/vector_store/base.py:145, in VectorStoreIndex._get_node_with_embedding(self, nodes, show_progress) 133 def _get_node_with_embedding( 134 self, 135 nodes: Sequence[BaseNode], 136 show_progress: bool = False, 137 ) -> List[BaseNode]: 138 """ 139 Get tuples of id, node, and embedding. 140 (...) 143 144 """ --> 145 id_to_embed_map = embed_nodes( 146 nodes, self._embed_model, show_progress=show_progress 147 ) 149 results = [] 150 for node in nodes:

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/llama_index/core/indices/utils.py:138, in embed_nodes(nodes, embed_model, show_progress) 135 else: 136 id_to_embed_map[node.node_id] = node.embedding --> 138 new_embeddings = embed_model.get_text_embedding_batch( 139 texts_to_embed, show_progress=show_progress 140 ) 142 for new_id, text_embedding in zip(ids_to_embed, new_embeddings): 143 id_to_embed_map[new_id] = text_embedding

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/llama_index/core/instrumentation/dispatcher.py:260, in Dispatcher.span..wrapper(func, instance, args, kwargs) 252 self.spanenter( 253 id=id_, 254 bound_args=bound_args, (...) 257 tags=tags, 258 ) 259 try: --> 260 result = func(*args, **kwargs) 261 except BaseException as e: 262 self.event(SpanDropEvent(spanid=id, err_str=str(e)))

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/llama_index/core/base/embeddings/base.py:332, in BaseEmbedding.get_text_embedding_batch(self, texts, show_progress, **kwargs) 323 dispatcher.event( 324 EmbeddingStartEvent( 325 model_dict=model_dict, 326 ) 327 ) 328 with self.callback_manager.event( 329 CBEventType.EMBEDDING, 330 payload={EventPayload.SERIALIZED: self.to_dict()}, 331 ) as event: --> 332 embeddings = self._get_text_embeddings(cur_batch) 333 result_embeddings.extend(embeddings) 334 event.on_end( 335 payload={ 336 EventPayload.CHUNKS: cur_batch, 337 EventPayload.EMBEDDINGS: embeddings, 338 }, 339 )

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/llama_index/embeddings/openai/base.py:432, in OpenAIEmbedding._get_text_embeddings(self, texts) 425 """Get text embeddings. 426 427 By default, this is a wrapper around _get_text_embedding. 428 Can be overridden for batch queries. 429 430 """ 431 client = self._get_client() --> 432 return get_embeddings( 433 client, 434 texts, 435 engine=self._text_engine, 436 **self.additional_kwargs, 437 )

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/tenacity/init.py:289, in BaseRetrying.wraps..wrapped_f(*args, kw) 287 @functools.wraps(f) 288 def wrapped_f(*args: t.Any, *kw: t.Any) -> t.Any: --> 289 return self(f, args, kw)

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/tenacity/init.py:379, in Retrying.call(self, fn, *args, **kwargs) 377 retry_state = RetryCallState(retry_object=self, fn=fn, args=args, kwargs=kwargs) 378 while True: --> 379 do = self.iter(retry_state=retry_state) 380 if isinstance(do, DoAttempt): 381 try:

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/tenacity/init.py:314, in BaseRetrying.iter(self, retry_state) 312 is_explicit_retry = fut.failed and isinstance(fut.exception(), TryAgain) 313 if not (is_explicit_retry or self.retry(retry_state)): --> 314 return fut.result() 316 if self.after is not None: 317 self.after(retry_state)

File /opt/anaconda3/envs/benchmark/lib/python3.11/concurrent/futures/_base.py:449, in Future.result(self, timeout) 447 raise CancelledError() 448 elif self._state == FINISHED: --> 449 return self.__get_result() 451 self._condition.wait(timeout) 453 if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:

File /opt/anaconda3/envs/benchmark/lib/python3.11/concurrent/futures/_base.py:401, in Future.__get_result(self) 399 if self._exception: 400 try: --> 401 raise self._exception 402 finally: 403 # Break a reference cycle with the exception in self._exception 404 self = None

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/tenacity/init.py:382, in Retrying.call(self, fn, *args, *kwargs) 380 if isinstance(do, DoAttempt): 381 try: --> 382 result = fn(args, **kwargs) 383 except BaseException: # noqa: B902 384 retry_state.set_exception(sys.exc_info()) # type: ignore[arg-type]

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/llama_index/embeddings/openai/base.py:180, in get_embeddings(client, list_of_text, engine, kwargs) 176 assert len(list_of_text) <= 2048, "The batch size should not be larger than 2048." 178 list_of_text = [text.replace("\n", " ") for text in list_of_text] --> 180 data = client.embeddings.create(input=list_of_text, model=engine, kwargs).data 181 return [d.embedding for d in data]

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/openai/resources/embeddings.py:124, in Embeddings.create(self, input, model, dimensions, encoding_format, user, extra_headers, extra_query, extra_body, timeout) 118 embedding.embedding = np.frombuffer( # type: ignore[no-untyped-call] 119 base64.b64decode(data), dtype="float32" 120 ).tolist() 122 return obj --> 124 return self._post( 125 "/embeddings", 126 body=maybe_transform(params, embedding_create_params.EmbeddingCreateParams), 127 options=make_request_options( 128 extra_headers=extra_headers, 129 extra_query=extra_query, 130 extra_body=extra_body, 131 timeout=timeout, 132 post_parser=parser, 133 ), 134 cast_to=CreateEmbeddingResponse, 135 )

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/openai/_base_client.py:1277, in SyncAPIClient.post(self, path, cast_to, body, options, files, stream, stream_cls) 1263 def post( 1264 self, 1265 path: str, (...) 1272 stream_cls: type[_StreamT] | None = None, 1273 ) -> ResponseT | _StreamT: 1274 opts = FinalRequestOptions.construct( 1275 method="post", url=path, json_data=body, files=to_httpx_files(files), **options 1276 ) -> 1277 return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/openai/_base_client.py:954, in SyncAPIClient.request(self, cast_to, options, remaining_retries, stream, stream_cls) 951 else: 952 retries_taken = 0 --> 954 return self._request( 955 cast_to=cast_to, 956 options=options, 957 stream=stream, 958 stream_cls=stream_cls, 959 retries_taken=retries_taken, 960 )

File /opt/anaconda3/envs/benchmark/lib/python3.11/site-packages/openai/_base_client.py:1058, in SyncAPIClient._request(self, cast_to, options, retries_taken, stream, stream_cls) 1055 err.response.read() 1057 log.debug("Re-raising status error") -> 1058 raise self._make_status_error_from_response(err.response) from None 1060 return self._process_response( 1061 cast_to=cast_to, 1062 options=options, (...) 1066 retries_taken=retries_taken, 1067 )

AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: 952221df****6080. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}

Version

0.11.18

Steps to Reproduce

use azureopenai api

Relevant Logs/Tracbacks

No response

dosubot[bot] commented 1 day ago

There are several similar issues related to authentication errors when using AzureOpenAI with LlamaIndex. Here are some solutions that have been suggested:

  1. Ensure Environment Variables are Set Correctly: Make sure that the environment variables for the API key and other parameters are set correctly before importing the AzureOpenAI module. This has resolved similar issues for other users [1].

  2. Use ServiceContext for Initialization: Consider using the ServiceContext for initializing the AzureOpenAI class. Here's an example:

    from llama_index import ServiceContext, set_global_service_context
    service_context = ServiceContext.from_defaults(
       llm=AzureOpenAI(engine="gpt-4-32k", model="gpt-4-32k"),
    )
    set_global_service_context(service_context)

    This setup ensures that the AzureOpenAI LLM is correctly configured and used across your application [2].

  3. Hardcode Values Temporarily: Double-check that the values for the API key, endpoint, and other configurations are being set as expected. You might want to try hardcoding these values temporarily to ensure they are correct [1].

  4. Set Environment Variables Before Importing Modules: Set the environment variables before importing the necessary modules to ensure they are recognized at the file level scope. Here's an example:

    import os
    os.environ["OPENAI_API_TYPE"] = "type"
    os.environ["OPENAI_API_VERSION"] = "version"
    os.environ["OPENAI_API_BASE"] = "api_base"
    os.environ["OPENAI_API_KEY"] = "azure_open_ai_key"
    
    from llama_index import VectorStoreIndex, SimpleDirectoryReader, LLMPredictor, ServiceContext
    from langchain.llms import AzureOpenAI
    import openai
    
    llm_predictor = LLMPredictor(llm=AzureOpenAI(aptemperature=0, model_name="model_name"))
    service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
    
    documents = SimpleDirectoryReader('Data/').load_data()
    custom_llm_index = VectorStoreIndex.from_documents(documents, service_context=service_context)
    custom_llm_query_engine = custom_llm_index.as_query_engine()
    response = custom_llm_query_engine.query("who is this text about?")
    print(response)

    Alternatively, store the credentials directly in the openai module arguments instead of using environment variables [3].

These steps should help address the authentication error you are encountering.

To continue talking to Dosu, mention @dosu.

logan-markewich commented 1 day ago

@AlexJJJChen I don't see you setting up an llm or embed model?

Typically you'd need to setup azure embeddings and pass it into the index VectorStoreIndex(..., embed_model=embed_model)