AI优化和自动重连优化

This commit is contained in:
milimoe 2025-08-08 20:55:24 +08:00
parent 1f7ae78257
commit a1c64d42d1
Signed by: milimoe
GPG Key ID: 9554D37E4B8991D0
3 changed files with 20 additions and 9 deletions

View File

@ -645,6 +645,10 @@ namespace Milimoe.RainBOT.ListeningTask
Bot.ColorfulCheckPass(sender, "随机复读", dice, GeneralSettings.PRepeat, delay);
GroupMessageContent content = new(e.group_id);
content.message.AddRange(e.message);
if (e.CheckThrow(50, out _) && content.message.LastOrDefault(m => m is TextMessage) is TextMessage text)
{
text.data.text += "desuwa";
}
_ = Bot.SendGroupMessage(e.group_id, "随机复读", content, delay * 1000);
_ = OshimaController.Instance.SCAdd(e.user_id, e.group_id, e.detail);
return quick_reply;
@ -706,8 +710,9 @@ namespace Milimoe.RainBOT.ListeningTask
public static void AIChat(GroupMessageEvent e, bool isAt = false)
{
AI.Add(e.user_id == GeneralSettings.Master ? (Random.Shared.Next(0, 10) < 5 ? "小音" : "米莉") : e.sender.title, string.Join("", e.message.Where(m => m.type == "text").Select(s => s.data.ToString())));
if (!AI.CD && (isAt || e.detail.StartsWith("小雪") || AI.List.Count > 4))
if (e.sender.title == "No.18") e.sender.title = "小音";
AI.Add(e.user_id == GeneralSettings.Master ? "米莉" : e.sender.title, string.Join("", e.message.Where(m => m.type == "text").Select(s => s.data.ToString())));
if (!AI.CD && (isAt || e.detail.StartsWith("小雪") || AI.Count > 4))
{
_ = Task.Run(async () =>
{

View File

@ -6,8 +6,8 @@ namespace Milimoe.RainBOT.Settings
{
public class AI
{
public static int Count { get; set; } = 0;
public static bool CD { get; set; } = false;
public static HashSet<string> Context { get; set; } = [];
public static HashSet<string> List { get; set; } = [];
private static readonly string apiKey = GeneralSettings.AIAPIToken;
private static readonly string apiUrl = "https://api.deepseek.com/chat/completions";
@ -19,6 +19,7 @@ namespace Milimoe.RainBOT.Settings
public static void Add(string user, string msg)
{
Count++;
if (msg.Trim() == "") return;
if (List.Count < 10)
{
@ -29,6 +30,7 @@ namespace Milimoe.RainBOT.Settings
public static async Task<string> Chat()
{
Count = 0;
CD = true;
using HttpClient httpClient = new();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
@ -74,15 +76,18 @@ namespace Milimoe.RainBOT.Settings
QQ群2CS2的群
80" }
80" }
],
MaxTokens = 140
};
if (Context.Count > 0) request.Messages.Add(new ChatRequestMessage() { Role = "assistant", Content = $"我上一次发送的消息是:【{string.Join(" && ", Context)}】。这是我本次回复可以参考的内容,我需要注意不能重复这些内容。" });
request.Messages.AddRange(List.Select(s => new ChatRequestMessage { Role = "user", Content = s }));
Console.WriteLine($"本次发送 AI 请求内容:{string.Join(". ", List)}");
if (Context.Count >= 2) Context.Remove(Context.First());
List.Clear();
if (List.Count > 8)
{
List<string> strings = [.. List];
strings.RemoveRange(0, strings.Count - 8);
List = [.. strings];
}
try
{
@ -103,8 +108,8 @@ namespace Milimoe.RainBOT.Settings
{
ChatCompletionResponse completionResponse = JsonSerializer.Deserialize<ChatCompletionResponse>(jsonResponse, jsonOptions) ?? new();
string reply = completionResponse?.Choices?[0].Message.Content ?? "";
Context.Add(reply);
Console.WriteLine($"Assistant: {reply}");
Add("小雪", reply);
Console.WriteLine($"返回结果: {reply}");
return reply;
}
else

View File

@ -104,6 +104,7 @@ namespace Milimoe.RainBOT.Settings
{
await Bot.SendFriendMessage(GeneralSettings.Master, "websocket", "重连服务器成功");
}
await ConnectToAnonymousServer();
}
else
{