-
Notifications
You must be signed in to change notification settings - Fork 924
/
Copy pathgenerative-model.ts
159 lines (152 loc) · 4.62 KB
/
generative-model.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* @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 {
generateContent,
generateContentStream
} from '../methods/generate-content';
import {
Content,
CountTokensRequest,
CountTokensResponse,
GenerateContentRequest,
GenerateContentResult,
GenerateContentStreamResult,
GenerationConfig,
ModelParams,
Part,
RequestOptions,
SafetySetting,
StartChatParams,
Tool,
ToolConfig
} from '../types';
import { ChatSession } from '../methods/chat-session';
import { countTokens } from '../methods/count-tokens';
import {
formatGenerateContentInput,
formatSystemInstruction
} from '../requests/request-helpers';
import { VertexAI } from '../public-types';
import { VertexAIModel } from './vertexai-model';
import { ChromeAdapter } from '../methods/chrome-adapter';
/**
* Class for generative model APIs.
* @public
*/
export class GenerativeModel extends VertexAIModel {
/**
* Defines the name of the default in-cloud model to use for hybrid inference.
*/
static DEFAULT_HYBRID_IN_CLOUD_MODEL = 'gemini-2.0-flash-lite';
generationConfig: GenerationConfig;
safetySettings: SafetySetting[];
requestOptions?: RequestOptions;
tools?: Tool[];
toolConfig?: ToolConfig;
systemInstruction?: Content;
constructor(
vertexAI: VertexAI,
modelParams: ModelParams,
private chromeAdapter: ChromeAdapter,
requestOptions?: RequestOptions
) {
super(vertexAI, modelParams.model);
this.generationConfig = modelParams.generationConfig || {};
this.safetySettings = modelParams.safetySettings || [];
this.tools = modelParams.tools;
this.toolConfig = modelParams.toolConfig;
this.systemInstruction = formatSystemInstruction(
modelParams.systemInstruction
);
this.requestOptions = requestOptions || {};
}
/**
* Makes a single non-streaming call to the model
* and returns an object containing a single <code>{@link GenerateContentResponse}</code>.
*/
async generateContent(
request: GenerateContentRequest | string | Array<string | Part>
): Promise<GenerateContentResult> {
const formattedParams = formatGenerateContentInput(request);
return generateContent(
this._apiSettings,
this.model,
{
generationConfig: this.generationConfig,
safetySettings: this.safetySettings,
tools: this.tools,
toolConfig: this.toolConfig,
systemInstruction: this.systemInstruction,
...formattedParams
},
this.chromeAdapter,
this.requestOptions
);
}
/**
* Makes a single streaming call to the model
* and returns an object containing an iterable stream that iterates
* over all chunks in the streaming response as well as
* a promise that returns the final aggregated response.
*/
async generateContentStream(
request: GenerateContentRequest | string | Array<string | Part>
): Promise<GenerateContentStreamResult> {
const formattedParams = formatGenerateContentInput(request);
return generateContentStream(
this._apiSettings,
this.model,
{
generationConfig: this.generationConfig,
safetySettings: this.safetySettings,
tools: this.tools,
toolConfig: this.toolConfig,
systemInstruction: this.systemInstruction,
...formattedParams
},
this.chromeAdapter,
this.requestOptions
);
}
/**
* Gets a new <code>{@link ChatSession}</code> instance which can be used for
* multi-turn chats.
*/
startChat(startChatParams?: StartChatParams): ChatSession {
return new ChatSession(
this._apiSettings,
this.model,
this.chromeAdapter,
{
tools: this.tools,
toolConfig: this.toolConfig,
systemInstruction: this.systemInstruction,
...startChatParams
},
this.requestOptions
);
}
/**
* Counts the tokens in the provided request.
*/
async countTokens(
request: CountTokensRequest | string | Array<string | Part>
): Promise<CountTokensResponse> {
const formattedParams = formatGenerateContentInput(request);
return countTokens(this._apiSettings, this.model, formattedParams);
}
}