|
| 1 | +try: |
| 2 | + from google import genai |
| 3 | + from google.auth.credentials import Credentials |
| 4 | + from google.genai import types |
| 5 | + from google.genai.client import DebugConfig |
| 6 | +except ImportError: |
| 7 | + raise ImportError( |
| 8 | + "To use the Fishjam Gemini integration, you need to import the `gemini` extra. " |
| 9 | + "Install it with `pip install 'fishjam-server-sdk[gemini]'`" |
| 10 | + ) |
| 11 | + |
| 12 | +from typing import Optional, Union |
| 13 | + |
| 14 | +from fishjam import AgentOutputOptions |
| 15 | +from fishjam.agent import OutgoingAudioTrackOptions |
| 16 | +from fishjam.events import TrackEncoding |
| 17 | +from fishjam.version import get_version |
| 18 | + |
| 19 | + |
| 20 | +def _get_headers(): |
| 21 | + return {"x-goog-api-client": f"fishjam-python-server-sdk/{get_version()}"} |
| 22 | + |
| 23 | + |
| 24 | +def _add_fishjam_header( |
| 25 | + http_options: Optional[Union[types.HttpOptions, types.HttpOptionsDict]], |
| 26 | +) -> Union[types.HttpOptions, types.HttpOptionsDict]: |
| 27 | + if http_options is None: |
| 28 | + return _add_fishjam_header_none() |
| 29 | + if isinstance(http_options, types.HttpOptions): |
| 30 | + return _add_fishjam_header_object(http_options) |
| 31 | + return _add_fishjam_header_dict(http_options) |
| 32 | + |
| 33 | + |
| 34 | +def _add_fishjam_header_object(http_options: types.HttpOptions) -> types.HttpOptions: |
| 35 | + http_options.headers = (http_options.headers or {}) | _get_headers() |
| 36 | + return http_options |
| 37 | + |
| 38 | + |
| 39 | +def _add_fishjam_header_dict( |
| 40 | + http_options: types.HttpOptionsDict, |
| 41 | +) -> types.HttpOptionsDict: |
| 42 | + headers = (http_options.get("headers") or {}) | _get_headers() |
| 43 | + return http_options | {"headers": headers} |
| 44 | + |
| 45 | + |
| 46 | +def _add_fishjam_header_none() -> types.HttpOptionsDict: |
| 47 | + return {"headers": _get_headers()} |
| 48 | + |
| 49 | + |
| 50 | +class _GeminiIntegration: |
| 51 | + def create_client( |
| 52 | + self, |
| 53 | + vertexai: Optional[bool] = None, |
| 54 | + api_key: Optional[str] = None, |
| 55 | + credentials: Optional[Credentials] = None, |
| 56 | + project: Optional[str] = None, |
| 57 | + location: Optional[str] = None, |
| 58 | + debug_config: Optional[DebugConfig] = None, |
| 59 | + http_options: Optional[Union[types.HttpOptions, types.HttpOptionsDict]] = None, |
| 60 | + ): |
| 61 | + """Creates and configures a Fishjam-compatible Google GenAI Client. |
| 62 | +
|
| 63 | + See `genai.Client` for configuration options. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + genai.Client: An instantiated and configured Gemini client. |
| 67 | + """ |
| 68 | + full_http_options = _add_fishjam_header(http_options) |
| 69 | + |
| 70 | + return genai.Client( |
| 71 | + vertexai=vertexai, |
| 72 | + api_key=api_key, |
| 73 | + credentials=credentials, |
| 74 | + project=project, |
| 75 | + location=location, |
| 76 | + debug_config=debug_config, |
| 77 | + http_options=full_http_options, |
| 78 | + ) |
| 79 | + |
| 80 | + @property |
| 81 | + def GEMINI_INPUT_AUDIO_SETTINGS(self) -> AgentOutputOptions: |
| 82 | + """Audio configuration required for Gemini input. |
| 83 | +
|
| 84 | + Gemini consumes PCM16 audio at 16,000 Hz. |
| 85 | +
|
| 86 | + Returns: |
| 87 | + AgentOutputOptions: Agent options compatible with the Gemini Live API. |
| 88 | + """ |
| 89 | + return AgentOutputOptions( |
| 90 | + audio_format="pcm16", |
| 91 | + audio_sample_rate=16_000, |
| 92 | + ) |
| 93 | + |
| 94 | + @property |
| 95 | + def GEMINI_OUTPUT_AUDIO_SETTINGS(self) -> OutgoingAudioTrackOptions: |
| 96 | + """Audio configuration for an agent's output track. |
| 97 | +
|
| 98 | + Gemini produces PCM16 audio at 24,000 Hz. |
| 99 | +
|
| 100 | + Returns: |
| 101 | + OutgoingAudioTrackOptions: Track options compatible with the Gemini Live API |
| 102 | + """ |
| 103 | + return OutgoingAudioTrackOptions( |
| 104 | + encoding=TrackEncoding.TRACK_ENCODING_PCM16, |
| 105 | + sample_rate=24_000, |
| 106 | + channels=1, |
| 107 | + ) |
| 108 | + |
| 109 | + @property |
| 110 | + def GEMINI_AUDIO_MIME_TYPE(self) -> str: |
| 111 | + """The mime type for Gemini audio input.""" |
| 112 | + return "audio/pcm;rate=16000" |
| 113 | + |
| 114 | + |
| 115 | +GeminiIntegration = _GeminiIntegration() |
| 116 | +"""Integration with the Gemini Live API.""" |
0 commit comments