Install

npm install @agentic/xsai xsai @xsai/tool

Usage

Note that createXSAITools is async because xsai’s underlying xsschema.toJsonSchema is async.

import 'dotenv/config'

import { WeatherClient } from '@agentic/weather'
import { createXSAITools } from '@agentic/xsai'
import { generateText } from 'xsai'

async function main() {
  const weather = new WeatherClient()

  const result = await generateText({
    apiKey: process.env.OPENAI_API_KEY!,
    baseURL: 'https://api.openai.com/v1/',
    model: 'gpt-4o-mini',
    tools: await createXSAITools(weather),
    toolChoice: 'required',
    temperature: 0,
    messages: [
      {
        role: 'system',
        content: 'You are a helpful assistant. Be as concise as possible.'
      },
      { role: 'user', content: 'What is the weather in San Francisco?' }
    ]
  })

  console.log(JSON.stringify(result, null, 2))
}

await main()

Note that this example snippet also requires you to install the Agentic weather tool and dotenv.

npm install @agentic/weather dotenv

Running this example

You’ll need a free API key from weatherapi.com to run this example. Store it in a local .env file as WEATHER_API_KEY.

You’ll need an OpenAI API key to run this example. Store it in a local .env file as OPENAI_API_KEY.

git clone git@github.com:transitive-bullshit/agentic.git
cd agentic
pnpm install
echo 'WEATHER_API_KEY=your-key' >> .env
echo 'OPENAI_API_KEY=your-key' >> .env
npx tsx examples/xsai/bin/weather.ts