GoogleCloudPlatform / GoogleCloudPlatform/generative-ai
[Bug]: The number of function response parts should be equal to number of function call parts of the function call turn
- Dominant language
- Jupyter Notebook
- Stars
- 17.7k
- Forks
- 4.5k
- Avg merge
- 12h 38m
- Merged PRs (30d)
- 42
Description
### File Name
gemini/function-calling/sql-talk-app/app.py
### What happened?
This error appears when the BigQuery dataset contains multiple tables, basically because there is one part response for each table, leading to having more function parts in the response that in the function call definition.
I tested replacing the **get_table** function for a **get_tables** that handles several tables. I think I had a different code base, but Im sharing what I did as a sample
```py
get_table_func = FunctionDeclaration(
name="get_tables",
description="Obtiene información sobre las tablas, incluida la descripción, el esquema y la cantidad de filas que ayudarán a responder la pregunta del usuario. Utilice siempre el nombre completo del conjunto de datos y de las tablas.",
parameters={
"type": "object",
"properties": {
"tables": {
"type": "array",
"description": "List of tables with informatioin in BQ",
"items": {
"type": "object",
"properties": {
"table_id": {
"type": "string",
"description": "ID del Dataset del que se recuperarán las tablas",
},
"description": {
"type": "string",
"description": "Description of the table",
}
},
"required": [
"table_id",
],
}
}
}
},
)
```
And the function implementation
```py
if response.function_call.name == "get_tables":
api_responses = []
processed_tables = None #Handles all tables in an array
for table in params["tables"]: # repeat for each table
table_id = table["table_id"]
ftable_id = f"{BIGQUERY_PROJECT_ID}.{BIGQUERY_DATASET_ID}.{table_id}"
api_response = client.get_table(ftable_id)
api_response = api_response.to_api_repr()
api_responses.append(api_response)
if processed_tables is None:
processed_tables = [
str([
str(api_response.get("description", "")),
str(
[
column["name"] + ":" + column.get("description", "No description")
for column in api_response["schema"]["fields"]
if "description" in column
]
)
])
]
else:
try:
processed_tables.append(
str([
str(api_response.get("description", "")),
str(
[
column["name"] + ":" + column.get("description", "No description")
for column in api_response["schema"]["fields"]
if "description" in column
]
)
])
)
except Exception as e:
print(e)
# Return all tables in the function response
api_requests_and_responses.append(
[
response.function_call.name,
params,
str( processed_tables )
]
)
api_response = api_responses
api_response = str( api_response)
```
### Relevant log output
```shell
```
### Code of Conduct
- [x] I agree to follow this project's Code of Conduct
Contributor guide
Assessment
This issue has not been assessed yet.