Skip to content

Commit b19079d

Browse files
authored
[Firebase AI] Add logic for Developer Live API (#1288)
* [Firebase AI] Add logic for Developer Live API * Update LiveSessionResponse.cs * Update readme.md
1 parent 43847e4 commit b19079d

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

docs/readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ Support
109109

110110
Release Notes
111111
-------------
112+
### Upcoming
113+
- Changes
114+
- Firebase AI: Add support for Developer API backend to LiveSessions.
115+
112116
### 13.0.0
113117
- Changes
114118
- General: Update to Firebase C++ SDK version 13.0.0.

firebaseai/src/FirebaseAI.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ public GenerativeModel GetGenerativeModel(
165165
///
166166
/// - Note: Refer to [Gemini models](https://firebase.google.com/docs/vertex-ai/gemini-models) for
167167
/// guidance on choosing an appropriate model for your use case.
168-
///
169-
/// - Note: Currently only supports the VertexAI backend.
170168
/// </summary>
171169
/// <param name="modelName">The name of the model to use, for example `"gemini-2.0-flash-live-preview-04-09"`; see
172170
/// [available model names
@@ -183,10 +181,6 @@ public LiveGenerativeModel GetLiveModel(
183181
Tool[] tools = null,
184182
ModelContent? systemInstruction = null,
185183
RequestOptions? requestOptions = null) {
186-
if (_backend.Provider != Backend.InternalProvider.VertexAI) {
187-
throw new NotSupportedException("LiveGenerativeModel is currently only supported with the VertexAI backend.");
188-
}
189-
190184
return new LiveGenerativeModel(_firebaseApp, _backend, modelName,
191185
liveGenerationConfig, tools,
192186
systemInstruction, requestOptions);

firebaseai/src/LiveGenerativeModel.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,30 @@ internal LiveGenerativeModel(FirebaseApp firebaseApp,
7070
}
7171

7272
private string GetURL() {
73-
return "wss://firebasevertexai.googleapis.com/ws" +
74-
"/google.firebase.vertexai.v1beta.LlmBidiService/BidiGenerateContent" +
75-
$"/locations/{_backend.Location}" +
76-
$"?key={_firebaseApp.Options.ApiKey}";
73+
if (_backend.Provider == FirebaseAI.Backend.InternalProvider.VertexAI) {
74+
return "wss://firebasevertexai.googleapis.com/ws" +
75+
"/google.firebase.vertexai.v1beta.LlmBidiService/BidiGenerateContent" +
76+
$"/locations/{_backend.Location}" +
77+
$"?key={_firebaseApp.Options.ApiKey}";
78+
} else if (_backend.Provider == FirebaseAI.Backend.InternalProvider.GoogleAI) {
79+
return "wss://firebasevertexai.googleapis.com/ws" +
80+
"/google.firebase.vertexai.v1beta.GenerativeService/BidiGenerateContent" +
81+
$"?key={_firebaseApp.Options.ApiKey}";
82+
} else {
83+
throw new NotSupportedException($"Missing support for backend: {_backend.Provider}");
84+
}
85+
}
86+
87+
private string GetModelName() {
88+
if (_backend.Provider == FirebaseAI.Backend.InternalProvider.VertexAI) {
89+
return $"projects/{_firebaseApp.Options.ProjectId}/locations/{_backend.Location}" +
90+
$"/publishers/google/models/{_modelName}";
91+
} else if (_backend.Provider == FirebaseAI.Backend.InternalProvider.GoogleAI) {
92+
return $"projects/{_firebaseApp.Options.ProjectId}" +
93+
$"/models/{_modelName}";
94+
} else {
95+
throw new NotSupportedException($"Missing support for backend: {_backend.Provider}");
96+
}
7797
}
7898

7999
/// <summary>

firebaseai/src/LiveSessionResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public IReadOnlyList<byte[]> Audio {
5858
if (Message is LiveSessionContent content) {
5959
return content.Content?.Parts
6060
.OfType<ModelContent.InlineDataPart>()
61-
.Where(part => part.MimeType == "audio/pcm")
61+
.Where(part => part.MimeType.StartsWith("audio/pcm"))
6262
.Select(part => part.Data.ToArray())
6363
.ToList();
6464
}

firebaseai/src/ModelContent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ internal Dictionary<string, object> ToJson() {
312312
/// This method is used for deserializing JSON responses and should not be called directly.
313313
/// </summary>
314314
internal static ModelContent FromJson(Dictionary<string, object> jsonDict) {
315-
// Both role and parts are required keys
316315
return new ModelContent(
317-
jsonDict.ParseValue<string>("role", JsonParseOptions.ThrowEverything),
316+
// If the role is missing, default to model since this is likely coming from the backend.
317+
jsonDict.ParseValue("role", defaultValue: "model"),
318318
// Unknown parts are converted to null, which we then want to filter out here
319319
jsonDict.ParseObjectList("parts", PartFromJson, JsonParseOptions.ThrowEverything).Where(p => p is not null));
320320
}

0 commit comments

Comments
 (0)