tedder / requests-aws4auth

Amazon Web Services version 4 authentication for the Python Requests module
MIT License
179 stars 63 forks source link

Type Error when using aws4auth for authenticating opensearch service in langchain. #72

Open NisargPipaliya opened 9 months ago

NisargPipaliya commented 9 months ago

Code

import boto3
from langchain.vectorstores import OpenSearchVectorSearch

service = "es" 
region = "ap-southeast-1"
credentials = boto3.Session().get_credentials()

awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, 
                  region, service, session_token=credentials.token)

docsearch = OpenSearchVectorSearch(
    index_name=index_name,
    embedding_function=embeddings_endpoint,
    opensearch_url= my_url,
    http_auth=awsauth,
    use_ssl=False,
    verify_certs=False,
    ssl_assert_hostname=False,
    ssl_show_warn=False    
)

Error

TypeError                                 Traceback (most recent call last)
File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/opensearchpy/connection/http_urllib3.py:262, in Urllib3HttpConnection.perform_request(self, method, url, params, body, timeout, ignore, headers)
    261     if isinstance(self.http_auth, Callable):  # type: ignore
--> 262         request_headers.update(self.http_auth(method, full_url, body))
    264 response = self.pool.urlopen(
    265     method, url, body, retries=Retry(False), headers=request_headers, **kw
    266 )

TypeError: AWS4Auth.__call__() takes 2 positional arguments but 4 were given

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
Cell In[29], line 1
----> 1 docsearch.add_documents(documents=text)

File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain_core/vectorstores.py:119, in VectorStore.add_documents(self, documents, **kwargs)
    117 texts = [doc.page_content for doc in documents]
    118 metadatas = [doc.metadata for doc in documents]
--> 119 return self.add_texts(texts, metadatas, **kwargs)

File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain_community/vectorstores/opensearch_vector_search.py:411, in OpenSearchVectorSearch.add_texts(self, texts, metadatas, ids, bulk_size, **kwargs)
    392 """Run more texts through the embeddings and add to the vectorstore.
    393 
    394 Args:
   (...)
    408     to "text".
    409 """
    410 embeddings = self.embedding_function.embed_documents(list(texts))
--> 411 return self.__add(
    412     texts,
    413     embeddings,
    414     metadatas=metadatas,
    415     ids=ids,
    416     bulk_size=bulk_size,
    417     **kwargs,
    418 )

File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain_community/vectorstores/opensearch_vector_search.py:370, in OpenSearchVectorSearch.__add(self, texts, embeddings, metadatas, ids, bulk_size, **kwargs)
    364 _validate_aoss_with_engines(self.is_aoss, engine)
    366 mapping = _default_text_mapping(
    367     dim, engine, space_type, ef_search, ef_construction, m, vector_field
    368 )
--> 370 return _bulk_ingest_embeddings(
    371     self.client,
    372     index_name,
    373     embeddings,
    374     texts,
    375     metadatas=metadatas,
    376     ids=ids,
    377     vector_field=vector_field,
    378     text_field=text_field,
    379     mapping=mapping,
    380     max_chunk_bytes=max_chunk_bytes,
    381     is_aoss=self.is_aoss,
    382 )

File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain_community/vectorstores/opensearch_vector_search.py:118, in _bulk_ingest_embeddings(client, index_name, embeddings, texts, metadatas, ids, vector_field, text_field, mapping, max_chunk_bytes, is_aoss)
    115 mapping = mapping
    117 try:
--> 118     client.indices.get(index=index_name)
    119 except not_found_error:
    120     client.indices.create(index=index_name, body=mapping)

File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/opensearchpy/client/utils.py:181, in query_params.<locals>._wrapper.<locals>._wrapped(*args, **kwargs)
    178         if v is not None:
    179             params[p] = _escape(v)
--> 181 return func(*args, params=params, headers=headers, **kwargs)

File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/opensearchpy/client/indices.py:255, in IndicesClient.get(self, index, params, headers)
    252 if index in SKIP_IN_PATH:
    253     raise ValueError("Empty value passed for a required argument 'index'.")
--> 255 return self.transport.perform_request(
    256     "GET", _make_path(index), params=params, headers=headers
    257 )

File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/opensearchpy/transport.py:446, in Transport.perform_request(self, method, url, params, body, timeout, ignore, headers)
    444     # raise exception on last retry
    445     if attempt == self.max_retries:
--> 446         raise e
    447 else:
    448     raise e

File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/opensearchpy/transport.py:409, in Transport.perform_request(self, method, url, params, body, timeout, ignore, headers)
    406 connection = self.get_connection()
    408 try:
--> 409     status, headers_response, data = connection.perform_request(
    410         method,
    411         url,
    412         params,
    413         body,
    414         headers=headers,
    415         ignore=ignore,
    416         timeout=timeout,
    417     )
    419     # Lowercase all the header names for consistency in accessing them.
    420     headers_response = {
    421         header.lower(): value for header, value in headers_response.items()
    422     }

File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/opensearchpy/connection/http_urllib3.py:279, in Urllib3HttpConnection.perform_request(self, method, url, params, body, timeout, ignore, headers)
    277     if isinstance(e, ReadTimeoutError):
    278         raise ConnectionTimeout("TIMEOUT", str(e), e)
--> 279     raise ConnectionError("N/A", str(e), e)
    281 # raise warnings if any from the 'Warnings' header.
    282 warning_headers = response.headers.get_all("warning", ())

ConnectionError: ConnectionError(AWS4Auth.__call__() takes 2 positional arguments but 4 were given) caused by: TypeError(AWS4Auth.__call__() takes 2 positional arguments but 4 were given)