Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23276

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

$
0
0

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:

import requestsurl = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent"api_key = "YOUR_API_KEY"  # Replace with your actual API keyheaders = {'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:

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


Viewing all articles
Browse latest Browse all 23276

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>