跳转到主要内容
单 Agent 系统涉及一个自主实体,它感知其环境并采取行动以实现特定目标。它独立做出决策,无需与其他 Agent 协调或交互。

安装

npm install @ag-kit/agents @ag-kit/tools zod

快速开始

创建您的第一个 Agent:
import { Agent, OpenAIProvider } from '@ag-kit/agents';

const agent = new Agent({
  name: 'my-assistant',
  description: '一个有用的 AI 助手',
  model: new OpenAIProvider({
    apiKey: process.env.OPENAI_API_KEY!,
    defaultModel: 'gpt-4o-mini'
  }),
  instructions: '你是一个有用的助手。'
});

const result = await agent.run('你好!');
console.log(result.output);

添加工具

工具扩展了 Agent 超越文本生成的能力。
import { tool } from '@ag-kit/tools';
import { z } from 'zod';

const weatherTool = tool(
  async (input: unknown) => {
    const { location } = input as { location: string };
    return { location, temperature: 72, condition: 'sunny' };
  },
  {
    name: 'get_weather',
    description: '获取某个位置的天气',
    schema: z.object({
      location: z.string().describe('城市名称')
    })
  }
);

const agent = new Agent({
  tools: [weatherTool],
  instructions: '当被问及天气时使用 get_weather 工具。'
});

下一步