@langchain/google-genai 0.1.12 → 0.2.0

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.
@@ -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: "gemini-pro"
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.modelName =
501
- fields?.model?.replace(/^models\//, "") ??
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?.temperature ?? this.temperature;
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?.topP ?? this.topP;
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?.topK ?? this.topK;
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?.stopSequences ?? this.stopSequences;
525
- this.apiKey = fields?.apiKey ?? (0, env_1.getEnvironmentVariable)("GOOGLE_API_KEY");
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?.safetySettings ?? this.safetySettings;
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?.streaming ?? this.streaming;
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
- ...(fields?.json ? { responseMimeType: "application/json" } : {}),
547
+ ...(this.json ? { responseMimeType: "application/json" } : {}),
551
548
  },
552
549
  }, {
553
- apiVersion: fields?.apiVersion,
554
- baseUrl: fields?.baseUrl,
550
+ apiVersion: fields.apiVersion,
551
+ baseUrl: fields.baseUrl,
555
552
  });
556
- this.streamUsage = fields?.streamUsage ?? this.streamUsage;
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.modelName === "gemini-1.0-pro-001") {
569
+ if (this.model === "gemini-1.0-pro-001") {
573
570
  return false;
574
571
  }
575
- else if (this.modelName.startsWith("gemini-pro-vision")) {
572
+ else if (this.model.startsWith("gemini-pro-vision")) {
576
573
  return false;
577
574
  }
578
- else if (this.modelName.startsWith("gemini-1.0-pro-vision")) {
575
+ else if (this.model.startsWith("gemini-1.0-pro-vision")) {
579
576
  return false;
580
577
  }
581
- else if (this.modelName === "gemini-pro") {
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,71 @@ 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 functionName = name ?? "extract";
753
+ let llm;
747
754
  let outputParser;
748
- let tools;
749
- if ((0, types_1.isZodSchema)(schema)) {
750
- const jsonSchema = (0, zod_to_genai_parameters_js_1.zodToGenerativeAIParameters)(schema);
751
- tools = [
752
- {
753
- functionDeclarations: [
754
- {
755
- name: functionName,
756
- description: jsonSchema.description ?? "A function available to call.",
757
- parameters: jsonSchema,
758
- },
759
- ],
760
- },
761
- ];
762
- outputParser = new output_parsers_js_1.GoogleGenerativeAIToolsOutputParser({
763
- returnSingle: true,
764
- keyName: functionName,
765
- zodSchema: schema,
766
- });
767
- }
768
- else {
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
- name: functionName,
779
- description: schema.description ?? "",
780
- parameters: schema,
781
- };
778
+ let geminiFunctionDefinition;
779
+ if (typeof schema.name === "string" &&
780
+ typeof schema.parameters === "object" &&
781
+ schema.parameters != null) {
782
+ geminiFunctionDefinition = schema;
783
+ functionName = schema.name;
784
+ }
785
+ else {
786
+ geminiFunctionDefinition = {
787
+ name: functionName,
788
+ description: schema.description ?? "",
789
+ parameters: schema,
790
+ };
791
+ }
792
+ tools = [
793
+ {
794
+ functionDeclarations: [geminiFunctionDefinition],
795
+ },
796
+ ];
797
+ outputParser = new output_parsers_js_1.GoogleGenerativeAIToolsOutputParser({
798
+ returnSingle: true,
799
+ keyName: functionName,
800
+ });
782
801
  }
783
- tools = [
784
- {
785
- functionDeclarations: [geminiFunctionDefinition],
786
- },
787
- ];
788
- outputParser = new output_parsers_js_1.GoogleGenerativeAIToolsOutputParser({
789
- returnSingle: true,
790
- keyName: functionName,
802
+ llm = this.bind({
803
+ tools,
804
+ tool_choice: functionName,
791
805
  });
792
806
  }
793
- const llm = this.bind({
794
- tools,
795
- tool_choice: functionName,
796
- });
807
+ else {
808
+ const jsonSchema = (0, types_1.isZodSchema)(schema)
809
+ ? (0, zod_to_genai_parameters_js_1.zodToGenerativeAIParameters)(schema)
810
+ : schema;
811
+ llm = this.bind({
812
+ responseSchema: jsonSchema,
813
+ });
814
+ outputParser = new output_parsers_1.JsonOutputParser();
815
+ }
797
816
  if (!includeRaw) {
798
817
  return llm.pipe(outputParser).withConfig({
799
818
  runName: "ChatGoogleGenerativeAIStructuredOutput",
@@ -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
- modelName?: string;
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?: GoogleGenerativeAIChatInput);
526
+ constructor(fields: GoogleGenerativeAIChatInput);
533
527
  useCachedContent(cachedContent: CachedContent, modelParams?: ModelParams, requestOptions?: RequestOptions): void;
534
528
  get useSystemInstruction(): boolean;
535
529
  get computeUseSystemInstruction(): boolean;
@@ -3,6 +3,7 @@ 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 { JsonOutputParser, } from "@langchain/core/output_parsers";
6
7
  import { zodToGenerativeAIParameters } 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";
@@ -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: "gemini-pro"
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.modelName =
498
- fields?.model?.replace(/^models\//, "") ??
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?.temperature ?? this.temperature;
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?.topP ?? this.topP;
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?.topK ?? this.topK;
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?.stopSequences ?? this.stopSequences;
522
- this.apiKey = fields?.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
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?.safetySettings ?? this.safetySettings;
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?.streaming ?? this.streaming;
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
- ...(fields?.json ? { responseMimeType: "application/json" } : {}),
544
+ ...(this.json ? { responseMimeType: "application/json" } : {}),
548
545
  },
549
546
  }, {
550
- apiVersion: fields?.apiVersion,
551
- baseUrl: fields?.baseUrl,
547
+ apiVersion: fields.apiVersion,
548
+ baseUrl: fields.baseUrl,
552
549
  });
553
- this.streamUsage = fields?.streamUsage ?? this.streamUsage;
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.modelName === "gemini-1.0-pro-001") {
566
+ if (this.model === "gemini-1.0-pro-001") {
570
567
  return false;
571
568
  }
572
- else if (this.modelName.startsWith("gemini-pro-vision")) {
569
+ else if (this.model.startsWith("gemini-pro-vision")) {
573
570
  return false;
574
571
  }
575
- else if (this.modelName.startsWith("gemini-1.0-pro-vision")) {
572
+ else if (this.model.startsWith("gemini-1.0-pro-vision")) {
576
573
  return false;
577
574
  }
578
- else if (this.modelName === "gemini-pro") {
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,71 @@ 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 functionName = name ?? "extract";
750
+ let llm;
744
751
  let outputParser;
745
- let tools;
746
- if (isZodSchema(schema)) {
747
- const jsonSchema = zodToGenerativeAIParameters(schema);
748
- tools = [
749
- {
750
- functionDeclarations: [
751
- {
752
- name: functionName,
753
- description: jsonSchema.description ?? "A function available to call.",
754
- parameters: jsonSchema,
755
- },
756
- ],
757
- },
758
- ];
759
- outputParser = new GoogleGenerativeAIToolsOutputParser({
760
- returnSingle: true,
761
- keyName: functionName,
762
- zodSchema: schema,
763
- });
764
- }
765
- else {
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
- name: functionName,
776
- description: schema.description ?? "",
777
- parameters: schema,
778
- };
775
+ let geminiFunctionDefinition;
776
+ if (typeof schema.name === "string" &&
777
+ typeof schema.parameters === "object" &&
778
+ schema.parameters != null) {
779
+ geminiFunctionDefinition = schema;
780
+ functionName = schema.name;
781
+ }
782
+ else {
783
+ geminiFunctionDefinition = {
784
+ name: functionName,
785
+ description: schema.description ?? "",
786
+ parameters: schema,
787
+ };
788
+ }
789
+ tools = [
790
+ {
791
+ functionDeclarations: [geminiFunctionDefinition],
792
+ },
793
+ ];
794
+ outputParser = new GoogleGenerativeAIToolsOutputParser({
795
+ returnSingle: true,
796
+ keyName: functionName,
797
+ });
779
798
  }
780
- tools = [
781
- {
782
- functionDeclarations: [geminiFunctionDefinition],
783
- },
784
- ];
785
- outputParser = new GoogleGenerativeAIToolsOutputParser({
786
- returnSingle: true,
787
- keyName: functionName,
799
+ llm = this.bind({
800
+ tools,
801
+ tool_choice: functionName,
788
802
  });
789
803
  }
790
- const llm = this.bind({
791
- tools,
792
- tool_choice: functionName,
793
- });
804
+ else {
805
+ const jsonSchema = isZodSchema(schema)
806
+ ? zodToGenerativeAIParameters(schema)
807
+ : schema;
808
+ llm = this.bind({
809
+ responseSchema: jsonSchema,
810
+ });
811
+ outputParser = new JsonOutputParser();
812
+ }
794
813
  if (!includeRaw) {
795
814
  return llm.pipe(outputParser).withConfig({
796
815
  runName: "ChatGoogleGenerativeAIStructuredOutput",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/google-genai",
3
- "version": "0.1.12",
3
+ "version": "0.2.0",
4
4
  "description": "Google Generative AI integration for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {