-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
chat_models.ts
549 lines (496 loc) Β· 15.8 KB
/
chat_models.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { UsageMetadata, type BaseMessage } from "@langchain/core/messages";
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
import {
BaseChatModel,
LangSmithParams,
type BaseChatModelParams,
} from "@langchain/core/language_models/chat_models";
import { ChatGenerationChunk, ChatResult } from "@langchain/core/outputs";
import { AIMessageChunk } from "@langchain/core/messages";
import {
BaseLanguageModelInput,
StructuredOutputMethodOptions,
} from "@langchain/core/language_models/base";
import type { z } from "zod";
import {
Runnable,
RunnablePassthrough,
RunnableSequence,
} from "@langchain/core/runnables";
import { JsonOutputKeyToolsParser } from "@langchain/core/output_parsers/openai_tools";
import { BaseLLMOutputParser } from "@langchain/core/output_parsers";
import { AsyncCaller } from "@langchain/core/utils/async_caller";
import { concat } from "@langchain/core/utils/stream";
import {
GoogleAIBaseLLMInput,
GoogleAIModelParams,
GoogleAISafetySetting,
GoogleConnectionParams,
GooglePlatformType,
GeminiTool,
GoogleAIBaseLanguageModelCallOptions,
GoogleAIAPI,
GoogleAIAPIParams,
GoogleSearchToolSetting,
} from "./types.js";
import {
convertToGeminiTools,
copyAIModelParams,
copyAndValidateModelParamsInto,
} from "./utils/common.js";
import { AbstractGoogleLLMConnection } from "./connection.js";
import { DefaultGeminiSafetyHandler, getGeminiAPI } from "./utils/gemini.js";
import { ApiKeyGoogleAuth, GoogleAbstractedClient } from "./auth.js";
import { JsonStream } from "./utils/stream.js";
import { ensureParams } from "./utils/failed_handler.js";
import type {
GoogleBaseLLMInput,
GoogleAISafetyHandler,
GoogleAISafetyParams,
GeminiFunctionDeclaration,
GeminiFunctionSchema,
GoogleAIToolType,
GeminiAPIConfig,
} from "./types.js";
import { zodToGeminiParameters } from "./utils/zod_to_gemini_parameters.js";
export class ChatConnection<AuthOptions> extends AbstractGoogleLLMConnection<
BaseMessage[],
AuthOptions
> {
convertSystemMessageToHumanContent: boolean | undefined;
constructor(
fields: GoogleAIBaseLLMInput<AuthOptions> | undefined,
caller: AsyncCaller,
client: GoogleAbstractedClient,
streaming: boolean
) {
super(fields, caller, client, streaming);
this.convertSystemMessageToHumanContent =
fields?.convertSystemMessageToHumanContent;
}
get useSystemInstruction(): boolean {
return typeof this.convertSystemMessageToHumanContent === "boolean"
? !this.convertSystemMessageToHumanContent
: this.computeUseSystemInstruction;
}
get computeUseSystemInstruction(): boolean {
// This works on models from April 2024 and later
// Vertex AI: gemini-1.5-pro and gemini-1.0-002 and later
// AI Studio: gemini-1.5-pro-latest
if (this.modelFamily === "palm") {
return false;
} else if (this.modelName === "gemini-1.0-pro-001") {
return false;
} else if (this.modelName.startsWith("gemini-pro-vision")) {
return false;
} else if (this.modelName.startsWith("gemini-1.0-pro-vision")) {
return false;
} else if (this.modelName === "gemini-pro" && this.platform === "gai") {
// on AI Studio gemini-pro is still pointing at gemini-1.0-pro-001
return false;
}
return true;
}
computeGoogleSearchToolAdjustmentFromModel(): Exclude<
GoogleSearchToolSetting,
boolean
> {
if (this.modelName.startsWith("gemini-1.0")) {
return "googleSearchRetrieval";
} else if (this.modelName.startsWith("gemini-1.5")) {
return "googleSearchRetrieval";
} else {
return "googleSearch";
}
}
computeGoogleSearchToolAdjustment(
apiConfig: GeminiAPIConfig
): Exclude<GoogleSearchToolSetting, true> {
const adj = apiConfig.googleSearchToolAdjustment;
if (adj === undefined || adj === true) {
return this.computeGoogleSearchToolAdjustmentFromModel();
} else {
return adj;
}
}
buildGeminiAPI(): GoogleAIAPI {
const apiConfig: GeminiAPIConfig =
(this.apiConfig as GeminiAPIConfig) ?? {};
const googleSearchToolAdjustment =
this.computeGoogleSearchToolAdjustment(apiConfig);
const geminiConfig: GeminiAPIConfig = {
useSystemInstruction: this.useSystemInstruction,
googleSearchToolAdjustment,
...apiConfig,
};
return getGeminiAPI(geminiConfig);
}
get api(): GoogleAIAPI {
switch (this.apiName) {
case "google":
return this.buildGeminiAPI();
default:
return super.api;
}
}
}
/**
* Input to chat model class.
*/
export interface ChatGoogleBaseInput<AuthOptions>
extends BaseChatModelParams,
GoogleConnectionParams<AuthOptions>,
GoogleAIModelParams,
GoogleAISafetyParams,
GoogleAIAPIParams,
Pick<GoogleAIBaseLanguageModelCallOptions, "streamUsage"> {}
/**
* Integration with a Google chat model.
*/
export abstract class ChatGoogleBase<AuthOptions>
extends BaseChatModel<GoogleAIBaseLanguageModelCallOptions, AIMessageChunk>
implements ChatGoogleBaseInput<AuthOptions>
{
// Used for tracing, replace with the same name as your class
static lc_name() {
return "ChatGoogle";
}
get lc_secrets(): { [key: string]: string } | undefined {
return {
authOptions: "GOOGLE_AUTH_OPTIONS",
};
}
lc_serializable = true;
// Set based on modelName
model: string;
modelName = "gemini-pro";
temperature = 0.7;
maxOutputTokens = 1024;
topP = 0.8;
topK = 40;
stopSequences: string[] = [];
safetySettings: GoogleAISafetySetting[] = [];
// May intentionally be undefined, meaning to compute this.
convertSystemMessageToHumanContent: boolean | undefined;
safetyHandler: GoogleAISafetyHandler;
streamUsage = true;
streaming = false;
protected connection: ChatConnection<AuthOptions>;
protected streamedConnection: ChatConnection<AuthOptions>;
constructor(fields?: ChatGoogleBaseInput<AuthOptions>) {
super(ensureParams(fields));
copyAndValidateModelParamsInto(fields, this);
this.safetyHandler =
fields?.safetyHandler ?? new DefaultGeminiSafetyHandler();
this.streamUsage = fields?.streamUsage ?? this.streamUsage;
const client = this.buildClient(fields);
this.buildConnection(fields ?? {}, client);
}
getLsParams(options: this["ParsedCallOptions"]): LangSmithParams {
const params = this.invocationParams(options);
return {
ls_provider: "google_vertexai",
ls_model_name: this.model,
ls_model_type: "chat",
ls_temperature: params.temperature ?? undefined,
ls_max_tokens: params.maxOutputTokens ?? undefined,
ls_stop: options.stop,
};
}
abstract buildAbstractedClient(
fields?: GoogleAIBaseLLMInput<AuthOptions>
): GoogleAbstractedClient;
buildApiKeyClient(apiKey: string): GoogleAbstractedClient {
return new ApiKeyGoogleAuth(apiKey);
}
buildApiKey(fields?: GoogleAIBaseLLMInput<AuthOptions>): string | undefined {
if (fields?.platformType !== "gcp") {
return fields?.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
} else {
// GCP doesn't support API Keys
return undefined;
}
}
buildClient(
fields?: GoogleAIBaseLLMInput<AuthOptions>
): GoogleAbstractedClient {
const apiKey = this.buildApiKey(fields);
if (apiKey) {
return this.buildApiKeyClient(apiKey);
} else {
return this.buildAbstractedClient(fields);
}
}
buildConnection(
fields: GoogleBaseLLMInput<AuthOptions>,
client: GoogleAbstractedClient
) {
this.connection = new ChatConnection(
{ ...fields, ...this },
this.caller,
client,
false
);
this.streamedConnection = new ChatConnection(
{ ...fields, ...this },
this.caller,
client,
true
);
}
get platform(): GooglePlatformType {
return this.connection.platform;
}
override bindTools(
tools: GoogleAIToolType[],
kwargs?: Partial<GoogleAIBaseLanguageModelCallOptions>
): Runnable<
BaseLanguageModelInput,
AIMessageChunk,
GoogleAIBaseLanguageModelCallOptions
> {
return this.bind({ tools: convertToGeminiTools(tools), ...kwargs });
}
// Replace
_llmType() {
return "chat_integration";
}
/**
* Get the parameters used to invoke the model
*/
override invocationParams(options?: this["ParsedCallOptions"]) {
return copyAIModelParams(this, options);
}
async _generate(
messages: BaseMessage[],
options: this["ParsedCallOptions"],
runManager: CallbackManagerForLLMRun | undefined
): Promise<ChatResult> {
const parameters = this.invocationParams(options);
if (this.streaming) {
const stream = this._streamResponseChunks(messages, options, runManager);
let finalChunk: ChatGenerationChunk | null = null;
for await (const chunk of stream) {
finalChunk = !finalChunk ? chunk : concat(finalChunk, chunk);
}
if (!finalChunk) {
throw new Error("No chunks were returned from the stream.");
}
return {
generations: [finalChunk],
};
}
const response = await this.connection.request(
messages,
parameters,
options,
runManager
);
const ret = this.connection.api.responseToChatResult(response);
const chunk = ret?.generations?.[0];
if (chunk) {
await runManager?.handleLLMNewToken(chunk.text || "");
}
return ret;
}
async *_streamResponseChunks(
_messages: BaseMessage[],
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): AsyncGenerator<ChatGenerationChunk> {
// Make the call as a streaming request
const parameters = this.invocationParams(options);
const response = await this.streamedConnection.request(
_messages,
parameters,
options,
runManager
);
// Get the streaming parser of the response
const stream = response.data as JsonStream;
let usageMetadata: UsageMetadata | undefined;
// Loop until the end of the stream
// During the loop, yield each time we get a chunk from the streaming parser
// that is either available or added to the queue
while (!stream.streamDone) {
const output = await stream.nextChunk();
await runManager?.handleCustomEvent(
`google-chunk-${this.constructor.name}`,
{
output,
}
);
if (
output &&
output.usageMetadata &&
this.streamUsage !== false &&
options.streamUsage !== false
) {
usageMetadata = {
input_tokens: output.usageMetadata.promptTokenCount,
output_tokens: output.usageMetadata.candidatesTokenCount,
total_tokens: output.usageMetadata.totalTokenCount,
};
}
const chunk =
output !== null
? this.connection.api.responseToChatGeneration({ data: output })
: new ChatGenerationChunk({
text: "",
generationInfo: { finishReason: "stop" },
message: new AIMessageChunk({
content: "",
usage_metadata: usageMetadata,
}),
});
if (chunk) {
yield chunk;
await runManager?.handleLLMNewToken(
chunk.text ?? "",
undefined,
undefined,
undefined,
undefined,
{ chunk }
);
}
}
}
/** @ignore */
_combineLLMOutput() {
return [];
}
withStructuredOutput<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
outputSchema:
| z.ZodType<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
config?: StructuredOutputMethodOptions<false>
): Runnable<BaseLanguageModelInput, RunOutput>;
withStructuredOutput<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
outputSchema:
| z.ZodType<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
config?: StructuredOutputMethodOptions<true>
): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;
withStructuredOutput<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
outputSchema:
| z.ZodType<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
config?: StructuredOutputMethodOptions<boolean>
):
| Runnable<BaseLanguageModelInput, RunOutput>
| Runnable<
BaseLanguageModelInput,
{ raw: BaseMessage; parsed: RunOutput }
> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const schema: z.ZodType<RunOutput> | Record<string, any> = outputSchema;
const name = config?.name;
const method = config?.method;
const includeRaw = config?.includeRaw;
if (method === "jsonMode") {
throw new Error(`Google only supports "functionCalling" as a method.`);
}
let functionName = name ?? "extract";
let outputParser: BaseLLMOutputParser<RunOutput>;
let tools: GeminiTool[];
if (isZodSchema(schema)) {
const jsonSchema = zodToGeminiParameters(schema);
tools = [
{
functionDeclarations: [
{
name: functionName,
description:
jsonSchema.description ?? "A function available to call.",
parameters: jsonSchema as GeminiFunctionSchema,
},
],
},
];
outputParser = new JsonOutputKeyToolsParser({
returnSingle: true,
keyName: functionName,
zodSchema: schema,
});
} else {
let geminiFunctionDefinition: GeminiFunctionDeclaration;
if (
typeof schema.name === "string" &&
typeof schema.parameters === "object" &&
schema.parameters != null
) {
geminiFunctionDefinition = schema as GeminiFunctionDeclaration;
functionName = schema.name;
} else {
geminiFunctionDefinition = {
name: functionName,
description: schema.description ?? "",
parameters: schema as GeminiFunctionSchema,
};
}
tools = [
{
functionDeclarations: [geminiFunctionDefinition],
},
];
outputParser = new JsonOutputKeyToolsParser<RunOutput>({
returnSingle: true,
keyName: functionName,
});
}
const llm = this.bind({
tools,
tool_choice: functionName,
});
if (!includeRaw) {
return llm.pipe(outputParser).withConfig({
runName: "ChatGoogleStructuredOutput",
}) as Runnable<BaseLanguageModelInput, RunOutput>;
}
const parserAssign = RunnablePassthrough.assign({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
parsed: (input: any, config) => outputParser.invoke(input.raw, config),
});
const parserNone = RunnablePassthrough.assign({
parsed: () => null,
});
const parsedWithFallback = parserAssign.withFallbacks({
fallbacks: [parserNone],
});
return RunnableSequence.from<
BaseLanguageModelInput,
{ raw: BaseMessage; parsed: RunOutput }
>([
{
raw: llm,
},
parsedWithFallback,
]).withConfig({
runName: "StructuredOutputRunnable",
});
}
}
function isZodSchema<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
input: z.ZodType<RunOutput> | Record<string, any>
): input is z.ZodType<RunOutput> {
// Check for a characteristic method of Zod schemas
return typeof (input as z.ZodType<RunOutput>)?.parse === "function";
}