OpenClaw技能开发入门:为Qwen3.5-9B-AWQ-4bit扩展自定义能力

张开发
2026/4/9 17:39:16 15 分钟阅读

分享文章

OpenClaw技能开发入门:为Qwen3.5-9B-AWQ-4bit扩展自定义能力
OpenClaw技能开发入门为Qwen3.5-9B-AWQ-4bit扩展自定义能力1. 为什么需要开发OpenClaw技能去年冬天我接手了一个图片管理项目需要为团队处理数千张产品图的水印添加和识别工作。当我尝试用传统脚本批量处理时发现不同图片的尺寸、比例差异导致水印位置很难统一更别提识别已有水印这种需要视觉分析的任务了。这时OpenClaw进入了我的视野——它不仅能调用本地模型处理文件还能通过技能扩展实现复杂的自动化流程。开发自定义技能的核心价值在于弥补模型短板Qwen3.5虽然能理解图片内容但缺乏具体的图片编辑能力封装复杂流程将多步骤操作如图片预处理→水印添加→结果验证打包成单一指令复用团队经验把水印位置计算等业务逻辑沉淀为可共享的技能包2. 开发环境准备2.1 基础工具链我的开发环境是macOS VS Code以下是经过验证的版本组合# 验证Node.js环境必须≥18.x node -v # v20.12.2 npm -v # 10.5.0 # 安装OpenClaw CLI工具 npm install -g openclaw/clilatest2.2 模型服务配置在~/.openclaw/openclaw.json中确保已配置Qwen3.5服务端点{ models: { providers: { qwen-multimodal: { baseUrl: http://localhost:8080/v1, // 本地模型服务地址 apiKey: your_api_key, api: openai-completions, models: [ { id: qwen3.5-9b-awq-4bit, name: Qwen Multimodal, contextWindow: 32768 } ] } } } }3. 创建水印处理技能3.1 初始化npm模块通过OpenClaw脚手架快速生成项目骨架clawhub create watermark-toolkit --templateskill cd watermark-toolkit关键文件结构说明. ├── package.json # 技能元数据 ├── src │ ├── index.ts # 技能入口 │ └── tools # 工具函数目录 │ ├── addWatermark.ts │ └── detectWatermark.ts └── test # 测试用例3.2 实现水印添加工具在src/tools/addWatermark.ts中我们使用Sharp库处理图片import sharp from sharp; import { Tool } from openclaw/core; interface WatermarkOptions { text: string; position: top-left | bottom-right; opacity: number; } export const addWatermark Tool({ name: addWatermark, description: Add text watermark to image, parameters: { imagePath: { type: string, description: Path to source image }, outputPath: { type: string, description: Path to save watermarked image }, options: { type: object, schema: WatermarkOptions } }, async execute({ imagePath, outputPath, options }) { const { width, height } await sharp(imagePath).metadata(); // 根据图片尺寸计算水印位置 const position calculatePosition(options.position, width!, height!); await sharp(imagePath) .composite([{ input: await generateTextBuffer(options.text), ...position, blend: over, opacity: options.opacity }]) .toFile(outputPath); return { success: true, outputPath }; } }); // 辅助函数生成文字Buffer const generateTextBuffer async (text: string) { return sharp({ text: { text, font: sans, width: 500 } }).toBuffer(); };3.3 对接Qwen多模态API在src/tools/detectWatermark.ts中调用模型分析图片import { Tool } from openclaw/core; export const detectWatermark Tool({ name: detectWatermark, description: Detect watermark presence using Qwen3.5, parameters: { imagePath: { type: string, description: Path to analyze } }, async execute({ imagePath }, { models }) { const response await models.multimodal({ model: qwen3.5-9b-awq-4bit, messages: [{ role: user, content: [ { type: text, text: 这张图片是否有文字水印回答格式{exists: boolean, text?: string} }, { type: image, image: imagePath } ] }] }); return JSON.parse(response.choices[0].message.content); } });4. 调试与本地测试4.1 编写测试用例创建test/integration.test.ts验证完整流程import { runTask } from openclaw/testing; import path from path; describe(Watermark Toolkit, () { const testImage path.join(__dirname, fixtures/sample.jpg); it(should add and detect watermark, async () { const output await runTask({ goal: 给图片添加水印并验证效果, tools: [watermark-toolkit/*], input: { imagePath: testImage, watermarkText: 测试水印 } }); expect(output).toHaveProperty(detected.text, 测试水印); }); });4.2 常见问题排查我遇到的三个典型问题及解决方案Sharp库安装失败在Apple Silicon设备上需要指定archnpm install --platformdarwin --archarm64 sharp模型返回格式不一致通过JSON Schema校验响应const schema { type: object, properties: { exists: { type: boolean }, text: { type: string, optional: true } } };图片路径权限问题使用path.resolve处理相对路径const absolutePath path.resolve(process.cwd(), imagePath);5. 发布到ClawHub技能市场5.1 准备发布包更新package.json中的关键字段{ name: yourname/watermark-toolkit, version: 1.0.0, keywords: [image, watermark, qwen], openclaw: { runtime: node18, compatibleModels: [qwen3.5-9b-awq-4bit] } }构建生产版本npm run build5.2 发布流程登录ClawHub账号clawhub login执行发布命令clawhub publish --accesspublic后续更新版本npm version patch clawhub publish6. 实际应用示例现在可以通过自然语言指令使用该技能openclaw run 给product_images目录下的所有图片添加机密水印位置在右下角执行流程分解遍历目录获取图片列表调用addWatermark工具处理每张图片使用detectWatermark抽样验证生成处理报告获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。

更多文章