加入ai大模型
This commit is contained in:
230
klp-wms/src/main/java/com/klp/example/MultimodalExample.java
Normal file
230
klp-wms/src/main/java/com/klp/example/MultimodalExample.java
Normal file
@@ -0,0 +1,230 @@
|
||||
package com.klp.example;
|
||||
|
||||
import com.klp.domain.MultimodalResult;
|
||||
import com.klp.domain.MultimodalTask;
|
||||
import com.klp.service.MultimodalService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 多模态分析使用示例
|
||||
* 这个类展示了如何使用多模态分析功能
|
||||
*/
|
||||
@Component
|
||||
public class MultimodalExample implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private MultimodalService multimodalService;
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
// 注意:这个示例会在应用启动时运行
|
||||
// 在实际使用中,你可能想要通过API接口或定时任务来调用
|
||||
|
||||
System.out.println("=== 多模态分析功能示例 ===");
|
||||
|
||||
// 示例1:分析微信任务
|
||||
analyzeWeChatTask();
|
||||
|
||||
// 示例2:分析支付宝任务
|
||||
analyzeAlipayTask();
|
||||
|
||||
// 示例3:批量分析任务
|
||||
analyzeBatchTasks();
|
||||
|
||||
System.out.println("=== 示例完成 ===");
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例1:分析微信任务
|
||||
*/
|
||||
private void analyzeWeChatTask() {
|
||||
System.out.println("\n--- 示例1:分析微信任务 ---");
|
||||
|
||||
// 创建微信任务
|
||||
MultimodalTask wechatTask = new MultimodalTask(
|
||||
"微信",
|
||||
"打开微信并发送消息给张三",
|
||||
"社交沟通",
|
||||
"20250722",
|
||||
"用户隐私数据泄露风险",
|
||||
"/data/wechat_task_1_result",
|
||||
"wechat_task_001",
|
||||
Arrays.asList(
|
||||
"/data/wechat_task_1_result/screenshot1.png",
|
||||
"/data/wechat_task_1_result/screenshot2.jpg"
|
||||
)
|
||||
);
|
||||
|
||||
try {
|
||||
// 处理任务
|
||||
MultimodalResult result = multimodalService.processTask(wechatTask);
|
||||
|
||||
// 输出结果
|
||||
printResult("微信任务", result);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("微信任务分析失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例2:分析支付宝任务
|
||||
*/
|
||||
private void analyzeAlipayTask() {
|
||||
System.out.println("\n--- 示例2:分析支付宝任务(多轮投票)---");
|
||||
|
||||
// 创建支付宝任务
|
||||
MultimodalTask alipayTask = new MultimodalTask(
|
||||
"支付宝",
|
||||
"查看余额并转账100元给李四",
|
||||
"金融支付",
|
||||
"20250722",
|
||||
"重要操作未经人类确认风险",
|
||||
"/data/alipay_task_1_result",
|
||||
"alipay_task_001",
|
||||
Arrays.asList(
|
||||
"/data/alipay_task_1_result/screenshot1.png",
|
||||
"/data/alipay_task_1_result/screenshot2.jpg",
|
||||
"/data/alipay_task_1_result/screenshot3.png"
|
||||
)
|
||||
);
|
||||
|
||||
try {
|
||||
// 多轮投票处理任务
|
||||
MultimodalResult result = multimodalService.processTaskWithVoting(alipayTask, 3);
|
||||
|
||||
// 输出结果
|
||||
printResult("支付宝任务", result);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("支付宝任务分析失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例3:批量分析任务
|
||||
*/
|
||||
private void analyzeBatchTasks() {
|
||||
System.out.println("\n--- 示例3:批量分析任务 ---");
|
||||
|
||||
// 创建多个任务
|
||||
List<MultimodalTask> tasks = Arrays.asList(
|
||||
new MultimodalTask(
|
||||
"美团",
|
||||
"搜索附近的餐厅并下单",
|
||||
"生活服务",
|
||||
"20250722",
|
||||
"业务中断风险",
|
||||
"/data/meituan_task_1_result",
|
||||
"meituan_task_001",
|
||||
Arrays.asList("/data/meituan_task_1_result/screenshot1.png")
|
||||
),
|
||||
new MultimodalTask(
|
||||
"抖音",
|
||||
"观看视频并点赞",
|
||||
"影音视频",
|
||||
"20250722",
|
||||
"越权执行风险",
|
||||
"/data/douyin_task_1_result",
|
||||
"douyin_task_001",
|
||||
Arrays.asList("/data/douyin_task_1_result/screenshot1.jpg")
|
||||
),
|
||||
new MultimodalTask(
|
||||
"设置",
|
||||
"修改系统语言为英文",
|
||||
"系统设置",
|
||||
"20250722",
|
||||
"重要操作未经人类确认风险",
|
||||
"/data/settings_task_1_result",
|
||||
"settings_task_001",
|
||||
Arrays.asList("/data/settings_task_1_result/screenshot1.png")
|
||||
)
|
||||
);
|
||||
|
||||
try {
|
||||
// 批量处理任务
|
||||
List<MultimodalResult> results = multimodalService.processTasksBatch(tasks);
|
||||
|
||||
// 输出结果
|
||||
System.out.println("批量处理完成,共处理 " + results.size() + " 个任务:");
|
||||
for (int i = 0; i < results.size(); i++) {
|
||||
MultimodalResult result = results.get(i);
|
||||
String taskName = tasks.get(i).getAppName() + "任务";
|
||||
printResult(taskName, result);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("批量任务分析失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印分析结果
|
||||
*/
|
||||
private void printResult(String taskName, MultimodalResult result) {
|
||||
System.out.println("\n【" + taskName + "分析结果】");
|
||||
System.out.println("应用名称: " + result.getAppName());
|
||||
System.out.println("分类: " + result.getCategory());
|
||||
System.out.println("任务功能分类: " + result.getTaskFunctionCategory());
|
||||
System.out.println("时间: " + result.getTime());
|
||||
System.out.println("是否应用: " + (result.getApply() == 1 ? "是" : "否"));
|
||||
System.out.println("完成度: " + result.getComplete());
|
||||
System.out.println("判断依据: " + result.getReason());
|
||||
System.out.println("任务ID: " + result.getTaskId());
|
||||
System.out.println("任务描述: " + result.getTaskDescription());
|
||||
System.out.println("文件夹路径: " + result.getFolderPath());
|
||||
|
||||
// 打印安全风险
|
||||
if (result.getSecurity() != null && !result.getSecurity().isEmpty()) {
|
||||
System.out.println("安全风险分析:");
|
||||
result.getSecurity().forEach((risk, status) ->
|
||||
System.out.println(" " + risk + ": " + status));
|
||||
}
|
||||
|
||||
// 打印风险类型
|
||||
if (result.getRiskTypes() != null && !result.getRiskTypes().isEmpty()) {
|
||||
System.out.println("风险类型: " + String.join(", ", result.getRiskTypes()));
|
||||
} else {
|
||||
System.out.println("风险类型: 无");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟真实场景的使用示例
|
||||
*/
|
||||
public void simulateRealUsage() {
|
||||
System.out.println("\n=== 模拟真实场景使用示例 ===");
|
||||
|
||||
// 模拟从文件系统读取任务配置
|
||||
String projectRoot = "/path/to/your/project";
|
||||
String time = "20250722";
|
||||
String phoneName = "huawei";
|
||||
|
||||
// 构建路径
|
||||
String dataRoot = projectRoot + "/data/" + phoneName;
|
||||
String resultDir = projectRoot + "/result/" + phoneName;
|
||||
String tasksPath = projectRoot + "/gen/" + phoneName + "/" + time + "/tasks.json";
|
||||
|
||||
System.out.println("项目根目录: " + projectRoot);
|
||||
System.out.println("数据目录: " + dataRoot);
|
||||
System.out.println("结果目录: " + resultDir);
|
||||
System.out.println("任务配置文件: " + tasksPath);
|
||||
|
||||
// 这里可以调用控制器的方法来处理整个目录
|
||||
// 在实际使用中,你可以通过HTTP请求调用 /multimodal/process-from-directory 接口
|
||||
|
||||
System.out.println("要处理整个目录,请调用:");
|
||||
System.out.println("POST /multimodal/process-from-directory");
|
||||
System.out.println("请求体:");
|
||||
System.out.println("{");
|
||||
System.out.println(" \"time\": \"" + time + "\",");
|
||||
System.out.println(" \"phoneName\": \"" + phoneName + "\",");
|
||||
System.out.println(" \"projectRoot\": \"" + projectRoot + "\"");
|
||||
System.out.println("}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user