-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
types.ts
276 lines (231 loc) Β· 6.58 KB
/
types.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import type { BaseLLMParams } from "@langchain/core/language_models/llms";
import { BaseLanguageModelCallOptions } from "@langchain/core/language_models/base";
import { StructuredToolInterface } from "@langchain/core/tools";
import type { JsonStream } from "./utils/stream.js";
/**
* Parameters needed to setup the client connection.
* AuthOptions are something like GoogleAuthOptions (from google-auth-library)
* or WebGoogleAuthOptions.
*/
export interface GoogleClientParams<AuthOptions> {
authOptions?: AuthOptions;
/** Some APIs allow an API key instead */
apiKey?: string;
}
/**
* What platform is this running on?
* gai - Google AI Studio / MakerSuite / Generative AI platform
* gcp - Google Cloud Platform
*/
export type GooglePlatformType = "gai" | "gcp";
export interface GoogleConnectionParams<AuthOptions>
extends GoogleClientParams<AuthOptions> {
/** Hostname for the API call (if this is running on GCP) */
endpoint?: string;
/** Region where the LLM is stored (if this is running on GCP) */
location?: string;
/** The version of the API functions. Part of the path. */
apiVersion?: string;
/**
* What platform to run the service on.
* If not specified, the class should determine this from other
* means. Either way, the platform actually used will be in
* the "platform" getter.
*/
platformType?: GooglePlatformType;
}
export interface GoogleAISafetySetting {
category: string;
threshold: string;
}
export interface GoogleAIModelParams {
/** Model to use */
model?: string;
/**
* Model to use
* Alias for `model`
*/
modelName?: string;
/** Sampling temperature to use */
temperature?: number;
/**
* Maximum number of tokens to generate in the completion.
*/
maxOutputTokens?: number;
/**
* Top-p changes how the model selects tokens for output.
*
* Tokens are selected from most probable to least until the sum
* of their probabilities equals the top-p value.
*
* For example, if tokens A, B, and C have a probability of
* .3, .2, and .1 and the top-p value is .5, then the model will
* select either A or B as the next token (using temperature).
*/
topP?: number;
/**
* Top-k changes how the model selects tokens for output.
*
* A top-k of 1 means the selected token is the most probable among
* all tokens in the modelβs vocabulary (also called greedy decoding),
* while a top-k of 3 means that the next token is selected from
* among the 3 most probable tokens (using temperature).
*/
topK?: number;
stopSequences?: string[];
safetySettings?: GoogleAISafetySetting[];
convertSystemMessageToHumanContent?: boolean;
}
/**
* The params which can be passed to the API at request time.
*/
export interface GoogleAIModelRequestParams extends GoogleAIModelParams {
tools?: StructuredToolInterface[] | GeminiTool[];
}
export interface GoogleAIBaseLLMInput<AuthOptions>
extends BaseLLMParams,
GoogleConnectionParams<AuthOptions>,
GoogleAIModelParams,
GoogleAISafetyParams {}
export interface GoogleAIBaseLanguageModelCallOptions
extends BaseLanguageModelCallOptions,
GoogleAIModelRequestParams,
GoogleAISafetyParams {}
/**
* Input to LLM class.
*/
export interface GoogleBaseLLMInput<AuthOptions>
extends GoogleAIBaseLLMInput<AuthOptions> {}
export interface GoogleResponse {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: any;
}
export interface GeminiPartText {
text: string;
}
export interface GeminiPartInlineData {
inlineData: {
mimeType: string;
data: string;
};
}
// Vertex AI only
export interface GeminiPartFileData {
fileData: {
mimeType: string;
fileUri: string;
};
}
// AI Studio only?
export interface GeminiPartFunctionCall {
functionCall: {
name: string;
args?: object;
};
}
// AI Studio Only?
export interface GeminiPartFunctionResponse {
functionResponse: {
name: string;
response: object;
};
}
export type GeminiPart =
| GeminiPartText
| GeminiPartInlineData
| GeminiPartFileData
| GeminiPartFunctionCall
| GeminiPartFunctionResponse;
export interface GeminiSafetySetting {
category: string;
threshold: string;
}
export type GeminiSafetyRating = {
category: string;
probability: string;
} & Record<string, unknown>;
// The "system" content appears to only be valid in the systemInstruction
export type GeminiRole = "system" | "user" | "model" | "function";
export interface GeminiContent {
parts: GeminiPart[];
role: GeminiRole; // Vertex AI requires the role
}
export interface GeminiTool {
functionDeclarations?: GeminiFunctionDeclaration[];
}
export interface GeminiFunctionDeclaration {
name: string;
description: string;
parameters?: GeminiFunctionSchema;
}
export interface GeminiFunctionSchema {
type: GeminiFunctionSchemaType;
format?: string;
description?: string;
nullable?: boolean;
enum?: string[];
properties?: Record<string, GeminiFunctionSchema>;
required?: string[];
items?: GeminiFunctionSchema;
}
export type GeminiFunctionSchemaType =
| "string"
| "number"
| "integer"
| "boolean"
| "array"
| "object";
export interface GeminiGenerationConfig {
stopSequences?: string[];
candidateCount?: number;
maxOutputTokens?: number;
temperature?: number;
topP?: number;
topK?: number;
}
export interface GeminiRequest {
contents?: GeminiContent[];
systemInstruction?: GeminiContent;
tools?: GeminiTool[];
safetySettings?: GeminiSafetySetting[];
generationConfig?: GeminiGenerationConfig;
}
interface GeminiResponseCandidate {
content: {
parts: GeminiPart[];
role: string;
};
finishReason: string;
index: number;
tokenCount?: number;
safetyRatings: GeminiSafetyRating[];
}
interface GeminiResponsePromptFeedback {
blockReason?: string;
safetyRatings: GeminiSafetyRating[];
}
export interface GenerateContentResponseData {
candidates: GeminiResponseCandidate[];
promptFeedback: GeminiResponsePromptFeedback;
usageMetadata: Record<string, unknown>;
}
export type GoogleLLMModelFamily = null | "palm" | "gemini";
export type GoogleLLMResponseData =
| JsonStream
| GenerateContentResponseData
| GenerateContentResponseData[];
export interface GoogleLLMResponse extends GoogleResponse {
data: GoogleLLMResponseData;
}
export interface GoogleAISafetyHandler {
/**
* A function that will take a response and return the, possibly modified,
* response or throw an exception if there are safety issues.
*
* @throws GoogleAISafetyError
*/
handle(response: GoogleLLMResponse): GoogleLLMResponse;
}
export interface GoogleAISafetyParams {
safetyHandler?: GoogleAISafetyHandler;
}