|
@@ -0,0 +1,130 @@
|
|
|
+package com.qmth.boot.ai.api.demo.test;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.databind.json.JsonMapper;
|
|
|
+import com.qmth.boot.core.ai.client.LlmApiClient;
|
|
|
+import com.qmth.boot.core.ai.client.OcrApiClient;
|
|
|
+import com.qmth.boot.core.ai.model.llm.*;
|
|
|
+import com.qmth.boot.core.ai.model.ocr.OcrType;
|
|
|
+import com.qmth.boot.core.ai.service.AiService;
|
|
|
+import com.qmth.boot.core.retrofit.utils.SignatureInfo;
|
|
|
+import com.qmth.boot.core.retrofit.utils.UploadFile;
|
|
|
+import okhttp3.MultipartBody;
|
|
|
+import org.assertj.core.util.Lists;
|
|
|
+import org.junit.Before;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+@SpringBootTest
|
|
|
+public class AiApiDemoTest {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private AiService aiService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private LlmApiClient llmApiClient;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private OcrApiClient ocrApiClient;
|
|
|
+
|
|
|
+ private SignatureInfo secret;
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void init() {
|
|
|
+ String accessKey = "41a9c68b71c9486db62de90980bd7e9a";
|
|
|
+ String accessSecret = "yhe22bNXMRljatkvJA4f56by9dIAJBjv";
|
|
|
+ secret = SignatureInfo.secret(accessKey, accessSecret);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testAll() throws Exception {
|
|
|
+ // getLlmBalance();
|
|
|
+ // autoGenerateQuestion();
|
|
|
+ // autoScore();
|
|
|
+ // chat();
|
|
|
+ // chatTemplate();
|
|
|
+ // getPromptTemplate();
|
|
|
+ // testOcr();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getLlmBalance() throws Exception {
|
|
|
+ LlmAppBalance result = aiService.getLlmBalance(LlmAppType.AUTO_SCORE, secret);
|
|
|
+ // LlmAppBalance result = llmApiClient.getBalance(secret, LlmAppType.AUTO_SCORE);
|
|
|
+ System.out.println(new JsonMapper().writeValueAsString(result));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void autoGenerateQuestion() throws Exception {
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>() {{
|
|
|
+ put("courseName", "语文");
|
|
|
+ put("questionTypeName", "单选题");
|
|
|
+ put("questionType", "SINGLE_ANSWER_QUESTION");
|
|
|
+ put("knowledgePoint", "诗词");
|
|
|
+ put("knowledgeNotes", "作者");
|
|
|
+ }};
|
|
|
+
|
|
|
+ List<String> result = aiService.autoGenerateQuestion(param, secret);
|
|
|
+ System.out.println(new JsonMapper().writeValueAsString(result));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void autoScore() throws Exception {
|
|
|
+ AutoScoreRequest request = new AutoScoreRequest();
|
|
|
+ request.setSubjectName("数学");
|
|
|
+ request.setQuestionBody("1加1等于几?");
|
|
|
+ StandardAnswer standardAnswer = new StandardAnswer();
|
|
|
+ standardAnswer.setContent("2");
|
|
|
+ standardAnswer.setScore(10.0);
|
|
|
+ request.setStandardAnswer(Lists.newArrayList(standardAnswer));
|
|
|
+ request.setStudentAnswer("2.0");
|
|
|
+ request.setTotalScore(10.0);
|
|
|
+ request.setIntervalScore(1);
|
|
|
+
|
|
|
+ AutoScoreResult result = aiService.autoScore(request, secret);
|
|
|
+ System.out.println(new JsonMapper().writeValueAsString(result));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void chat() throws Exception {
|
|
|
+ ChatRequest request = new ChatRequest();
|
|
|
+ request.addMessage(ChatRole.user, "请输出1加1等于几?");
|
|
|
+
|
|
|
+ ChatResult result = llmApiClient.chat(secret, LlmAppType.AUTO_SCORE, request);
|
|
|
+ List<String> values = result.getChoices().stream().filter(choice -> choice.getMessage().getRole() == ChatRole.assistant)
|
|
|
+ .map(choice -> choice.getMessage().getContent()).collect(Collectors.toList());
|
|
|
+ System.out.println(new JsonMapper().writeValueAsString(values));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void chatTemplate() throws Exception {
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>() {{
|
|
|
+ put("courseName", "语文");
|
|
|
+ put("questionTypeName", "单选题");
|
|
|
+ put("questionType", "SINGLE_ANSWER_QUESTION");
|
|
|
+ put("knowledgePoint", "诗词");
|
|
|
+ put("knowledgeNotes", "作者");
|
|
|
+ }};
|
|
|
+
|
|
|
+ ChatResult result = llmApiClient.chatTemplate(secret, LlmAppType.AUTO_GENERATE_QUESTION, param);
|
|
|
+ System.out.println(new JsonMapper().writeValueAsString(result));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getPromptTemplate() throws Exception {
|
|
|
+ PromptTemplate result = llmApiClient.getPromptTemplate(secret, LlmAppType.AUTO_SCORE);
|
|
|
+ System.out.println(new JsonMapper().writeValueAsString(result));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void testOcr() throws Exception {
|
|
|
+ File file = new File("D:\\home\\test.png");
|
|
|
+ MultipartBody.Part part = UploadFile.build("image", "", file);
|
|
|
+
|
|
|
+ String result = ocrApiClient.forImage(secret, OcrType.HANDWRITING, part);
|
|
|
+ System.out.println(result);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|