feat: add Gemini client identification.

This commit is contained in:
thorwebdev
2025-12-05 12:06:28 -05:00
parent 3d93285bdf
commit af089a65ae
4 changed files with 54 additions and 2 deletions

View File

@@ -24,6 +24,7 @@ from loguru import logger
from PIL import Image from PIL import Image
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from pipecat import __version__
from pipecat.adapters.schemas.tools_schema import ToolsSchema from pipecat.adapters.schemas.tools_schema import ToolsSchema
from pipecat.adapters.services.gemini_adapter import GeminiLLMAdapter from pipecat.adapters.services.gemini_adapter import GeminiLLMAdapter
from pipecat.frames.frames import ( from pipecat.frames.frames import (
@@ -682,6 +683,21 @@ class GeminiLiveLLMService(LLMService):
self._context = None self._context = None
self._api_key = api_key self._api_key = api_key
self._http_options = http_options self._http_options = http_options
# Add client header
client_header = {"x-goog-api-client": f"pipecat/{__version__}"}
if self._http_options is None:
self._http_options = {"headers": client_header}
elif isinstance(self._http_options, dict):
if "headers" in self._http_options:
self._http_options["headers"].update(client_header)
else:
self._http_options["headers"] = client_header
elif hasattr(self._http_options, "headers"):
if self._http_options.headers is None:
self._http_options.headers = client_header
else:
self._http_options.headers.update(client_header)
self._session: AsyncSession = None self._session: AsyncSession = None
self._connection_task = None self._connection_task = None

View File

@@ -126,6 +126,7 @@ class GeminiLiveVertexLLMService(GeminiLiveLLMService):
credentials=self._credentials, credentials=self._credentials,
project=self._project_id, project=self._project_id,
location=self._location, location=self._location,
http_options=self._http_options,
) )
@property @property

View File

@@ -16,12 +16,13 @@ import os
# Suppress gRPC fork warnings # Suppress gRPC fork warnings
os.environ["GRPC_ENABLE_FORK_SUPPORT"] = "false" os.environ["GRPC_ENABLE_FORK_SUPPORT"] = "false"
from typing import AsyncGenerator, Optional from typing import Any, AsyncGenerator, Optional
from loguru import logger from loguru import logger
from PIL import Image from PIL import Image
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from pipecat import __version__
from pipecat.frames.frames import ErrorFrame, Frame, URLImageRawFrame from pipecat.frames.frames import ErrorFrame, Frame, URLImageRawFrame
from pipecat.services.image_service import ImageGenService from pipecat.services.image_service import ImageGenService
@@ -60,6 +61,7 @@ class GoogleImageGenService(ImageGenService):
*, *,
api_key: str, api_key: str,
params: Optional[InputParams] = None, params: Optional[InputParams] = None,
http_options: Optional[Any] = None,
**kwargs, **kwargs,
): ):
"""Initialize the GoogleImageGenService with API key and parameters. """Initialize the GoogleImageGenService with API key and parameters.
@@ -67,11 +69,28 @@ class GoogleImageGenService(ImageGenService):
Args: Args:
api_key: Google AI API key for authentication. api_key: Google AI API key for authentication.
params: Configuration parameters for image generation. Defaults to InputParams(). params: Configuration parameters for image generation. Defaults to InputParams().
http_options: HTTP options for the client.
**kwargs: Additional arguments passed to the parent ImageGenService. **kwargs: Additional arguments passed to the parent ImageGenService.
""" """
super().__init__(**kwargs) super().__init__(**kwargs)
self._params = params or GoogleImageGenService.InputParams() self._params = params or GoogleImageGenService.InputParams()
self._client = genai.Client(api_key=api_key)
# Add client header
client_header = {"x-goog-api-client": f"pipecat/{__version__}"}
if http_options is None:
http_options = {"headers": client_header}
elif isinstance(http_options, dict):
if "headers" in http_options:
http_options["headers"].update(client_header)
else:
http_options["headers"] = client_header
elif hasattr(http_options, "headers"):
if http_options.headers is None:
http_options.headers = client_header
else:
http_options.headers.update(client_header)
self._client = genai.Client(api_key=api_key, http_options=http_options)
self.set_model_name(self._params.model) self.set_model_name(self._params.model)
def can_generate_metrics(self) -> bool: def can_generate_metrics(self) -> bool:

View File

@@ -22,6 +22,7 @@ from loguru import logger
from PIL import Image from PIL import Image
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from pipecat import __version__
from pipecat.adapters.services.gemini_adapter import GeminiLLMAdapter, GeminiLLMInvocationParams from pipecat.adapters.services.gemini_adapter import GeminiLLMAdapter, GeminiLLMInvocationParams
from pipecat.frames.frames import ( from pipecat.frames.frames import (
AudioRawFrame, AudioRawFrame,
@@ -715,6 +716,21 @@ class GoogleLLMService(LLMService):
self._system_instruction = system_instruction self._system_instruction = system_instruction
self._http_options = http_options self._http_options = http_options
# Add client header
client_header = {"x-goog-api-client": f"pipecat/{__version__}"}
if self._http_options is None:
self._http_options = {"headers": client_header}
elif isinstance(self._http_options, dict):
if "headers" in self._http_options:
self._http_options["headers"].update(client_header)
else:
self._http_options["headers"] = client_header
elif hasattr(self._http_options, "headers"):
if self._http_options.headers is None:
self._http_options.headers = client_header
else:
self._http_options.headers.update(client_header)
self._settings = { self._settings = {
"max_tokens": params.max_tokens, "max_tokens": params.max_tokens,
"temperature": params.temperature, "temperature": params.temperature,