Browse Source

core-ai 流式输出和思考模式

deason 1 month ago
parent
commit
a33a6118fa

+ 10 - 9
core-ai/src/main/java/com/qmth/boot/core/ai/client/LlmApiClient.java

@@ -6,10 +6,7 @@ import com.qmth.boot.core.ai.model.llm.*;
 import com.qmth.boot.core.ai.model.llm.endpoint.ChatEndpoint;
 import com.qmth.boot.core.retrofit.annotatioin.RetrofitClient;
 import com.qmth.boot.core.retrofit.utils.SignatureInfo;
-import retrofit2.http.Body;
-import retrofit2.http.Header;
-import retrofit2.http.POST;
-import retrofit2.http.Tag;
+import retrofit2.http.*;
 
 /**
  * 大模型应用服务接口
@@ -20,16 +17,20 @@ public interface LlmApiClient {
     /**
      * 构建大模型chat类型请求信息
      *
-     * @param baseUrl   接口前缀地址,默认取配置文件的值(选填)
-     * @param signature 使用机构AK构造Secret类型签名
-     * @param type      大模型应用类型
-     * @param param     提示词模版变量参数
+     * @param baseUrl        接口前缀地址,默认取配置文件的值(选填)
+     * @param signature      使用机构AK构造Secret类型签名
+     * @param type           大模型应用类型
+     * @param param          提示词模版变量参数
+     * @param stream         是否流式输出
+     * @param enableThinking 是否启用思考模式(若开启思考模式,则默认流式输出)
      * @return
      */
     @POST(AiConstants.LLM_CHAT_ENDPOINT_PATH)
     ChatEndpoint chatEndpoint(@Header(AiConstants.BASE_URL) String baseUrl, @Tag SignatureInfo signature,
                               @Header(AiConstants.LLM_APP_TYPE) LlmAppType type,
-                              @Body Object param);
+                              @Body Object param,
+                              @Query("stream") Boolean stream,
+                              @Query("enableThinking") Boolean enableThinking);
 
     /**
      * 调用大模型chat类型请求(预设提示词模版)

+ 68 - 1
core-ai/src/main/java/com/qmth/boot/core/ai/model/llm/ChatRequest.java

@@ -1,18 +1,53 @@
 package com.qmth.boot.core.ai.model.llm;
 
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
 import java.util.LinkedList;
 import java.util.List;
 
 /**
  * 大模型通用chat请求
  */
+@JsonIgnoreProperties(ignoreUnknown = true)
 public class ChatRequest {
 
     private List<ChatMessage> messages;
 
+    /**
+     * 模型名称
+     */
     private String model;
 
-    private Boolean stream;
+    /**
+     * 是否流式输出
+     */
+    private Boolean stream = false;
+
+    /**
+     * 是否启用思考模式(若开启思考模式,则默认流式输出)
+     */
+    @JsonProperty("enable_thinking")
+    private Boolean enableThinking = false;
+
+    /**
+     * 采样温度 [0,2]
+     * 值越高生成的文本更多样,反之生成的文本越确定
+     */
+    private Float temperature;
+
+    /**
+     * 核采样的概率阈值 [0,1]
+     * 值越高生成的文本越随机,反之生成的文本更确定
+     */
+    @JsonProperty("top_p")
+    private Float topP;
+
+    @JsonProperty("result_format")
+    private String resultFormat;
+
+    // @JsonProperty("response_format")
+    // private String responseFormat;
 
     public ChatRequest() {
         this.messages = new LinkedList<>();
@@ -49,4 +84,36 @@ public class ChatRequest {
         this.stream = stream;
     }
 
+    public Boolean getEnableThinking() {
+        return enableThinking;
+    }
+
+    public void setEnableThinking(Boolean enableThinking) {
+        this.enableThinking = enableThinking;
+    }
+
+    public Float getTemperature() {
+        return temperature;
+    }
+
+    public void setTemperature(Float temperature) {
+        this.temperature = temperature;
+    }
+
+    public Float getTopP() {
+        return topP;
+    }
+
+    public void setTopP(Float topP) {
+        this.topP = topP;
+    }
+
+    public String getResultFormat() {
+        return resultFormat;
+    }
+
+    public void setResultFormat(String resultFormat) {
+        this.resultFormat = resultFormat;
+    }
+
 }