Skip to content

Commit 59cdb86

Browse files
Add Gemini integration with optional dependencies
1 parent 8183f0a commit 59cdb86

File tree

6 files changed

+103
-4
lines changed

6 files changed

+103
-4
lines changed

fishjam/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# pylint: disable=locally-disabled, no-name-in-module, import-error
99

1010
# Exceptions and Server Messages
11-
from fishjam import agent, errors, events, peer, room
11+
from fishjam import agent, errors, events, peer, room, version
1212
from fishjam._openapi_client.models import PeerMetadata
1313

1414
# API
@@ -24,6 +24,8 @@
2424
RoomOptions,
2525
)
2626

27+
__version__ = version.__version__
28+
2729
__all__ = [
2830
"FishjamClient",
2931
"FishjamNotifier",

fishjam/integrations/__init__.py

Whitespace-only changes.

fishjam/integrations/gemini.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 functools import singledispatch
13+
from typing import Optional, Union
14+
15+
from fishjam import AgentOutputOptions
16+
from fishjam.version import get_version
17+
18+
19+
def _get_headers():
20+
return {"x-goog-api-client": f"fishjam-python-server-sdk/{get_version()}"}
21+
22+
23+
@singledispatch
24+
def _add_fishjam_header(
25+
http_options: Optional[Union[types.HttpOptions, types.HttpOptionsDict]],
26+
) -> Union[types.HttpOptions, types.HttpOptionsDict]: ...
27+
28+
29+
@_add_fishjam_header.register
30+
def _(http_options: types.HttpOptions) -> types.HttpOptions:
31+
http_options.headers = (http_options.headers or {}) | _get_headers()
32+
return http_options
33+
34+
35+
@_add_fishjam_header.register
36+
def _(http_options: types.HttpOptionsDict) -> types.HttpOptionsDict:
37+
headers = (http_options.get("headers") or {}) | _get_headers()
38+
return http_options | {"headers": headers}
39+
40+
41+
@_add_fishjam_header.register
42+
def _(_http_options: None) -> types.HttpOptionsDict:
43+
return {"headers": _get_headers()}
44+
45+
46+
class _GeminiIntegration:
47+
def create_client(
48+
self,
49+
vertexai: Optional[bool] = None,
50+
api_key: Optional[str] = None,
51+
credentials: Optional[Credentials] = None,
52+
project: Optional[str] = None,
53+
location: Optional[str] = None,
54+
debug_config: Optional[DebugConfig] = None,
55+
http_options: Optional[Union[types.HttpOptions, types.HttpOptionsDict]] = None,
56+
):
57+
full_http_options = _add_fishjam_header(http_options)
58+
59+
return genai.Client(
60+
vertexai=vertexai,
61+
api_key=api_key,
62+
credentials=credentials,
63+
project=project,
64+
location=location,
65+
debug_config=debug_config,
66+
http_options=full_http_options,
67+
)
68+
69+
@property
70+
def GeminiInputAudioSettings(self) -> AgentOutputOptions:
71+
return AgentOutputOptions(
72+
audio_format="pcm16",
73+
audio_sample_rate=16_000,
74+
)
75+
76+
77+
GeminiIntegration = _GeminiIntegration()

fishjam/version.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from importlib.metadata import version
2+
3+
__version__ = version("fishjam-server-sdk")
4+
5+
6+
def get_version():
7+
return __version__

pyproject.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ generate_docusaurus = "scripts:generate_docusaurus"
3333
update_client = "scripts:update_client"
3434
room_manager = "scripts:start_room_manager"
3535

36+
[project.optional-dependencies]
37+
gemini = ["google-genai>=1.43.0"]
38+
3639
[dependency-groups]
3740
dev = [
3841
"betterproto[compiler]== 2.0.0b6",
@@ -55,7 +58,12 @@ test = [
5558
default-groups = ["dev", "test"]
5659

5760
[tool.uv.workspace]
58-
members = ["examples/transcription", ".", "examples/poet_chat", "examples/selective_subscription"]
61+
members = [
62+
"examples/transcription",
63+
".",
64+
"examples/poet_chat",
65+
"examples/selective_subscription",
66+
]
5967

6068
[tool.hatch.build.targets.sdist]
6169
include = ["fishjam"]
@@ -86,8 +94,6 @@ convention = "google"
8694
"scripts.py" = ["D"]
8795

8896

89-
90-
9197
[tool.pytest.ini_options]
9298
markers = [
9399
"file_component_sources: Tests requiring files uploaded for File Component",

uv.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)