Compare commits

...

3 Commits

Author SHA1 Message Date
6c034ab2a7 Add reasoning agent
Update version to v1.5.0
2025-03-06 11:56:28 +08:00
e44ddd0552 Update message history display 2025-02-28 23:48:10 +08:00
de17af77f2 Add message history context 2025-02-28 23:16:07 +08:00
5 changed files with 323 additions and 104 deletions

View File

@ -1,5 +1,5 @@
plugins {
val kotlinVersion = "1.8.10"
val kotlinVersion = "2.0.20"
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion
@ -7,21 +7,29 @@ plugins {
}
group = "top.jie65535.mirai"
version = "1.3.0"
version = "1.5.0"
mirai {
jvmTarget = JavaVersion.VERSION_11
}
repositories {
mavenCentral()
maven("https://maven.aliyun.com/repository/public")
}
val openaiClientVersion = "3.8.2"
val ktorVersion = "2.3.12"
val openaiClientVersion = "4.0.1"
val ktorVersion = "3.0.3"
val jLatexMathVersion = "1.0.7"
val commonTextVersion = "1.13.0"
val hibernateVersion = "2.9.0"
dependencies {
implementation("com.aallam.openai:openai-client:$openaiClientVersion")
implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
implementation("org.scilab.forge:jlatexmath:$jLatexMathVersion")
implementation("org.apache.commons:commons-text:$commonTextVersion")
// 聊天记录插件
compileOnly("xyz.cssxsh.mirai:mirai-hibernate-plugin:$hibernateVersion")
}

View File

@ -1,9 +1,12 @@
package top.jie65535.mirai
import com.aallam.openai.api.chat.*
import com.aallam.openai.api.core.Role
import com.aallam.openai.api.chat.ChatCompletionRequest
import com.aallam.openai.api.chat.ChatMessage
import com.aallam.openai.api.chat.ChatRole
import com.aallam.openai.api.chat.ToolCall
import com.aallam.openai.api.http.Timeout
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.Chat
import com.aallam.openai.client.OpenAI
import com.aallam.openai.client.OpenAIHost
import io.ktor.util.collections.*
@ -16,10 +19,8 @@ import net.mamoe.mirai.console.permission.PermissionService
import net.mamoe.mirai.console.permission.PermissionService.Companion.hasPermission
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.contact.*
import net.mamoe.mirai.contact.MemberPermission.*
import net.mamoe.mirai.contact.isOperator
import net.mamoe.mirai.contact.nameCardOrNick
import net.mamoe.mirai.event.GlobalEventChannel
import net.mamoe.mirai.event.events.FriendMessageEvent
import net.mamoe.mirai.event.events.GroupMessageEvent
@ -28,13 +29,16 @@ import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.MessageSource.Key.quote
import net.mamoe.mirai.message.sourceIds
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.info
import top.jie65535.mirai.tools.*
import xyz.cssxsh.mirai.hibernate.MiraiHibernateRecorder
import java.time.Instant
import java.time.OffsetDateTime
import java.time.format.TextStyle
import java.util.*
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.regex.Pattern
import kotlin.collections.*
import kotlin.math.max
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
@ -42,12 +46,18 @@ object JChatGPT : KotlinPlugin(
JvmPluginDescription(
id = "top.jie65535.mirai.JChatGPT",
name = "J ChatGPT",
version = "1.3.0",
version = "1.5.0",
) {
author("jie65535")
// dependsOn("xyz.cssxsh.mirai.plugin.mirai-hibernate-plugin", true)
}
) {
private var openAi: OpenAI? = null
private var llm: Chat? = null
/**
* 是否包含历史对话
*/
private var includeHistory: Boolean = false
val chatPermission = PermissionId("JChatGPT", "Chat")
@ -64,6 +74,14 @@ object JChatGPT : KotlinPlugin(
// 注册插件命令
PluginCommands.register()
// 检查消息记录插件是否存在
includeHistory = try {
MiraiHibernateRecorder
true
} catch (_: Throwable) {
false
}
GlobalEventChannel.parentScope(this)
.subscribeAlways<MessageEvent> { event -> onMessage(event) }
@ -72,22 +90,28 @@ object JChatGPT : KotlinPlugin(
fun updateOpenAiToken(token: String) {
val timeout = PluginConfig.timeout.milliseconds
openAi = OpenAI(
llm = OpenAI(
token,
host = OpenAIHost(baseUrl = PluginConfig.openAiApi),
timeout = Timeout(request = timeout, connect = timeout, socket = timeout)
timeout = Timeout(request = timeout, connect = timeout, socket = timeout),
// logging = LoggingConfig(LogLevel.All)
)
reasoningAgent.llm = llm
}
private val timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss")
.withZone(ZoneOffset.systemDefault())
private val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd E HH:mm:ss")
// private val userContext = ConcurrentMap<Long, MutableList<ChatMessage>>()
private const val REPLAY_QUEUE_MAX = 10
private val replyMap = ConcurrentMap<Int, MutableList<ChatMessage>>()
private val replyMap = ConcurrentMap<Int, MutableList<ChatMessage>>(REPLAY_QUEUE_MAX)
private val replyQueue = mutableListOf<Int>()
private val requestMap = ConcurrentSet<Long>()
private suspend fun onMessage(event: MessageEvent) {
// 检查Token是否设置
if (openAi == null) return
if (llm == null) return
// 发送者是否有权限
if (!event.toCommandSender().hasPermission(chatPermission)) {
if (event is GroupMessageEvent) {
@ -147,9 +171,11 @@ object JChatGPT : KotlinPlugin(
prompt.replace(i, i + target.length, replacement())
}
}
replace("{time}") {
"$now ${now.dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA)}"
dateTimeFormatter.format(now)
}
replace("{subject}") {
if (event is GroupMessageEvent) {
"\"${event.subject.name}\" 群聊中"
@ -157,33 +183,103 @@ object JChatGPT : KotlinPlugin(
"私聊中"
}
}
replace("{sender}") {
if (event is GroupMessageEvent) {
event.sender.specialTitle
val permissionName = when (event.sender.permission) {
MEMBER -> "普通群员"
ADMINISTRATOR -> "管理员"
OWNER -> "群主"
}
"\"${event.senderName}\" 身份:$permissionName"
} else {
"\"${event.senderName}\""
// replace("{sender}") {
// if (event is GroupMessageEvent) {
// event.sender.specialTitle
// val permissionName = when (event.sender.permission) {
// MEMBER -> "普通群员"
// ADMINISTRATOR -> "管理员"
// OWNER -> "群主"
// }
// "\"${event.senderName}\" 身份:$permissionName"
// } else {
// "\"${event.senderName}\""
// }
// }
replace("{history}") {
if (!includeHistory) {
return@replace "暂无内容"
}
// 一段时间内的消息
val beforeTimestamp = now.minusMinutes(PluginConfig.historyWindowMin.toLong()).toEpochSecond().toInt()
val nowTimestamp = now.toEpochSecond().toInt()
// 最近这段时间的历史对话
val history = MiraiHibernateRecorder[event.subject, beforeTimestamp, nowTimestamp]
.take(PluginConfig.historyMessageLimit) // 只取最近的部分消息,避免上下文过长
.sortedBy { it.time } // 按时间排序
// 构造历史消息
val historyText = StringBuilder()
if (event is GroupMessageEvent) {
for (record in history) {
if (event.bot.id == record.fromId) {
historyText.append("")
} else {
val recordSender = event.subject[record.fromId]
if (recordSender != null) {
// 群活跃等级
historyText.append(getNameCard(recordSender))
} else {
// 未知群员
historyText.append("未知群员(").append(record.fromId).append(")")
}
}
historyText
.append(" ")
// 发言时间
.append(timeFormatter.format(Instant.ofEpochSecond(record.time.toLong())))
// 消息内容
.append(" 说:").appendLine(record.toMessageChain().joinToString("") {
when (it) {
is At -> {
it.getDisplay(event.subject)
}
is ForwardMessage -> {
it.title + "\n" + it.preview
}
is QuoteReply -> {
">" + it.source.originalMessage.contentToString().replace("\n", "\n> ") + "\n"
}
else -> {
it.contentToString()
}
}
})
}
} else {
// TODO 私聊
}
historyText.toString()
}
return prompt.toString()
}
@OptIn(MiraiExperimentalApi::class)
private suspend fun startChat(event: MessageEvent, context: List<ChatMessage>? = null) {
val history = mutableListOf<ChatMessage>()
if (!context.isNullOrEmpty()) {
history.addAll(context)
} else if (PluginConfig.prompt.isNotEmpty()) {
history.add(ChatMessage(ChatRole.System, getSystemPrompt(event)))
val prompt = getSystemPrompt(event)
if (PluginConfig.logPrompt) {
logger.info("Prompt: $prompt")
}
history.add(ChatMessage(ChatRole.System, prompt))
}
val msg = event.message.plainText()
if (msg.isNotEmpty()) {
history.add(ChatMessage(ChatRole.User, msg))
history.add(ChatMessage(ChatRole.User, if (event is GroupMessageEvent) {
"${getNameCard(event.sender)} 说:$msg"
} else {
msg
}))
}
try {
@ -192,76 +288,89 @@ object JChatGPT : KotlinPlugin(
return
}
var done: Boolean
var retry = 2
var hasTools = true
var done = true
// 至少重试两次
var retry = max(PluginConfig.retryMax, 2)
do {
val reply = chatCompletion(history, hasTools)
history.add(reply)
done = true
try {
val reply = chatCompletion(history, retry > 1)
history.add(reply)
done = true
for (toolCall in reply.toolCalls.orEmpty()) {
require(toolCall is ToolCall.Function) { "Tool call is not a function" }
val functionResponse = toolCall.execute(event)
history.add(
ChatMessage(
role = ChatRole.Tool,
toolCallId = toolCall.id,
name = toolCall.function.name,
content = functionResponse
for (toolCall in reply.toolCalls.orEmpty()) {
require(toolCall is ToolCall.Function) { "Tool call is not a function" }
val functionResponse = toolCall.execute(event)
history.add(
ChatMessage(
role = ChatRole.Tool,
toolCallId = toolCall.id,
name = toolCall.function.name,
content = functionResponse
)
)
)
done = false
hasTools = false
done = false
}
} catch (e: Exception) {
if (retry <= 1) {
throw e
} else {
logger.warning("调用llm时发生异常重试中", e)
event.subject.sendMessage(event.message.quote() + "出错了...正在重试...")
}
}
} while (!done && 0 < --retry)
} while (!done && 0 <-- retry)
val content = history.last().content ?: "..."
val replyMsg = event.subject.sendMessage(
if (content.length < 128) {
if (content.length < PluginConfig.messageMergeThreshold) {
event.message.quote() + toMessage(event.subject, content)
} else {
// 消息内容太长则转为转发消息避免刷屏
event.buildForwardMessage {
for (item in history) {
if (item.content.isNullOrEmpty())
continue
val temp = toMessage(event.subject, item.content!!)
when (item.role) {
Role.User -> event.sender says temp
Role.Assistant -> event.bot says temp
}
}
// 检查并移除超出转发消息上限的消息
var isOverflow = false
var count = 0
for (i in size - 1 downTo 0) {
if (count > 4900) {
isOverflow = true
// 删除早期上下文消息
removeAt(i)
} else {
for (text in this[i].messageChain.filterIsInstance<PlainText>()) {
count += text.content.length
}
}
}
if (count > 5000) {
removeAt(0)
}
if (isOverflow) {
// 如果溢出了,插入一条提示到最开始
add(
0, ForwardMessage.Node(
senderId = event.bot.id,
time = this[0].time - 1,
senderName = event.bot.nameCardOrNick,
message = PlainText("更早的消息已隐藏,避免超出转发消息上限。")
)
)
}
event.bot says toMessage(event.subject, content)
}
// 不再将历史对话记录加入其中
// event.buildForwardMessage {
// for (item in history) {
// if (item.content.isNullOrEmpty())
// continue
// val temp = toMessage(event.subject, item.content!!)
// when (item.role) {
// Role.User -> event.sender says temp
// Role.Assistant -> event.bot says temp
// }
// }
//
// // 检查并移除超出转发消息上限的消息
// var isOverflow = false
// var count = 0
// for (i in size - 1 downTo 0) {
// if (count > 4900) {
// isOverflow = true
// // 删除早期上下文消息
// removeAt(i)
// } else {
// for (text in this[i].messageChain.filterIsInstance<PlainText>()) {
// count += text.content.length
// }
// }
// }
// if (count > 5000) {
// removeAt(0)
// }
// if (isOverflow) {
// // 如果溢出了,插入一条提示到最开始
// add(
// 0, ForwardMessage.Node(
// senderId = event.bot.id,
// time = this[0].time - 1,
// senderName = event.bot.nameCardOrNick,
// message = PlainText("更早的消息已隐藏,避免超出转发消息上限。")
// )
// )
// }
// }
}
)
@ -277,17 +386,20 @@ object JChatGPT : KotlinPlugin(
}
} catch (ex: Throwable) {
logger.warning(ex)
event.subject.sendMessage(event.message.quote() + "发生异常,请重试")
event.subject.sendMessage(event.message.quote() + "很抱歉,发生异常,请稍后重试")
} finally {
requestMap.remove(event.sender.id)
}
// catch (ex: OpenAITimeoutException) {
// event.subject.sendMessage(event.message.quote() + "很抱歉,服务器没响应,请稍后重试")
// }
}
private val laTeXPattern = Pattern.compile(
"\\\\\\((.+?)\\\\\\)|" + // 匹配行内公式 \(...\)
"\\\\\\[(.+?)\\\\\\]|" + // 匹配独立公式 \[...\]
"\\$\\$([^$]+?)\\$\\$|" + // 匹配独立公式 $$...$$
"\\$(.+?)\\$|" + // 匹配行内公式 $...$
"\\$\\s(.+?)\\s\\$|" + // 匹配行内公式 $...$
"```latex\\s*([^`]+?)\\s*```" // 匹配 ```latex ... ```
, Pattern.DOTALL
)
@ -336,6 +448,8 @@ object JChatGPT : KotlinPlugin(
}
}
private val reasoningAgent = ReasoningAgent()
/**
* 工具列表
*/
@ -346,6 +460,9 @@ object JChatGPT : KotlinPlugin(
// 运行代码
RunCode(),
// 推理代理
reasoningAgent,
// 天气服务
WeatherService(),
@ -361,7 +478,7 @@ object JChatGPT : KotlinPlugin(
chatMessages: List<ChatMessage>,
hasTools: Boolean = true
): ChatMessage {
val openAi = this.openAi ?: throw NullPointerException("OpenAI Token 未设置,无法开始")
val llm = this.llm ?: throw NullPointerException("OpenAI Token 未设置,无法开始")
val availableTools = if (hasTools) {
myTools.filter { it.isEnabled }.map { it.tool }
} else null
@ -369,19 +486,38 @@ object JChatGPT : KotlinPlugin(
model = ModelId(PluginConfig.chatModel),
messages = chatMessages,
tools = availableTools,
toolChoice = ToolChoice.Auto
)
logger.info(
"API Requesting..." +
" Model=${PluginConfig.chatModel}" +
" Tools=${availableTools?.joinToString(prefix = "[", postfix = "]")}"
logger.info("API Requesting... Model=${PluginConfig.chatModel}"
// " Tools=${availableTools?.joinToString(prefix = "[", postfix = "]")}"
)
val response = openAi.chatCompletion(request)
val response = llm.chatCompletion(request)
val message = response.choices.first().message
logger.info("Response: $message ${response.usage}")
return message
}
private fun getNameCard(member: Member): String {
val nameCard = StringBuilder()
// 群活跃等级
nameCard.append("【lv").append(member.active.temperature).append(" ")
// 群头衔
if (member.specialTitle.isNotEmpty()) {
nameCard.append(member.specialTitle)
} else {
nameCard.append(
when (member.permission) {
OWNER -> "群主"
ADMINISTRATOR -> "管理员"
MEMBER -> member.temperatureTitle
}
)
}
// 群名片
nameCard.append("").append(member.nameCardOrNick)
// .append(" (").append(recordSender.id).append(")")
return nameCard.toString()
}
private fun MessageChain.plainText() = this.filterIsInstance<PlainText>().joinToString().trim()
private suspend fun ToolCall.Function.execute(event: MessageEvent): String {
@ -401,11 +537,11 @@ object JChatGPT : KotlinPlugin(
logger.error("Failed to call ${function.name}", e)
"工具调用失败,请尝试自行回答用户,或如实告知。"
}
logger.info("Result=$result")
logger.info("Result=\"$result\"")
// 过会撤回加载消息
if (receipt != null) {
launch {
delay(3.seconds);
delay(3.seconds)
try {
receipt.recall()
} catch (e: Throwable) {

View File

@ -12,7 +12,10 @@ object PluginConfig : AutoSavePluginConfig("Config") {
var openAiToken: String by value("")
@ValueDescription("Chat模型")
var chatModel: String by value("gpt-3.5-turbo-1106")
var chatModel: String by value("qwen-max")
@ValueDescription("推理模型")
var reasoningModel: String by value("qwq-plus")
@ValueDescription("Chat默认提示")
var prompt: String by value("")
@ -35,4 +38,18 @@ object PluginConfig : AutoSavePluginConfig("Config") {
@ValueDescription("在线运行代码 glot.io 的 api token在官网注册账号即可获取。")
val glotToken: String by value("")
@ValueDescription("创建Prompt时取最近多少分钟内的消息")
val historyWindowMin: Int by value(10)
@ValueDescription("创建Prompt时取最多几条消息")
val historyMessageLimit: Int by value(20)
@ValueDescription("是否打印Prompt便于调试")
val logPrompt by value(false)
@ValueDescription("达到需要合并转发消息的阈值")
val messageMergeThreshold by value(150)
@ValueDescription("最大重试次数至少2次最后一次请求不会带工具非工具调用相当于正常回复")
val retryMax: Int by value(3)
}

View File

@ -0,0 +1,58 @@
package top.jie65535.mirai.tools
import com.aallam.openai.api.chat.ChatCompletionRequest
import com.aallam.openai.api.chat.ChatMessage
import com.aallam.openai.api.chat.Tool
import com.aallam.openai.api.core.Parameters
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.Chat
import kotlinx.serialization.json.*
import top.jie65535.mirai.PluginConfig
class ReasoningAgent : BaseAgent(
tool = Tool.function(
name = "reasoning",
description = "可通过调用推理模型对问题进行深度思考。",
parameters = Parameters.buildJsonObject {
put("type", "object")
putJsonObject("properties") {
putJsonObject("prompt") {
put("type", "string")
put("description", "用于调用推理模型的提示")
}
}
putJsonArray("required") {
add("question")
}
},
)
) {
var llm: Chat? = null
override val loadingMessage: String
get() = "深度思考中..."
override val isEnabled: Boolean
get() = llm != null
override suspend fun execute(args: JsonObject?): String {
requireNotNull(args)
val llm = llm ?: return "未配置llm无法进行推理。"
val prompt = args.getValue("prompt").jsonPrimitive.content
val answerContent = StringBuilder()
llm.chatCompletions(ChatCompletionRequest(
model = ModelId(PluginConfig.reasoningModel),
messages = listOf(ChatMessage.Companion.User(prompt))
)).collect {
if (it.choices.isNotEmpty()) {
val delta = it.choices[0].delta ?: return@collect
if (!delta.content.isNullOrEmpty()) {
answerContent.append(delta.content)
}
}
}
return answerContent.toString().ifEmpty { "推理出错,结果为空" }
}
}

View File

@ -11,7 +11,7 @@ import top.jie65535.mirai.PluginConfig
class WebSearch : BaseAgent(
tool = Tool.function(
name = "search",
name = "webSearch",
description = "Provides meta-search functionality through SearXNG," +
" aggregating results from multiple search engines.",
parameters = Parameters.buildJsonObject {