pythonjsonkeywordgoogle-gemini

Google/generative AI call function doesn't work


I want to use function calling as tools in generative AI. Here are more details about function calling in Gemini and a correct structure of tool property.

Function calling would allow Gemini to call functions instead of a text response, just like ChatGPT.

I have written the following code:

import google.generativeai as genai

datas = {
  "contents": {
    "role": "user",
    "parts": {
      "text": "Where can I watch Oppenheimer today?"
    }
  },
  "tools": [
    {
      "function_declarations": [
        {
          "name": "find_movies",
          "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "description": {
                "type": "string",
                "description": "Any kind of description including category or genre, title words, attributes, etc."
              }
            },
            "required": [
              "description"
            ]
          }
        }
      ]
    }
  ]
}

genai.configure(api_key="MY_API_KEY")

model = genai.GenerativeModel('gemini-1.0-pro')

response = model.generate_content(
    **datas
)

and I have the following error:

Traceback (most recent call last):
  File "...\main.py", line 42, in <module>
    response = model.generate_content(
  File "...\AppData\Local\Programs\Python\Python311\Lib\site-packages\google\generativeai\generative_models.py", line 234, in generate_content
    request = self._prepare_request(
  File "...\AppData\Local\Programs\Python\Python311\Lib\site-packages\google\generativeai\generative_models.py", line 215, in _prepare_request
    return glm.GenerateContentRequest(
TypeError: google.ai.generativelanguage_v1beta.types.generative_service.GenerateContentRequest() got multiple values for keyword argument 'tools'

Solution

  • In your script, how about the following modification?

    Modified script:

    import google.generativeai as genai
    import google.ai.generativelanguage as glm
    
    
    q = "Where can I watch Oppenheimer today?"
    tool = glm.Tool(
        function_declarations=[
            glm.FunctionDeclaration(
                name="find_movies",
                description="find movie titles currently playing in theaters based on any description, genre, title words, etc.",
                parameters=glm.Schema(
                    type=glm.Type.OBJECT,
                    properties={
                        "location": glm.Schema(
                            type=glm.Type.STRING,
                            description="The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
                        ),
                        "description": glm.Schema(
                            type=glm.Type.STRING,
                            description="Any kind of description including category or genre, title words, attributes, etc.",
                        ),
                    },
                    required=["description"],
                ),
            )
        ]
    )
    genai.configure(api_key="MY_API_KEY")
    model = genai.GenerativeModel("gemini-1.0-pro", tools=[tool])
    response = model.generate_content(q)
    
    print(response.candidates)
    

    Reference: