Azure/azure-search-vector-samples

Getting - AttributeError: module 'openai' has no attribute 'error' in azure-search-vector-python-langchain-sample.ipynb notebook

khalidshamim1 opened this issue · 0 comments

Trying to integrate vector search using langchain, have added everything as per the notebook(azure-search-vector-python-langchain-sample.ipynb); added text-embeddings-ada-002 models details; added azure ai search details; created an index ("langchain-vector-demo") and added content and metadata fields there.

Tried executing the code but its throwing this error; from traceback i can see its coming from: ~\anaconda3\envs\TGS_Mock\envs\gap\lib\site-packages\langchain\embeddings\openai.py .

Getting this error:-

AttributeError                            Traceback (most recent call last)
Cell In[9], line 9
      1 embeddings: OpenAIEmbeddings = OpenAIEmbeddings(
      2                                 deployment=model,
      3                                 model=model, 
   (...)
      6                                 openai_api_type="azure",
      7                                 openai_api_key=key)
      8 index_name: str = "langchain-vector-demo"
----> 9 vector_store: AzureSearch = AzureSearch(
     10     azure_search_endpoint=vector_store_address,
     11     azure_search_key=vector_store_password,
     12     index_name=index_name,
     13     embedding_function=embeddings.embed_query,
     14     semantic_configuration_name='config',
     15         semantic_settings=SemanticSettings(
     16             default_configuration='config',
     17             configurations=[
     18                 SemanticConfiguration(
     19                     name='config',
     20                     prioritized_fields=PrioritizedFields(
     21                         title_field=SemanticField(field_name='content'),
     22                         prioritized_content_fields=[SemanticField(field_name='content')],
     23                         prioritized_keywords_fields=[SemanticField(field_name='metadata')]
     24                     ))
     25             ])
     26     )

File ~\anaconda3\envs\TGS_Mock\envs\gap\lib\site-packages\langchain\vectorstores\azuresearch.py:226, in AzureSearch.__init__(self, azure_search_endpoint, azure_search_key, index_name, embedding_function, search_type, semantic_configuration_name, semantic_query_language, fields, vector_search, semantic_settings, scoring_profiles, default_scoring_profile, **kwargs)
    209 # Initialize base class
    210 self.embedding_function = embedding_function
    211 default_fields = [
    212     SimpleField(
    213         name=FIELDS_ID,
    214         type=SearchFieldDataType.String,
    215         key=True,
    216         filterable=True,
    217     ),
    218     SearchableField(
    219         name=FIELDS_CONTENT,
    220         type=SearchFieldDataType.String,
    221     ),
    222     SearchField(
    223         name=FIELDS_CONTENT_VECTOR,
    224         type=SearchFieldDataType.Collection(SearchFieldDataType.Single),
    225         searchable=True,
--> 226         vector_search_dimensions=len(embedding_function("Text")),
    227         vector_search_configuration="default",
    228     ),
    229     SearchableField(
    230         name=FIELDS_METADATA,
    231         type=SearchFieldDataType.String,
    232     ),
    233 ]
    234 user_agent = "langchain"
    235 if "user_agent" in kwargs and kwargs["user_agent"]:

File ~\anaconda3\envs\TGS_Mock\envs\gap\lib\site-packages\langchain\embeddings\openai.py:518, in OpenAIEmbeddings.embed_query(self, text)
    509 def embed_query(self, text: str) -> List[float]:
    510     """Call out to OpenAI's embedding endpoint for embedding query text.
    511 
    512     Args:
   (...)
    516         Embedding for the text.
    517     """
--> 518     return self.embed_documents([text])[0]

File ~\anaconda3\envs\TGS_Mock\envs\gap\lib\site-packages\langchain\embeddings\openai.py:490, in OpenAIEmbeddings.embed_documents(self, texts, chunk_size)
    478 """Call out to OpenAI's embedding endpoint for embedding search docs.
    479 
    480 Args:
   (...)
    486     List of embeddings, one for each text.
    487 """
    488 # NOTE: to keep things simple, we assume the list may contain texts longer
    489 #       than the maximum context and use length-safe embedding function.
--> 490 return self._get_len_safe_embeddings(texts, engine=self.deployment)

File ~\anaconda3\envs\TGS_Mock\envs\gap\lib\site-packages\langchain\embeddings\openai.py:374, in OpenAIEmbeddings._get_len_safe_embeddings(self, texts, engine, chunk_size)
    371     _iter = range(0, len(tokens), _chunk_size)
    373 for i in _iter:
--> 374     response = embed_with_retry(
    375         self,
    376         input=tokens[i : i + _chunk_size],
    377         **self._invocation_params,
    378     )
    379     batched_embeddings.extend(r["embedding"] for r in response["data"])
    381 results: List[List[List[float]]] = [[] for _ in range(len(texts))]

File ~\anaconda3\envs\TGS_Mock\envs\gap\lib\site-packages\langchain\embeddings\openai.py:100, in embed_with_retry(embeddings, **kwargs)
     98 def embed_with_retry(embeddings: OpenAIEmbeddings, **kwargs: Any) -> Any:
     99     """Use tenacity to retry the embedding call."""
--> 100     retry_decorator = _create_retry_decorator(embeddings)
    102     @retry_decorator
    103     def _embed_with_retry(**kwargs: Any) -> Any:
    104         response = embeddings.client.create(**kwargs)

File ~\anaconda3\envs\TGS_Mock\envs\gap\lib\site-packages\langchain\embeddings\openai.py:47, in _create_retry_decorator(embeddings)
     39 max_seconds = 10
     40 # Wait 2^x * 1 second between each retry starting with
     41 # 4 seconds, then up to 10 seconds, then 10 seconds afterwards
     42 return retry(
     43     reraise=True,
     44     stop=stop_after_attempt(embeddings.max_retries),
     45     wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds),
     46     retry=(
---> 47         retry_if_exception_type(openai.error.Timeout)
     48         | retry_if_exception_type(openai.error.APIError)
     49         | retry_if_exception_type(openai.error.APIConnectionError)
     50         | retry_if_exception_type(openai.error.RateLimitError)
     51         | retry_if_exception_type(openai.error.ServiceUnavailableError)
     52     ),
     53     before_sleep=before_sleep_log(logger, logging.WARNING),
     54 )

AttributeError: module 'openai' has no attribute 'error'

@openai , @azure-search team kindly do the needful.