ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
60 lines (59 loc) • 2.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useToolOrGenerateText = exports.useTool = void 0;
const generateJson_js_1 = require("../../model-function/generate-json/generateJson.cjs");
const generateJsonOrText_js_1 = require("../../model-function/generate-json/generateJsonOrText.cjs");
const NoSuchToolError_js_1 = require("./NoSuchToolError.cjs");
// In this file, using 'any' is required to allow for flexibility in the inputs. The actual types are
// retrieved through lookups such as TOOL["name"], such that any does not affect any client.
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
*
* @returns The result contains the name of the tool (`tool` property),
* the parameters (`parameters` property, typed),
* and the result of the tool execution (`result` property, typed).
*/
async function useTool(model, tool, prompt, options) {
const { value } = await (0, generateJson_js_1.generateJson)(model, {
name: tool.name,
description: tool.description,
schema: tool.inputSchema,
}, () => prompt(tool), options);
return {
tool: tool.name,
parameters: value,
result: await tool.execute(value),
};
}
exports.useTool = useTool;
async function useToolOrGenerateText(model, tools, prompt, options) {
const expandedPrompt = prompt(tools);
const modelResponse = await (0, generateJsonOrText_js_1.generateJsonOrText)(model, tools.map((tool) => ({
name: tool.name,
description: tool.description,
schema: tool.inputSchema,
})), () => expandedPrompt, options);
const { schema, text } = modelResponse;
if (schema == null) {
return {
tool: null,
parameters: null,
result: null,
text,
};
}
const tool = tools.find((tool) => tool.name === schema);
if (tool == null) {
throw new NoSuchToolError_js_1.NoSuchToolError(schema.toString());
}
const toolParameters = modelResponse.value;
const result = await tool.execute(toolParameters);
return {
tool: schema,
result,
parameters: toolParameters,
text: text, // string | null is the expected value here
};
}
exports.useToolOrGenerateText = useToolOrGenerateText;