Discord Botを使ってローカルLLMをしゃべらせる(CeVIO AI編)

VoiceVoxと代り映えしませんが、MemoryStreamが使えませんので一旦wavファイルに書き出すところが変更されています。
トークボイスのキャラクター名はベタで書いていますので、お持ちのキャラクター名に変えてくださいね。

Nuget情報



Program.cs

using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.VoiceNext;
using LLama.Common;
using LLama;
using DSharpPlus.EventArgs;
using System.Diagnostics;

namespace DSharpPlus_Bots
{
    public sealed class Program
    {
        private static bool Op = true;
        private static VoiceNextConnection? VoiceCon;
        public static async Task Main()
        {
            try
            {
                ChatSession chtSess;
                InferenceParams infPara;
                // LLMモデルの場所
                string strModelPath = Environment.GetEnvironmentVariable("LLMPATH", System.EnvironmentVariableTarget.User) + @"mradermacher\Shadows-MoE-GGUF\Shadows-MoE.Q8_0.gguf"; 

                //チャットログ
                string strChatlogPath = Environment.GetEnvironmentVariable("CHATDB", System.EnvironmentVariableTarget.User) + @"ChatDB.db";
                ChatHistoryDB chtDB;

                //LLMモデルのロードとパラメータの設定
                Console.ForegroundColor = ConsoleColor.Blue;
                ModelParams modPara = new(strModelPath)
                {
                    ContextSize = 4096,
                    Seed = 1337,
                    GpuLayerCount = 24,
                };

                using LLamaWeights llmWeit = LLamaWeights.LoadFromFile(modPara);
                using LLamaContext llmContx = llmWeit.CreateContext(modPara);
                InteractiveExecutor itrEx = new(llmContx);

                //チャットログを読み込みます。
                ChatHistory chtHis = new ChatHistory();
                chtDB = new ChatHistoryDB(strChatlogPath, chtHis);

                chtSess = new(itrEx, chtHis);
                var varHidewd = new LLamaTransforms.KeywordTextOutputStreamTransform(["User: ", "Assistant: "]);
                chtSess.WithOutputTransform(varHidewd);
                infPara = new()
                {
                    Temperature = 0.8f,
                    AntiPrompts = ["User:"],
                    //AntiPrompts = ["User:", "<|eot_id|>"],    //Llama3用
                    MaxTokens = 256,
                };

                //ここからDiscordの処理
                // ユーザー環境変数に登録した「DISCORD_TOKEN」を持ってくる
                string? token = Environment.GetEnvironmentVariable("DISCORD_TOKEN", System.EnvironmentVariableTarget.User);
                if (string.IsNullOrWhiteSpace(token))
                {
                    Console.WriteLine("Please specify a token in the DISCORD_TOKEN environment variable.");
                    Environment.Exit(1);

                    return;
                }

                DiscordConfiguration config = new()
                {
                    Token = token,
                    TokenType = TokenType.Bot,
                    Intents = DiscordIntents.AllUnprivileged | DiscordIntents.MessageContents | DiscordIntents.GuildVoiceStates
                };

                DiscordClient client = new(config);
                client.UseVoiceNext();

                string wavFilePath = Environment.GetEnvironmentVariable("TESTDATA", System.EnvironmentVariableTarget.User) + "output.wav";

                //メッセージイベント
                client.MessageCreated += async (client, eventArgs) =>
                {
                    //ボットに応答しない
                    if (eventArgs.Message.Author.Id==client.CurrentUser.Id)
                        return;

                    // ユーザーのターン
                    ChatHistory.Message msg = new(AuthorRole.User, "User: " + eventArgs.Message.Content);
                    if (Op) chtDB.WriteHistory(AuthorRole.User, "User: " +  eventArgs.Message.Content);

                    // AIのターン
                    string strMsg = "";
                    await foreach (string strAns in chtSess.ChatAsync(msg, infPara))
                    {
                        strMsg += strAns;
                    }
                    //Discordに発信するときは「User:」や「Assistant:」を抜く
                    string strSndmsg = strMsg.Replace("User:", "").Replace("Assistant:", "").Replace("assistant:", "").Trim();
                    await eventArgs.Message.RespondAsync(strSndmsg);
                    if (Op) chtDB.WriteHistory(AuthorRole.Assistant, strMsg.Replace("User:", "").Trim()+"\n"+"User:");

                    if (VoiceCon is not null) //Voiceチャンネルが開いているときは音声を送信
                    {
                        CevioAI(strSndmsg, wavFilePath);
                        await SendAsync(VoiceCon, wavFilePath);
                    }
                };

                client.Ready += OnClientReady;
                client.GuildAvailable += OnGuildAvailable;
                client.VoiceStateUpdated += OnVoiceStateUpdated;

                DiscordActivity status = new("with fire", ActivityType.Playing);

                await client.ConnectAsync(status, UserStatus.Online);

                await Task.Delay(-1);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        private static Task OnClientReady(DiscordClient client, ReadyEventArgs e)
        {
            Console.WriteLine("Bot is ready!");
            return Task.CompletedTask;
        }

        private static Task OnGuildAvailable(DiscordClient client, GuildCreateEventArgs e)
        {
            Console.WriteLine($"Guild available: {e.Guild.Name}");
            return Task.CompletedTask;
        }

        private static async Task OnVoiceStateUpdated(DiscordClient client, VoiceStateUpdateEventArgs e)
        {
            try
            {
                //Bot以外のチャンネルが開いた場合
                if (e.After.Channel != null && !e.After.User.IsBot)
                {
                    Console.WriteLine($"{e.After.User.Username} joined voice channel {e.After.Channel.Name}");

                    var vnext = client.GetVoiceNext();
                    var connection = await vnext.ConnectAsync(e.After.Channel);

                    VoiceCon = connection;

                }
                //Botが退出した場合はコネクションをNULLにする
                if (e.After.Channel == null && e.After.User.IsBot)
                {
                    VoiceCon = null;
                }
            }
            catch (Exception ex) 
            { 
                Console.WriteLine(ex.ToString());
            }
        }

        private static async Task SendAsync(VoiceNextConnection connection, string filePath)
        {
            var transmit = connection.GetTransmitSink();

            var pcm = ConvertAudioToPcm(filePath);
            await pcm.CopyToAsync(transmit);
            await pcm.DisposeAsync();

        }

        private static Stream ConvertAudioToPcm(string filePath)
        {
            var ffmpeg = Process.Start(new ProcessStartInfo
            {
                FileName = "ffmpeg",
                Arguments = $@"-i ""{filePath}"" -ac 2 -f s16le -ar 48000 pipe:1",
                RedirectStandardOutput = true,
                UseShellExecute = false
            });

            return ffmpeg.StandardOutput.BaseStream;

        }

        public static void CevioAI(string strMsg, string filePath)
        {
            try
            {
                dynamic service = Activator.CreateInstance(Type.GetTypeFromProgID("CeVIO.Talk.RemoteService2.ServiceControl2V40"));
                service.StartHost(false);

                dynamic talker = Activator.CreateInstance(Type.GetTypeFromProgID("CeVIO.Talk.RemoteService2.Talker2V40"));
                talker.Cast = "双葉湊音";

                dynamic result = talker.OutputWaveToFile(strMsg, filePath);

                //開放忘れるとメモリリーク
                System.Runtime.InteropServices.Marshal.ReleaseComObject(talker);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(service);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }


    }
}




ChatHistoryDB.cs

using LLama.Common;
using System.Data.SQLite;

namespace DSharpPlus_Bots
{
    class ChatHistoryDB
    {
        ChatHistory? chtHis;
        string strDbpath;
        Dictionary<string, AuthorRole>? Roles = new Dictionary<string, AuthorRole> { { "System" ,AuthorRole.System}, { "User", AuthorRole.User }, { "Assistant", AuthorRole.Assistant } };

        public ChatHistoryDB(string strDbpath, ChatHistory chtHis){

            this.chtHis= chtHis;
            this.strDbpath= strDbpath;
            try
            {
                var conSb = new SQLiteConnectionStringBuilder { DataSource = strDbpath };

                var con = new SQLiteConnection(conSb.ToString());
                con.Open();

                using (var cmd = new SQLiteCommand(con))
                {
                    cmd.CommandText="CREATE TABLE IF NOT EXISTS ch(" +
                        "sq  INTEGER PRIMARY KEY," +
                        "dt  TEXT NOT NULL," +
                        "id  TEXT NOT NULL," +
                        "msg TEXT)";
                    cmd.ExecuteNonQuery();

                    cmd.CommandText="select * from ch order by sq";
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if (chtHis is null) chtHis=new ChatHistory();
                            chtHis.AddMessage(Roles[(string)reader["id"]], (string)reader["msg"]);
                        }
                    }
                }

                con.Close();

            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex.Message);
            }

        }

        public interface IDisposable
        {
            void Dispose();
        }


        public void WriteHistory(AuthorRole aurID, string strMsg,bool booHis=false) 
        {
            try
            {
                var conSb = new SQLiteConnectionStringBuilder { DataSource = strDbpath };

                var con = new SQLiteConnection(conSb.ToString());
                con.Open();

                using (var cmd = new SQLiteCommand(con))
                {
                    if (booHis)
                    {
                        if (chtHis is null) chtHis=new ChatHistory();
                        chtHis.AddMessage(aurID, strMsg);
                    }
                    cmd.CommandText = $"insert into ch(dt,id,msg) values(datetime('now', 'localtime'),'{Roles.FirstOrDefault(v => v.Value.Equals(aurID)).Key}','{strMsg}')";
                    cmd.ExecuteNonQuery();
                }
                con.Close();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Follow me!

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です