-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathgemini_adapter.py
More file actions
364 lines (295 loc) · 11.8 KB
/
gemini_adapter.py
File metadata and controls
364 lines (295 loc) · 11.8 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
Google Gemini Integration
Wraps Google's Generative AI SDK with Agent OS governance.
Usage:
from agent_os.integrations.gemini_adapter import GeminiKernel
import google.generativeai as genai
kernel = GeminiKernel(policy=GovernancePolicy(
max_tokens=4096,
allowed_tools=["web_search"],
blocked_patterns=["password"],
))
model = genai.GenerativeModel("gemini-pro")
governed = kernel.wrap(model)
response = governed.generate_content("Hello")
Features:
- Pre-execution policy checks on prompts
- Tool call interception and validation
- Token limit enforcement
- Content filtering via blocked patterns
- Audit logging for all calls
- Health check endpoint
"""
from __future__ import annotations
import logging
import time
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any
from .base import BaseIntegration, ExecutionContext, GovernancePolicy
logger = logging.getLogger("agent_os.gemini")
try:
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", FutureWarning)
import google.generativeai as _genai_mod # noqa: F401
_HAS_GENAI = True
except ImportError:
_HAS_GENAI = False
def _check_genai_available() -> None:
"""Raise a helpful error when the ``google-generativeai`` package is missing."""
if not _HAS_GENAI:
raise ImportError(
"The 'google-generativeai' package is required for GeminiKernel. "
"Install it with: pip install google-generativeai"
)
@dataclass
class GeminiContext(ExecutionContext):
"""Execution context for Google Gemini interactions.
Attributes:
model_name: The Gemini model used for this session.
generation_ids: Recorded generation response identifiers.
function_calls: History of function calls returned by Gemini.
prompt_tokens: Cumulative prompt tokens consumed.
completion_tokens: Cumulative candidate tokens consumed.
"""
model_name: str = ""
generation_ids: list[str] = field(default_factory=list)
function_calls: list[dict[str, Any]] = field(default_factory=list)
prompt_tokens: int = 0
completion_tokens: int = 0
class PolicyViolationError(Exception):
"""Raised when a Gemini request violates governance policy."""
pass
class GeminiKernel(BaseIntegration):
"""Google Gemini adapter for Agent OS.
Provides governance for ``GenerativeModel.generate_content()`` calls
including policy enforcement, tool-call validation, token tracking,
and audit logging.
Example:
>>> kernel = GeminiKernel(policy=GovernancePolicy(max_tokens=8192))
>>> governed = kernel.wrap(genai.GenerativeModel("gemini-pro"))
>>> response = governed.generate_content("Explain quantum computing")
"""
def __init__(
self,
policy: GovernancePolicy | None = None,
) -> None:
"""Initialise the Gemini governance kernel.
Args:
policy: Governance policy to enforce. Uses default when ``None``.
"""
super().__init__(policy)
self._wrapped_models: dict[int, Any] = {}
self._start_time = time.monotonic()
self._last_error: str | None = None
def wrap(self, model: Any) -> GovernedGeminiModel:
"""Wrap a Gemini GenerativeModel with governance.
Args:
model: A ``google.generativeai.GenerativeModel`` instance.
Returns:
A ``GovernedGeminiModel`` that enforces policy on all
``generate_content()`` calls.
"""
_check_genai_available()
model_id = id(model)
model_name = getattr(model, "model_name", "unknown")
ctx = GeminiContext(
agent_id=f"gemini-{model_id}",
session_id=f"gem-{int(time.time())}",
policy=self.policy,
model_name=model_name,
)
self.contexts[ctx.agent_id] = ctx
self._wrapped_models[model_id] = model
return GovernedGeminiModel(
model=model,
kernel=self,
ctx=ctx,
)
def unwrap(self, governed_agent: Any) -> Any:
"""Retrieve the original unwrapped Gemini model.
Args:
governed_agent: A ``GovernedGeminiModel`` or any object.
Returns:
The original GenerativeModel if applicable, otherwise
*governed_agent* as-is.
"""
if isinstance(governed_agent, GovernedGeminiModel):
return governed_agent._model
return governed_agent
def health_check(self) -> dict[str, Any]:
"""Return adapter health status.
Returns:
A dict with ``status``, ``backend``, ``last_error``, and
``uptime_seconds`` keys.
"""
uptime = time.monotonic() - self._start_time
has_models = bool(self._wrapped_models)
status = "degraded" if self._last_error else "healthy"
return {
"status": status,
"backend": "gemini",
"backend_connected": has_models,
"last_error": self._last_error,
"uptime_seconds": round(uptime, 2),
}
class GovernedGeminiModel:
"""Gemini GenerativeModel wrapped with Agent OS governance.
Intercepts ``generate_content()`` for policy enforcement while
proxying all other attributes to the underlying model.
"""
def __init__(
self,
model: Any,
kernel: GeminiKernel,
ctx: GeminiContext,
) -> None:
self._model = model
self._kernel = kernel
self._ctx = ctx
def generate_content(self, contents: Any, **kwargs: Any) -> Any:
"""Generate content with governance enforcement.
Validates prompt content against blocked patterns, enforces
tool-call allowlists, checks token limits, and records an
audit trail.
Args:
contents: The prompt content (string, list, or Content object).
**kwargs: Forwarded to ``model.generate_content()``.
Returns:
The Gemini generation response.
Raises:
PolicyViolationError: If a governance policy is violated.
"""
# --- pre-execution checks ---
content_str = str(contents)
allowed, reason = self._kernel.pre_execute(self._ctx, content_str)
if not allowed:
raise PolicyViolationError(f"Content blocked: {reason}")
# Validate tools against policy
tools = kwargs.get("tools")
if tools:
self._validate_tools(tools)
# Audit log
logger.info(
"Gemini generate_content | agent=%s model=%s",
self._ctx.agent_id,
self._ctx.model_name,
)
# --- execute ---
try:
response = self._kernel._wrapped_models.get(
id(self._model), self._model
).generate_content(contents, **kwargs)
except Exception as exc:
self._kernel._last_error = str(exc)
raise
# --- post-execution checks ---
gen_id = f"gen-{int(time.time())}-{self._ctx.call_count}"
self._ctx.generation_ids.append(gen_id)
# Track tokens from usage_metadata
usage = getattr(response, "usage_metadata", None)
if usage:
self._ctx.prompt_tokens += getattr(usage, "prompt_token_count", 0)
self._ctx.completion_tokens += getattr(
usage, "candidates_token_count", 0
)
total = self._ctx.prompt_tokens + self._ctx.completion_tokens
if total > self._kernel.policy.max_tokens:
raise PolicyViolationError(
f"Token limit exceeded: {total} > "
f"{self._kernel.policy.max_tokens}"
)
# Check for function calls in candidates
candidates = getattr(response, "candidates", [])
for candidate in candidates:
content = getattr(candidate, "content", None)
if content is None:
continue
parts = getattr(content, "parts", [])
for part in parts:
fn_call = getattr(part, "function_call", None)
if fn_call is None:
continue
fn_name = getattr(fn_call, "name", "")
call_info = {
"name": fn_name,
"args": dict(getattr(fn_call, "args", {})),
"timestamp": datetime.now().isoformat(),
}
self._ctx.function_calls.append(call_info)
self._ctx.tool_calls.append(call_info)
if len(self._ctx.tool_calls) > self._kernel.policy.max_tool_calls:
raise PolicyViolationError(
f"Tool call limit exceeded: "
f"{len(self._ctx.tool_calls)} > "
f"{self._kernel.policy.max_tool_calls}"
)
if self._kernel.policy.allowed_tools:
if fn_name not in self._kernel.policy.allowed_tools:
raise PolicyViolationError(
f"Tool not allowed: {fn_name}"
)
if self._kernel.policy.require_human_approval:
raise PolicyViolationError(
f"Tool '{fn_name}' requires human approval per governance policy"
)
# Post-execute bookkeeping
self._kernel.post_execute(self._ctx, response)
return response
def get_context(self) -> GeminiContext:
"""Return the execution context with the full audit trail.
Returns:
The ``GeminiContext`` for this governed model.
"""
return self._ctx
def get_token_usage(self) -> dict[str, Any]:
"""Return cumulative token usage statistics.
Returns:
A dict with ``prompt_tokens``, ``completion_tokens``,
``total_tokens``, and ``limit``.
"""
return {
"prompt_tokens": self._ctx.prompt_tokens,
"completion_tokens": self._ctx.completion_tokens,
"total_tokens": self._ctx.prompt_tokens + self._ctx.completion_tokens,
"limit": self._kernel.policy.max_tokens,
}
def _validate_tools(self, tools: Any) -> None:
"""Validate tool definitions against policy allowlist.
Args:
tools: Tool definitions from the request.
Raises:
PolicyViolationError: If a tool is not in the allowed list.
"""
if not self._kernel.policy.allowed_tools:
return
tool_list = tools if isinstance(tools, list) else [tools]
for tool in tool_list:
declarations = getattr(tool, "function_declarations", None)
if declarations:
for decl in declarations:
name = getattr(decl, "name", "") if not isinstance(decl, dict) else decl.get("name", "")
if name and name not in self._kernel.policy.allowed_tools:
raise PolicyViolationError(f"Tool not allowed: {name}")
def __getattr__(self, name: str) -> Any:
"""Proxy attribute access to the underlying Gemini model."""
return getattr(self._model, name)
def wrap_model(
model: Any,
policy: GovernancePolicy | None = None,
) -> GovernedGeminiModel:
"""Quick wrapper for Gemini GenerativeModel.
Args:
model: A ``google.generativeai.GenerativeModel`` instance.
policy: Optional governance policy.
Returns:
A governed model.
Example:
>>> from agent_os.integrations.gemini_adapter import wrap_model
>>> governed = wrap_model(my_model)
>>> response = governed.generate_content("Hello")
"""
return GeminiKernel(policy=policy).wrap(model)