ソースを参照

fix(ai): 优化AI参数层级合并并调低默认温度至0.7(兼容智谱等模型的0~1限制),同时章节摘要支持Markdown渲染

lyc 4 ヶ月 前
親
コミット
b8137ec991

+ 9 - 2
app/src/main/java/io/legado/app/data/repository/AiProfileRepository.kt

@@ -254,7 +254,7 @@ class AiProfileRepository(
                 name = "Default Chapter Summary",
                 modelProfileId = modelProfileId,
                 promptTemplate = AiPromptTemplate.DEFAULT_CHAPTER_SUMMARY,
-                paramsJson = GSON.toJson(params.copy(maxOutputTokens = 1200)),
+                paramsJson = GSON.toJson(params),
                 isDefault = true,
                 createdAt = existingSummaryPreset?.createdAt ?: now,
                 updatedAt = now
@@ -279,13 +279,20 @@ class AiProfileRepository(
     private suspend fun AiTaskPreset.toConfig(): AiTaskPresetConfig? {
         val model = aiProfileDao.getModel(modelProfileId) ?: return null
         val provider = aiProfileDao.getProvider(model.providerId) ?: return null
+        val presetParams = parseParams(paramsJson)
+        val modelParams = parseParams(model.defaultParamsJson)
+        val mergedParams = presetParams.mergeWithFallback(
+            modelParams = modelParams,
+            modelMaxOutputTokens = model.maxOutputTokens,
+            taskType = taskType
+        )
         return AiTaskPresetConfig(
             id = id,
             taskType = taskType,
             name = name,
             model = model.toConfig(provider),
             promptTemplate = promptTemplate,
-            params = parseParams(paramsJson),
+            params = mergedParams,
             runtimeOptions = parseRuntimeOptions(chunkPolicyJson)
         )
     }

+ 47 - 1
app/src/main/java/io/legado/app/domain/model/AiModels.kt

@@ -241,7 +241,53 @@ data class AiGenerationParams(
     val maxOutputTokens: Int? = null,
     val topP: Float? = null,
     val reasoningLevel: AiReasoningLevel = AiReasoningLevel.AUTO
-)
+) {
+    fun mergeWithFallback(
+        modelParams: AiGenerationParams,
+        modelMaxOutputTokens: Int = 0,
+        taskType: String? = null
+    ): AiGenerationParams {
+        val mergedTemperature = this.temperature
+            ?: modelParams.temperature
+            ?: TranslationConstants.DEFAULT_TEMPERATURE
+
+        val effectiveModelMaxTokens = when {
+            modelMaxOutputTokens > 0 -> modelMaxOutputTokens
+            modelParams.maxOutputTokens != null && modelParams.maxOutputTokens > 0 -> modelParams.maxOutputTokens
+            else -> null
+        }
+        val effectivePresetMaxTokens = if (this.maxOutputTokens != null && this.maxOutputTokens > 0) {
+            this.maxOutputTokens
+        } else {
+            null
+        }
+
+        val mergedMaxTokens = effectivePresetMaxTokens
+            ?: effectiveModelMaxTokens
+            ?: when (taskType) {
+                AiTaskType.SUMMARIZE_CHAPTER,
+                AiTaskType.SUMMARIZE_BOOK,
+                AiTaskType.CLEAN_SELECTION -> 1200
+                else -> null
+            }
+
+        val mergedTopP = this.topP ?: modelParams.topP
+        val mergedReasoningLevel = if (this.reasoningLevel != AiReasoningLevel.AUTO) {
+            this.reasoningLevel
+        } else if (modelParams.reasoningLevel != AiReasoningLevel.AUTO) {
+            modelParams.reasoningLevel
+        } else {
+            AiReasoningLevel.AUTO
+        }
+
+        return AiGenerationParams(
+            temperature = mergedTemperature,
+            maxOutputTokens = mergedMaxTokens,
+            topP = mergedTopP,
+            reasoningLevel = mergedReasoningLevel
+        )
+    }
+}
 
 @Keep
 data class AiTaskRuntimeOptions(

+ 1 - 1
app/src/main/java/io/legado/app/domain/model/TranslationConstants.kt

@@ -7,7 +7,7 @@ object TranslationConstants {
     const val PROVIDER_GOOGLE = "google"
     const val MIN_TEMPERATURE = 0f
     const val MAX_TEMPERATURE = 2f
-    const val DEFAULT_TEMPERATURE = 1.3f
+    const val DEFAULT_TEMPERATURE = 0.7f
 
     val providerDisplayNames = listOf("Google Translate", "应用 AI 接口")
     val providerValues = listOf(PROVIDER_GOOGLE, PROVIDER_APP_AI)

+ 9 - 4
app/src/main/java/io/legado/app/ui/book/read/sheet/ChapterSummarySheet.kt

@@ -23,6 +23,7 @@ import io.legado.app.ui.widget.components.button.series.SmallTonalButton
 import io.legado.app.ui.widget.components.modalBottomSheet.AppModalBottomSheet
 import io.legado.app.ui.widget.components.progressIndicator.AppCircularProgressIndicator
 import io.legado.app.ui.widget.components.text.AppText
+import io.legado.app.ui.widget.components.text.MarkdownBlock
 
 @Composable
 fun ChapterSummarySheet(
@@ -78,15 +79,19 @@ fun ChapterSummarySheet(
 
             else -> {
                 SelectionContainer {
-                    AppText(
-                        text = state.summary,
+                    Column(
                         modifier = Modifier
                             .fillMaxWidth()
                             .heightIn(max = 520.dp)
                             .verticalScroll(rememberScrollState())
                             .padding(bottom = 24.dp),
-                        style = LegadoTheme.typography.bodyLarge,
-                    )
+                    ) {
+                        MarkdownBlock(
+                            content = state.summary,
+                            modifier = Modifier.fillMaxWidth(),
+                            style = LegadoTheme.typography.bodyLarge,
+                        )
+                    }
                 }
             }
         }