跳转到主要内容

Agent 概述

AG-Kit 提供两种主要 Agent 类型用于构建人工智能应用。

Agent 类型

核心特性

快速开始

import { Agent, OpenAIProvider } from '@ag-kit/agents';

const modelProvider = new OpenAIProvider({
  apiKey: process.env.OPENAI_API_KEY,
  defaultModel: 'gpt-4'
});

// 定义带参数的工具
const weatherTool = {
  name: 'get_weather',
  description: '获取指定位置的当前天气',
  schema: z.object({
    location: z.string().describe('城市名称或坐标'),
    unit: z.enum(['celsius', 'fahrenheit']).default('celsius')
  }),
  handler: async (input: { location: string; unit: string }) => {
    // 工具实现
    return { temperature: 22, condition: 'sunny', unit: input.unit };
  }
};

const agent = new Agent({
  name: 'my-agent',
  model: modelProvider,
  instructions: '你是一个可以获取天气信息的助手',
  tools: [weatherTool]
});

const result = await agent.run('纽约的天气如何?');
console.log(result.data);

安装

npm install @ag-kit/agents