xsAI v0.1 is now available! Read Announcement
xsAI0.2.0-beta.1

Tool Calling

Connect LLMs to external data and systems.

For convenience, we use valibot as an example.

But this package uses xsschema internally, so it supports any schema library that xsschema supports: e.g. zod, valibot, arktype, etc.

See xsschema for more information.

install sizeminified sizeminzipped size
npm i @xsai/tool

Examples

tool

import {  } from '@xsai/tool'
import * as  from 'valibot'
 
const  = await ({
  : 'Get the weather in a location',
  : ({  }) => .({
    ,
    : 42,
  }),
  : 'weather',
  : .({
    : .(
      .(),
      .('The location to get the weather for'),
    ),
  }),
})

tool (with returns schema)

import {  } from '@xsai/tool'
import * as  from 'valibot'
 
const  = await ({
  : 'Get the weather in a location',
  : ({  }) => ({
    ,
    : 42,
  }),
  : 'weather',
  : .({
    : .(
      .(),
      .('The location to get the weather for'),
    ),
  }),
  : .({
    : .(
      .(),
      .('The location to get the weather for'),
    ),
    : .(),
  }),
})

with generateText

import {  } from '@xsai/generate-text'
import {  } from '@xsai/tool'
import {  } from 'node:process'
import * as  from 'valibot'
 
const  = await ({
  : 'Get the weather in a location',
  : ({  }) => .({
    ,
    : 42,
  }),
  : 'weather',
  : .({
    : .(
      .(),
      .('The location to get the weather for'),
    ),
  }),
})
 
const {  } = await ({
  : .!,
  : 'https://api.openai.com/v1/',
  : 2, 
  : [
    {
      : 'You are a helpful assistant.',
      : 'system',
    },
    {
      : 'What is the weather in San Francisco?',
      : 'user',
    },
  ],
  : 'gpt-4o',
  : [], 
})
Edit on GitHub

Last updated on

On this page