|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2024 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import contextlib |
| 19 | + |
| 20 | +_tool_names_to_append = [] |
| 21 | + |
| 22 | + |
| 23 | +@contextlib.contextmanager |
| 24 | +def tool_context_manager(tool_name: str) -> None: |
| 25 | + """Context manager for appending tool name to client instantiations. |
| 26 | +
|
| 27 | + Most client instantiations occur at construction time. There are a few |
| 28 | + exceptions such as generate_content that uses lazy instantiation at |
| 29 | + inference time (b/328511605). |
| 30 | +
|
| 31 | + Example Usage: |
| 32 | +
|
| 33 | + aiplatform.init(...) |
| 34 | + with telemetry.tool_context_manager('ClientName'): |
| 35 | + model = GenerativeModel("gemini-pro") |
| 36 | + responses = model.generate_content("Why is the sky blue?", stream=True) |
| 37 | +
|
| 38 | + Args: |
| 39 | + tool_name: The name of the client library to attribute usage to |
| 40 | +
|
| 41 | + Returns: |
| 42 | + None |
| 43 | + """ |
| 44 | + _append_tool_name(tool_name) |
| 45 | + try: |
| 46 | + yield |
| 47 | + finally: |
| 48 | + _pop_tool_name(tool_name) |
| 49 | + |
| 50 | + |
| 51 | +def _append_tool_name(tool_name: str) -> None: |
| 52 | + _tool_names_to_append.append(tool_name) |
| 53 | + |
| 54 | + |
| 55 | +def _pop_tool_name(tool_name: str) -> None: |
| 56 | + if not _tool_names_to_append or _tool_names_to_append[-1] != tool_name: |
| 57 | + raise RuntimeError( |
| 58 | + "Tool context error detected. This can occur due to parallelization." |
| 59 | + ) |
| 60 | + _tool_names_to_append.pop() |
0 commit comments