1
0

feat: 优化多个大模型接口失败轮询

This commit is contained in:
2024-11-04 18:46:00 +08:00
parent 5829b6e145
commit 162f9bb569
3 changed files with 41 additions and 24 deletions

View File

@@ -3,6 +3,8 @@ package com.lanyuanxiaoyao.digtal.market
import com.lanyuanxiaoyao.digtal.market.runner.NewsRunner import com.lanyuanxiaoyao.digtal.market.runner.NewsRunner
import com.lanyuanxiaoyao.digtal.market.runner.PushRunner import com.lanyuanxiaoyao.digtal.market.runner.PushRunner
import com.lanyuanxiaoyao.squirrel.core.common.Management import com.lanyuanxiaoyao.squirrel.core.common.Management
import com.lanyuanxiaoyao.squirrel.core.jvm.BINARY_PATH
import com.lanyuanxiaoyao.squirrel.core.jvm.DRIVER_PATH
import com.lanyuanxiaoyao.squirrel.core.jvm.JvmManagement import com.lanyuanxiaoyao.squirrel.core.jvm.JvmManagement
import com.lanyuanxiaoyao.squirrel.core.jvm.LocalFileDatabase import com.lanyuanxiaoyao.squirrel.core.jvm.LocalFileDatabase
import jakarta.annotation.Resource import jakarta.annotation.Resource
@@ -81,7 +83,13 @@ class Configuration {
management.changeDownloader("basicCacheDownloader") management.changeDownloader("basicCacheDownloader")
} }
if (information.browserDownloaderName.isBlank()) { if (information.browserDownloaderName.isBlank()) {
management.changeDownloader("htmlUnitCacheDownloader") management.changeDownloader(
"chromeCacheDownloader",
mapOf(
BINARY_PATH to driverProperties.binaryPath,
DRIVER_PATH to driverProperties.driverPath,
)
)
} }
} }
management.importSites(sites) management.importSites(sites)

View File

@@ -1,14 +1,12 @@
package com.lanyuanxiaoyao.digtal.market.service package com.lanyuanxiaoyao.digtal.market.service
import cn.hutool.json.JSONUtil import cn.hutool.json.JSONUtil
import com.lanyuanxiaoyao.digtal.market.ArticleRepository
import com.lanyuanxiaoyao.digtal.market.ai.Chat import com.lanyuanxiaoyao.digtal.market.ai.Chat
import com.lanyuanxiaoyao.digtal.market.ai.QianfanChat import com.lanyuanxiaoyao.digtal.market.ai.QianfanChat
import com.lanyuanxiaoyao.digtal.market.ai.ZhipuChat import com.lanyuanxiaoyao.digtal.market.ai.ZhipuChat
import dev.failsafe.Failsafe import dev.failsafe.Failsafe
import dev.failsafe.RetryPolicy import dev.failsafe.RetryPolicy
import dev.failsafe.function.CheckedSupplier import dev.failsafe.function.CheckedSupplier
import jakarta.annotation.Resource
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
import kotlin.time.toJavaDuration import kotlin.time.toJavaDuration
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@@ -23,40 +21,46 @@ class DescriptionService {
.withMaxRetries(2) .withMaxRetries(2)
.build() .build()
// private val chat: Chat = ZhipuChat() private val chats = listOf(
private val chat: Chat = QianfanChat() ZhipuChat(),
QianfanChat(),
)
fun parseDescription(content: String?): Triple<String, String, Int>? { fun parseDescription(content: String?): Triple<String, String, Int>? {
return content?.let { return content?.let {
if (it.isNotBlank()) { if (it.isNotBlank()) {
val description = Failsafe var description: String?
.with(retryPolicy) for (chat in chats) {
.get(CheckedSupplier { try {
chat.ask( description = Failsafe
// language=text .with(retryPolicy)
Chat.Companion.SystemMessage("数据要素是指那些以电子形式存在的、通过计算的方式参与到生产经营活动并发挥重要价值的数据资源。在数字经济中,数据要素的角色可与传统的生产要素(如劳动力、资本和土地)相提并论。数据要素是推动数字经济发展的核心引擎,是赋能行业数字化转型和智能化升级的重要支撑,也是国家基础性战略资源。数据要素市场化的含义是指通过市场机制来配置数据这一新型生产要素,旨在建立一个更加开放、安全和高效的数据流通环境,不断释放数据要素价值。具体来说,数据要素市场化建设就是将尚未完全由市场配置的数据要素转向由市场配置的动态过程,形成以市场为根本的数据资源体系,实现以数据流促进生产、分配、流通、消费各个环节高效贯通。数据要素市场化的目的是提升数据要素的价值创造能力,促进数据要素的有效配置和优化利用,推动数字经济和实体经济的协同发展。"), .get(CheckedSupplier {
// language=text chat.ask(
Chat.Companion.SystemMessage("您作为专职新闻编辑您需要总结归纳输入的文稿强调时间、数据、人物、政策、法律等关键信息。请注意您只能写一段总述不能分点或分段也不能添加任何其他与内容无关的提示性文字。请使用以下模板输出符合JSON格式的结果(不添加任何Markdown代码格式){\"title\":\"(一句话总结)\",\"description\":\"(500字概要)\",\"tags\":\"(文本的关键词标签最多10个使用英文逗号分隔)\",\"score\":\"(文本内容和「数据要素市场化建设」的相关程度0到100相关程度越大数字越高精确到个位数)\"}"), // language=text
Chat.Companion.UserMessage(content) Chat.Companion.SystemMessage("数据要素是指那些以电子形式存在的、通过计算的方式参与到生产经营活动并发挥重要价值的数据资源。在数字经济中,数据要素的角色可与传统的生产要素(如劳动力、资本和土地)相提并论。数据要素是推动数字经济发展的核心引擎,是赋能行业数字化转型和智能化升级的重要支撑,也是国家基础性战略资源。数据要素市场化的含义是指通过市场机制来配置数据这一新型生产要素,旨在建立一个更加开放、安全和高效的数据流通环境,不断释放数据要素价值。具体来说,数据要素市场化建设就是将尚未完全由市场配置的数据要素转向由市场配置的动态过程,形成以市场为根本的数据资源体系,实现以数据流促进生产、分配、流通、消费各个环节高效贯通。数据要素市场化的目的是提升数据要素的价值创造能力,促进数据要素的有效配置和优化利用,推动数字经济和实体经济的协同发展。"),
) // language=text
}) Chat.Companion.SystemMessage("您作为专职新闻编辑您需要总结归纳输入的文稿强调时间、数据、人物、政策、法律等关键信息。请注意您只能写一段总述不能分点或分段也不能添加任何其他与内容无关的提示性文字。请使用以下模板输出符合JSON格式的结果(不添加任何Markdown代码格式){\"title\":\"(一句话总结)\",\"description\":\"(500字概要)\",\"tags\":\"(文本的关键词标签最多10个使用英文逗号分隔)\",\"score\":\"(文本内容和「数据要素市场化建设」的相关程度0到100相关程度越大数字越高精确到个位数)\"}"),
logger.info("description: {}", description) Chat.Companion.UserMessage(content)
try { )
if (!description.isNullOrBlank()) { })
logger.info("description: {}", description)
if (description.isNullOrBlank()) {
continue
}
val root = JSONUtil.parse( val root = JSONUtil.parse(
description description
.replace("```json", "") .replace("```json", "")
.replace("```", "") .replace("```", "")
.replace(Regex("//.+"), "")
) )
val subtitle = root.getByPath("title", String::class.java) val subtitle = root.getByPath("title", String::class.java)
val desc = root.getByPath("description", String::class.java) val desc = root.getByPath("description", String::class.java)
val score = root.getByPath("score", Int::class.java) val score = root.getByPath("score", Int::class.java)
Triple(subtitle, desc, score) return@let Triple(subtitle, desc, score)
} else null } catch (e: Throwable) {
} catch (e: Throwable) { continue
null }
} }
null
} else null } else null
} }
} }

View File

@@ -28,6 +28,11 @@ logging:
org: org:
htmlunit: htmlunit:
IncorrectnessListenerImpl: error IncorrectnessListenerImpl: error
apache:
http:
impl:
execchain:
RetryExec: error
messenger: messenger:
mail: mail:
targets: targets: