-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemini_tool_calls_example.py
292 lines (255 loc) · 9.73 KB
/
gemini_tool_calls_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import os
import google.generativeai as genai
from google.ai.generativelanguage_v1beta.types import content
genai.configure(api_key=os.environ.get("GEMINI_API_KEY", ""))
# Define Python functions for your tools
def subtract_numbers(a: float, b: float) -> float:
print(f"Subtracting {a} and {b}")
return a - b
def add_numbers(a: float, b: float) -> float:
print(f"Adding {a} and {b}")
return a + b
def multiply_numbers(a: float, b: float) -> float:
print(f"Multiplying {a} and {b}")
return a * b
def divide_numbers(a: float, b: float) -> float:
print(f"Dividing {a} and {b}")
return a / b
def square_number(a: float) -> float:
print(f"Squaring {a}")
return a ** 2
def cube_number(a: float) -> float:
print(f"Cubing {a}")
return a ** 3
# Map JSON schema types to Gemini content.Type
def map_json_type_to_content_type(json_type: str) -> content.Type:
json_type = json_type.lower()
if json_type == "string":
return content.Type.STRING
elif json_type == "number":
return content.Type.NUMBER
elif json_type == "boolean":
return content.Type.BOOL
elif json_type == "array":
return content.Type.ARRAY
elif json_type == "object":
return content.Type.OBJECT
else:
return content.Type.OBJECT
def convert_schema_to_gemini(parameters: dict) -> content.Schema:
schema_type = parameters.get("type", parameters.get("type_", "object"))
ctype = map_json_type_to_content_type(schema_type)
schema_builder = content.Schema(type=ctype)
# required fields
required_fields = parameters.get("required", [])
if required_fields:
schema_builder.required.extend(required_fields)
# enum
if "enum" in parameters:
schema_builder.enum[:] = parameters["enum"]
# properties if object
props = parameters.get("properties", {})
for prop_name, prop_schema in props.items():
child_schema = convert_schema_to_gemini(prop_schema)
schema_builder.properties[prop_name] = child_schema
# items if array
if ctype == content.Type.ARRAY:
items = parameters.get("items", {})
if items:
item_schema = convert_schema_to_gemini(items)
schema_builder.items.CopyFrom(item_schema)
# description
description = parameters.get("description")
if description:
schema_builder.description = description
return schema_builder
def convert_tool_schema_to_gemini(tools_schema: list[dict]) -> list[genai.protos.Tool]:
declarations = []
for tool in tools_schema:
name = tool["name"]
description = tool.get("description", "")
params = tool.get("parameters", tool.get("input_schema", {}))
gemini_schema = convert_schema_to_gemini(params)
fd = genai.protos.FunctionDeclaration(
name=name,
description=description,
parameters=gemini_schema
)
declarations.append(fd)
return [genai.protos.Tool(function_declarations=declarations)]
class Assistant:
def __init__(self, tools_schema: list[dict], functions_map: dict):
"""
Initialize the assistant with a dynamic schema and a map of function names to callables.
:param tools_schema: A list of tool definitions in a JSON-like schema (like OpenAI format)
:param functions_map: A dict mapping function names (str) to Python callables
"""
self.functions_map = functions_map
# Convert the provided schema to Gemini format
gemini_tools = convert_tool_schema_to_gemini(tools_schema)
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 40,
"max_output_tokens": 8192,
"response_mime_type": "text/plain",
}
self.model = genai.GenerativeModel(
model_name="gemini-2.0-flash-exp",
generation_config=generation_config,
tools=gemini_tools,
)
print(self.model)
self.history = []
self.chat = self.model.start_chat(enable_automatic_function_calling=False, history=self.history)
def send_user_message(self, user_text: str):
self.history.append({"role": "user", "parts": [user_text]})
return self.chat.send_message(user_text)
def extract_function_calls(self, response) -> list[dict]:
function_calls = []
for candidate in response.candidates:
for part in candidate.content.parts:
if part.function_call:
function_calls.append({part.function_call.name: part.function_call.args})
return function_calls
def execute_functions(self, function_calls: list[dict]) -> list[dict]:
results = []
for fc in function_calls:
fn_name = list(fc.keys())[0]
fn_args = fc[fn_name]
if fn_name in self.functions_map:
result = self.functions_map[fn_name](**fn_args)
results.append({fn_name: result})
print(f"Function {fn_name} executed with result: {result}")
else:
results.append({fn_name: "Error: Function not found."})
return results
def send_function_response(self, function_results: list[dict]):
response_parts = []
for res in function_results:
fn_name = list(res.keys())[0]
value = res[fn_name]
response_parts.append(
genai.protos.Part(
function_response=genai.protos.FunctionResponse(
name=fn_name,
response={"result": value}
)
)
)
return self.chat.send_message(response_parts)
def run_query(self, user_input: str):
response = self.send_user_message(user_input)
while True:
function_calls = self.extract_function_calls(response)
if not function_calls:
break
results = self.execute_functions(function_calls)
response = self.send_function_response(results)
final_text = ""
for candidate in response.candidates:
for part in candidate.content.parts:
if part.text:
final_text += part.text
return final_text.strip()
if __name__ == "__main__":
# Example tools schema (like the openAI one with summarize_text and calculate_area)
tools_schema_openai = [
{
"name": "add_numbers",
"description": "Adds two numbers",
"strict": True,
"parameters": {
"type": "object",
"required": ["a", "b"],
"properties": {
"a": {"type": "number", "description": "First number"},
"b": {"type": "number", "description": "Second number"}
},
"additionalProperties": False
}
},
{
"name": "subtract_numbers",
"description": "Subtracts two numbers",
"strict": True,
"parameters": {
"type": "object",
"required": ["a", "b"],
"properties": {
"a": {"type": "number", "description": "First number"},
"b": {"type": "number", "description": "Second number"}
},
"additionalProperties": False
}
},
{
"name": "multiply_numbers",
"description": "Multiplies two numbers",
"strict": True,
"parameters": {
"type": "object",
"required": ["a", "b"],
"properties": {
"a": {"type": "number", "description": "First number"},
"b": {"type": "number", "description": "Second number"}
},
"additionalProperties": False
}
},
{
"name": "divide_numbers",
"description": "Divides two numbers",
"strict": True,
"parameters": {
"type": "object",
"required": ["a", "b"],
"properties": {
"a": {"type": "number", "description": "First number"},
"b": {"type": "number", "description": "Second number"}
},
"additionalProperties": False
}
},
{
"name": "square_number",
"description": "Squares a number",
"strict": True,
"parameters": {
"type": "object",
"required": ["a"],
"properties": {
"a": {"type": "number", "description": "Number to square"}
},
"additionalProperties": False
}
},
{
"name": "cube_number",
"description": "Cubes a number",
"strict": True,
"parameters": {
"type": "object",
"required": ["a"],
"properties": {
"a": {"type": "number", "description": "Number to cube"}
},
"additionalProperties": False
}
}
]
# Map function names to actual callables
functions_map = {
"add_numbers": add_numbers,
"subtract_numbers": subtract_numbers,
"multiply_numbers": multiply_numbers,
"divide_numbers": divide_numbers,
"square_number": square_number,
"cube_number": cube_number,
}
assistant = Assistant(tools_schema_openai, functions_map)
# Test a query
query = "Can you test the functions we have defined? And report the results as a detailed summary'?"
answer = assistant.run_query(query)
print("Final Answer:")
print(answer)