- Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathLiveGenerativeModel.cs
142 lines (126 loc) · 5.62 KB
/
LiveGenerativeModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Net.WebSockets;
usingSystem.Text;
usingSystem.Threading;
usingSystem.Threading.Tasks;
usingFirebase.VertexAI.Internal;
usingGoogle.MiniJSON;
namespaceFirebase.VertexAI{
/// <summary>
/// A live, generative AI model for real-time interaction.
///
/// See the [Cloud
/// documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/multimodal-live)
/// for more details about the low-latency, two-way interactions that use text,
/// audio, and video input, with audio and text output.
///
/// > Warning: For Vertex AI in Firebase, Live Model
/// is in Public Preview, which means that the feature is not subject to any SLA
/// or deprecation policy and could change in backwards-incompatible ways.
/// </summary>
publicclassLiveGenerativeModel{
privatereadonlyFirebaseApp_firebaseApp;
// Various setting fields provided by the user.
privatereadonlyFirebaseAI.Backend_backend;
privatereadonlystring_modelName;
privatereadonlyLiveGenerationConfig?_liveConfig;
privatereadonlyTool[]_tools;
privatereadonlyModelContent?_systemInstruction;
privatereadonlyRequestOptions?_requestOptions;
/// <summary>
/// Intended for internal use only.
/// Use `VertexAI.GetLiveModel` instead to ensure proper initialization and configuration of the `LiveGenerativeModel`.
/// </summary>
internalLiveGenerativeModel(FirebaseAppfirebaseApp,
FirebaseAI.Backendbackend,
stringmodelName,
LiveGenerationConfig?liveConfig=null,
Tool[]tools=null,
ModelContent?systemInstruction=null,
RequestOptions?requestOptions=null){
_firebaseApp=firebaseApp;
_backend=backend;
_modelName=modelName;
_liveConfig=liveConfig;
_tools=tools;
_systemInstruction=systemInstruction;
_requestOptions=requestOptions;
}
privatestringGetURL(){
return"wss://firebasevertexai.googleapis.com/ws"+
"/google.firebase.vertexai.v1beta.LlmBidiService/BidiGenerateContent"+
$"/locations/{_backend.Location}"+
$"?key={_firebaseApp.Options.ApiKey}";
}
/// <summary>
/// Establishes a connection to a live generation service.
///
/// This function handles the WebSocket connection setup and returns an `LiveSession`
/// object that can be used to communicate with the service.
/// </summary>
/// <param name="cancellationToken">The token that can be used to cancel the creation of the session.</param>
/// <returns>The LiveSession, once it is established.</returns>
publicasyncTask<LiveSession>ConnectAsync(CancellationTokencancellationToken=default){
ClientWebSocketclientWebSocket=new();
stringendpoint=GetURL();
// Set initial headers
// TODO: Get the Version from the Firebase.VersionInfo.SdkVersion (requires exposing it via App)
clientWebSocket.Options.SetRequestHeader("x-goog-api-client","genai-csharp/0.1.0");
// Add additional Firebase tokens to the header.
awaitFirebaseInterops.AddFirebaseTokensAsync(clientWebSocket,_firebaseApp);
// Add a timeout to the initial connection, using the RequestOptions.
usingvarconnectionCts=CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
TimeSpanconnectionTimeout=_requestOptions?.Timeout??RequestOptions.DefaultTimeout;
connectionCts.CancelAfter(connectionTimeout);
awaitclientWebSocket.ConnectAsync(newUri(endpoint),connectionCts.Token);
if(clientWebSocket.State!=WebSocketState.Open){
thrownewWebSocketException("ClientWebSocket failed to connect, can't create LiveSession.");
}
try{
// Send the initial setup message
Dictionary<string,object>setupDict=new(){
{"model",$"projects/{_firebaseApp.Options.ProjectId}/locations/{_backend.Location}/publishers/google/models/{_modelName}"}
};
if(_liveConfig!=null){
setupDict["generationConfig"]=_liveConfig?.ToJson();
}
if(_systemInstruction.HasValue){
setupDict["systemInstruction"]=_systemInstruction?.ToJson();
}
if(_tools!=null&&_tools.Length>0){
setupDict["tools"]=_tools.Select(t =>t.ToJson()).ToList();
}
Dictionary<string,object>jsonDict=new(){
{"setup",setupDict}
};
varbyteArray=Encoding.UTF8.GetBytes(Json.Serialize(jsonDict));
awaitclientWebSocket.SendAsync(newArraySegment<byte>(byteArray),WebSocketMessageType.Binary,true,cancellationToken);
returnnewLiveSession(clientWebSocket);
}catch(Exception){
if(clientWebSocket.State==WebSocketState.Open){
// Try to clean up the WebSocket, to avoid leaking connections.
awaitclientWebSocket.CloseAsync(WebSocketCloseStatus.EndpointUnavailable,
"Failed to send initial setup message.",CancellationToken.None);
}
throw;
}
}
}
}