zhouc 增加转发接口
This commit is contained in:
parent
9b3db92c12
commit
eaad020f16
@ -41,6 +41,23 @@
|
||||
<artifactId>pdfbox</artifactId>
|
||||
<version>2.0.29</version>
|
||||
</dependency>
|
||||
<!-- 火山 -->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.volcengine</groupId>-->
|
||||
<!-- <artifactId>volcengine-java-sdk-ark-runtime</artifactId>-->
|
||||
<!-- <version>LATEST</version>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.volcengine</groupId>
|
||||
<artifactId>volcengine-java-sdk-ark-runtime</artifactId>
|
||||
<version>LATEST</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>io.swagger.core.v3</groupId>
|
||||
<artifactId>swagger-annotations</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!-- 业务组件 -->
|
||||
<dependency>
|
||||
<groupId>com.wmyun</groupId>
|
||||
@ -103,10 +120,10 @@
|
||||
</dependency>
|
||||
|
||||
<!-- 服务保障相关 TODO 芋艿:暂时去掉 -->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.wmyun</groupId>-->
|
||||
<!-- <artifactId>wmyun-spring-boot-starter-protection</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.wmyun</groupId>-->
|
||||
<!-- <artifactId>wmyun-spring-boot-starter-protection</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<!-- Test 测试相关 -->
|
||||
<dependency>
|
||||
|
@ -0,0 +1,106 @@
|
||||
package com.wmyun.module.system.controller.admin.forward;
|
||||
|
||||
import com.volcengine.ark.runtime.exception.ArkHttpException;
|
||||
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionContentPart;
|
||||
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
|
||||
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
|
||||
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
|
||||
import com.volcengine.ark.runtime.service.ArkService;
|
||||
import com.wmyun.framework.common.pojo.CommonResult;
|
||||
import com.wmyun.module.system.controller.admin.forward.vo.OCRReqVO;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import jodd.util.StringUtil;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static com.wmyun.framework.common.pojo.CommonResult.error;
|
||||
import static com.wmyun.framework.common.pojo.CommonResult.success;
|
||||
|
||||
/**
|
||||
* @author zhouc
|
||||
* @date 2025/3/3 21:01
|
||||
* @className OCRController
|
||||
* @description
|
||||
*/
|
||||
@Tag(name = "管理后台 -OCR识别")
|
||||
@RestController
|
||||
@RequestMapping("/system/ocr")
|
||||
@Validated
|
||||
@Transactional
|
||||
public class OCRController {
|
||||
private static final String API_KEY = "83e8d382-1f6f-47f3-aa8e-1ea1f135ca2b";
|
||||
private static final String DEEPSEEK_OCR_MODEL = "ep-20250224214646-xxb4g";
|
||||
private static final String baseUrl = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";
|
||||
|
||||
@PostMapping(value = "/OCRinterface", consumes = "multipart/form-data")
|
||||
@Operation(summary = "OCR识别")
|
||||
@PreAuthorize("@ss.hasPermission('system:forward:OCRinterface')")
|
||||
public CommonResult<String> OCRinterface(@RequestParam("file") MultipartFile file,
|
||||
@RequestParam("mag") String mag) {
|
||||
try {
|
||||
String imageurl = "";
|
||||
// 校验文件非空
|
||||
if (file.isEmpty()) {
|
||||
throw new IllegalArgumentException("文件为空");
|
||||
}
|
||||
String contentType = file.getContentType();
|
||||
// 读取字节流并编码
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
byte[] bytes = inputStream.readAllBytes();
|
||||
imageurl = Base64.getEncoder().encodeToString(bytes);
|
||||
}
|
||||
imageurl = "data:" + contentType + ";base64," + imageurl;
|
||||
Object getmsg = getmsg(mag, imageurl);
|
||||
return success(getmsg.toString());
|
||||
} catch (Exception exception) {
|
||||
String message = exception.getMessage();
|
||||
return error(411, message);
|
||||
}
|
||||
}
|
||||
|
||||
public Object getmsg(String mag, String imageurl) {
|
||||
AtomicReference<Object> msg = new AtomicReference<>("");
|
||||
try {
|
||||
ArkService service = ArkService.builder().apiKey(API_KEY).baseUrl(baseUrl).build();
|
||||
// ArkService service = new ArkService(ARK_API_KEY);
|
||||
System.out.println("----- image input -----");
|
||||
final List<ChatMessage> messages = new ArrayList<>();
|
||||
final List<ChatCompletionContentPart> multiParts = new ArrayList<>();
|
||||
multiParts.add(ChatCompletionContentPart.builder().type("text").text(mag).build());
|
||||
multiParts.add(ChatCompletionContentPart.builder().type("image_url").imageUrl(
|
||||
new ChatCompletionContentPart.ChatCompletionContentPartImageURL(imageurl)
|
||||
).build());
|
||||
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER)
|
||||
.multiContent(multiParts).build();
|
||||
messages.add(userMessage);
|
||||
|
||||
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
|
||||
.model(DEEPSEEK_OCR_MODEL)
|
||||
.messages(messages)
|
||||
.build();
|
||||
|
||||
service.createChatCompletion(chatCompletionRequest).getChoices().forEach(
|
||||
choice ->
|
||||
msg.set(choice.getMessage().getContent()));
|
||||
|
||||
service.shutdownExecutor();
|
||||
} catch (ArkHttpException e) {
|
||||
System.out.print(e.toString());
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.wmyun.module.system.controller.admin.forward.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* @author zhouc
|
||||
* @date 2025/3/4 11:41
|
||||
* @className OCRReqVO
|
||||
* @description
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Schema(description = "管理后台 - OCR Request VO")
|
||||
public class OCRReqVO {
|
||||
@Schema(description = "文件", example = "https://XXXXXX/login")
|
||||
private MultipartFile file;
|
||||
@Schema(description = "用户消息", example = "今天天气怎么样!")
|
||||
private String mag;
|
||||
}
|
Loading…
Reference in New Issue
Block a user