ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
55 lines (54 loc) • 2.11 kB
JavaScript
import { generateJson } from "../../model-function/generate-json/generateJson.js";
import { generateJsonOrText } from "../../model-function/generate-json/generateJsonOrText.js";
import { NoSuchToolError } from "./NoSuchToolError.js";
// 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).
*/
export async function useTool(model, tool, prompt, options) {
const { value } = await 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),
};
}
export async function useToolOrGenerateText(model, tools, prompt, options) {
const expandedPrompt = prompt(tools);
const modelResponse = await 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(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
};
}