mirror of
https://github.com/jie65535/JChatGPT.git
synced 2025-12-08 18:31:36 +08:00
Compare commits
3 Commits
0f482407d4
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 6705859604 | |||
| 621a045a62 | |||
| 634f0c1026 |
@@ -33,6 +33,7 @@ import net.mamoe.mirai.utils.info
|
||||
import top.jie65535.mirai.tools.*
|
||||
import xyz.cssxsh.mirai.hibernate.MiraiHibernateRecorder
|
||||
import xyz.cssxsh.mirai.hibernate.entry.MessageRecord
|
||||
import java.io.File
|
||||
import java.time.Instant
|
||||
import java.time.OffsetDateTime
|
||||
import java.time.ZoneOffset
|
||||
@@ -165,6 +166,8 @@ object JChatGPT : KotlinPlugin(
|
||||
startChat(event)
|
||||
}
|
||||
|
||||
private var memePrompt: String? = null
|
||||
|
||||
private fun getSystemPrompt(event: MessageEvent): String {
|
||||
val now = OffsetDateTime.now()
|
||||
val prompt = StringBuilder(LargeLanguageModels.systemPrompt)
|
||||
@@ -194,6 +197,46 @@ object JChatGPT : KotlinPlugin(
|
||||
} else memoryText
|
||||
}
|
||||
|
||||
replace("{meme}") {
|
||||
memePrompt?.let { return@replace it }
|
||||
|
||||
if (PluginConfig.memeDir.isEmpty()) {
|
||||
""
|
||||
} else {
|
||||
buildString {
|
||||
val dir = File(PluginConfig.memeDir)
|
||||
if (dir.exists() && dir.isDirectory) {
|
||||
append("memes文件夹地址为:")
|
||||
append(PluginConfig.memeDir)
|
||||
appendLine()
|
||||
|
||||
val memes = dir.list()
|
||||
|
||||
if (memes.isEmpty()) {
|
||||
append("暂无表情包~")
|
||||
} else {
|
||||
for (name in memes) {
|
||||
append("- ")
|
||||
append(name)
|
||||
appendLine()
|
||||
}
|
||||
appendLine()
|
||||
append("表情包示例:![")
|
||||
append(memes[0])
|
||||
append("](")
|
||||
append(File(dir, memes[0]).absoluteFile)
|
||||
append(")")
|
||||
appendLine()
|
||||
}
|
||||
} else {
|
||||
append("配置的meme路径不存在!")
|
||||
}
|
||||
}.also {
|
||||
memePrompt = it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return prompt.toString()
|
||||
}
|
||||
|
||||
@@ -583,7 +626,7 @@ object JChatGPT : KotlinPlugin(
|
||||
* @param content 文本内容
|
||||
* @return 构造的消息
|
||||
*/
|
||||
suspend fun toMessage(contact: Contact, content: String): Message {
|
||||
fun toMessage(contact: Contact, content: String): Message {
|
||||
return if (content.isEmpty()) {
|
||||
PlainText("...")
|
||||
} else if (content.length < 3) {
|
||||
@@ -674,6 +717,9 @@ object JChatGPT : KotlinPlugin(
|
||||
// 好感度调整
|
||||
AdjustUserFavorabilityAgent(),
|
||||
|
||||
// 请求主人帮助
|
||||
RequestOwner(),
|
||||
|
||||
// Epic 免费游戏
|
||||
// EpicFreeGame(),
|
||||
|
||||
|
||||
@@ -5,6 +5,9 @@ import net.mamoe.mirai.console.data.ValueDescription
|
||||
import net.mamoe.mirai.console.data.value
|
||||
|
||||
object PluginConfig : AutoSavePluginConfig("Config") {
|
||||
@ValueDescription("主人QQ,AI可以通过工具向主人发起请求,会等待一段时间")
|
||||
val ownerId: Long by value()
|
||||
|
||||
@ValueDescription("OpenAI API base url")
|
||||
val openAiApi: String by value("https://dashscope.aliyuncs.com/compatible-mode/v1/")
|
||||
|
||||
@@ -104,4 +107,10 @@ object PluginConfig : AutoSavePluginConfig("Config") {
|
||||
|
||||
@ValueDescription("好感度每日基础偏移速度(点/天)")
|
||||
val favorabilityBaseShiftSpeed by value(2.0)
|
||||
|
||||
@ValueDescription("表情包路径,配置后会加载目录下的文件名,提示词中需要用{meme}来插入上下文")
|
||||
val memeDir: String by value("")
|
||||
|
||||
@ValueDescription("请求主人回复等待时间,单位毫秒,默认300秒")
|
||||
val requestOwnerWaitTimeout: Long by value(300000L)
|
||||
}
|
||||
57
src/main/kotlin/tools/RequestOwner.kt
Normal file
57
src/main/kotlin/tools/RequestOwner.kt
Normal file
@@ -0,0 +1,57 @@
|
||||
package top.jie65535.mirai.tools
|
||||
|
||||
import com.aallam.openai.api.chat.Tool
|
||||
import com.aallam.openai.api.core.Parameters
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.add
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import kotlinx.serialization.json.put
|
||||
import kotlinx.serialization.json.putJsonArray
|
||||
import kotlinx.serialization.json.putJsonObject
|
||||
import net.mamoe.mirai.event.EventPriority
|
||||
import net.mamoe.mirai.event.GlobalEventChannel
|
||||
import net.mamoe.mirai.event.events.FriendMessageEvent
|
||||
import net.mamoe.mirai.event.events.MessageEvent
|
||||
import net.mamoe.mirai.event.nextEvent
|
||||
import net.mamoe.mirai.message.data.content
|
||||
import top.jie65535.mirai.JChatGPT
|
||||
import top.jie65535.mirai.PluginConfig
|
||||
import kotlin.collections.getValue
|
||||
|
||||
class RequestOwner : BaseAgent(
|
||||
tool = Tool.function(
|
||||
name = "requestOwnerHelp",
|
||||
description = "当遇到无法处理的问题时,通过私聊请求系统管理员并阻塞等待其回复,默认超时时间5分钟,请求前建议提前告知群友。",
|
||||
parameters = Parameters.buildJsonObject {
|
||||
put("type", "object")
|
||||
putJsonObject("properties") {
|
||||
putJsonObject("content") {
|
||||
put("type", "string")
|
||||
put("description", "请求内容与上下文")
|
||||
}
|
||||
}
|
||||
putJsonArray("required") {
|
||||
add("content")
|
||||
}
|
||||
}
|
||||
)
|
||||
) {
|
||||
override val isEnabled: Boolean
|
||||
get() = PluginConfig.ownerId > 0 && PluginConfig.requestOwnerWaitTimeout > 0
|
||||
|
||||
override suspend fun execute(args: JsonObject?, event: MessageEvent): String {
|
||||
requireNotNull(args)
|
||||
val owner = event.bot.getFriendOrFail(PluginConfig.ownerId)
|
||||
val content = args.getValue("content").jsonPrimitive.content
|
||||
JChatGPT.logger.info("请求主人协助中:$content")
|
||||
owner.sendMessage(JChatGPT.toMessage(owner, content))
|
||||
|
||||
val nextEvent: FriendMessageEvent = withTimeout(PluginConfig.requestOwnerWaitTimeout) {
|
||||
GlobalEventChannel.nextEvent(EventPriority.MONITOR) { it.sender.id == PluginConfig.ownerId }
|
||||
}
|
||||
val response = nextEvent.message.content
|
||||
JChatGPT.logger.info("主人回复:$response")
|
||||
return response
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ class VisitWeb : BaseAgent(
|
||||
}
|
||||
|
||||
override val isEnabled: Boolean
|
||||
get() = PluginConfig.jinaApiKey.isNotEmpty()
|
||||
get() = true // PluginConfig.jinaApiKey.isNotEmpty()
|
||||
|
||||
override val loadingMessage: String
|
||||
get() = "上网中..."
|
||||
@@ -62,7 +62,9 @@ class VisitWeb : BaseAgent(
|
||||
private suspend fun jinaReadPage(url: String): String {
|
||||
return try {
|
||||
httpClient.get(JINA_READER_URL_PREFIX + url) {
|
||||
header("Authorization", "Bearer ${PluginConfig.jinaApiKey}")
|
||||
if (PluginConfig.jinaApiKey.isNotEmpty()) {
|
||||
header("Authorization", "Bearer ${PluginConfig.jinaApiKey}")
|
||||
}
|
||||
}.bodyAsText()
|
||||
} catch (e: Throwable) {
|
||||
"Error fetching \"$url\": ${e.message}"
|
||||
|
||||
Reference in New Issue
Block a user