aws-samples/amazon-kendra-langchain-extensions

KeyError:'client' while running kendra_chat_open_ai.py

devender-git opened this issue · 6 comments

File "C:\Users\User\ML_PATH\amazon-kendra-langchain-project-v2\amazon-kendra-langchain-extensions-main\kendra_retriever_samples\kendra_chat_open_ai.py", line 67, in
qa = build_chain()
File "C:\Users\User\ML_PATH\amazon-kendra-langchain-project-v2\amazon-kendra-langchain-extensions-main\kendra_retriever_samples\kendra_chat_open_ai.py", line 18, in build_chain
retriever = AmazonKendraRetriever(index_id=kendra_index_id)
File "C:\Users\User\ML_PATH\amazon-kendra-langchain-project-v2\venv\lib\site-packages\langchain\load\serializable.py", line 74, in init
super().init(**kwargs)
File "pydantic\main.py", line 339, in pydantic.main.BaseModel.init
File "pydantic\main.py", line 1050, in pydantic.main.validate_model
File "C:\Users\User\ML_PATH\amazon-kendra-langchain-project-v2\venv\lib\site-packages\langchain\retrievers\kendra.py", line 210, in create_client
if values["client"] is not None:
KeyError: 'client'

Anyone getting the above error while running sample file? I am using openai APIs. Have checked the Kendra Index id, Region & API key. Everything is coming correctly but still getting the above error.

I have set up AWS credentials locally, but still getting this error when I try to run the OpenAI example.

python kendra_retriever_open_ai.py 
Traceback (most recent call last):
  File "source/amazon-kendra-langchain-extensions/kendra_retriever_samples/kendra_retriever_open_ai.py", line 47, in <module>
    chain = build_chain()
  File "source/amazon-kendra-langchain-extensions/kendra_retriever_samples/kendra_retriever_open_ai.py", line 14, in build_chain
    retriever = AmazonKendraRetriever(index_id=kendra_index_id)
  File "source/amazon-kendra-langchain-extensions/kendra_retriever_samples/venv/lib/python3.10/site-packages/langchain/load/serializable.py", line 74, in __init__
    super().__init__(**kwargs)
  File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for AmazonKendraRetriever
__root__
  Could not load credentials to authenticate with AWS client. Please check that credentials in the specified profile name are valid. (type=value_error)

Please let me know what needs to be done in order to resolve this issue.

@devender-git The error occurs with the latest version of LangChain (0.0.228 as of today). If you use the version from a few days ago (e.g., 0.0.219), it performs fine with no errors. Just install with pip install langchain==0.0.219 and run again.

I was able to resolve this same error by editing /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain/retrievers/kendra.py

After doing some troubleshooting (you will see I've added traceback and importing os variables and hardcoding the region_name, etc), I was able to make it work. Here's the code I used for the function create_client. You may want to clean it up a little or it may be fine if you just want to run a test or demo.

    @root_validator(pre=True)
    def create_client(cls, values: Dict[str, Any]) -> Dict[str, Any]:
        import traceback
        import os

        print("During create_client")
        if "client" in values and values["client"] is not None:
            return values

        try:
            import boto3

            # if values["credentials_profile_name"] is not None:
            #     session = boto3.Session(profile_name=values["credentials_profile_name"])
            # else:
            #     # use default credentials
            #     session = boto3.Session()

            print("AWS_ACCESS_KEY_ID:", os.environ["AWS_ACCESS_KEY_ID"])
            print("AWS_SECRET_ACCESS_KEY:", os.environ["AWS_SECRET_ACCESS_KEY"])
            print("AWS_SESSION_TOKEN:", os.environ["AWS_SESSION_TOKEN"])

            session = boto3.Session(
                aws_access_key_id=os.environ["AWS_ACCESS_KEY_ID"],
                aws_secret_access_key=os.environ["AWS_SECRET_ACCESS_KEY"],
                aws_session_token=os.environ["AWS_SESSION_TOKEN"],
                region_name="eu-west-1"
            )

            client_params = {}
            #if values["region_name"] is not None:
            #    client_params["region_name"] = values["region_name"]
            if "region_name" in values:
                client_params["region_name"] = values["region_name"]

            values["client"] = session.client("kendra", **client_params)

            return values
        except ImportError:
            raise ModuleNotFoundError(
                "Could not import boto3 python package. "
                "Please install it with `pip install boto3`."
            )
        except Exception as e:
            traceback.print_exc()  # Print the exception traceback
            raise ValueError(
                "Could not load credentials to authenticate with AWS client. "
                "Please check that credentials in the specified "
                "profile name are valid."
            ) from e

@devender-git The error occurs with the latest version of LangChain (0.0.228 as of today). If you use the version from a few days ago (e.g., 0.0.219), it performs fine with no errors. Just install with pip install langchain==0.0.219 and run again.

Thank you, installing langchain v0.0.219 worked for me.

@devender-git The error occurs with the latest version of LangChain (0.0.228 as of today). If you use the version from a few days ago (e.g., 0.0.219), it performs fine with no errors. Just install with pip install langchain==0.0.219 and run again.

Thanks, it worked!

upgraded to langchain 0.0.263. @daekeun-ml if you still have access to open AI, can you please double check if this version works?