-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathapi.ts
More file actions
123 lines (115 loc) · 3.72 KB
/
api.ts
File metadata and controls
123 lines (115 loc) · 3.72 KB
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
/**
* @license
* Copyright 2024 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.
*/
import { FirebaseApp, getApp, _getProvider } from '@firebase/app';
import { Provider } from '@firebase/component';
import { getModularInstance } from '@firebase/util';
import { DEFAULT_LOCATION, VERTEX_TYPE } from './constants';
import { VertexAIService } from './service';
import { VertexAI, VertexAIOptions } from './public-types';
import {
ImagenModelParams,
HybridParams,
ModelParams,
RequestOptions,
VertexAIErrorCode
} from './types';
import { VertexAIError } from './errors';
import { VertexAIModel, GenerativeModel, ImagenModel } from './models';
export { ChatSession } from './methods/chat-session';
export * from './requests/schema-builder';
export { ImagenImageFormat } from './requests/imagen-image-format';
export { VertexAIModel, GenerativeModel, ImagenModel };
export { VertexAIError };
declare module '@firebase/component' {
interface NameServiceMapping {
[VERTEX_TYPE]: VertexAIService;
}
}
/**
* Returns a {@link VertexAI} instance for the given app.
*
* @public
*
* @param app - The {@link @firebase/app#FirebaseApp} to use.
*/
export function getVertexAI(
app: FirebaseApp = getApp(),
options?: VertexAIOptions
): VertexAI {
app = getModularInstance(app);
// Dependencies
const vertexProvider: Provider<'vertexAI'> = _getProvider(app, VERTEX_TYPE);
return vertexProvider.getImmediate({
identifier: options?.location || DEFAULT_LOCATION
});
}
/**
* Returns a {@link GenerativeModel} class with methods for inference
* and other functionality.
*
* @public
*/
export function getGenerativeModel(
vertexAI: VertexAI,
modelParams: ModelParams | HybridParams,
requestOptions?: RequestOptions
): GenerativeModel {
// Uses the existence of HybridParams.mode to clarify the type of the modelParams input.
const hybridParams = modelParams as HybridParams;
let inCloudParams: ModelParams;
if (hybridParams.mode) {
inCloudParams = hybridParams.inCloudParams || {
model: GenerativeModel.DEFAULT_HYBRID_IN_CLOUD_MODEL
};
} else {
inCloudParams = modelParams as ModelParams;
}
if (!inCloudParams.model) {
throw new VertexAIError(
VertexAIErrorCode.NO_MODEL,
`Must provide a model name. Example: getGenerativeModel({ model: 'my-model-name' })`
);
}
return new GenerativeModel(vertexAI, inCloudParams, requestOptions);
}
/**
* Returns an {@link ImagenModel} class with methods for using Imagen.
*
* Only Imagen 3 models (named `imagen-3.0-*`) are supported.
*
* @param vertexAI - An instance of the Vertex AI in Firebase SDK.
* @param modelParams - Parameters to use when making Imagen requests.
* @param requestOptions - Additional options to use when making requests.
*
* @throws If the `apiKey` or `projectId` fields are missing in your
* Firebase config.
*
* @beta
*/
export function getImagenModel(
vertexAI: VertexAI,
modelParams: ImagenModelParams,
requestOptions?: RequestOptions
): ImagenModel {
if (!modelParams.model) {
throw new VertexAIError(
VertexAIErrorCode.NO_MODEL,
`Must provide a model name. Example: getImagenModel({ model: 'my-model-name' })`
);
}
return new ImagenModel(vertexAI, modelParams, requestOptions);
}