Skip to content

Commit 1e49799

Browse files
Ark-kuncopybara-github
authored andcommitted
chore: GenAI - Part.function_call.args is now a proper dict
* Added `generative_models.FunctionCall` class (returned by `Part.FunctionCall`). Fixes #4079 PiperOrigin-RevId: 683761041
1 parent ca4d8c6 commit 1e49799

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

vertexai/generative_models/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
ChatSession,
2222
Content,
2323
FinishReason,
24+
FunctionCall,
2425
FunctionDeclaration,
2526
GenerationConfig,
2627
GenerativeModel,
@@ -44,6 +45,7 @@
4445
"ChatSession",
4546
"Content",
4647
"FinishReason",
48+
"FunctionCall",
4749
"FunctionDeclaration",
4850
"HarmCategory",
4951
"HarmBlockThreshold",

vertexai/generative_models/_generative_models.py

+45-8
Original file line numberDiff line numberDiff line change
@@ -2302,13 +2302,13 @@ def text(self) -> str:
23022302
) from e
23032303

23042304
@property
2305-
def function_calls(self) -> Sequence[gapic_tool_types.FunctionCall]:
2305+
def function_calls(self) -> Sequence["FunctionCall"]:
23062306
if not self.content or not self.content.parts:
23072307
return []
23082308
return [
23092309
part.function_call
23102310
for part in self.content.parts
2311-
if part and part.function_call
2311+
if part._raw_part._pb.WhichOneof("data") == "function_call"
23122312
]
23132313

23142314

@@ -2483,8 +2483,12 @@ def file_data(self) -> gapic_content_types.FileData:
24832483
return self._raw_part.file_data
24842484

24852485
@property
2486-
def function_call(self) -> gapic_tool_types.FunctionCall:
2487-
return self._raw_part.function_call
2486+
def function_call(self) -> "FunctionCall":
2487+
return (
2488+
FunctionCall._from_gapic(self._raw_part.function_call)
2489+
if self._raw_part.function_call
2490+
else None
2491+
)
24882492

24892493
@property
24902494
def function_response(self) -> gapic_tool_types.FunctionResponse:
@@ -2495,6 +2499,40 @@ def _image(self) -> "Image":
24952499
return Image.from_bytes(data=self._raw_part.inline_data.data)
24962500

24972501

2502+
class FunctionCall:
2503+
"""Function call."""
2504+
2505+
def __init__(
2506+
self,
2507+
*,
2508+
name: str,
2509+
args: Dict[str, Any],
2510+
):
2511+
self._raw_message = aiplatform_types.FunctionCall(name=name, args=args)
2512+
2513+
@classmethod
2514+
def _from_gapic(cls, raw_message: aiplatform_types.FunctionCall) -> "FunctionCall":
2515+
response = cls.__new__(cls)
2516+
response._raw_message = raw_message
2517+
return response
2518+
2519+
def to_dict(self) -> Dict[str, Any]:
2520+
return _proto_to_dict(self._raw_message)
2521+
2522+
def __repr__(self) -> str:
2523+
return self._raw_message.__repr__()
2524+
2525+
@property
2526+
def name(self) -> str:
2527+
return self._raw_message.name
2528+
2529+
@property
2530+
def args(self) -> Dict[str, Any]:
2531+
# We cannot use `type(self.args).to_dict(self.args)`
2532+
# due to: AttributeError: type object 'MapComposite' has no attribute 'to_dict'
2533+
return self.to_dict().get("args")
2534+
2535+
24982536
class SafetySetting:
24992537
"""Parameters for the generation."""
25002538

@@ -2953,10 +2991,9 @@ def respond_to_model_response(
29532991
)
29542992

29552993
try:
2956-
# We cannot use `function_args = type(function_call.args).to_dict(function_call.args)`
2957-
# due to: AttributeError: type object 'MapComposite' has no attribute 'to_dict'
2958-
function_args = type(function_call).to_dict(function_call)["args"]
2959-
function_call_result = callable_function._function(**function_args)
2994+
function_call_result = callable_function._function(
2995+
**function_call.args
2996+
)
29602997
if not isinstance(function_call_result, Mapping):
29612998
# If the function returns a single value, wrap it in the
29622999
# format that Part.from_function_response can accept.

vertexai/preview/generative_models.py

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
Candidate,
2828
Content,
2929
FinishReason,
30+
FunctionCall,
3031
FunctionDeclaration,
3132
HarmCategory,
3233
HarmBlockThreshold,
@@ -59,6 +60,7 @@ class ChatSession(_PreviewChatSession):
5960
"ChatSession",
6061
"Content",
6162
"FinishReason",
63+
"FunctionCall",
6264
"FunctionDeclaration",
6365
"HarmCategory",
6466
"HarmBlockThreshold",

0 commit comments

Comments
 (0)