@langchain/google-common 0.1.3 → 0.1.4

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/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,29 +788,21 @@ 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") {
@@ -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,29 +783,21 @@ 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") {
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.4",
4
4
  "description": "Core types and classes for Google services.",
5
5
  "type": "module",
6
6
  "engines": {