agsamantha/node_modules/langchain/dist/tools/render.js
2024-10-02 15:15:21 -05:00

44 lines
1.6 KiB
JavaScript

import { zodToJsonSchema } from "zod-to-json-schema";
import { isOpenAITool, } from "@langchain/core/language_models/base";
/**
* Render the tool name and description in plain text.
*
* Output will be in the format of:
* ```
* search: This tool is used for search
* calculator: This tool is used for math
* ```
* @param tools
* @returns a string of all tools and their descriptions
*/
export function renderTextDescription(tools) {
if (tools.every(isOpenAITool)) {
return tools
.map((tool) => `${tool.function.name}${tool.function.description ? `: ${tool.function.description}` : ""}`)
.join("\n");
}
return tools
.map((tool) => `${tool.name}: ${tool.description}`)
.join("\n");
}
/**
* Render the tool name, description, and args in plain text.
* Output will be in the format of:'
* ```
* search: This tool is used for search, args: {"query": {"type": "string"}}
* calculator: This tool is used for math,
* args: {"expression": {"type": "string"}}
* ```
* @param tools
* @returns a string of all tools, their descriptions and a stringified version of their schemas
*/
export function renderTextDescriptionAndArgs(tools) {
if (tools.every(isOpenAITool)) {
return tools
.map((tool) => `${tool.function.name}${tool.function.description ? `: ${tool.function.description}` : ""}, args: ${JSON.stringify(tool.function.parameters)}`)
.join("\n");
}
return tools
.map((tool) => `${tool.name}: ${tool.description}, args: ${JSON.stringify(zodToJsonSchema(tool.schema).properties)}`)
.join("\n");
}