OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Google gemini generate_content is not working in python API using function calling function_response

  • Thread starter Thread starter giulianoBarbosa
  • Start date Start date
G

giulianoBarbosa

Guest
I'm trying to make the same request that I did in CURL which works. But in Python is not working.

The gemini docs: https://ai.google.dev/gemini-api/docs/function-calling?hl=pt-br#multi-turn-example-1

The curl request:

Code:
import requests

url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent"
api_key = "YOUR_API_KEY"  # Replace with your actual API key

headers = {
    'Content-Type': 'application/json'
}

data = {
    "contents": [
        {
            "role": "user",
            "parts": [
                {
                    "text": "Which theaters in Mountain View show Barbie movie?"
                }
            ]
        },
        {
            "role": "model",
            "parts": [
                {
                    "functionCall": {
                        "name": "find_theaters",
                        "args": {
                            "location": "Mountain View, CA",
                            "movie": "Barbie"
                        }
                    }
                }
            ]
        },
        {
            "role": "function",
            "parts": [
                {
                    "functionResponse": {
                        "name": "find_theaters",
                        "response": {
                            "name": "find_theaters",
                            "content": {
                                "movie": "Barbie",
                                "theaters": [
                                    {
                                        "name": "AMC Mountain View 16",
                                        "address": "2000 W El Camino Real, Mountain View, CA 94040"
                                    },
                                    {
                                        "name": "Regal Edwards 14",
                                        "address": "245 Castro St, Mountain View, CA 94040"
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    ],
    "tools": [
        {
            "functionDeclarations": [
                {
                    "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"]
                    }
                },
                {
                    "name": "find_theaters",
                    "description": "find theaters based on location and optionally movie title which is currently playing in theaters",
                    "parameters": {
                        "type": "OBJECT",
                        "properties": {
                            "location": {
                                "type": "STRING",
                                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
                            },
                            "movie": {
                                "type": "STRING",
                                "description": "Any movie title"
                            }
                        },
                        "required": ["location"]
                    }
                },
                {
                    "name": "get_showtimes",
                    "description": "Find the start times for movies playing in a specific theater",
                    "parameters": {
                        "type": "OBJECT",
                        "properties": {
                            "location": {
                                "type": "STRING",
                                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
                            },
                            "movie": {
                                "type": "STRING",
                                "description": "Any movie title"
                            },
                            "theater": {
                                "type": "STRING",
                                "description": "Name of the theater"
                            },
                            "date": {
                                "type": "STRING",
                                "description": "Date for requested showtime"
                            }
                        },
                        "required": ["location", "movie", "theater", "date"]
                    }
                }
            ]
        }
    ]
}

response = requests.post(url, headers=headers, json=data, params={'key': api_key})

print(response.json())

Using vertex API:

Code:
messages = [
        {
            "role": "user",
            "parts": [{"text": "Which theaters in Mountain View show Barbie movie?"}],
        },
        {
            "role": "model",
            "parts": [
                {
                    "function_call": {
                        "name": "find_theaters",
                        "args": {"location": "Mountain View, CA", "movie": "Barbie"},
                    }
                }
            ],
        },
        {
            "role": "function",
            "parts": [
                {
                    "function_response": {
                        "name": "find_theaters",
                        "response": {
                            "name": "find_theaters",
                            "content": {
                                "movie": "Barbie",
                                "theaters": [
                                    {
                                        "name": "AMC Mountain View 16",
                                        "address": "2000 W El Camino Real, Mountain View, CA 94040",
                                    },
                                    {
                                        "name": "Regal Edwards 14",
                                        "address": "245 Castro St, Mountain View, CA 94040",
                                    },
                                ],
                            },
                        },
                    }
                }
            ],
        },
    ]

    # Define the tools
    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"],
                    },
                },
                {
                    "name": "find_theaters",
                    "description": "find theaters based on location and optionally movie title which is currently playing in theaters",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "location": {
                                "type": "string",
                                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
                            },
                            "movie": {
                                "type": "string",
                                "description": "Any movie title",
                            },
                        },
                        "required": ["location"],
                    },
                },
                {
                    "name": "get_showtimes",
                    "description": "Find the start times for movies playing in a specific theater",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "location": {
                                "type": "string",
                                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
                            },
                            "movie": {
                                "type": "string",
                                "description": "Any movie title",
                            },
                            "theater": {
                                "type": "string",
                                "description": "Name of the theater",
                            },
                            "date": {
                                "type": "string",
                                "description": "Date for requested showtime",
                            },
                        },
                        "required": ["location", "movie", "theater", "date"],
                    },
                },
            ]
        }
    ]

    model = GenerativeModel("gemini-pro")

    model.generate_content(messages, tool_config=tool_config, tools=tools)

The error:

ValueError: Protocol message Struct has no "location" field.

Please note that I took it from Gemini documentation, but I could not find how it should work in python
<p>I'm trying to make the same request that I did in CURL which works. But in Python is not working.</p>
<p>The gemini docs:
<a href="https://ai.google.dev/gemini-api/docs/function-calling?hl=pt-br#multi-turn-example-1" rel="nofollow noreferrer">https://ai.google.dev/gemini-api/docs/function-calling?hl=pt-br#multi-turn-example-1</a></p>
<p>The curl request:</p>
<pre><code>import requests

url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent"
api_key = "YOUR_API_KEY" # Replace with your actual API key

headers = {
'Content-Type': 'application/json'
}

data = {
"contents": [
{
"role": "user",
"parts": [
{
"text": "Which theaters in Mountain View show Barbie movie?"
}
]
},
{
"role": "model",
"parts": [
{
"functionCall": {
"name": "find_theaters",
"args": {
"location": "Mountain View, CA",
"movie": "Barbie"
}
}
}
]
},
{
"role": "function",
"parts": [
{
"functionResponse": {
"name": "find_theaters",
"response": {
"name": "find_theaters",
"content": {
"movie": "Barbie",
"theaters": [
{
"name": "AMC Mountain View 16",
"address": "2000 W El Camino Real, Mountain View, CA 94040"
},
{
"name": "Regal Edwards 14",
"address": "245 Castro St, Mountain View, CA 94040"
}
]
}
}
}
}
]
}
],
"tools": [
{
"functionDeclarations": [
{
"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"]
}
},
{
"name": "find_theaters",
"description": "find theaters based on location and optionally movie title which is currently playing in theaters",
"parameters": {
"type": "OBJECT",
"properties": {
"location": {
"type": "STRING",
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
},
"movie": {
"type": "STRING",
"description": "Any movie title"
}
},
"required": ["location"]
}
},
{
"name": "get_showtimes",
"description": "Find the start times for movies playing in a specific theater",
"parameters": {
"type": "OBJECT",
"properties": {
"location": {
"type": "STRING",
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
},
"movie": {
"type": "STRING",
"description": "Any movie title"
},
"theater": {
"type": "STRING",
"description": "Name of the theater"
},
"date": {
"type": "STRING",
"description": "Date for requested showtime"
}
},
"required": ["location", "movie", "theater", "date"]
}
}
]
}
]
}

response = requests.post(url, headers=headers, json=data, params={'key': api_key})

print(response.json())
</code></pre>
<p>Using vertex API:</p>
<pre><code>messages = [
{
"role": "user",
"parts": [{"text": "Which theaters in Mountain View show Barbie movie?"}],
},
{
"role": "model",
"parts": [
{
"function_call": {
"name": "find_theaters",
"args": {"location": "Mountain View, CA", "movie": "Barbie"},
}
}
],
},
{
"role": "function",
"parts": [
{
"function_response": {
"name": "find_theaters",
"response": {
"name": "find_theaters",
"content": {
"movie": "Barbie",
"theaters": [
{
"name": "AMC Mountain View 16",
"address": "2000 W El Camino Real, Mountain View, CA 94040",
},
{
"name": "Regal Edwards 14",
"address": "245 Castro St, Mountain View, CA 94040",
},
],
},
},
}
}
],
},
]

# Define the tools
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"],
},
},
{
"name": "find_theaters",
"description": "find theaters based on location and optionally movie title which is currently playing in theaters",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
},
"movie": {
"type": "string",
"description": "Any movie title",
},
},
"required": ["location"],
},
},
{
"name": "get_showtimes",
"description": "Find the start times for movies playing in a specific theater",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
},
"movie": {
"type": "string",
"description": "Any movie title",
},
"theater": {
"type": "string",
"description": "Name of the theater",
},
"date": {
"type": "string",
"description": "Date for requested showtime",
},
},
"required": ["location", "movie", "theater", "date"],
},
},
]
}
]

model = GenerativeModel("gemini-pro")

model.generate_content(messages, tool_config=tool_config, tools=tools)
</code></pre>
<p>The error:</p>
<p><code>ValueError: Protocol message Struct has no "location" field.</code></p>
<p>Please note that I took it from Gemini documentation, but I could not find how it should work in python</p>
 

Latest posts

J
Replies
0
Views
1
jbowerbir
J
V
Replies
0
Views
1
Vinicius Martin
V
Top