forked from ComposioHQ/composio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.py
More file actions
114 lines (101 loc) · 3.91 KB
/
Copy pathprovider.py
File metadata and controls
114 lines (101 loc) · 3.91 KB
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
"""
Google AI Python Gemini tool spec.
"""
import typing as t
from proto.marshal.collections.maps import MapComposite
from vertexai.generative_models import (
Content,
FunctionDeclaration,
GenerationResponse,
Part,
)
from composio.core.provider import NonAgenticProvider
from composio.types import Modifiers, Tool, ToolExecutionResponse
from composio.utils.json_schema import dereference_json_schema
from composio.utils.shared import normalize_tool_arguments
def _convert_map_composite(obj):
if isinstance(obj, MapComposite):
return {k: _convert_map_composite(v) for k, v in obj.items()}
if isinstance(obj, (list, tuple)):
return [_convert_map_composite(item) for item in obj]
return obj
class GoogleProvider(
NonAgenticProvider[FunctionDeclaration, list[FunctionDeclaration]],
name="google",
):
"""
Composio toolset for Google AI Python Gemini framework.
"""
def wrap_tool(self, tool: Tool) -> FunctionDeclaration:
"""Wraps composio tool as Google AI Python Gemini FunctionDeclaration object."""
input_parameters = dereference_json_schema(
tool.input_parameters,
on_unresolved="sentinel",
)
# Clean up properties by removing 'examples' field
properties = t.cast(
dict[str, dict],
input_parameters.get("properties", {}),
)
cleaned_properties = {
prop_name: {k: v for k, v in prop_schema.items() if k != "examples"}
for prop_name, prop_schema in properties.items()
}
return FunctionDeclaration(
name=tool.slug,
description=tool.description,
parameters={
"type": "object",
"properties": cleaned_properties,
"required": input_parameters.get("required", []),
},
)
def wrap_tools(self, tools: t.Sequence[Tool]) -> list[FunctionDeclaration]:
return [self.wrap_tool(tool) for tool in tools]
def execute_tool_call(
self,
user_id: str,
function_call: t.Any,
modifiers: t.Optional[Modifiers] = None,
) -> ToolExecutionResponse:
"""
Execute a function call.
:param function_call: Function call metadata from Gemini model response.
:param user_id: User ID to use for executing the function call.
:return: Object containing output data from the function call.
"""
# Gemini returns args as a MapComposite; normalize after converting to a
# plain dict so a stringified payload is handled uniformly too (issue #2406).
return self.execute_tool(
slug=function_call.name,
arguments=normalize_tool_arguments(
_convert_map_composite(function_call.args)
),
modifiers=modifiers,
user_id=user_id,
)
def handle_response(
self,
user_id: str,
response: GenerationResponse,
modifiers: t.Optional[Modifiers] = None,
) -> t.List[ToolExecutionResponse]:
"""
Handle response from Google AI Python Gemini model.
:param response: Generation response from the Gemini model.
:param user_id: User ID to use for executing the function call.
:return: A list of output objects from the function calls.
"""
outputs = []
for candidate in response.candidates:
if isinstance(candidate.content, Content) and candidate.content.parts:
for part in candidate.content.parts:
if isinstance(part, Part) and part.function_call:
outputs.append(
self.execute_tool_call(
user_id=user_id,
function_call=part.function_call,
modifiers=modifiers,
)
)
return outputs