1+ from restack_ai .function import function , log
2+ from pydantic import BaseModel
3+ from google import genai
4+ from google .genai import types
5+ from typing import List , Optional
6+
7+ import os
8+
9+ class ChatMessage (BaseModel ):
10+ role : str
11+ content : str
12+
13+ class FunctionInputParams (BaseModel ):
14+ user_content : str
15+ chat_history : Optional [List [ChatMessage ]] = None
16+
17+ class WeatherInput (BaseModel ):
18+ location : str
19+
20+ class HumidityInput (BaseModel ):
21+ location : str
22+
23+ class AirQualityInput (BaseModel ):
24+ location : str
25+
26+ @function .defn ()
27+ async def get_current_weather (input : WeatherInput ) -> str :
28+ log .info ("get_current_weather function started" , location = input .location )
29+ return 'sunny'
30+
31+ @function .defn ()
32+ async def get_humidity (input : HumidityInput ) -> str :
33+ log .info ("get_humidity function started" , location = input .location )
34+ return '65%'
35+
36+ @function .defn ()
37+ async def get_air_quality (input : AirQualityInput ) -> str :
38+ log .info ("get_air_quality function started" , location = input .location )
39+ return 'good'
40+
41+ @function .defn ()
42+ async def gemini_multi_function_call_advanced (input : FunctionInputParams ) :
43+ try :
44+ log .info ("gemini_multi_function_call_advanced function started" , input = input )
45+ client = genai .Client (api_key = os .environ .get ("GEMINI_API_KEY" ))
46+
47+ functions = [
48+ {
49+ "name" : "get_current_weather" ,
50+ "description" : "Get the current weather in a given location" ,
51+ "parameters" : {
52+ "type" : "OBJECT" ,
53+ "properties" : {
54+ "location" : {
55+ "type" : "STRING" ,
56+ "description" : "The city and state, e.g. San Francisco, CA" ,
57+ },
58+ },
59+ "required" : ["location" ],
60+ }
61+ },
62+ {
63+ "name" : "get_humidity" ,
64+ "description" : "Get the current humidity in a given location" ,
65+ "parameters" : {
66+ "type" : "OBJECT" ,
67+ "properties" : {
68+ "location" : {
69+ "type" : "STRING" ,
70+ "description" : "The city and state, e.g. San Francisco, CA" ,
71+ },
72+ },
73+ "required" : ["location" ],
74+ }
75+ },
76+ {
77+ "name" : "get_air_quality" ,
78+ "description" : "Get the current air quality in a given location" ,
79+ "parameters" : {
80+ "type" : "OBJECT" ,
81+ "properties" : {
82+ "location" : {
83+ "type" : "STRING" ,
84+ "description" : "The city and state, e.g. San Francisco, CA" ,
85+ },
86+ },
87+ "required" : ["location" ],
88+ }
89+ }
90+ ]
91+
92+ tools = [types .Tool (function_declarations = functions )]
93+
94+ response = client .models .generate_content (
95+ model = 'gemini-2.0-flash-exp' ,
96+ contents = [input .user_content ] + ([msg .content for msg in input .chat_history ] if input .chat_history else []),
97+ config = types .GenerateContentConfig (
98+ tools = tools
99+ )
100+ )
101+ return response
102+
103+ except Exception as e :
104+ log .error ("Error in gemini_multi_function_call_advanced" , error = str (e ))
105+ raise e
0 commit comments