@langchain/google-common
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -21,2 +21,54 @@ import { isModelGemini, validateGeminiParams } from "./gemini.js"; | ||
ret.tools = options?.tools; | ||
// Ensure tools are formatted properly for Gemini | ||
const geminiTools = options?.tools | ||
?.map((tool) => { | ||
if ("function" in tool && | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
"parameters" in tool.function) { | ||
// Tool is in OpenAI format. Convert to Gemini then return. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const castTool = tool.function; | ||
const cleanedParameters = castTool.parameters; | ||
if ("$schema" in cleanedParameters) { | ||
delete cleanedParameters.$schema; | ||
} | ||
if ("additionalProperties" in cleanedParameters) { | ||
delete cleanedParameters.additionalProperties; | ||
} | ||
const toolInGeminiFormat = { | ||
functionDeclarations: [ | ||
{ | ||
name: castTool.name, | ||
description: castTool.description, | ||
parameters: cleanedParameters, | ||
}, | ||
], | ||
}; | ||
return toolInGeminiFormat; | ||
} | ||
else if ("functionDeclarations" in tool) { | ||
return tool; | ||
} | ||
else { | ||
return null; | ||
} | ||
}) | ||
.filter((tool) => tool !== null); | ||
const structuredOutputTools = options?.tools | ||
?.map((tool) => { | ||
if ("lc_namespace" in tool) { | ||
return tool; | ||
} | ||
else { | ||
return null; | ||
} | ||
}) | ||
.filter((tool) => tool !== null); | ||
if (structuredOutputTools && | ||
structuredOutputTools.length > 0 && | ||
geminiTools && | ||
geminiTools.length > 0) { | ||
throw new Error(`Cannot mix structured tools with Gemini tools.\nReceived ${structuredOutputTools.length} structured tools and ${geminiTools.length} Gemini tools.`); | ||
} | ||
ret.tools = geminiTools ?? structuredOutputTools; | ||
return ret; | ||
@@ -23,0 +75,0 @@ } |
@@ -124,16 +124,35 @@ import { AIMessage, AIMessageChunk, } from "@langchain/core/messages"; | ||
}, ""); | ||
const content = JSON.parse(contentStr); | ||
return [ | ||
{ | ||
role: "function", | ||
parts: [ | ||
{ | ||
functionResponse: { | ||
name: message.tool_call_id, | ||
response: content, | ||
try { | ||
const content = JSON.parse(contentStr); | ||
return [ | ||
{ | ||
role: "function", | ||
parts: [ | ||
{ | ||
functionResponse: { | ||
name: message.tool_call_id, | ||
response: content, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
]; | ||
], | ||
}, | ||
]; | ||
} | ||
catch (_) { | ||
return [ | ||
{ | ||
role: "function", | ||
parts: [ | ||
{ | ||
functionResponse: { | ||
name: message.tool_call_id, | ||
response: { | ||
response: contentStr, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
]; | ||
} | ||
} | ||
@@ -334,2 +353,14 @@ export function baseMessageToContent(message) { | ||
const fields = partsToBaseMessageFields([part]); | ||
if (typeof fields.content === "string") { | ||
return new AIMessageChunk(fields); | ||
} | ||
else if (fields.content.every((item) => item.type === "text")) { | ||
const newContent = fields.content | ||
.map((item) => ("text" in item ? item.text : "")) | ||
.join(""); | ||
return new AIMessageChunk({ | ||
...fields, | ||
content: newContent, | ||
}); | ||
} | ||
return new AIMessageChunk(fields); | ||
@@ -336,0 +367,0 @@ } |
{ | ||
"name": "@langchain/google-common", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Core types and classes for Google services.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
164870
4390
2