@@ -24,9 +24,7 @@ import {
2424 BackendType ,
2525 AI ,
2626 AIOptions ,
27- GoogleAIBackend ,
2827 VertexAI ,
29- VertexAIBackend ,
3028 VertexAIOptions
3129} from './public-types' ;
3230import {
@@ -38,6 +36,7 @@ import {
3836import { AIError } from './errors' ;
3937import { AIModel , GenerativeModel , ImagenModel } from './models' ;
4038import { encodeInstanceIdentifier } from './helpers' ;
39+ import { GoogleAIBackend , VertexAIBackend } from './backend' ;
4140
4241export { ChatSession } from './methods/chat-session' ;
4342export * from './requests/schema-builder' ;
@@ -72,7 +71,7 @@ declare module '@firebase/component' {
7271
7372/**
7473 * It is recommended to use the new {@link getAI | getAI()}.
75- *
74+ *
7675 * Returns a {@link VertexAI} instance for the given app.
7776 *
7877 * @public
@@ -109,13 +108,13 @@ export function getVertexAI(
109108 * @example
110109 * ```javascript
111110 * // Get an AI instance configured to use Google AI.
112- * const ai = getAI(app, { backend: googleAIBackend () });
111+ * const ai = getAI(app, { backend: new GoogleAIBackend () });
113112 * ```
114113 *
115114 * @example
116115 * ```javascript
117116 * // Get an AI instance configured to use Vertex AI.
118- * const ai = getAI(app, { backend: vertexAIBackend () });
117+ * const ai = getAI(app, { backend: new VertexAIBackend () });
119118 * ```
120119 *
121120 * @param app - The {@link @firebase/app#FirebaseApp } to use.
@@ -126,52 +125,33 @@ export function getVertexAI(
126125 */
127126export function getAI (
128127 app : FirebaseApp = getApp ( ) ,
129- options : AIOptions = { backend : googleAIBackend ( ) }
128+ options : AIOptions = { backend : new GoogleAIBackend ( ) }
130129) : AI {
131130 app = getModularInstance ( app ) ;
132131 // Dependencies
133132 const AIProvider : Provider < 'AI' > = _getProvider ( app , AI_TYPE ) ;
134133
135- const identifier = encodeInstanceIdentifier ( options . backend ) ;
134+ let identifier : string ;
135+ if ( options . backend instanceof GoogleAIBackend ) {
136+ identifier = encodeInstanceIdentifier ( {
137+ backendType : BackendType . GOOGLE_AI
138+ } ) ;
139+ } else if ( options . backend instanceof VertexAIBackend ) {
140+ identifier = encodeInstanceIdentifier ( {
141+ backendType : BackendType . VERTEX_AI ,
142+ location : options . backend . location ?? DEFAULT_LOCATION
143+ } ) ;
144+ } else {
145+ throw new AIError (
146+ AIErrorCode . ERROR ,
147+ `Invalid backend type: ${ options . backend . backendType } `
148+ ) ;
149+ }
136150 return AIProvider . getImmediate ( {
137151 identifier
138152 } ) ;
139153}
140154
141- /**
142- * Creates a {@link Backend} instance configured to use Google AI.
143- *
144- * @returns A {@link GoogleAIBackend} object.
145- *
146- * @public
147- */
148- export function googleAIBackend ( ) : GoogleAIBackend {
149- const backend : GoogleAIBackend = {
150- backendType : BackendType . GOOGLE_AI
151- } ;
152-
153- return backend ;
154- }
155-
156- /**
157- * Creates a {@link Backend} instance configured to use Vertex AI.
158- *
159- * @param location - The region identifier, defaulting to `us-central1`;
160- * see {@link https://firebase.google.com/docs/vertex-ai/locations?platform=ios#available-locations | Vertex AI locations}
161- * for a list of supported locations.
162- * @returns A {@link VertexAIBackend} object.
163- *
164- * @public
165- */
166- export function vertexAIBackend ( location ?: string ) : VertexAIBackend {
167- const backend : VertexAIBackend = {
168- backendType : BackendType . VERTEX_AI ,
169- location : location ?? DEFAULT_LOCATION
170- } ;
171-
172- return backend ;
173- }
174-
175155/**
176156 * Returns a {@link GenerativeModel} class with methods for inference
177157 * and other functionality.
0 commit comments