Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: Error raised by bedrock service: 'str' object has no attribute 'invoke_model' #635

Closed
anth0nyhak1m opened this issue Sep 4, 2024 · 6 comments · Fixed by #636
Labels
bug Something isn't working released on @dev

Comments

@anth0nyhak1m
Copy link

Here is the example code I am using, with some minor adjustments. Bedrock will not instantiate with a temperature parameter, so I deleted that, and I went into the code for abstract_graph.py and deleted any default instantiation of a temperature parameter.

"""
Basic example of scraping pipeline using SmartScraper
"""

import os
from dotenv import load_dotenv
from scrapegraphai.graphs import SmartScraperGraph
from scrapegraphai.utils import prettify_exec_info

load_dotenv()

************************************************

Define the configuration for the graph

************************************************

graph_config = {
"llm": {
"client": "bedrock",
"model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0"
}
}

************************************************

Create the SmartScraperGraph instance and run it

************************************************

smart_scraper_graph = SmartScraperGraph(
prompt="List me all the projects with their description",
# also accepts a string with the already downloaded HTML code
source="https://perinim.github.io/projects/",
config=graph_config
)

result = smart_scraper_graph.run()
print(result)

************************************************

Get graph execution info

************************************************

graph_exec_info = smart_scraper_graph.get_execution_info()
print(prettify_exec_info(graph_exec_info))

Here is the error that I get that I cannot solve, I think it's an issue deep within the langchain library, however I am not sure if there are any workarounds:

Traceback (most recent call last):
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\langchain_aws\llms\bedrock.py", line 715, in _prepare_input_and_invoke
response = self.client.invoke_model(**request_options)
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'invoke_model'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\AnthonyHakim\\test.py", line 72, in
result = smart_scraper_graph.run()
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\scrapegraphai\graphs\smart_scraper_graph.py", line 114, in run
self.final_state, self.execution_info = self.graph.execute(inputs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\scrapegraphai\graphs\base_graph.py", line 263, in execute
return self._execute_standard(initial_state)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\scrapegraphai\graphs\base_graph.py", line 184, in _execute_standard
raise e
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\scrapegraphai\graphs\base_graph.py", line 168, in _execute_standard
result = current_node.execute(state)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\scrapegraphai\nodes\generate_answer_node.py", line 134, in execute
answer = chain.invoke({"question": user_prompt})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\langchain_core\runnables\base.py", line 2878, in invoke
input = context.run(step.invoke, input, config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\langchain_core\language_models\chat_models.py", line 276, in invoke
self.generate_prompt(
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\langchain_core\language_models\chat_models.py", line 776, in generate_prompt
return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\langchain_core\language_models\chat_models.py", line 633, in generate
raise e
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\langchain_core\language_models\chat_models.py", line 623, in generate
self._generate_with_cache(
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\langchain_core\language_models\chat_models.py", line 845, in _generate_with_cache
result = self._generate(
^^^^^^^^^^^^^^^
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\langchain_aws\chat_models\bedrock.py", line 552, in _generate
completion, tool_calls, llm_output = self._prepare_input_and_invoke(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\AnthonyHakim\anaconda3\envs\Lib\site-packages\langchain_aws\llms\bedrock.py", line 726, in _prepare_input_and_invoke
raise ValueError(f"Error raised by bedrock service: {e}")
ValueError: Error raised by bedrock service: 'str' object has no attribute 'invoke_model'

(I removed project names from paths)

@VinciGit00
Copy link
Collaborator

Does the smart scraper works?

@f-aguzzi
Copy link
Member

f-aguzzi commented Sep 5, 2024

@anth0nyhak1m Thanks for reporting this.

The bug should be fixed by pull request #636, along with issue #634.

@f-aguzzi f-aguzzi linked a pull request Sep 5, 2024 that will close this issue
@f-aguzzi f-aguzzi added bug Something isn't working released on @dev labels Sep 5, 2024
@anth0nyhak1m
Copy link
Author

So the way that I got this to work around the error, and this should be mentioned in the comments or docs, is that in graph_config, "client" should map to an instance of a boto3 'bedrock-runtime' client.

If you try to instantiate a 'bedrock' client you will get an error that says bedrock object has no attribute called invoke model. If you pass in a string, it will say string object has no attribute invoke model.

Must be a 'bedrock-runtime' client.

I found this fix directly in the issues of langchain:
langchain-ai/langchain#2828 (comment)

@f-aguzzi
Copy link
Member

f-aguzzi commented Sep 5, 2024

Thanks for your work and your research.

Meanwhile I found out that there is a different way to pass the temperature parameter to Bedrock models, so I added a workaround in the beta. Let's wait for the LangChain to fix their thing, and then the Bedrock support in ScrapeGraph will be operational again.

@VinciGit00
Copy link
Collaborator

Hi, can you add in the examples the bedrock runtime? Please

@VinciGit00
Copy link
Collaborator

please update to the new version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released on @dev
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants