@langchain/google-genai 0.1.12 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/chat_models.cjs
CHANGED
|
@@ -6,6 +6,7 @@ const env_1 = require("@langchain/core/utils/env");
|
|
|
6
6
|
const chat_models_1 = require("@langchain/core/language_models/chat_models");
|
|
7
7
|
const runnables_1 = require("@langchain/core/runnables");
|
|
8
8
|
const types_1 = require("@langchain/core/utils/types");
|
|
9
|
+
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
9
10
|
const zod_to_genai_parameters_js_1 = require("./utils/zod_to_genai_parameters.cjs");
|
|
10
11
|
const common_js_1 = require("./utils/common.cjs");
|
|
11
12
|
const output_parsers_js_1 = require("./output_parsers.cjs");
|
|
@@ -406,7 +407,7 @@ class ChatGoogleGenerativeAI extends chat_models_1.BaseChatModel {
|
|
|
406
407
|
this.model.startsWith("gemini-2"));
|
|
407
408
|
}
|
|
408
409
|
constructor(fields) {
|
|
409
|
-
super(fields
|
|
410
|
+
super(fields);
|
|
410
411
|
Object.defineProperty(this, "lc_serializable", {
|
|
411
412
|
enumerable: true,
|
|
412
413
|
configurable: true,
|
|
@@ -419,17 +420,11 @@ class ChatGoogleGenerativeAI extends chat_models_1.BaseChatModel {
|
|
|
419
420
|
writable: true,
|
|
420
421
|
value: ["langchain", "chat_models", "google_genai"]
|
|
421
422
|
});
|
|
422
|
-
Object.defineProperty(this, "modelName", {
|
|
423
|
-
enumerable: true,
|
|
424
|
-
configurable: true,
|
|
425
|
-
writable: true,
|
|
426
|
-
value: "gemini-pro"
|
|
427
|
-
});
|
|
428
423
|
Object.defineProperty(this, "model", {
|
|
429
424
|
enumerable: true,
|
|
430
425
|
configurable: true,
|
|
431
426
|
writable: true,
|
|
432
|
-
value:
|
|
427
|
+
value: void 0
|
|
433
428
|
});
|
|
434
429
|
Object.defineProperty(this, "temperature", {
|
|
435
430
|
enumerable: true,
|
|
@@ -479,6 +474,12 @@ class ChatGoogleGenerativeAI extends chat_models_1.BaseChatModel {
|
|
|
479
474
|
writable: true,
|
|
480
475
|
value: false
|
|
481
476
|
});
|
|
477
|
+
Object.defineProperty(this, "json", {
|
|
478
|
+
enumerable: true,
|
|
479
|
+
configurable: true,
|
|
480
|
+
writable: true,
|
|
481
|
+
value: void 0
|
|
482
|
+
});
|
|
482
483
|
Object.defineProperty(this, "streamUsage", {
|
|
483
484
|
enumerable: true,
|
|
484
485
|
configurable: true,
|
|
@@ -497,63 +498,59 @@ class ChatGoogleGenerativeAI extends chat_models_1.BaseChatModel {
|
|
|
497
498
|
writable: true,
|
|
498
499
|
value: void 0
|
|
499
500
|
});
|
|
500
|
-
this.
|
|
501
|
-
|
|
502
|
-
fields?.modelName?.replace(/^models\//, "") ??
|
|
503
|
-
this.model;
|
|
504
|
-
this.model = this.modelName;
|
|
505
|
-
this.maxOutputTokens = fields?.maxOutputTokens ?? this.maxOutputTokens;
|
|
501
|
+
this.model = fields.model.replace(/^models\//, "");
|
|
502
|
+
this.maxOutputTokens = fields.maxOutputTokens ?? this.maxOutputTokens;
|
|
506
503
|
if (this.maxOutputTokens && this.maxOutputTokens < 0) {
|
|
507
504
|
throw new Error("`maxOutputTokens` must be a positive integer");
|
|
508
505
|
}
|
|
509
|
-
this.temperature = fields
|
|
506
|
+
this.temperature = fields.temperature ?? this.temperature;
|
|
510
507
|
if (this.temperature && (this.temperature < 0 || this.temperature > 2)) {
|
|
511
508
|
throw new Error("`temperature` must be in the range of [0.0,2.0]");
|
|
512
509
|
}
|
|
513
|
-
this.topP = fields
|
|
510
|
+
this.topP = fields.topP ?? this.topP;
|
|
514
511
|
if (this.topP && this.topP < 0) {
|
|
515
512
|
throw new Error("`topP` must be a positive integer");
|
|
516
513
|
}
|
|
517
514
|
if (this.topP && this.topP > 1) {
|
|
518
515
|
throw new Error("`topP` must be below 1.");
|
|
519
516
|
}
|
|
520
|
-
this.topK = fields
|
|
517
|
+
this.topK = fields.topK ?? this.topK;
|
|
521
518
|
if (this.topK && this.topK < 0) {
|
|
522
519
|
throw new Error("`topK` must be a positive integer");
|
|
523
520
|
}
|
|
524
|
-
this.stopSequences = fields
|
|
525
|
-
this.apiKey = fields
|
|
521
|
+
this.stopSequences = fields.stopSequences ?? this.stopSequences;
|
|
522
|
+
this.apiKey = fields.apiKey ?? (0, env_1.getEnvironmentVariable)("GOOGLE_API_KEY");
|
|
526
523
|
if (!this.apiKey) {
|
|
527
524
|
throw new Error("Please set an API key for Google GenerativeAI " +
|
|
528
525
|
"in the environment variable GOOGLE_API_KEY " +
|
|
529
526
|
"or in the `apiKey` field of the " +
|
|
530
527
|
"ChatGoogleGenerativeAI constructor");
|
|
531
528
|
}
|
|
532
|
-
this.safetySettings = fields
|
|
529
|
+
this.safetySettings = fields.safetySettings ?? this.safetySettings;
|
|
533
530
|
if (this.safetySettings && this.safetySettings.length > 0) {
|
|
534
531
|
const safetySettingsSet = new Set(this.safetySettings.map((s) => s.category));
|
|
535
532
|
if (safetySettingsSet.size !== this.safetySettings.length) {
|
|
536
533
|
throw new Error("The categories in `safetySettings` array must be unique");
|
|
537
534
|
}
|
|
538
535
|
}
|
|
539
|
-
this.streaming = fields
|
|
536
|
+
this.streaming = fields.streaming ?? this.streaming;
|
|
537
|
+
this.json = fields.json;
|
|
540
538
|
this.client = new generative_ai_1.GoogleGenerativeAI(this.apiKey).getGenerativeModel({
|
|
541
539
|
model: this.model,
|
|
542
540
|
safetySettings: this.safetySettings,
|
|
543
541
|
generationConfig: {
|
|
544
|
-
candidateCount: 1,
|
|
545
542
|
stopSequences: this.stopSequences,
|
|
546
543
|
maxOutputTokens: this.maxOutputTokens,
|
|
547
544
|
temperature: this.temperature,
|
|
548
545
|
topP: this.topP,
|
|
549
546
|
topK: this.topK,
|
|
550
|
-
...(
|
|
547
|
+
...(this.json ? { responseMimeType: "application/json" } : {}),
|
|
551
548
|
},
|
|
552
549
|
}, {
|
|
553
|
-
apiVersion: fields
|
|
554
|
-
baseUrl: fields
|
|
550
|
+
apiVersion: fields.apiVersion,
|
|
551
|
+
baseUrl: fields.baseUrl,
|
|
555
552
|
});
|
|
556
|
-
this.streamUsage = fields
|
|
553
|
+
this.streamUsage = fields.streamUsage ?? this.streamUsage;
|
|
557
554
|
}
|
|
558
555
|
useCachedContent(cachedContent, modelParams, requestOptions) {
|
|
559
556
|
if (!this.apiKey)
|
|
@@ -569,16 +566,16 @@ class ChatGoogleGenerativeAI extends chat_models_1.BaseChatModel {
|
|
|
569
566
|
// This works on models from April 2024 and later
|
|
570
567
|
// Vertex AI: gemini-1.5-pro and gemini-1.0-002 and later
|
|
571
568
|
// AI Studio: gemini-1.5-pro-latest
|
|
572
|
-
if (this.
|
|
569
|
+
if (this.model === "gemini-1.0-pro-001") {
|
|
573
570
|
return false;
|
|
574
571
|
}
|
|
575
|
-
else if (this.
|
|
572
|
+
else if (this.model.startsWith("gemini-pro-vision")) {
|
|
576
573
|
return false;
|
|
577
574
|
}
|
|
578
|
-
else if (this.
|
|
575
|
+
else if (this.model.startsWith("gemini-1.0-pro-vision")) {
|
|
579
576
|
return false;
|
|
580
577
|
}
|
|
581
|
-
else if (this.
|
|
578
|
+
else if (this.model === "gemini-pro") {
|
|
582
579
|
// on AI Studio gemini-pro is still pointing at gemini-1.0-pro-001
|
|
583
580
|
return false;
|
|
584
581
|
}
|
|
@@ -610,6 +607,16 @@ class ChatGoogleGenerativeAI extends chat_models_1.BaseChatModel {
|
|
|
610
607
|
allowedFunctionNames: options.allowedFunctionNames,
|
|
611
608
|
})
|
|
612
609
|
: undefined;
|
|
610
|
+
if (options?.responseSchema) {
|
|
611
|
+
this.client.generationConfig.responseSchema = options.responseSchema;
|
|
612
|
+
this.client.generationConfig.responseMimeType = "application/json";
|
|
613
|
+
}
|
|
614
|
+
else {
|
|
615
|
+
this.client.generationConfig.responseSchema = undefined;
|
|
616
|
+
this.client.generationConfig.responseMimeType = this.json
|
|
617
|
+
? "application/json"
|
|
618
|
+
: undefined;
|
|
619
|
+
}
|
|
613
620
|
return {
|
|
614
621
|
...(toolsAndConfig?.tools ? { tools: toolsAndConfig.tools } : {}),
|
|
615
622
|
...(toolsAndConfig?.toolConfig
|
|
@@ -741,59 +748,72 @@ class ChatGoogleGenerativeAI extends chat_models_1.BaseChatModel {
|
|
|
741
748
|
const method = config?.method;
|
|
742
749
|
const includeRaw = config?.includeRaw;
|
|
743
750
|
if (method === "jsonMode") {
|
|
744
|
-
throw new Error(`ChatGoogleGenerativeAI only supports "functionCalling" as a method.`);
|
|
751
|
+
throw new Error(`ChatGoogleGenerativeAI only supports "jsonSchema" or "functionCalling" as a method.`);
|
|
745
752
|
}
|
|
746
|
-
let
|
|
753
|
+
let llm;
|
|
747
754
|
let outputParser;
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
let geminiFunctionDefinition;
|
|
770
|
-
if (typeof schema.name === "string" &&
|
|
771
|
-
typeof schema.parameters === "object" &&
|
|
772
|
-
schema.parameters != null) {
|
|
773
|
-
geminiFunctionDefinition = schema;
|
|
774
|
-
functionName = schema.name;
|
|
755
|
+
if (method === "functionCalling") {
|
|
756
|
+
let functionName = name ?? "extract";
|
|
757
|
+
let tools;
|
|
758
|
+
if ((0, types_1.isZodSchema)(schema)) {
|
|
759
|
+
const jsonSchema = (0, zod_to_genai_parameters_js_1.zodToGenerativeAIParameters)(schema);
|
|
760
|
+
tools = [
|
|
761
|
+
{
|
|
762
|
+
functionDeclarations: [
|
|
763
|
+
{
|
|
764
|
+
name: functionName,
|
|
765
|
+
description: jsonSchema.description ?? "A function available to call.",
|
|
766
|
+
parameters: jsonSchema,
|
|
767
|
+
},
|
|
768
|
+
],
|
|
769
|
+
},
|
|
770
|
+
];
|
|
771
|
+
outputParser = new output_parsers_js_1.GoogleGenerativeAIToolsOutputParser({
|
|
772
|
+
returnSingle: true,
|
|
773
|
+
keyName: functionName,
|
|
774
|
+
zodSchema: schema,
|
|
775
|
+
});
|
|
775
776
|
}
|
|
776
777
|
else {
|
|
777
|
-
geminiFunctionDefinition
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
parameters
|
|
781
|
-
|
|
778
|
+
let geminiFunctionDefinition;
|
|
779
|
+
if (typeof schema.name === "string" &&
|
|
780
|
+
typeof schema.parameters === "object" &&
|
|
781
|
+
schema.parameters != null) {
|
|
782
|
+
geminiFunctionDefinition = schema;
|
|
783
|
+
geminiFunctionDefinition.parameters = (0, zod_to_genai_parameters_js_1.removeAdditionalProperties)(schema.parameters);
|
|
784
|
+
functionName = schema.name;
|
|
785
|
+
}
|
|
786
|
+
else {
|
|
787
|
+
geminiFunctionDefinition = {
|
|
788
|
+
name: functionName,
|
|
789
|
+
description: schema.description ?? "",
|
|
790
|
+
parameters: (0, zod_to_genai_parameters_js_1.removeAdditionalProperties)(schema),
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
tools = [
|
|
794
|
+
{
|
|
795
|
+
functionDeclarations: [geminiFunctionDefinition],
|
|
796
|
+
},
|
|
797
|
+
];
|
|
798
|
+
outputParser = new output_parsers_js_1.GoogleGenerativeAIToolsOutputParser({
|
|
799
|
+
returnSingle: true,
|
|
800
|
+
keyName: functionName,
|
|
801
|
+
});
|
|
782
802
|
}
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
},
|
|
787
|
-
];
|
|
788
|
-
outputParser = new output_parsers_js_1.GoogleGenerativeAIToolsOutputParser({
|
|
789
|
-
returnSingle: true,
|
|
790
|
-
keyName: functionName,
|
|
803
|
+
llm = this.bind({
|
|
804
|
+
tools,
|
|
805
|
+
tool_choice: functionName,
|
|
791
806
|
});
|
|
792
807
|
}
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
808
|
+
else {
|
|
809
|
+
const jsonSchema = (0, types_1.isZodSchema)(schema)
|
|
810
|
+
? (0, zod_to_genai_parameters_js_1.zodToGenerativeAIParameters)(schema)
|
|
811
|
+
: (0, zod_to_genai_parameters_js_1.removeAdditionalProperties)(schema);
|
|
812
|
+
llm = this.bind({
|
|
813
|
+
responseSchema: jsonSchema,
|
|
814
|
+
});
|
|
815
|
+
outputParser = new output_parsers_1.JsonOutputParser();
|
|
816
|
+
}
|
|
797
817
|
if (!includeRaw) {
|
|
798
818
|
return llm.pipe(outputParser).withConfig({
|
|
799
819
|
runName: "ChatGoogleGenerativeAIStructuredOutput",
|
package/dist/chat_models.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GenerateContentRequest, SafetySetting, Part as GenerativeAIPart, ModelParams, RequestOptions, type CachedContent } from "@google/generative-ai";
|
|
1
|
+
import { GenerateContentRequest, SafetySetting, Part as GenerativeAIPart, ModelParams, RequestOptions, type CachedContent, Schema } from "@google/generative-ai";
|
|
2
2
|
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
|
|
3
3
|
import { AIMessageChunk, BaseMessage } from "@langchain/core/messages";
|
|
4
4
|
import { ChatGenerationChunk, ChatResult } from "@langchain/core/outputs";
|
|
@@ -24,27 +24,21 @@ export interface GoogleGenerativeAIChatCallOptions extends BaseChatModelCallOpti
|
|
|
24
24
|
* @default true
|
|
25
25
|
*/
|
|
26
26
|
streamUsage?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* JSON schema to be returned by the model.
|
|
29
|
+
*/
|
|
30
|
+
responseSchema?: Schema;
|
|
27
31
|
}
|
|
28
32
|
/**
|
|
29
33
|
* An interface defining the input to the ChatGoogleGenerativeAI class.
|
|
30
34
|
*/
|
|
31
35
|
export interface GoogleGenerativeAIChatInput extends BaseChatModelParams, Pick<GoogleGenerativeAIChatCallOptions, "streamUsage"> {
|
|
32
36
|
/**
|
|
33
|
-
* @deprecated Use "model" instead.
|
|
34
|
-
*
|
|
35
37
|
* Model Name to use
|
|
36
38
|
*
|
|
37
|
-
* Alias for `model`
|
|
38
|
-
*
|
|
39
39
|
* Note: The format must follow the pattern - `{model}`
|
|
40
40
|
*/
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Model Name to use
|
|
44
|
-
*
|
|
45
|
-
* Note: The format must follow the pattern - `{model}`
|
|
46
|
-
*/
|
|
47
|
-
model?: string;
|
|
41
|
+
model: string;
|
|
48
42
|
/**
|
|
49
43
|
* Controls the randomness of the output.
|
|
50
44
|
*
|
|
@@ -515,7 +509,6 @@ export declare class ChatGoogleGenerativeAI extends BaseChatModel<GoogleGenerati
|
|
|
515
509
|
get lc_aliases(): {
|
|
516
510
|
apiKey: string;
|
|
517
511
|
};
|
|
518
|
-
modelName: string;
|
|
519
512
|
model: string;
|
|
520
513
|
temperature?: number;
|
|
521
514
|
maxOutputTokens?: number;
|
|
@@ -525,11 +518,12 @@ export declare class ChatGoogleGenerativeAI extends BaseChatModel<GoogleGenerati
|
|
|
525
518
|
safetySettings?: SafetySetting[];
|
|
526
519
|
apiKey?: string;
|
|
527
520
|
streaming: boolean;
|
|
521
|
+
json?: boolean;
|
|
528
522
|
streamUsage: boolean;
|
|
529
523
|
convertSystemMessageToHumanContent: boolean | undefined;
|
|
530
524
|
private client;
|
|
531
525
|
get _isMultimodalModel(): boolean;
|
|
532
|
-
constructor(fields
|
|
526
|
+
constructor(fields: GoogleGenerativeAIChatInput);
|
|
533
527
|
useCachedContent(cachedContent: CachedContent, modelParams?: ModelParams, requestOptions?: RequestOptions): void;
|
|
534
528
|
get useSystemInstruction(): boolean;
|
|
535
529
|
get computeUseSystemInstruction(): boolean;
|
package/dist/chat_models.js
CHANGED
|
@@ -3,7 +3,8 @@ import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
|
3
3
|
import { BaseChatModel, } from "@langchain/core/language_models/chat_models";
|
|
4
4
|
import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
|
|
5
5
|
import { isZodSchema } from "@langchain/core/utils/types";
|
|
6
|
-
import {
|
|
6
|
+
import { JsonOutputParser, } from "@langchain/core/output_parsers";
|
|
7
|
+
import { zodToGenerativeAIParameters, removeAdditionalProperties, } from "./utils/zod_to_genai_parameters.js";
|
|
7
8
|
import { convertBaseMessagesToContent, convertResponseContentToChatGenerationChunk, mapGenerateContentResultToChatResult, } from "./utils/common.js";
|
|
8
9
|
import { GoogleGenerativeAIToolsOutputParser } from "./output_parsers.js";
|
|
9
10
|
import { convertToolsToGenAI } from "./utils/tools.js";
|
|
@@ -403,7 +404,7 @@ export class ChatGoogleGenerativeAI extends BaseChatModel {
|
|
|
403
404
|
this.model.startsWith("gemini-2"));
|
|
404
405
|
}
|
|
405
406
|
constructor(fields) {
|
|
406
|
-
super(fields
|
|
407
|
+
super(fields);
|
|
407
408
|
Object.defineProperty(this, "lc_serializable", {
|
|
408
409
|
enumerable: true,
|
|
409
410
|
configurable: true,
|
|
@@ -416,17 +417,11 @@ export class ChatGoogleGenerativeAI extends BaseChatModel {
|
|
|
416
417
|
writable: true,
|
|
417
418
|
value: ["langchain", "chat_models", "google_genai"]
|
|
418
419
|
});
|
|
419
|
-
Object.defineProperty(this, "modelName", {
|
|
420
|
-
enumerable: true,
|
|
421
|
-
configurable: true,
|
|
422
|
-
writable: true,
|
|
423
|
-
value: "gemini-pro"
|
|
424
|
-
});
|
|
425
420
|
Object.defineProperty(this, "model", {
|
|
426
421
|
enumerable: true,
|
|
427
422
|
configurable: true,
|
|
428
423
|
writable: true,
|
|
429
|
-
value:
|
|
424
|
+
value: void 0
|
|
430
425
|
});
|
|
431
426
|
Object.defineProperty(this, "temperature", {
|
|
432
427
|
enumerable: true,
|
|
@@ -476,6 +471,12 @@ export class ChatGoogleGenerativeAI extends BaseChatModel {
|
|
|
476
471
|
writable: true,
|
|
477
472
|
value: false
|
|
478
473
|
});
|
|
474
|
+
Object.defineProperty(this, "json", {
|
|
475
|
+
enumerable: true,
|
|
476
|
+
configurable: true,
|
|
477
|
+
writable: true,
|
|
478
|
+
value: void 0
|
|
479
|
+
});
|
|
479
480
|
Object.defineProperty(this, "streamUsage", {
|
|
480
481
|
enumerable: true,
|
|
481
482
|
configurable: true,
|
|
@@ -494,63 +495,59 @@ export class ChatGoogleGenerativeAI extends BaseChatModel {
|
|
|
494
495
|
writable: true,
|
|
495
496
|
value: void 0
|
|
496
497
|
});
|
|
497
|
-
this.
|
|
498
|
-
|
|
499
|
-
fields?.modelName?.replace(/^models\//, "") ??
|
|
500
|
-
this.model;
|
|
501
|
-
this.model = this.modelName;
|
|
502
|
-
this.maxOutputTokens = fields?.maxOutputTokens ?? this.maxOutputTokens;
|
|
498
|
+
this.model = fields.model.replace(/^models\//, "");
|
|
499
|
+
this.maxOutputTokens = fields.maxOutputTokens ?? this.maxOutputTokens;
|
|
503
500
|
if (this.maxOutputTokens && this.maxOutputTokens < 0) {
|
|
504
501
|
throw new Error("`maxOutputTokens` must be a positive integer");
|
|
505
502
|
}
|
|
506
|
-
this.temperature = fields
|
|
503
|
+
this.temperature = fields.temperature ?? this.temperature;
|
|
507
504
|
if (this.temperature && (this.temperature < 0 || this.temperature > 2)) {
|
|
508
505
|
throw new Error("`temperature` must be in the range of [0.0,2.0]");
|
|
509
506
|
}
|
|
510
|
-
this.topP = fields
|
|
507
|
+
this.topP = fields.topP ?? this.topP;
|
|
511
508
|
if (this.topP && this.topP < 0) {
|
|
512
509
|
throw new Error("`topP` must be a positive integer");
|
|
513
510
|
}
|
|
514
511
|
if (this.topP && this.topP > 1) {
|
|
515
512
|
throw new Error("`topP` must be below 1.");
|
|
516
513
|
}
|
|
517
|
-
this.topK = fields
|
|
514
|
+
this.topK = fields.topK ?? this.topK;
|
|
518
515
|
if (this.topK && this.topK < 0) {
|
|
519
516
|
throw new Error("`topK` must be a positive integer");
|
|
520
517
|
}
|
|
521
|
-
this.stopSequences = fields
|
|
522
|
-
this.apiKey = fields
|
|
518
|
+
this.stopSequences = fields.stopSequences ?? this.stopSequences;
|
|
519
|
+
this.apiKey = fields.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
|
|
523
520
|
if (!this.apiKey) {
|
|
524
521
|
throw new Error("Please set an API key for Google GenerativeAI " +
|
|
525
522
|
"in the environment variable GOOGLE_API_KEY " +
|
|
526
523
|
"or in the `apiKey` field of the " +
|
|
527
524
|
"ChatGoogleGenerativeAI constructor");
|
|
528
525
|
}
|
|
529
|
-
this.safetySettings = fields
|
|
526
|
+
this.safetySettings = fields.safetySettings ?? this.safetySettings;
|
|
530
527
|
if (this.safetySettings && this.safetySettings.length > 0) {
|
|
531
528
|
const safetySettingsSet = new Set(this.safetySettings.map((s) => s.category));
|
|
532
529
|
if (safetySettingsSet.size !== this.safetySettings.length) {
|
|
533
530
|
throw new Error("The categories in `safetySettings` array must be unique");
|
|
534
531
|
}
|
|
535
532
|
}
|
|
536
|
-
this.streaming = fields
|
|
533
|
+
this.streaming = fields.streaming ?? this.streaming;
|
|
534
|
+
this.json = fields.json;
|
|
537
535
|
this.client = new GenerativeAI(this.apiKey).getGenerativeModel({
|
|
538
536
|
model: this.model,
|
|
539
537
|
safetySettings: this.safetySettings,
|
|
540
538
|
generationConfig: {
|
|
541
|
-
candidateCount: 1,
|
|
542
539
|
stopSequences: this.stopSequences,
|
|
543
540
|
maxOutputTokens: this.maxOutputTokens,
|
|
544
541
|
temperature: this.temperature,
|
|
545
542
|
topP: this.topP,
|
|
546
543
|
topK: this.topK,
|
|
547
|
-
...(
|
|
544
|
+
...(this.json ? { responseMimeType: "application/json" } : {}),
|
|
548
545
|
},
|
|
549
546
|
}, {
|
|
550
|
-
apiVersion: fields
|
|
551
|
-
baseUrl: fields
|
|
547
|
+
apiVersion: fields.apiVersion,
|
|
548
|
+
baseUrl: fields.baseUrl,
|
|
552
549
|
});
|
|
553
|
-
this.streamUsage = fields
|
|
550
|
+
this.streamUsage = fields.streamUsage ?? this.streamUsage;
|
|
554
551
|
}
|
|
555
552
|
useCachedContent(cachedContent, modelParams, requestOptions) {
|
|
556
553
|
if (!this.apiKey)
|
|
@@ -566,16 +563,16 @@ export class ChatGoogleGenerativeAI extends BaseChatModel {
|
|
|
566
563
|
// This works on models from April 2024 and later
|
|
567
564
|
// Vertex AI: gemini-1.5-pro and gemini-1.0-002 and later
|
|
568
565
|
// AI Studio: gemini-1.5-pro-latest
|
|
569
|
-
if (this.
|
|
566
|
+
if (this.model === "gemini-1.0-pro-001") {
|
|
570
567
|
return false;
|
|
571
568
|
}
|
|
572
|
-
else if (this.
|
|
569
|
+
else if (this.model.startsWith("gemini-pro-vision")) {
|
|
573
570
|
return false;
|
|
574
571
|
}
|
|
575
|
-
else if (this.
|
|
572
|
+
else if (this.model.startsWith("gemini-1.0-pro-vision")) {
|
|
576
573
|
return false;
|
|
577
574
|
}
|
|
578
|
-
else if (this.
|
|
575
|
+
else if (this.model === "gemini-pro") {
|
|
579
576
|
// on AI Studio gemini-pro is still pointing at gemini-1.0-pro-001
|
|
580
577
|
return false;
|
|
581
578
|
}
|
|
@@ -607,6 +604,16 @@ export class ChatGoogleGenerativeAI extends BaseChatModel {
|
|
|
607
604
|
allowedFunctionNames: options.allowedFunctionNames,
|
|
608
605
|
})
|
|
609
606
|
: undefined;
|
|
607
|
+
if (options?.responseSchema) {
|
|
608
|
+
this.client.generationConfig.responseSchema = options.responseSchema;
|
|
609
|
+
this.client.generationConfig.responseMimeType = "application/json";
|
|
610
|
+
}
|
|
611
|
+
else {
|
|
612
|
+
this.client.generationConfig.responseSchema = undefined;
|
|
613
|
+
this.client.generationConfig.responseMimeType = this.json
|
|
614
|
+
? "application/json"
|
|
615
|
+
: undefined;
|
|
616
|
+
}
|
|
610
617
|
return {
|
|
611
618
|
...(toolsAndConfig?.tools ? { tools: toolsAndConfig.tools } : {}),
|
|
612
619
|
...(toolsAndConfig?.toolConfig
|
|
@@ -738,59 +745,72 @@ export class ChatGoogleGenerativeAI extends BaseChatModel {
|
|
|
738
745
|
const method = config?.method;
|
|
739
746
|
const includeRaw = config?.includeRaw;
|
|
740
747
|
if (method === "jsonMode") {
|
|
741
|
-
throw new Error(`ChatGoogleGenerativeAI only supports "functionCalling" as a method.`);
|
|
748
|
+
throw new Error(`ChatGoogleGenerativeAI only supports "jsonSchema" or "functionCalling" as a method.`);
|
|
742
749
|
}
|
|
743
|
-
let
|
|
750
|
+
let llm;
|
|
744
751
|
let outputParser;
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
let geminiFunctionDefinition;
|
|
767
|
-
if (typeof schema.name === "string" &&
|
|
768
|
-
typeof schema.parameters === "object" &&
|
|
769
|
-
schema.parameters != null) {
|
|
770
|
-
geminiFunctionDefinition = schema;
|
|
771
|
-
functionName = schema.name;
|
|
752
|
+
if (method === "functionCalling") {
|
|
753
|
+
let functionName = name ?? "extract";
|
|
754
|
+
let tools;
|
|
755
|
+
if (isZodSchema(schema)) {
|
|
756
|
+
const jsonSchema = zodToGenerativeAIParameters(schema);
|
|
757
|
+
tools = [
|
|
758
|
+
{
|
|
759
|
+
functionDeclarations: [
|
|
760
|
+
{
|
|
761
|
+
name: functionName,
|
|
762
|
+
description: jsonSchema.description ?? "A function available to call.",
|
|
763
|
+
parameters: jsonSchema,
|
|
764
|
+
},
|
|
765
|
+
],
|
|
766
|
+
},
|
|
767
|
+
];
|
|
768
|
+
outputParser = new GoogleGenerativeAIToolsOutputParser({
|
|
769
|
+
returnSingle: true,
|
|
770
|
+
keyName: functionName,
|
|
771
|
+
zodSchema: schema,
|
|
772
|
+
});
|
|
772
773
|
}
|
|
773
774
|
else {
|
|
774
|
-
geminiFunctionDefinition
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
parameters
|
|
778
|
-
|
|
775
|
+
let geminiFunctionDefinition;
|
|
776
|
+
if (typeof schema.name === "string" &&
|
|
777
|
+
typeof schema.parameters === "object" &&
|
|
778
|
+
schema.parameters != null) {
|
|
779
|
+
geminiFunctionDefinition = schema;
|
|
780
|
+
geminiFunctionDefinition.parameters = removeAdditionalProperties(schema.parameters);
|
|
781
|
+
functionName = schema.name;
|
|
782
|
+
}
|
|
783
|
+
else {
|
|
784
|
+
geminiFunctionDefinition = {
|
|
785
|
+
name: functionName,
|
|
786
|
+
description: schema.description ?? "",
|
|
787
|
+
parameters: removeAdditionalProperties(schema),
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
tools = [
|
|
791
|
+
{
|
|
792
|
+
functionDeclarations: [geminiFunctionDefinition],
|
|
793
|
+
},
|
|
794
|
+
];
|
|
795
|
+
outputParser = new GoogleGenerativeAIToolsOutputParser({
|
|
796
|
+
returnSingle: true,
|
|
797
|
+
keyName: functionName,
|
|
798
|
+
});
|
|
779
799
|
}
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
},
|
|
784
|
-
];
|
|
785
|
-
outputParser = new GoogleGenerativeAIToolsOutputParser({
|
|
786
|
-
returnSingle: true,
|
|
787
|
-
keyName: functionName,
|
|
800
|
+
llm = this.bind({
|
|
801
|
+
tools,
|
|
802
|
+
tool_choice: functionName,
|
|
788
803
|
});
|
|
789
804
|
}
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
805
|
+
else {
|
|
806
|
+
const jsonSchema = isZodSchema(schema)
|
|
807
|
+
? zodToGenerativeAIParameters(schema)
|
|
808
|
+
: removeAdditionalProperties(schema);
|
|
809
|
+
llm = this.bind({
|
|
810
|
+
responseSchema: jsonSchema,
|
|
811
|
+
});
|
|
812
|
+
outputParser = new JsonOutputParser();
|
|
813
|
+
}
|
|
794
814
|
if (!includeRaw) {
|
|
795
815
|
return llm.pipe(outputParser).withConfig({
|
|
796
816
|
runName: "ChatGoogleGenerativeAIStructuredOutput",
|