Skip to content

Commit 2580638

Browse files
jaycee-licopybara-github
authored andcommitted
fix: do not run functions once the automatic function calling budget is spent
PiperOrigin-RevId: 979649999
1 parent 7c674ff commit 2580638

4 files changed

Lines changed: 322 additions & 59 deletions

File tree

‎google/genai/chats.py‎

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,20 @@ def send_message(
342342
):
343343
break
344344

345+
logger.info(f"AFC remote call {i} is done.")
346+
remaining_remote_calls_afc -= 1
347+
if remaining_remote_calls_afc == 0:
348+
# No request is left to send a result with, so the functions are not
349+
# called at all. Breaking here leaves the turn to be recorded once,
350+
# below, as the user's message followed by the model's function call.
351+
logger.info("Reached max remote calls for automatic function calling.")
352+
break
353+
345354
func_response_parts = _extra_utils.get_function_response_parts(
346355
response, function_map
347356
)
348357
if not func_response_parts:
349358
break
350-
logger.info(f"AFC remote call {i} is done.")
351-
remaining_remote_calls_afc -= 1
352-
if remaining_remote_calls_afc == 0:
353-
logger.info("Reached max remote calls for automatic function calling.")
354359
func_call_content = response.candidates[0].content
355360
func_response_content = types.Content(
356361
role="user", parts=func_response_parts
@@ -377,7 +382,6 @@ def send_message(
377382
)
378383
return response
379384

380-
381385
def send_message_stream(
382386
self,
383387
message: Union[list[PartUnionDict], PartUnionDict],
@@ -489,6 +493,15 @@ def send_message_stream(
489493
contents=contents_to_model, # type: ignore[arg-type]
490494
config=parsed_config,
491495
)
496+
remaining_remote_calls_afc -= 1
497+
# No request is left to send a result with, so the functions are not
498+
# called at all. The chunks are still yielded, and the turn is recorded
499+
# once below, ending on the model's unanswered function call.
500+
is_last_remote_call_afc = remaining_remote_calls_afc == 0
501+
if is_last_remote_call_afc:
502+
logger.info(
503+
"Reached max remote calls for automatic function calling."
504+
)
492505

493506
model_output = []
494507
finish_reason = None
@@ -501,7 +514,8 @@ def send_message_stream(
501514
is_valid = False
502515

503516
if (
504-
function_map
517+
not is_last_remote_call_afc
518+
and function_map
505519
and chunk.candidates
506520
and chunk.candidates[0].content
507521
and chunk.candidates[0].content.parts
@@ -518,15 +532,12 @@ def send_message_stream(
518532
finish_reason = chunk.candidates[0].finish_reason
519533
yield chunk
520534

535+
if is_last_remote_call_afc:
536+
break
521537
if not function_map or not func_response_parts:
522538
break
523539

524540
logger.info(f"AFC remote call {i} is done.")
525-
remaining_remote_calls_afc -= 1
526-
if remaining_remote_calls_afc == 0:
527-
logger.info(
528-
"Reached max remote calls for automatic function calling."
529-
)
530541

531542
if chunk and chunk.candidates and chunk.candidates[0].content:
532543
func_response_content = types.Content(
@@ -802,6 +813,17 @@ async def send_message(
802813
):
803814
break
804815

816+
logger.info(f"AFC remote call {i} is done.")
817+
remaining_remote_calls_afc -= 1
818+
if remaining_remote_calls_afc == 0:
819+
# No request is left to send a result with, so the functions are not
820+
# called at all. Breaking here leaves the turn to be recorded once,
821+
# below, as the user's message followed by the model's function call.
822+
logger.info(
823+
"Reached max remote calls for automatic function calling."
824+
)
825+
break
826+
805827
func_response_parts = (
806828
await _extra_utils.get_function_response_parts_async(
807829
response, function_map
@@ -810,13 +832,6 @@ async def send_message(
810832
if not func_response_parts:
811833
break
812834

813-
logger.info(f"AFC remote call {i} is done.")
814-
remaining_remote_calls_afc -= 1
815-
if remaining_remote_calls_afc == 0:
816-
logger.info(
817-
"Reached max remote calls for automatic function calling."
818-
)
819-
820835
func_call_content = response.candidates[0].content
821836
func_response_content = types.Content(
822837
role="user", parts=func_response_parts
@@ -845,7 +860,6 @@ async def send_message(
845860
)
846861
return response
847862

848-
849863
async def send_message_stream(
850864
self,
851865
message: Union[list[PartUnionDict], PartUnionDict],
@@ -1034,6 +1048,15 @@ async def async_generator(): # type: ignore[no-untyped-def]
10341048
contents=contents_to_model, # type: ignore[arg-type]
10351049
config=final_parsed_config,
10361050
)
1051+
remaining_remote_calls_afc -= 1
1052+
# No request is left to send a result with, so the functions are not
1053+
# called at all. The chunks are still yielded, and the turn is
1054+
# recorded once below, ending on the model's unanswered function call.
1055+
is_last_remote_call_afc = remaining_remote_calls_afc == 0
1056+
if is_last_remote_call_afc:
1057+
logger.info(
1058+
"Reached max remote calls for automatic function calling."
1059+
)
10371060

10381061
model_output = []
10391062
finish_reason = None
@@ -1046,7 +1069,8 @@ async def async_generator(): # type: ignore[no-untyped-def]
10461069
is_valid = False
10471070

10481071
if (
1049-
function_map
1072+
not is_last_remote_call_afc
1073+
and function_map
10501074
and chunk.candidates
10511075
and chunk.candidates[0].content
10521076
and chunk.candidates[0].content.parts
@@ -1065,15 +1089,12 @@ async def async_generator(): # type: ignore[no-untyped-def]
10651089
finish_reason = chunk.candidates[0].finish_reason
10661090
yield chunk
10671091

1092+
if is_last_remote_call_afc:
1093+
break
10681094
if not function_map or not func_response_parts:
10691095
break
10701096

10711097
logger.info(f"AFC remote call {i} is done.")
1072-
remaining_remote_calls_afc -= 1
1073-
if remaining_remote_calls_afc == 0:
1074-
logger.info(
1075-
"Reached max remote calls for automatic function calling."
1076-
)
10771098

10781099
func_response_content = types.Content(
10791100
role="user", parts=func_response_parts

‎google/genai/models.py‎

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6281,15 +6281,19 @@ def generate_content(
62816281
or not response.candidates[0].content.parts
62826282
):
62836283
break
6284+
logger.info(f'AFC remote call {i} is done.')
6285+
remaining_remote_calls_afc -= 1
6286+
if remaining_remote_calls_afc == 0:
6287+
# No request is left to send a result with, so the functions are not
6288+
# called at all. The model's function call is returned to the caller to
6289+
# run and answer themselves.
6290+
logger.info('Reached max remote calls for automatic function calling.')
6291+
break
62846292
func_response_parts = _extra_utils.get_function_response_parts(
62856293
response, function_map
62866294
)
62876295
if not func_response_parts:
62886296
break
6289-
logger.info(f'AFC remote call {i} is done.')
6290-
remaining_remote_calls_afc -= 1
6291-
if remaining_remote_calls_afc == 0:
6292-
logger.info('Reached max remote calls for automatic function calling.')
62936297

62946298
func_call_content = response.candidates[0].content
62956299
func_response_content = types.Content(
@@ -6440,6 +6444,13 @@ def generate_content_stream(
64406444
response = self._generate_content_stream(
64416445
model=model, contents=contents, config=parsed_config_to_call
64426446
)
6447+
remaining_remote_calls_afc -= 1
6448+
# No request is left to send a result with, so the functions are not
6449+
# called at all. The chunks are still yielded, and the model's function
6450+
# call is left for the caller to run and answer themselves.
6451+
is_last_remote_call_afc = remaining_remote_calls_afc == 0
6452+
if is_last_remote_call_afc:
6453+
logger.info('Reached max remote calls for automatic function calling.')
64436454

64446455
model_output = []
64456456
func_response_parts = []
@@ -6455,7 +6466,8 @@ def generate_content_stream(
64556466
)
64566467

64576468
if (
6458-
function_map
6469+
not is_last_remote_call_afc
6470+
and function_map
64596471
and chunk.candidates
64606472
and chunk.candidates[0].content
64616473
and chunk.candidates[0].content.parts
@@ -6471,13 +6483,12 @@ def generate_content_stream(
64716483

64726484
yield chunk
64736485

6486+
if is_last_remote_call_afc:
6487+
break
64746488
if not function_map or not func_response_parts:
64756489
break
64766490

64776491
logger.info(f'AFC remote call {i} is done.')
6478-
remaining_remote_calls_afc -= 1
6479-
if remaining_remote_calls_afc == 0:
6480-
logger.info('Reached max remote calls for automatic function calling.')
64816492

64826493
# Append function call and function response parts to contents for the next request.
64836494
func_response_content = types.Content(
@@ -8453,9 +8464,13 @@ async def generate_content(
84538464
)
84548465
remaining_remote_calls_afc -= 1
84558466
if remaining_remote_calls_afc == 0:
8467+
# No request is left to send a result with, so the functions are not
8468+
# called at all. The model's function call is returned to the caller
8469+
# to run and answer themselves.
84568470
logger.info(
84578471
'Reached max remote calls for automatic function calling.'
84588472
)
8473+
break
84598474

84608475
if not function_map:
84618476
break
@@ -8705,6 +8720,15 @@ async def stream_generator(): # type: ignore[no-untyped-def]
87058720
contents=loop_contents,
87068721
config=final_parsed_config_to_call,
87078722
)
8723+
remaining_remote_calls_afc -= 1
8724+
# No request is left to send a result with, so the functions are not
8725+
# called at all. The chunks are still yielded, and the model's
8726+
# function call is left for the caller to run and answer themselves.
8727+
is_last_remote_call_afc = remaining_remote_calls_afc == 0
8728+
if is_last_remote_call_afc:
8729+
logger.info(
8730+
'Reached max remote calls for automatic function calling.'
8731+
)
87088732

87098733
model_output = []
87108734
func_response_parts = []
@@ -8720,7 +8744,8 @@ async def stream_generator(): # type: ignore[no-untyped-def]
87208744
)
87218745

87228746
if (
8723-
function_map
8747+
not is_last_remote_call_afc
8748+
and function_map
87248749
and chunk.candidates
87258750
and chunk.candidates[0].content
87268751
and chunk.candidates[0].content.parts
@@ -8738,15 +8763,12 @@ async def stream_generator(): # type: ignore[no-untyped-def]
87388763

87398764
yield chunk
87408765

8766+
if is_last_remote_call_afc:
8767+
break
87418768
if not function_map or not func_response_parts:
87428769
break
87438770

87448771
logger.info(f'AFC remote call {i} is done.')
8745-
remaining_remote_calls_afc -= 1
8746-
if remaining_remote_calls_afc == 0:
8747-
logger.info(
8748-
'Reached max remote calls for automatic function calling.'
8749-
)
87508772

87518773
# Append function response parts to contents for the next request.
87528774
func_response_content = types.Content(

0 commit comments

Comments
 (0)