Prechádzať zdrojové kódy

Merge branch 'wyp/0303-kimiJSON' of Server/long-article-recommend into master

wangyunpeng 4 mesiacov pred
rodič
commit
16d60d8e14

+ 6 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/remote/KimiApiService.java

@@ -33,7 +33,7 @@ public class KimiApiService {
     }
 
 
-    public KimiResult requestOfficialApi(String prompt, String model, Double temperature) {
+    public KimiResult requestOfficialApi(String prompt, String model, Double temperature, Boolean isJSON) {
         KimiResult result = new KimiResult();
         result.setSuccess(false);
         if (TextUtils.isBlank(prompt) || TextUtils.isBlank(prompt.trim())) {
@@ -54,6 +54,11 @@ public class KimiApiService {
                     .put("temperature", Optional.ofNullable(temperature).orElse(0.3))
                     .put("messages", jsonArray)
                     .build();
+            if (isJSON) {
+                JSONObject formatJSON = new JSONObject();
+                formatJSON.put("type", "json_object");
+                bodyParam.put("response_format", formatJSON);
+            }
 
             MediaType mediaType = MediaType.parse("application/json");
             RequestBody body = RequestBody.create(mediaType, JSONObject.toJSONString(bodyParam));

+ 2 - 2
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recommend/ArticleCategoryService.java

@@ -102,7 +102,7 @@ public class ArticleCategoryService {
         for (List<ArticleCategory> partition : partitionList) {
             List<String> partitionTitles = partition.stream().map(ArticleCategory::getTitle).distinct().collect(Collectors.toList());
             String prompt = buildKimiPrompt(partitionTitles);
-            KimiResult kimiResult = kimiApiService.requestOfficialApi(prompt, null, null);
+            KimiResult kimiResult = kimiApiService.requestOfficialApi(prompt, null, null, true);
             long now = System.currentTimeMillis();
             JSONObject obj = null;
             if (kimiResult.isSuccess()) {
@@ -292,7 +292,7 @@ public class ArticleCategoryService {
         for (ArticleCategory articleCategory : dealList) {
             List<String> partitionTitles = Collections.singletonList(articleCategory.getTitle());
             String prompt = buildKimiPrompt(partitionTitles);
-            KimiResult kimiResult = kimiApiService.requestOfficialApi(prompt, null, null);
+            KimiResult kimiResult = kimiApiService.requestOfficialApi(prompt, null, null, true);
             long now = System.currentTimeMillis();
             JSONObject obj = null;
             if (kimiResult.isSuccess()) {

+ 1 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recommend/score/strategy/SimilarityStrategy.java

@@ -67,7 +67,7 @@ public class SimilarityStrategy implements ScoreStrategy {
                 if (!StringUtils.hasText(type)) {
                     String prompt = kimiSimilarityTypePrompt.replace("accountName", param.getAccountName());
                     // 调用kimi判断账号类型
-                    KimiResult kimiResult = kimiApiService.requestOfficialApi(prompt, null, null);
+                    KimiResult kimiResult = kimiApiService.requestOfficialApi(prompt, null, null, false);
                     if (kimiResult.isSuccess()) {
                         try {
                             type = kimiResult.getResponse().getChoices().get(0).getMessage().getContent();