@langchain/google-common 0.1.3 → 0.1.5

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.
@@ -356,6 +356,7 @@ class ChatGoogleBase extends chat_models_1.BaseChatModel {
356
356
  }
357
357
  const llm = this.bind({
358
358
  tools,
359
+ tool_choice: functionName,
359
360
  });
360
361
  if (!includeRaw) {
361
362
  return llm.pipe(outputParser).withConfig({
@@ -352,6 +352,7 @@ export class ChatGoogleBase extends BaseChatModel {
352
352
  }
353
353
  const llm = this.bind({
354
354
  tools,
355
+ tool_choice: functionName,
355
356
  });
356
357
  if (!includeRaw) {
357
358
  return llm.pipe(outputParser).withConfig({
@@ -19,6 +19,10 @@ class EmbeddingsConnection extends connection_js_1.GoogleAIConnection {
19
19
  async buildUrlMethod() {
20
20
  return "predict";
21
21
  }
22
+ get modelPublisher() {
23
+ // All the embedding models are currently published by "google"
24
+ return "google";
25
+ }
22
26
  async formatData(input, parameters) {
23
27
  return {
24
28
  instances: input,
@@ -16,6 +16,10 @@ class EmbeddingsConnection extends GoogleAIConnection {
16
16
  async buildUrlMethod() {
17
17
  return "predict";
18
18
  }
19
+ get modelPublisher() {
20
+ // All the embedding models are currently published by "google"
21
+ return "google";
22
+ }
19
23
  async formatData(input, parameters) {
20
24
  return {
21
25
  instances: input,
package/dist/types.d.ts CHANGED
@@ -226,6 +226,20 @@ export interface GeminiContent {
226
226
  }
227
227
  export interface GeminiTool {
228
228
  functionDeclarations?: GeminiFunctionDeclaration[];
229
+ googleSearchRetrieval?: GoogleSearchRetrieval;
230
+ retrieval?: VertexAIRetrieval;
231
+ }
232
+ export interface GoogleSearchRetrieval {
233
+ dynamicRetrievalConfig?: {
234
+ mode?: string;
235
+ dynamicThreshold?: number;
236
+ };
237
+ }
238
+ export interface VertexAIRetrieval {
239
+ vertexAiSearch: {
240
+ datastore: string;
241
+ };
242
+ disableAttribution?: boolean;
229
243
  }
230
244
  export interface GeminiFunctionDeclaration {
231
245
  name: string;
@@ -38,31 +38,42 @@ function processToolChoice(toolChoice, allowedFunctionNames) {
38
38
  throw new Error("Object inputs for tool_choice not supported.");
39
39
  }
40
40
  function convertToGeminiTools(tools) {
41
- const geminiTools = [
42
- {
43
- functionDeclarations: [],
44
- },
45
- ];
41
+ const geminiTools = [];
42
+ let functionDeclarationsIndex = -1;
46
43
  tools.forEach((tool) => {
47
- if ("functionDeclarations" in tool &&
48
- Array.isArray(tool.functionDeclarations)) {
49
- const funcs = tool.functionDeclarations;
50
- geminiTools[0].functionDeclarations?.push(...funcs);
44
+ if ("googleSearchRetrieval" in tool || "retrieval" in tool) {
45
+ geminiTools.push(tool);
51
46
  }
52
- else if ((0, function_calling_1.isLangChainTool)(tool)) {
53
- const jsonSchema = (0, zod_to_gemini_parameters_js_1.zodToGeminiParameters)(tool.schema);
54
- geminiTools[0].functionDeclarations?.push({
55
- name: tool.name,
56
- description: tool.description ?? `A function available to call.`,
57
- parameters: jsonSchema,
58
- });
59
- }
60
- else if ((0, base_1.isOpenAITool)(tool)) {
61
- geminiTools[0].functionDeclarations?.push({
62
- name: tool.function.name,
63
- description: tool.function.description ?? `A function available to call.`,
64
- parameters: (0, zod_to_gemini_parameters_js_1.jsonSchemaToGeminiParameters)(tool.function.parameters),
65
- });
47
+ else {
48
+ if (functionDeclarationsIndex === -1) {
49
+ geminiTools.push({
50
+ functionDeclarations: [],
51
+ });
52
+ functionDeclarationsIndex = geminiTools.length - 1;
53
+ }
54
+ if ("functionDeclarations" in tool &&
55
+ Array.isArray(tool.functionDeclarations)) {
56
+ const funcs = tool.functionDeclarations;
57
+ geminiTools[functionDeclarationsIndex].functionDeclarations.push(...funcs);
58
+ }
59
+ else if ((0, function_calling_1.isLangChainTool)(tool)) {
60
+ const jsonSchema = (0, zod_to_gemini_parameters_js_1.zodToGeminiParameters)(tool.schema);
61
+ geminiTools[functionDeclarationsIndex].functionDeclarations.push({
62
+ name: tool.name,
63
+ description: tool.description ?? `A function available to call.`,
64
+ parameters: jsonSchema,
65
+ });
66
+ }
67
+ else if ((0, base_1.isOpenAITool)(tool)) {
68
+ geminiTools[functionDeclarationsIndex].functionDeclarations.push({
69
+ name: tool.function.name,
70
+ description: tool.function.description ?? `A function available to call.`,
71
+ parameters: (0, zod_to_gemini_parameters_js_1.jsonSchemaToGeminiParameters)(tool.function.parameters),
72
+ });
73
+ }
74
+ else {
75
+ throw new Error(`Received invalid tool: ${JSON.stringify(tool)}`);
76
+ }
66
77
  }
67
78
  });
68
79
  return geminiTools;
@@ -34,31 +34,42 @@ function processToolChoice(toolChoice, allowedFunctionNames) {
34
34
  throw new Error("Object inputs for tool_choice not supported.");
35
35
  }
36
36
  export function convertToGeminiTools(tools) {
37
- const geminiTools = [
38
- {
39
- functionDeclarations: [],
40
- },
41
- ];
37
+ const geminiTools = [];
38
+ let functionDeclarationsIndex = -1;
42
39
  tools.forEach((tool) => {
43
- if ("functionDeclarations" in tool &&
44
- Array.isArray(tool.functionDeclarations)) {
45
- const funcs = tool.functionDeclarations;
46
- geminiTools[0].functionDeclarations?.push(...funcs);
40
+ if ("googleSearchRetrieval" in tool || "retrieval" in tool) {
41
+ geminiTools.push(tool);
47
42
  }
48
- else if (isLangChainTool(tool)) {
49
- const jsonSchema = zodToGeminiParameters(tool.schema);
50
- geminiTools[0].functionDeclarations?.push({
51
- name: tool.name,
52
- description: tool.description ?? `A function available to call.`,
53
- parameters: jsonSchema,
54
- });
55
- }
56
- else if (isOpenAITool(tool)) {
57
- geminiTools[0].functionDeclarations?.push({
58
- name: tool.function.name,
59
- description: tool.function.description ?? `A function available to call.`,
60
- parameters: jsonSchemaToGeminiParameters(tool.function.parameters),
61
- });
43
+ else {
44
+ if (functionDeclarationsIndex === -1) {
45
+ geminiTools.push({
46
+ functionDeclarations: [],
47
+ });
48
+ functionDeclarationsIndex = geminiTools.length - 1;
49
+ }
50
+ if ("functionDeclarations" in tool &&
51
+ Array.isArray(tool.functionDeclarations)) {
52
+ const funcs = tool.functionDeclarations;
53
+ geminiTools[functionDeclarationsIndex].functionDeclarations.push(...funcs);
54
+ }
55
+ else if (isLangChainTool(tool)) {
56
+ const jsonSchema = zodToGeminiParameters(tool.schema);
57
+ geminiTools[functionDeclarationsIndex].functionDeclarations.push({
58
+ name: tool.name,
59
+ description: tool.description ?? `A function available to call.`,
60
+ parameters: jsonSchema,
61
+ });
62
+ }
63
+ else if (isOpenAITool(tool)) {
64
+ geminiTools[functionDeclarationsIndex].functionDeclarations.push({
65
+ name: tool.function.name,
66
+ description: tool.function.description ?? `A function available to call.`,
67
+ parameters: jsonSchemaToGeminiParameters(tool.function.parameters),
68
+ });
69
+ }
70
+ else {
71
+ throw new Error(`Received invalid tool: ${JSON.stringify(tool)}`);
72
+ }
62
73
  }
63
74
  });
64
75
  return geminiTools;
@@ -141,10 +141,10 @@ function getGeminiAPI(config) {
141
141
  if (!url) {
142
142
  throw new Error("Missing Image URL");
143
143
  }
144
- const mineTypeAndData = extractMimeType(url);
145
- if (mineTypeAndData) {
144
+ const mimeTypeAndData = extractMimeType(url);
145
+ if (mimeTypeAndData) {
146
146
  return {
147
- inlineData: mineTypeAndData,
147
+ inlineData: mimeTypeAndData,
148
148
  };
149
149
  }
150
150
  else {
@@ -788,38 +788,39 @@ function getGeminiAPI(config) {
788
788
  parameters: jsonSchema,
789
789
  };
790
790
  }
791
- function structuredToolsToGeminiTools(tools) {
792
- return [
793
- {
794
- functionDeclarations: tools.map(structuredToolToFunctionDeclaration),
795
- },
796
- ];
797
- }
798
791
  function formatTools(parameters) {
799
792
  const tools = parameters?.tools;
800
793
  if (!tools || tools.length === 0) {
801
794
  return [];
802
795
  }
803
- if (tools.every(function_calling_1.isLangChainTool)) {
804
- return structuredToolsToGeminiTools(tools);
805
- }
806
- else {
807
- if (tools.length === 1 &&
808
- (!("functionDeclarations" in tools[0]) ||
809
- !tools[0].functionDeclarations?.length)) {
810
- return [];
811
- }
812
- return tools;
796
+ // Group all LangChain tools into a single functionDeclarations array
797
+ const langChainTools = tools.filter(function_calling_1.isLangChainTool);
798
+ const otherTools = tools.filter((tool) => !(0, function_calling_1.isLangChainTool)(tool));
799
+ const result = [...otherTools];
800
+ if (langChainTools.length > 0) {
801
+ result.push({
802
+ functionDeclarations: langChainTools.map(structuredToolToFunctionDeclaration),
803
+ });
813
804
  }
805
+ return result;
814
806
  }
815
807
  function formatToolConfig(parameters) {
816
808
  if (!parameters.tool_choice || typeof parameters.tool_choice !== "string") {
817
809
  return undefined;
818
810
  }
811
+ if (["auto", "any", "none"].includes(parameters.tool_choice)) {
812
+ return {
813
+ functionCallingConfig: {
814
+ mode: parameters.tool_choice,
815
+ allowedFunctionNames: parameters.allowed_function_names,
816
+ },
817
+ };
818
+ }
819
+ // force tool choice to be a single function name in case of structured output
819
820
  return {
820
821
  functionCallingConfig: {
821
- mode: parameters.tool_choice,
822
- allowedFunctionNames: parameters.allowed_function_names,
822
+ mode: "any",
823
+ allowedFunctionNames: [parameters.tool_choice],
823
824
  },
824
825
  };
825
826
  }
@@ -136,10 +136,10 @@ export function getGeminiAPI(config) {
136
136
  if (!url) {
137
137
  throw new Error("Missing Image URL");
138
138
  }
139
- const mineTypeAndData = extractMimeType(url);
140
- if (mineTypeAndData) {
139
+ const mimeTypeAndData = extractMimeType(url);
140
+ if (mimeTypeAndData) {
141
141
  return {
142
- inlineData: mineTypeAndData,
142
+ inlineData: mimeTypeAndData,
143
143
  };
144
144
  }
145
145
  else {
@@ -783,38 +783,39 @@ export function getGeminiAPI(config) {
783
783
  parameters: jsonSchema,
784
784
  };
785
785
  }
786
- function structuredToolsToGeminiTools(tools) {
787
- return [
788
- {
789
- functionDeclarations: tools.map(structuredToolToFunctionDeclaration),
790
- },
791
- ];
792
- }
793
786
  function formatTools(parameters) {
794
787
  const tools = parameters?.tools;
795
788
  if (!tools || tools.length === 0) {
796
789
  return [];
797
790
  }
798
- if (tools.every(isLangChainTool)) {
799
- return structuredToolsToGeminiTools(tools);
800
- }
801
- else {
802
- if (tools.length === 1 &&
803
- (!("functionDeclarations" in tools[0]) ||
804
- !tools[0].functionDeclarations?.length)) {
805
- return [];
806
- }
807
- return tools;
791
+ // Group all LangChain tools into a single functionDeclarations array
792
+ const langChainTools = tools.filter(isLangChainTool);
793
+ const otherTools = tools.filter((tool) => !isLangChainTool(tool));
794
+ const result = [...otherTools];
795
+ if (langChainTools.length > 0) {
796
+ result.push({
797
+ functionDeclarations: langChainTools.map(structuredToolToFunctionDeclaration),
798
+ });
808
799
  }
800
+ return result;
809
801
  }
810
802
  function formatToolConfig(parameters) {
811
803
  if (!parameters.tool_choice || typeof parameters.tool_choice !== "string") {
812
804
  return undefined;
813
805
  }
806
+ if (["auto", "any", "none"].includes(parameters.tool_choice)) {
807
+ return {
808
+ functionCallingConfig: {
809
+ mode: parameters.tool_choice,
810
+ allowedFunctionNames: parameters.allowed_function_names,
811
+ },
812
+ };
813
+ }
814
+ // force tool choice to be a single function name in case of structured output
814
815
  return {
815
816
  functionCallingConfig: {
816
- mode: parameters.tool_choice,
817
- allowedFunctionNames: parameters.allowed_function_names,
817
+ mode: "any",
818
+ allowedFunctionNames: [parameters.tool_choice],
818
819
  },
819
820
  };
820
821
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/google-common",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Core types and classes for Google services.",
5
5
  "type": "module",
6
6
  "engines": {