Access Denied Exception while accessing Cross Account Sagemaker endpoints.
Labels: name: area: embeddings, name: auto:question, name: area: models, name: integration: aws

### System Info

Langchain: 0.0.308
Python: 3.8    

def get_ml_response(self, question):
        try:
            chain = create_conversation_retrieval_chain()
            llm_response = chain({'question': question, "chat_history": []})
            logger.info(f"LLm Response: {llm_response}")
        except Exception:
            err_msg = "Execption! {}".format(traceback.format_exc())
            logger.info(err_msg)

    def create_conversation_retrieval_chain(self):
        try:
            session = boto3.Session()
            sts_client = session.client("sts")
            assumed_role = sts_client.assume_role(RoleArn=role_arn, RoleSessionName="AssumeRoleSession")

            # Get the temporary credentials
            credentials = assumed_role["Credentials"]
            access_key = credentials["AccessKeyId"]
            secret_key = credentials["SecretAccessKey"]
            session_token = credentials["SessionToken"]

            # Configure the default AWS Session with assumed role credentials
            # config = Config(region_name="us-west-2")
            session = boto3.Session(
                aws_access_key_id=access_key,
                aws_secret_access_key=secret_key,
                aws_session_token=session_token
            )
            custom_template = """Given the following conversation and a follow up question, rephrase the follow up question
                                to be a standalone question. At the end of standalone question add this 'Answer the question
                                 in English language.If you do not know the answer reply with 'I am sorry'.
                                Chat History: {chat_history}
                                Follow Up Input: {question}
                                Standalone question:"""

            custom_question_prompt = PromptTemplate.from_template(custom_template)

            auth = AWSV4SignerAuth(credentials, 'us-west-2', 'aoss')

            embeddings = SagemakerEndpointEmbeddingsJumpStart(
                endpoint_name="jumpstart-dft-hf-textembedding-all-minilm-l6-v2",
                region_name="us-west-2",
                content_handler=content_handler_embeddings
            )

            opensearch_vector_search = OpenSearchVectorSearch(
                opensearch_url="<opensearch_url>",
                embedding_function=embeddings,
                index_name="<index_name>",
                http_auth=auth,
                connection_class=RequestsHttpConnection,
            )
            memory = ConversationBufferMemory(
                memory_key="chat_history",
                return_messages=True,
                input_key="question",
                output_key="answer",
            )

            chain = ConversationalRetrievalChain.from_llm(
                llm=SagemakerEndpoint(
                    endpoint_name="jumpstart-dft-hf-llm-falcon-40b-instruct-bf16",
                    region_name="us-west-2",
                    content_handler=content_handler_llm,
                ),
                # for experimentation, you can change number of documents to retrieve here
                retriever=opensearch_vector_search.as_retriever(
                    search_kwargs={
                        "k": 3,
                    }
                ),
                memory=memory,
                condense_question_prompt=custom_question_prompt,
                return_source_documents=True,
            )

            return chain

        except Exception:
            err_msg = "Execption! {}".format(traceback.format_exc())
            logger.info(err_msg)
      


### Who can help?

@hwchase17
@agola11

### Information

- [ ] The official example notebooks/scripts
- [ ] My own modified scripts

### Related Components

- [ ] LLMs/Chat Models
- [X] Embedding Models
- [ ] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [ ] Tools / Toolkits
- [X] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

chain = self.create_conversation_retrieval_chain()
llm_response = chain({'question': question, "chat_history": []})

### Expected behavior

I am expecting to connect to connect to Sagemaker endpoints using Assume role permissions but getting Access Denied Exception. I tried invoking the endpoints through the lambda function which is working fine. But not able to figure out how to pass the credentials using Langchain. Could you please help me with this.