Agents

AI agents with tool calling.

1 min readEdit this page

Setup

import { Agent, createTool, calculatorTool } from '@restjs/ai'
 
const agent = new Agent({
  provider: ai,
  tools: [calculatorTool, weatherTool],
  maxIterations: 10
})

Run

const result = await agent.run('What is 25 * 4?')
result.response
result.steps

Custom tools

const weatherTool = createTool({
  name: 'get_weather',
  description: 'Get weather for a city',
  parameters: {
    type: 'object',
    properties: { city: { type: 'string' } },
    required: ['city']
  },
  handler: async ({ city }) => ({ city, temp: '22C' })
})

Stream

for await (const step of agent.runStream('Calculate 2^10')) {
  console.log(step.thought, step.toolCalls, step.finalAnswer)
}