diff --git a/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/controller/AiCompareController.java b/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/controller/AiCompareController.java new file mode 100644 index 0000000..e8844cf --- /dev/null +++ b/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/controller/AiCompareController.java @@ -0,0 +1,26 @@ +package com.volvo.ai.analytic.center.controller; + +import com.volvo.ai.analytic.center.service.impl.DeepSeekServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.configurationprocessor.json.JSONException; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; + +@RestController +@RequestMapping("/api/v1") +public class AiCompareController { + + @Autowired + private DeepSeekServiceImpl deepSeekService; + + @GetMapping("/chat1") + public String chat1( + @RequestParam(value = "source") String source,@RequestParam(value = "target") String tag + ) throws IOException, JSONException { + return deepSeekService.chat(source, tag); + } +} diff --git a/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/service/impl/DeepSeekServiceImpl.java b/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/service/impl/DeepSeekServiceImpl.java new file mode 100644 index 0000000..5325c95 --- /dev/null +++ b/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/service/impl/DeepSeekServiceImpl.java @@ -0,0 +1,65 @@ +package com.volvo.ai.analytic.center.service.impl; + +import lombok.extern.slf4j.Slf4j; +import okhttp3.*; +import org.springframework.boot.configurationprocessor.json.JSONException; +import org.springframework.boot.configurationprocessor.json.JSONObject; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.stereotype.Service; + +import java.io.IOException; +@Slf4j +@Service +public class DeepSeekServiceImpl { + + private final StringHttpMessageConverter stringHttpMessageConverter; + + public DeepSeekServiceImpl(StringHttpMessageConverter stringHttpMessageConverter) { + this.stringHttpMessageConverter = stringHttpMessageConverter; + } + + public String chat(String source, String tag) throws IOException, JSONException { + + OkHttpClient client = new OkHttpClient().newBuilder() + .build(); + MediaType mediaType = MediaType.parse("application/json"); + +// String last = "请直接给到我智能体输出和人工标注样本数据最终的准确率和准召率分别是多少,不需要过程,只要百分比,准确率和准召率和标准定义保持一致。"; +// String intput = "人工标注样本:"+source+",智能体输出:"+tag+last; + String prompt = "人工标注样本:" + source + ";智能体输出:" + tag + "。" + + "请根据以下公式计算准确率和准召率:" + + "准确率 = (正确输出的答案数) / (所有输出的答案数)" + + "准召率 = (正确输出的答案数) / (所有正确答案数)" +// + "请返回准确率和准召率的计算结果。"; + + "请返回准确率和准召率为多少,不需要过程,只要百分比"; + // 动态构建请求体 + String requestBody = String.format( + "{\"model\": \"deepseek-chat\", \"messages\": [{\"role\": \"user\", \"content\": \"%s\"}], \"stream\": false}", + prompt + ); + + +// RequestBody body = RequestBody.create(mediaType, "{\n \"messages\": [\n {\n \"content\": \"You are a helpful assistant\",\n \"role\": \"system\"\n },\n {\n \"content\": \"Hi\",\n \"role\": \"user\"\n }\n ],\n \"model\": \"deepseek-chat\",\n \"frequency_penalty\": 0,\n \"max_tokens\": 2048,\n \"presence_penalty\": 0,\n \"response_format\": {\n \"type\": \"text\"\n },\n \"stop\": null,\n \"stream\": false,\n \"stream_options\": null,\n \"temperature\": 1,\n \"top_p\": 1,\n \"tools\": null,\n \"tool_choice\": \"none\",\n \"logprobs\": false,\n \"top_logprobs\": null\n}"); + System.out.println("模型入参:" + requestBody); + RequestBody body = RequestBody.create(mediaType, requestBody); + Request request = new Request.Builder() + .url("https://api.deepseek.com/chat/completions") + .method("POST", body) + .addHeader("Content-Type", "application/json") + .addHeader("Accept", "application/json") + .addHeader("Authorization", "Bearer sk-307a0cb753024b629e94bd87210f757f") + .build(); + Response response = client.newCall(request).execute(); + + String responseBodyString = response.body().string(); + System.out.println("模型出参:" + responseBodyString); + + JSONObject jsonObject = new JSONObject(responseBodyString); + String split = jsonObject.getJSONArray("choices") + .getJSONObject(0) + .getJSONObject("message") + .getString("content"); + System.out.println("模型出参:" + split); + return split; + } +}