aws-samples / amazon-bedrock-workshop

This is a workshop designed for Amazon Bedrock a foundational model service.
https://catalog.us-east-1.prod.workshops.aws/workshops/a4bdb007-5600-4368-81c5-ff5b4154f518/en-US/20-intro
MIT No Attribution
1.38k stars 591 forks source link

Error: AttributeError: 'Bedrock' object has no attribute 'invoke_model' encountered in chatbot session #146

Closed hrmb-hector closed 5 months ago

hrmb-hector commented 9 months ago

Hello!

I am trying to run the following piece of code from this tutorial (chatbot with claude):

from langchain.chains import ConversationChain
from langchain.llms.bedrock import Bedrock
from langchain.memory import ConversationBufferMemory
modelId = "anthropic.claude-v2"
cl_llm = Bedrock(
    model_id=modelId,
    client=boto3_bedrock,
    model_kwargs={"max_tokens_to_sample": 1000},
)
memory = ConversationBufferMemory()
conversation = ConversationChain(
    llm=cl_llm, verbose=True, memory=memory
)

try:

    print(conversation.predict(input="Hi there!"))

except ValueError as error:
    if  "AccessDeniedException" in str(error):
        print(f"\x1b[41m{error}\
        \nTo troubeshoot this issue please refer to the following resources.\
         \nhttps://docs.aws.amazon.com/IAM/latest/UserGuide/troubleshoot_access-denied.html\
         \nhttps://docs.aws.amazon.com/bedrock/latest/userguide/security-iam.html\x1b[0m\n")      
        class StopExecution(ValueError):
            def _render_traceback_(self):
                pass
        raise StopExecution        
    else:
        raise error

However I get the following error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/anaconda3/envs/python3/lib/python3.10/site-packages/langchain/llms/bedrock.py:242, in BedrockBase._prepare_input_and_invoke(self, prompt, stop, run_manager, **kwargs)
    241 try:
--> 242     response = self.client.invoke_model(
    243         body=body, modelId=self.model_id, accept=accept, contentType=contentType
    244     )
    245     text = LLMInputOutputAdapter.prepare_output(provider, response)

File ~/anaconda3/envs/python3/lib/python3.10/site-packages/botocore/client.py:906, in BaseClient.__getattr__(self, item)
    904     return event_response
--> 906 raise AttributeError(
    907     f"'{self.__class__.__name__}' object has no attribute '{item}'"
    908 )

AttributeError: 'Bedrock' object has no attribute 'invoke_model'

I can see the first part of the conversation: "Hi there!" but that is basically it.

lovaarte commented 9 months ago

Is this still an issue? If so, did you run the setup script beforehand?

adnanrizve commented 6 months ago

Make sure to set runtime= True in the setup script and try to run the commands which involve Invoke operations

boto3_bedrock = bedrock.get_bedrock_client( assumed_role=os.environ.get("BEDROCK_ASSUME_ROLE", None), region=os.environ.get("AWS_DEFAULT_REGION", None), runtime=False )

w601sxs commented 5 months ago

Closing this; please refer to the latest bedrock workshop update and reopen if still relevant