deepset-ai / haystack-core-integrations

Additional packages (components, document stores and the likes) to extend the capabilities of Haystack version 2.0 and onwards
https://haystack.deepset.ai
Apache License 2.0
81 stars 75 forks source link

GoogleAIGeminiChatGenerator 400 Request contains an invalid argument issue #855

Open Chance-Obondo opened 1 week ago

Chance-Obondo commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

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

Chance-Obondo commented 1 week ago

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