|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -# [START aiplatform_function_calling] |
| 15 | +# [START aiplatform_gemini_function_calling] |
16 | 16 | from vertexai.preview.generative_models import (
|
17 | 17 | FunctionDeclaration,
|
18 | 18 | GenerativeModel,
|
19 | 19 | Tool,
|
20 | 20 | )
|
21 | 21 |
|
22 |
| -# Load the Vertex AI Gemini API to use function calling |
23 |
| -model = GenerativeModel("gemini-pro") |
24 |
| - |
25 |
| -# Specify a function declaration and parameters for an API request |
26 |
| -get_current_weather_func = FunctionDeclaration( |
27 |
| - name="get_current_weather", |
28 |
| - description="Get the current weather in a given location", |
29 |
| - # Function parameters are specified in OpenAPI JSON schema format |
30 |
| - parameters={ |
31 |
| - "type": "object", |
32 |
| - "properties": {"location": {"type": "string", "description": "Location"}}, |
33 |
| - }, |
34 |
| -) |
35 | 22 |
|
36 |
| -# Define a tool that includes the above get_current_weather_func |
37 |
| -weather_tool = Tool( |
38 |
| - function_declarations=[get_current_weather_func], |
39 |
| -) |
| 23 | +def generate_function_call(prompt: str) -> str: |
| 24 | + # Load the Vertex AI Gemini API to use function calling |
| 25 | + model = GenerativeModel("gemini-pro") |
40 | 26 |
|
41 |
| -# Prompt to ask the model about weather, which will invoke the Tool |
42 |
| -prompt = "What is the weather like in Boston?" |
| 27 | + # Specify a function declaration and parameters for an API request |
| 28 | + get_current_weather_func = FunctionDeclaration( |
| 29 | + name="get_current_weather", |
| 30 | + description="Get the current weather in a given location", |
| 31 | + # Function parameters are specified in OpenAPI JSON schema format |
| 32 | + parameters={ |
| 33 | + "type": "object", |
| 34 | + "properties": {"location": {"type": "string", "description": "Location"}}, |
| 35 | + }, |
| 36 | + ) |
| 37 | + |
| 38 | + # Define a tool that includes the above get_current_weather_func |
| 39 | + weather_tool = Tool( |
| 40 | + function_declarations=[get_current_weather_func], |
| 41 | + ) |
| 42 | + |
| 43 | + # Prompt to ask the model about weather, which will invoke the Tool |
| 44 | + prompt = prompt |
| 45 | + |
| 46 | + # Instruct the model to generate content using the Tool that you just created: |
| 47 | + response = model.generate_content( |
| 48 | + prompt, |
| 49 | + generation_config={"temperature": 0}, |
| 50 | + tools=[weather_tool], |
| 51 | + ) |
| 52 | + |
| 53 | + return str(response) |
43 | 54 |
|
44 |
| -# Instruct the model to generate content using the Tool that you just created: |
45 |
| -response = model.generate_content( |
46 |
| - prompt, |
47 |
| - generation_config={"temperature": 0}, |
48 |
| - tools=[weather_tool], |
49 |
| -) |
50 | 55 |
|
51 |
| -# Print the entire response |
52 |
| -print(response) |
| 56 | +# [END aiplatform_gemini_function_calling] |
53 | 57 |
|
54 |
| -# Print the part of the response that contains info about the function call |
55 |
| -print(response.candidates[0].content.parts[0].function_call) |
56 |
| -# [END aiplatform_function_calling] |
| 58 | +if __name__ == "__main__": |
| 59 | + generate_function_call("What is the weather like in Boston?") |
0 commit comments