Astreus

自定义插件

在 Astreus 文档中了解 自定义插件,获取用于构建智能体系统的设置指导、API 模式和实用示例。 了解构建可靠的 Astreus 智能体系统所需的设置模式、API 和实用示例。

为智能体创建并注册带有工具的自定义插件。

快速开始

克隆完整示例

最简单的开始方式是克隆完整的示例仓库:

git clone https://github.com/astreus-ai/examples
cd examples/custom-plugins
npm install

或仅安装包

如果你更喜欢从零开始构建:

npm install @astreus-ai/astreus

环境配置

# .env
OPENAI_API_KEY=sk-your-openai-api-key-here
DB_URL=sqlite://./astreus.db

自定义天气插件

import { Agent, ToolDefinition, PluginDefinition } from '@astreus-ai/astreus';

// Define a custom tool
const weatherTool: ToolDefinition = {
  name: 'get_weather',
  description: 'Get current weather for a location',
  parameters: {
    location: {
      name: 'location',
      type: 'string',
      description: 'City name',
      required: true
    },
    units: {
      name: 'units',
      type: 'string',
      description: 'Temperature units (celsius or fahrenheit)',
      required: false,
      enum: ['celsius', 'fahrenheit']
    }
  },
  handler: async (params) => {
    // Simulate weather API call
    const weather = {
      temperature: 22,
      conditions: 'sunny',
      location: params.location as string
    };

    return {
      success: true,
      data: weather
    };
  }
};

// Create plugin
const weatherPlugin: PluginDefinition = {
  name: 'weather-plugin',
  version: '1.0.0',
  description: 'Weather information tools',
  tools: [weatherTool]
};

// Create agent and register plugin
const agent = await Agent.create({
  name: 'WeatherAgent',
  model: 'gpt-4o'
});

await agent.registerPlugin(weatherPlugin);

// Use the plugin in conversation
const response = await agent.ask("What's the weather like in Tokyo?");
console.log(response); // Agent automatically uses the weather tool

运行示例

如果你克隆了仓库:

npm run dev

如果你从零开始构建,请创建一个 index.ts 文件并写入上面的代码,然后运行:

npx tsx index.ts

仓库

完整示例可在 GitHub 上获取:examples/custom-plugins

最后更新时间:2026年7月6日