15
15
part of 'base_model.dart' ;
16
16
17
17
const _apiUrl = 'ws/google.firebase.vertexai' ;
18
- const _apiUrlSuffix = 'LlmBidiService/BidiGenerateContent/locations' ;
18
+ const _apiUrlSuffixVertexAI = 'LlmBidiService/BidiGenerateContent/locations' ;
19
+ const _apiUrlSuffixGoogleAI = 'GenerativeService/BidiGenerateContent' ;
19
20
20
21
/// A live, generative AI model for real-time interaction.
21
22
///
@@ -32,36 +33,56 @@ final class LiveGenerativeModel extends BaseModel {
32
33
{required String model,
33
34
required String location,
34
35
required FirebaseApp app,
36
+ required bool useVertexBackend,
35
37
FirebaseAppCheck ? appCheck,
36
38
FirebaseAuth ? auth,
37
39
LiveGenerationConfig ? liveGenerationConfig,
38
40
List <Tool >? tools,
39
41
Content ? systemInstruction})
40
42
: _app = app,
41
43
_location = location,
44
+ _useVertexBackend = useVertexBackend,
42
45
_appCheck = appCheck,
43
46
_auth = auth,
44
47
_liveGenerationConfig = liveGenerationConfig,
45
48
_tools = tools,
46
49
_systemInstruction = systemInstruction,
47
50
super ._(
48
51
serializationStrategy: VertexSerialization (),
49
- modelUri: _VertexUri (
50
- model: model,
51
- app: app,
52
- location: location,
53
- ),
52
+ modelUri: useVertexBackend
53
+ ? _VertexUri (
54
+ model: model,
55
+ app: app,
56
+ location: location,
57
+ )
58
+ : _GoogleAIUri (
59
+ model: model,
60
+ app: app,
61
+ ),
54
62
);
55
- static const _apiVersion = 'v1beta' ;
56
63
57
64
final FirebaseApp _app;
58
65
final String _location;
66
+ final bool _useVertexBackend;
59
67
final FirebaseAppCheck ? _appCheck;
60
68
final FirebaseAuth ? _auth;
61
69
final LiveGenerationConfig ? _liveGenerationConfig;
62
70
final List <Tool >? _tools;
63
71
final Content ? _systemInstruction;
64
72
73
+ String _vertexAIUri () => 'wss://${_modelUri .baseAuthority }/'
74
+ '$_apiUrl .${_modelUri .apiVersion }.$_apiUrlSuffixVertexAI /'
75
+ '$_location ?key=${_app .options .apiKey }' ;
76
+
77
+ String _vertexAIModelString () => 'projects/${_app .options .projectId }/'
78
+ 'locations/$_location /publishers/google/models/${model .name }' ;
79
+
80
+ String _googleAIUri () => 'wss://${_modelUri .baseAuthority }/'
81
+ '$_apiUrl .${_modelUri .apiVersion }.$_apiUrlSuffixGoogleAI ?key=${_app .options .apiKey }' ;
82
+
83
+ String _googleAIModelString () =>
84
+ 'projects/${_app .options .projectId }/models/${model .name }' ;
85
+
65
86
/// Establishes a connection to a live generation service.
66
87
///
67
88
/// This function handles the WebSocket connection setup and returns an [LiveSession]
@@ -70,11 +91,9 @@ final class LiveGenerativeModel extends BaseModel {
70
91
/// Returns a [Future] that resolves to an [LiveSession] object upon successful
71
92
/// connection.
72
93
Future <LiveSession > connect () async {
73
- final uri = 'wss://${_modelUri .baseAuthority }/'
74
- '$_apiUrl .$_apiVersion .$_apiUrlSuffix /'
75
- '$_location ?key=${_app .options .apiKey }' ;
76
- final modelString = 'projects/${_app .options .projectId }/'
77
- 'locations/$_location /publishers/google/models/${model .name }' ;
94
+ final uri = _useVertexBackend ? _vertexAIUri () : _googleAIUri ();
95
+ final modelString =
96
+ _useVertexBackend ? _vertexAIModelString () : _googleAIModelString ();
78
97
79
98
final setupJson = {
80
99
'setup' : {
@@ -96,6 +115,7 @@ final class LiveGenerativeModel extends BaseModel {
96
115
await ws.ready;
97
116
98
117
ws.sink.add (request);
118
+
99
119
return LiveSession (ws);
100
120
}
101
121
}
@@ -105,6 +125,7 @@ LiveGenerativeModel createLiveGenerativeModel({
105
125
required FirebaseApp app,
106
126
required String location,
107
127
required String model,
128
+ required bool useVertexBackend,
108
129
FirebaseAppCheck ? appCheck,
109
130
FirebaseAuth ? auth,
110
131
LiveGenerationConfig ? liveGenerationConfig,
@@ -117,6 +138,7 @@ LiveGenerativeModel createLiveGenerativeModel({
117
138
appCheck: appCheck,
118
139
auth: auth,
119
140
location: location,
141
+ useVertexBackend: useVertexBackend,
120
142
liveGenerationConfig: liveGenerationConfig,
121
143
tools: tools,
122
144
systemInstruction: systemInstruction,
0 commit comments