Skip to content

Commit 4288fec

Browse files
Ark-kuncopybara-github
authored andcommitted
feat: GenAI - Support FunctionDeclaration.response schema
PiperOrigin-RevId: 700543936
1 parent e6d3df8 commit 4288fec

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

Diff for: tests/unit/vertexai/test_generative_models.py

+24
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,27 @@
126126
"required": ["location"],
127127
}
128128

129+
_REQUEST_FUNCTION_RESPONSE_SCHEMA_STRUCT = {
130+
"type": "object",
131+
"properties": {
132+
"location": {
133+
"type": "string",
134+
"description": "The city and state, e.g. San Francisco, CA",
135+
},
136+
"unit": {
137+
"type": "string",
138+
"enum": [
139+
"celsius",
140+
"fahrenheit",
141+
],
142+
},
143+
"weather": {
144+
"type": "string",
145+
},
146+
},
147+
}
148+
149+
129150
# Input and expected output schema for renaming tests.
130151
_RENAMING_INPUT_SCHEMA = {
131152
"type": "object",
@@ -1085,6 +1106,7 @@ def test_chat_function_calling(self, generative_models: generative_models):
10851106
name="get_current_weather",
10861107
description="Get the current weather in a given location",
10871108
parameters=_REQUEST_FUNCTION_PARAMETER_SCHEMA_STRUCT,
1109+
response=_REQUEST_FUNCTION_RESPONSE_SCHEMA_STRUCT,
10881110
)
10891111
weather_tool = generative_models.Tool(
10901112
function_declarations=[get_current_weather_func],
@@ -1180,6 +1202,7 @@ def test_chat_forced_function_calling(self, generative_models: generative_models
11801202
name="get_current_weather",
11811203
description="Get the current weather in a given location",
11821204
parameters=_REQUEST_FUNCTION_PARAMETER_SCHEMA_STRUCT,
1205+
response=_REQUEST_FUNCTION_RESPONSE_SCHEMA_STRUCT,
11831206
)
11841207
weather_tool = generative_models.Tool(
11851208
function_declarations=[get_current_weather_func],
@@ -1239,6 +1262,7 @@ def test_conversion_methods(self, generative_models: generative_models):
12391262
name="get_current_weather",
12401263
description="Get the current weather in a given location",
12411264
parameters=_REQUEST_FUNCTION_PARAMETER_SCHEMA_STRUCT,
1265+
response=_REQUEST_FUNCTION_RESPONSE_SCHEMA_STRUCT,
12421266
)
12431267
weather_tool = generative_models.Tool(
12441268
function_declarations=[get_current_weather_func],

Diff for: vertexai/generative_models/_generative_models.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,16 @@ class FunctionDeclaration:
21202120
"location"
21212121
]
21222122
},
2123+
# Optional:
2124+
response={
2125+
"type": "object",
2126+
"properties": {
2127+
"weather": {
2128+
"type": "string",
2129+
"description": "The weather in the city"
2130+
},
2131+
},
2132+
},
21232133
)
21242134
weather_tool = generative_models.Tool(
21252135
function_declarations=[get_current_weather_func],
@@ -2160,6 +2170,7 @@ def __init__(
21602170
name: str,
21612171
parameters: Dict[str, Any],
21622172
description: Optional[str] = None,
2173+
response: Dict[str, Any] = None,
21632174
):
21642175
"""Constructs a FunctionDeclaration.
21652176
@@ -2168,12 +2179,24 @@ def __init__(
21682179
parameters: Describes the parameters to this function in JSON Schema Object format.
21692180
description: Description and purpose of the function.
21702181
Model uses it to decide how and whether to call the function.
2182+
response: Describes the response type of this function in JSON Schema format.
21712183
"""
21722184
parameters = copy.deepcopy(parameters)
21732185
_fix_schema_dict_for_gapic_in_place(parameters)
21742186
raw_schema = _dict_to_proto(aiplatform_types.Schema, parameters)
2187+
2188+
if response:
2189+
response = copy.deepcopy(response)
2190+
_fix_schema_dict_for_gapic_in_place(response)
2191+
raw_response_schema = _dict_to_proto(aiplatform_types.Schema, response)
2192+
else:
2193+
raw_response_schema = None
2194+
21752195
self._raw_function_declaration = gapic_tool_types.FunctionDeclaration(
2176-
name=name, description=description, parameters=raw_schema
2196+
name=name,
2197+
description=description,
2198+
parameters=raw_schema,
2199+
response=raw_response_schema,
21772200
)
21782201

21792202
@classmethod

0 commit comments

Comments
 (0)