|
@@ -1,182 +0,0 @@
|
|
|
-package com.fdkankan.manage.common;
|
|
|
-
|
|
|
-import cn.hutool.core.img.ImgUtil;
|
|
|
-import cn.hutool.core.io.FileUtil;
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
-import com.theokanning.openai.completion.chat.*;
|
|
|
-import com.theokanning.openai.service.OpenAiService;
|
|
|
-
|
|
|
-import java.awt.*;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.time.Duration;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author Xiewj
|
|
|
- * @date 2024/10/11
|
|
|
- */
|
|
|
-public class openai {
|
|
|
- public static void main(String[] args) throws IOException {
|
|
|
- gptVision("D:\\abc\\3dcb2408019b82d1b02c05e847504abd.png","http://192.168.9.61:30000/v1/");
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将给定路径的图像转换为 Base64 编码字符串
|
|
|
- *
|
|
|
- * @param imagePath 图像文件的路径
|
|
|
- * @return Base64 编码的字符串
|
|
|
- * @throws IOException 如果读取文件时发生错误
|
|
|
- */
|
|
|
- public static String convertImageToBase64(String imagePath) throws IOException {
|
|
|
- File imageFile = new File(imagePath);
|
|
|
- byte[] fileContent = new byte[(int) imageFile.length()];
|
|
|
-
|
|
|
- try (FileInputStream fileInputStream = new FileInputStream(imageFile)) {
|
|
|
- fileInputStream.read(fileContent);
|
|
|
- }
|
|
|
-
|
|
|
- return java.util.Base64.getEncoder().encodeToString(fileContent);
|
|
|
- }
|
|
|
-
|
|
|
- public static String getSwitchData(String filename, String base64) {
|
|
|
-
|
|
|
- String str = filename.substring(filename.lastIndexOf(".") + 1);
|
|
|
- //png
|
|
|
- String pngPrefix = "data:image/png;base64,";
|
|
|
- //jpg
|
|
|
- String jpegPrefix = "data:image/jpeg;base64,";
|
|
|
- //docx
|
|
|
- String docxPrefix = "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document ;base64,";
|
|
|
- //doc
|
|
|
- String docPrefix = "data:application/msword;base64,";
|
|
|
- //pdf
|
|
|
- String pdfPrefix = "data:application/pdf;base64,";
|
|
|
-
|
|
|
- String filebase64 = null;
|
|
|
-
|
|
|
- if (StrUtil.isNotBlank(str)) {
|
|
|
-
|
|
|
- switch (str) {
|
|
|
- case "png":
|
|
|
- filebase64 = pngPrefix.concat(base64);
|
|
|
- break;
|
|
|
- case "jpg":
|
|
|
- filebase64 = jpegPrefix.concat(base64);
|
|
|
- break;
|
|
|
- case "docx":
|
|
|
- filebase64 = docxPrefix.concat(base64);
|
|
|
- break;
|
|
|
- case "doc":
|
|
|
- filebase64 = docPrefix.concat(base64);
|
|
|
- break;
|
|
|
- case "pdf":
|
|
|
- filebase64 = pdfPrefix.concat(base64);
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- return filebase64;
|
|
|
- }
|
|
|
- public static String getSwitchType(String filename) {
|
|
|
- String str = filename.substring(filename.lastIndexOf(".") + 1);
|
|
|
-
|
|
|
- String filebase64 = null;
|
|
|
-
|
|
|
- if (StrUtil.isNotBlank(str)) {
|
|
|
-
|
|
|
- switch (str) {
|
|
|
- case "png":
|
|
|
- filebase64 = ImgUtil.IMAGE_TYPE_PNG;
|
|
|
- break;
|
|
|
- case "jpg":
|
|
|
- filebase64 = ImgUtil.IMAGE_TYPE_JPG;
|
|
|
- break;
|
|
|
- case "jpeg":
|
|
|
- filebase64 = ImgUtil.IMAGE_TYPE_JPEG;
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- return filebase64;
|
|
|
- }
|
|
|
-
|
|
|
- static void gptVision(String imgPath,String httpUrl) throws IOException {
|
|
|
- OpenAiService openAiService1 = new OpenAiService("", Duration.ofSeconds(120), httpUrl);
|
|
|
- final List<ChatMessage> messages = new ArrayList<>();
|
|
|
-// final ChatMessage systemMessage = new SystemMessage("You are a helpful assistant.");
|
|
|
- //Here, the imageMessage is intended for image recognition
|
|
|
- Image image = ImgUtil.read(imgPath);
|
|
|
- Image scale = ImgUtil.scale(image, 2560, 2560);
|
|
|
- //IMAGE_TYPE_PNG需要根据图片类型自行修改
|
|
|
- String name = FileUtil.getName(imgPath);
|
|
|
- String base64 = ImgUtil.toBase64(scale,getSwitchType(name));
|
|
|
- base64 = getSwitchData(FileUtil.getName(name), base64);
|
|
|
- String text = "任务指令:根据提供的户型图,识别并描述其结构及房间详情。请按照上北、下南、左西、右东的方向进行定位。\n" +
|
|
|
- "\n" +
|
|
|
- "输出格式要求:\n" +
|
|
|
- "不能使用Markdown(MD)格式。\n" +
|
|
|
- "结构清晰,易于理解。\n" +
|
|
|
- "请遵循以下具体步骤:\n" +
|
|
|
- "\n" +
|
|
|
- "整体描述:\n" +
|
|
|
- "1. 概述:首先给出整个户型的结构概述,简要描述户型。对于无法明确识别房间名的房间,可以采用代码形式表示。\n" +
|
|
|
- "\n" +
|
|
|
- "详细描述:\n" +
|
|
|
- "1. 单个房间描述:接下来,针对每个房间单独一段进行描述。每段开头以房间名称开始,接着说明该房间位于图片的相对位置,以及房间内可见的具体家具数量和类型。注意,仅需描述实际识别到的家具,不要使用诸如“等”这样的词汇来泛指其他可能存在的物品。\n" +
|
|
|
- "\n" +
|
|
|
- "示例模板:\n" +
|
|
|
- "整体描述:“这是一个${房间数}室${客厅数}厅${卫生间数}卫的户型。”\n" +
|
|
|
- "房间描述:“${房间名称}:位于户型图${坐标方位}${房间名}通过${连接方式}与${房间名}相连,放置了${家具数量}个${家具类型}家具...”\n" +
|
|
|
- "\n" +
|
|
|
- "注意事项:\n" +
|
|
|
- "确保输出的整体描述数据和房间数量对应上,室排除厨房数量。\n" +
|
|
|
- "确保所有信息基于提供的户型图准确无误地提取。\n" +
|
|
|
- "描述时保持语言简洁明了,直接传达关键信息。\n" +
|
|
|
- "\n" +
|
|
|
- "请确保您的描述符合上述要求,并尽可能提供详尽的信息。";
|
|
|
-// System.out.println(text);
|
|
|
- final ChatMessage imageMessage = UserMessage.buildImageMessage(text,base64);
|
|
|
-// final ChatMessage imageMessage = UserMessage.buildImageMessage(text,"https://laser-oss.4dkankan.com/textX/c67c81972c396b4f191156f9ec983226.png");
|
|
|
-
|
|
|
-
|
|
|
-// messages.add(systemMessage);
|
|
|
- messages.add(imageMessage);
|
|
|
- ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
|
|
|
- .model("Qwen2-VL-7B")
|
|
|
- //.model("qwen2-vl-instruct")
|
|
|
- .messages(messages)
|
|
|
- .temperature(0.06)
|
|
|
- .topP(0.75)
|
|
|
- .maxTokens(1024)
|
|
|
- .n(1)
|
|
|
- .build();
|
|
|
- ChatCompletionChoice choice = openAiService1.createChatCompletion(chatCompletionRequest).getChoices().get(0);
|
|
|
- System.out.println(choice.getMessage().getContent());
|
|
|
- }
|
|
|
-
|
|
|
- static void simpleChat() {
|
|
|
- //从环境变量OPENAI_api_key获取api-key
|
|
|
- OpenAiService service = new OpenAiService(Duration.ofSeconds(30));
|
|
|
- List<ChatMessage> messages = new ArrayList<>();
|
|
|
- ChatMessage systemMessage = new SystemMessage("你是一只可爱的猫,会这样说话。");
|
|
|
- messages.add(systemMessage);
|
|
|
- ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
|
|
|
- .model("gpt-4o-mini")
|
|
|
- .messages(messages)
|
|
|
- .n(1)
|
|
|
- .maxTokens(50)
|
|
|
- .build();
|
|
|
- ChatCompletionResult chatCompletion = service.createChatCompletion(chatCompletionRequest);
|
|
|
- System.out.println(chatCompletion.getChoices().get(0).getMessage().getContent());
|
|
|
- }
|
|
|
-}
|