mirror of
https://github.com/jie65535/JChatGPT.git
synced 2025-10-20 17:13:37 +08:00
Separate LaTeX parsing into independent tool for LLM calling
This commit is contained in:
@@ -512,18 +512,12 @@ object JChatGPT : KotlinPlugin(
|
||||
|
||||
private val regexAtQq = Regex("""@(\d{5,12})""")
|
||||
|
||||
private val regexLaTeX = Regex(
|
||||
"""\\\((.+?)\\\)|""" + // 匹配行内公式 \(...\)
|
||||
"""\\\[(.+?)\\]|""" + // 匹配独立公式 \[...\]
|
||||
"""\$(.+?)\$""" // 匹配行内公式 $...$
|
||||
)
|
||||
|
||||
private val regexImage = Regex("""!\[(.*?)]\(([^\s"']+).*?\)""")
|
||||
|
||||
private data class MessageChunk(val range: IntRange, val content: Message)
|
||||
|
||||
/**
|
||||
* 将聊天内容转为聊天消息,如果聊天中包含LaTeX表达式,将会转为图片拼接到消息中。
|
||||
* 将聊天内容转为聊天消息
|
||||
*
|
||||
* @param contact 联系对象
|
||||
* @param content 文本内容
|
||||
@@ -553,24 +547,6 @@ object JChatGPT : KotlinPlugin(
|
||||
Image(url)))
|
||||
}
|
||||
|
||||
// LeTeX渲染
|
||||
regexLaTeX.findAll(content).forEach {
|
||||
it.groups.forEach { group ->
|
||||
if (group == null || group.value.isEmpty()) return@forEach
|
||||
try {
|
||||
// 将所有匹配的LaTeX公式转为图片拼接到消息中
|
||||
val formula = group.value
|
||||
val imageByteArray = LaTeXConverter.convertToImage(formula, "png")
|
||||
val resource = imageByteArray.toExternalResource("png")
|
||||
val image = contact.uploadImage(resource)
|
||||
|
||||
t.add(MessageChunk(group.range, image))
|
||||
} catch (ex: Throwable) {
|
||||
logger.warning("处理LaTeX表达式时异常", ex)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 构造消息链
|
||||
buildMessageChain {
|
||||
var index = 0
|
||||
@@ -602,6 +578,9 @@ object JChatGPT : KotlinPlugin(
|
||||
// 发送语音消息
|
||||
SendVoiceMessage(),
|
||||
|
||||
// 发送LaTeX表达式
|
||||
SendLaTeXExpression(),
|
||||
|
||||
// 结束循环
|
||||
StopLoopAgent(),
|
||||
|
||||
|
46
src/main/kotlin/tools/SendLaTeXExpression.kt
Normal file
46
src/main/kotlin/tools/SendLaTeXExpression.kt
Normal file
@@ -0,0 +1,46 @@
|
||||
package top.jie65535.mirai.tools
|
||||
|
||||
import com.aallam.openai.api.chat.Tool
|
||||
import com.aallam.openai.api.core.Parameters
|
||||
import kotlinx.serialization.json.*
|
||||
import net.mamoe.mirai.event.events.MessageEvent
|
||||
import top.jie65535.mirai.LaTeXConverter
|
||||
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
|
||||
|
||||
class SendLaTeXExpression : BaseAgent(
|
||||
tool = Tool.function(
|
||||
name = "sendLaTeXExpression",
|
||||
description = "发送LaTeX数学表达式,将其渲染为图片并发送",
|
||||
parameters = Parameters.buildJsonObject {
|
||||
put("type", "object")
|
||||
putJsonObject("properties") {
|
||||
putJsonObject("expression") {
|
||||
put("type", "string")
|
||||
put("description", "LaTeX数学表达式")
|
||||
}
|
||||
}
|
||||
putJsonArray("required") {
|
||||
add("expression")
|
||||
}
|
||||
}
|
||||
)
|
||||
) {
|
||||
override suspend fun execute(args: JsonObject?, event: MessageEvent): String {
|
||||
requireNotNull(args)
|
||||
val expression = args.getValue("expression").jsonPrimitive.content
|
||||
|
||||
try {
|
||||
// 将LaTeX表达式转换为图片
|
||||
val imageByteArray = LaTeXConverter.convertToImage(expression, "png")
|
||||
val resource = imageByteArray.toExternalResource("png")
|
||||
val image = event.subject.uploadImage(resource)
|
||||
|
||||
// 发送图片消息
|
||||
event.subject.sendMessage(image)
|
||||
|
||||
return "成功发送LaTeX表达式"
|
||||
} catch (ex: Throwable) {
|
||||
return "处理LaTeX表达式时发生异常: ${ex.message}"
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user