Loading...
Skip to main content

Module: batteries/use-tools

This module provides the UseTools component to allow a Large Language Model to invoke external functions.

Interfaces

Functions

ExecuteFunction

ExecuteFunction<T>(«destructured», «destructured»): Promise<Element>

Executes a function during rendering and wraps the result in a <FunctionResponse>.

Type parameters

Name
T

Parameters

NameType
«destructured»Object
› argsT
› funcComponent<T>
› namestring
› id?string
«destructured»ComponentContext

Returns

Promise<Element>

Defined in

ai-jsx/src/batteries/use-tools.tsx:69


UseTools

UseTools(props, «destructured»): Promise<0 | Element | Element<{ children: Node ; metadata?: Record<string, Jsonifiable> ; name?: string }> | Element<{ children: Node ; metadata?: Record<string, Jsonifiable> }> | Element<{ args: Record<string, null | string | number | boolean> ; name: string ; id?: string ; metadata?: Record<string, Jsonifiable> ; partial?: boolean }> | Element<{ children: Node ; name: string ; failed?: boolean ; id?: string ; metadata?: Record<string, Jsonifiable> }>>

Give a model tools it can use, like a calculator, or ability to call an API.

This is conceptually similar to chatGPT plugins.

Example

 async function turnLightsOn() { ... Code to turn lights on ... }
async function turnLightsOff() { ... Code to turn lights off ... }
// Activate a scene in the user's lighting settings, like "Bedtime" or "Midday".
async function activeScene({sceneName}: {sceneName: string}) { ... Code to activate a scene ... }

import z from 'zod';
const tools: Record<string, Tool> = {
turnLightsOn: {
description: "Turn the lights on in the user's home",
parameters: {},
func: turnLightsOn,
},
turnLightsOff: {
description: "Turn the lights off in the user's home",
parameters: {},
func: turnLightsOff,
},
activeScene: {
description: `Activate a scene in the user's lighting settings, like "Bedtime" or "Midday".`,
parameters: {
sceneName: {
description: "The scene to activate the lighting in.",
type: "string",
required: true,
},
},
func: activeScene,
},
};

<UseTools tools={tools}>
<SystemMessage>
You control a home automation system. The user will request an action in their home. You should take an action and
then generate a response telling the user what you've done.
</SystemMessage>
<UserMessage>{userRequest}</UserMessage>
</UseTools>;

Parameters

NameType
propsUseToolsProps
«destructured»RenderContext

Returns

Promise<0 | Element | Element<{ children: Node ; metadata?: Record<string, Jsonifiable> ; name?: string }> | Element<{ children: Node ; metadata?: Record<string, Jsonifiable> }> | Element<{ args: Record<string, null | string | number | boolean> ; name: string ; id?: string ; metadata?: Record<string, Jsonifiable> ; partial?: boolean }> | Element<{ children: Node ; name: string ; failed?: boolean ; id?: string ; metadata?: Record<string, Jsonifiable> }>>

Defined in

ai-jsx/src/batteries/use-tools.tsx:140