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

GoogleAIGeminiChatGenerator 400 Request contains an invalid argument issue #855

Closed
Chance-Obondo opened this issue Jun 28, 2024 · 4 comments · Fixed by #885
Closed

GoogleAIGeminiChatGenerator 400 Request contains an invalid argument issue #855

Chance-Obondo opened this issue Jun 28, 2024 · 4 comments · Fixed by #885
Assignees
Labels
bug Something isn't working integration:google-ai P1

Comments

@Chance-Obondo
Copy link

Chance-Obondo commented Jun 28, 2024

I am trying to use GoogleAIGeminiChatGenerator for function calling. The code I am using is from the haystack documentation example for using GoogleAIGeminiChatGenerator to respond to questions about the current weather. It was initially working when I tested this on 26/06/2024 but since yesterday 27/06/2024 it has not been working.I am getting the below error

  File "/Users/chanceobondo/Desktop/bynarybots_api/bynarybotsFunctions/haystackGenerators/functionsTest.py", line 44, in <module>
    res = generator.run(messages=messages)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/chanceobondo/Desktop/bynarybots_api/bynarybotsFunctions/.venv/lib/python3.11/site-packages/haystack_integrations/components/generators/google_ai/chat/gemini.py", line 264, in run
    res = session.send_message(
          ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/chanceobondo/Desktop/bynarybots_api/bynarybotsFunctions/.venv/lib/python3.11/site-packages/google/generativeai/generative_models.py", line 578, in send_message
    response = self.model.generate_content(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/chanceobondo/Desktop/bynarybots_api/bynarybotsFunctions/.venv/lib/python3.11/site-packages/google/generativeai/generative_models.py", line 331, in generate_content
    response = self._client.generate_content(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/chanceobondo/Desktop/bynarybots_api/bynarybotsFunctions/.venv/lib/python3.11/site-packages/google/ai/generativelanguage_v1beta/services/generative_service/client.py", line 827, in generate_content
    response = rpc(
               ^^^^
  File "/Users/chanceobondo/Desktop/bynarybots_api/bynarybotsFunctions/.venv/lib/python3.11/site-packages/google/api_core/gapic_v1/method.py", line 131, in __call__
    return wrapped_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/chanceobondo/Desktop/bynarybots_api/bynarybotsFunctions/.venv/lib/python3.11/site-packages/google/api_core/retry/retry_unary.py", line 293, in retry_wrapped_func
    return retry_target(
           ^^^^^^^^^^^^^
  File "/Users/chanceobondo/Desktop/bynarybots_api/bynarybotsFunctions/.venv/lib/python3.11/site-packages/google/api_core/retry/retry_unary.py", line 153, in retry_target
    _retry_error_helper(
  File "/Users/chanceobondo/Desktop/bynarybots_api/bynarybotsFunctions/.venv/lib/python3.11/site-packages/google/api_core/retry/retry_base.py", line 212, in _retry_error_helper
    raise final_exc from source_exc
  File "/Users/chanceobondo/Desktop/bynarybots_api/bynarybotsFunctions/.venv/lib/python3.11/site-packages/google/api_core/retry/retry_unary.py", line 144, in retry_target
    result = target()
             ^^^^^^^^
  File "/Users/chanceobondo/Desktop/bynarybots_api/bynarybotsFunctions/.venv/lib/python3.11/site-packages/google/api_core/timeout.py", line 120, in func_with_timeout
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/chanceobondo/Desktop/bynarybots_api/bynarybotsFunctions/.venv/lib/python3.11/site-packages/google/api_core/grpc_helpers.py", line 78, in error_remapped_callable
    raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.InvalidArgument: 400 Request contains an invalid argument.

Here is the code i am using, you can run and have a look, please kindly help because I am using Haystack to create an agent that uses function calling.

from haystack.utils import Secret
from haystack.dataclasses.chat_message import ChatMessage
from google.ai.generativelanguage import FunctionDeclaration, Tool

from haystack_integrations.components.generators.google_ai import GoogleAIGeminiChatGenerator

def get_current_weather(location: str, unit: str = "celsius"):
  return {"weather": "sunny", "temperature": 21.8, "unit": unit}

get_current_weather_func = FunctionDeclaration(
    name="get_current_weather",
    description="Get the current weather in a given location",
    parameters={
        "type_": "OBJECT",
        "properties": {
            "location": {"type_": "STRING", "description": "The city and state, e.g. San Francisco, CA"},
            "unit": {
                "type_": "STRING",
                "enum": [
                    "celsius",
                    "fahrenheit",
                ],
            },
        },
        "required": ["location"],
    },
)

tool = Tool(function_declarations= [get_current_weather_func])

gemini_chat = GoogleAIGeminiChatGenerator(model="gemini-pro", api_key=Secret.from_token(""),
                                          tools=[tool])

messages = [ChatMessage.from_user("What is the temperature in celsius in Berlin?")]
res = gemini_chat.run(messages=messages)

weather = get_current_weather(**res["replies"][0].content)
messages += res["replies"] + [ChatMessage.from_function(content=weather, name="get_current_weather")]
res = gemini_chat.run(messages=messages)

for reply in res["replies"]:
    print(reply.content)
@anakin87 anakin87 transferred this issue from deepset-ai/haystack Jun 28, 2024
@anakin87 anakin87 added integration:google-ai bug Something isn't working labels Jun 28, 2024
@anakin87
Copy link
Member

I can confirm it.
Our tests are failing https://github.com/deepset-ai/haystack-core-integrations/actions/runs/9705415312/job/26787441393

As a temporary workaround, you can try pinning google-generativeai<0.7.1.

@Chance-Obondo
Copy link
Author

Chance-Obondo commented Jun 28, 2024

Thanks for the suggestion. Which version do you recommend using for google-generativeai ? And is there an issue that you might have noticed that is causing this issue

@anakin87
Copy link
Member

Sorry unfortunately pinning google-generativeai<0.7.1 does not work.
Should be investigated better.

@Chance-Obondo
Copy link
Author

Chance-Obondo commented Jun 28, 2024

I agree,I have tried pinning google-generativeai<0.7.1 it hasnt worked on my end. If there is any update please lemme know. But it seems for now the only way is to use a different generator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working integration:google-ai P1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants