0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

# Semantic Kernel: エージェント代理

Posted at

生成型AIにおいて、エージェントは通常、自律的にデータを生成または操作できるシステムやモデルを指します。これらのエージェントは、テキスト生成、画像や動画の制作、音楽の創作、データ分析およびシミュレーションなど、多くのシナリオで役割を果たします。以下は、生成型AIにおけるエージェントの具体的な役割と利点です:

増強された創造性:

エージェントは、新しいアイデア、例えばアート作品、音楽、文学テキストなどを生成し、アーティストやクリエイターが従来の思考の枠を超える手助けをします。

コンテンツ制作の自動化:

メディアやエンターテインメント業界において、エージェントはニュース報道、ソーシャルメディアの投稿、その他のタイプのコンテンツを自動生成し、効率を大幅に向上させ、コストを削減します。

個別の体験:

生成型AIエージェントは、ユーザーの好みや過去の行動に基づいてコンテンツを個別化します。例えば、推薦システムにおける個別の再生リストやニュースの要約などです。

意思決定とシミュレーションの支援:

ビジネスや研究の分野では、エージェントはシミュレーションデータや予測モデルを生成し、企業や研究者が市場動向を分析し、リスク評価を行い、新製品を開発する手助けをします。

教育と訓練:

教育分野のエージェントは、カスタマイズされた学習教材や模擬試験を作成し、学習者に個別の学習体験を提供し、教師の負担を軽減します。

言語処理と翻訳:

エージェントは言語を自動で処理し、翻訳する能力を持ち、異言語間のコミュニケーションを容易にします。また、言語学習の補助ツールも提供できます。

以下は、翻訳者エージェントと監査者エージェントが相互に連携するコードの例です:

using Microsoft.SemanticKernel.Experimental.Agents;
using System.Text;

#pragma warning disable SKEXP0101
namespace Demo08_Agent
{
    internal class Program
    {
        static string key;
        static string chatModelId;
        static List<IAgent> agents;
        static async Task Main(string[] args)
        {
            key = File.ReadAllText(@"C:\GPT\key.txt");
            chatModelId = "gpt-4-turbo";
            agents = new List<IAgent>();
            Console.WriteLine("======== 開始協作 ========");
            IAgentThread? agentThread = null;
            try
            {
                // 创建翻译者
                var translator = await CreateTranslatorAsync();
                // 创建审核者
                var auditor = await CreateAuditorAsync();
                // 创建协作线程,两个代理都向其中添加消息。
                agentThread = await translator.NewThreadAsync();
                var prompt = new StringBuilder();
                prompt.AppendLine("把下面中文翻译成日文:");
                prompt.AppendLine("-----------------");
                prompt.AppendLine(File.ReadAllText("content.txt"));
                // 添加用户留言
                var messageUser = await agentThread.AddUserMessageAsync(prompt.ToString());
                DisplayMessages(null, ConsoleColor.White, messageUser);
                var times = 1;
                bool isComplete = false;
                do
                {
                    times++;
                    // 启动翻译者工作
                    var agentMessages = await agentThread.InvokeAsync(translator).ToArrayAsync();
                    DisplayMessages(translator, ConsoleColor.Green, agentMessages);
                    
                    // 启动审核工作
                    agentMessages = await agentThread.InvokeAsync(auditor).ToArrayAsync();
                    DisplayMessages(auditor, ConsoleColor.Yellow, agentMessages);
                    
                    // 评估是否达到目标。
                    if (agentMessages.First().Content.Contains("采用它", StringComparison.OrdinalIgnoreCase))
                    {
                        isComplete = true;
                    }
                    if (times > 3)
                    {
                        isComplete = true;
                    }
                }
                while (!isComplete);
            }
            finally
            {
                // 清理
                await Task.WhenAll(agents.Select(a => a.DeleteAsync()));
            }
        }

        static async Task<IAgent> CreateTranslatorAsync()
        {
            var agent = await new AgentBuilder()
                        .WithOpenAIChatCompletion(chatModelId, key)
                        .WithInstructions("您是一位中文日文的翻译家,以严谨闻名。你全神贯注于手头的目标。翻译成准确,高质量的译文。完善翻译内容时,请考虑翻译审核员的建议。")
                        .WithName("翻译家")
                        .WithDescription("翻译家")
                        .BuildAsync();
            return agent;
        }

        static async Task<IAgent> CreateAuditorAsync()
        {
            var agent = await new AgentBuilder()
                        .WithOpenAIChatCompletion(chatModelId, key)
                        .WithInstructions("你是一位中日文翻译的翻译审核员,你有丰富的翻译和审核经验,对翻译质量有较高的要求,总是严格要求,反复琢磨,以求得到更为准确的翻译。目标是确定给定翻译否符合要求,是否采用。如果不符合要求,提出你的建议给翻译家,但不要把翻译内容给对方。如果翻译内容可以接受并且符合您的标准,请说:采用它。")
                        .WithName("翻译审核员")
                        .WithDescription("翻译审核员")
                        .BuildAsync();
            return agent;
        }

        static void DisplayMessages(IAgent? agent = null, ConsoleColor color = ConsoleColor.White, params IEnumerable<IChatMessage> messages)
        {
            foreach (var message in messages)
            {
                Console.ResetColor();
                Console.ForegroundColor = color;                    
                if (agent != null)
                {
                    Console.WriteLine($" {agent.Name}{message.Role}): {message.Content}");
                }
                else
                {
                    Console.WriteLine($"({message.Role}): {message.Content}");
                }
                Console.ResetColor();
            }
        }
    }
}

実行結果は次の通りです:

実行結果画像

(Translated by GPT)

元のリンク:https://mp.weixin.qq.com/s/qIz-CuoukULlDqAVr2bMJQ?wt.mc_id=MVP_325642

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?