Skip to content

google_tracer

GenerateContentResponse(done, iterator, result, impacts, *args, **kwargs)

Bases: GenerateContentResponse

Wrapper of google.generativeai.types.GenerateContentResponse with ImpactsOutput

Source code in ecologits/tracers/google_tracer.py
def __init__(self, done, iterator, result, impacts, *args, **kwargs) -> None:   # noqa: ANN001 ANN002 ANN003
    super().__init__(done, iterator, result, impacts, *args, **kwargs)
    self.impacts = impacts

AsyncGenerateContentResponse(done, iterator, result, impacts, *args, **kwargs)

Bases: AsyncGenerateContentResponse

Wrapper of google.generativeai.types.AsyncGenerateContentResponse with ImpactsOutput

Source code in ecologits/tracers/google_tracer.py
def __init__(self, done, iterator, result, impacts, *args, **kwargs) -> None: # noqa: ANN001 ANN002 ANN003
    super().__init__(done, iterator, result, impacts, *args, **kwargs)
    self.impacts = impacts

GoogleInstrumentor()

Instrumentor initialized by EcoLogits to automatically wrap all Google calls

Source code in ecologits/tracers/google_tracer.py
def __init__(self) -> None:
    self.wrapped_methods = [
        {
            "module": "google.generativeai",
            "name": "GenerativeModel.generate_content",
            "wrapper": google_chat_wrapper,
        },
        {
            "module": "google.generativeai",
            "name": "GenerativeModel.generate_content_async",
            "wrapper": google_async_chat_wrapper
        }
    ]

google_chat_wrapper(wrapped, instance, args, kwargs)

Function that wraps a Google answer with computed impacts

Parameters:

Name Type Description Default
wrapped Callable

Callable that returns the LLM response

required
instance GenerativeModel

Never used - for compatibility with wrapt

required
args Any

Arguments of the callable

required
kwargs Any

Keyword arguments of the callable

required

Returns:

Type Description
Union[GenerateContentResponse, Iterable[GenerateContentResponse]]

A wrapped GenerateContentResponse or Iterable[GenerateContentResponse] with impacts

Source code in ecologits/tracers/google_tracer.py
def google_chat_wrapper(
    wrapped: Callable, instance: GenerativeModel, args: Any, kwargs: Any
) -> Union[GenerateContentResponse, Iterable[GenerateContentResponse]]:
    """
    Function that wraps a Google answer with computed impacts

    Args:
        wrapped: Callable that returns the LLM response
        instance: Never used - for compatibility with `wrapt`
        args: Arguments of the callable
        kwargs: Keyword arguments of the callable

    Returns:
        A wrapped `GenerateContentResponse` or `Iterable[GenerateContentResponse]` with impacts
    """

    if kwargs.get("stream", False):
        return google_chat_wrapper_stream(wrapped, instance, args, kwargs)
    else:
        return google_chat_wrapper_non_stream(wrapped, instance, args, kwargs)

google_async_chat_wrapper(wrapped, instance, args, kwargs) async

Function that wraps a Google answer with computed impacts in async mode

Parameters:

Name Type Description Default
wrapped Callable

Async callable that returns the LLM response

required
instance GenerativeModel

Never used - for compatibility with wrapt

required
args Any

Arguments of the callable

required
kwargs Any

Keyword arguments of the callable

required

Returns:

Type Description
Union[AsyncGenerateContentResponse, Iterable[AsyncGenerateContentResponse]]

A wrapped AsyncGenerateContentResponse or Iterable[AsyncGenerateContentResponse]] with impacts

Source code in ecologits/tracers/google_tracer.py
async def google_async_chat_wrapper(
    wrapped: Callable, instance: GenerativeModel, args: Any, kwargs: Any
) -> Union[AsyncGenerateContentResponse, Iterable[AsyncGenerateContentResponse]]:
    """
    Function that wraps a Google answer with computed impacts in async mode

    Args:
        wrapped: Async callable that returns the LLM response
        instance: Never used - for compatibility with `wrapt`
        args: Arguments of the callable
        kwargs: Keyword arguments of the callable

    Returns:
        A wrapped `AsyncGenerateContentResponse` or `Iterable[AsyncGenerateContentResponse]]` with impacts
    """

    if kwargs.get("stream", False):
        return google_async_chat_wrapper_stream(wrapped, instance, args, kwargs)
    else:
        return await google_async_chat_wrapper_non_stream(wrapped, instance, args, kwargs)