人妻夜夜爽天天爽三区丁香花-人妻夜夜爽天天爽三-人妻夜夜爽天天爽欧美色院-人妻夜夜爽天天爽免费视频-人妻夜夜爽天天爽-人妻夜夜爽天天

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C# AIModelRouter:使用不同的AI模型完成不同的任務

freeflydom
2025年1月9日 9:34 本文熱度 296

AIModelRouter

AI模型路由,模型的能力有大小之分,有些簡單任務,能力小一點的模型也能很好地完成,而有些比較難的或者希望模型做得更好的,則可以選擇能力強的模型。為什么要這樣做呢?可以降低AI模型的使用成本,畢竟能力強的模型會更貴一點,省著用挺好的。

Semantic Kernel中可以很簡便地使用一個AIModelRouter。

實踐

先來一個簡單的例子

來自https://github.com/microsoft/semantic-kernel/tree/main/dotnet/samples/Demos/AIModelRouter

新建一個CustomRouter類,如下所示:

internal sealed class CustomRouter()
{
    internal string GetService(string lookupPrompt, List<string> serviceIds)
    {
        // The order matters, if the keyword is not found, the first one is used.
        foreach (var serviceId in serviceIds)
        {
            if (Contains(lookupPrompt, serviceId))
            {
                return serviceId;
            }
        }
        return serviceIds[0];
    }
    // Ensure compatibility with both netstandard2.0 and net8.0 by using IndexOf instead of Contains
    private static bool Contains(string prompt, string pattern)
        => prompt.IndexOf(pattern, StringComparison.CurrentCultureIgnoreCase) >= 0;
}

新建一個SelectedServiceFilter類用于打印一些信息:

 internal sealed class SelectedServiceFilter : IPromptRenderFilter
 {
     /// <inheritdoc/>
     public Task OnPromptRenderAsync(PromptRenderContext context, Func<PromptRenderContext, Task> next)
     {
         Console.ForegroundColor = ConsoleColor.Yellow;
         Console.WriteLine($"Selected service id: '{context.Arguments.ExecutionSettings?.FirstOrDefault().Key}'");
         Console.ForegroundColor = ConsoleColor.White;
         Console.Write("Assistant > ");
         return next(context);
     }
 }

使用多個模型:

為捕獲路由器選擇的服務 ID 添加自定義過濾器:

開啟一個聊天循環(huán):

        Console.ForegroundColor = ConsoleColor.White;
        ChatHistory history = [];
        string history1 = string.Empty;
        bool isComplete = false;
        do
        {
            Console.WriteLine();
            Console.Write("> ");
            string? input = Console.ReadLine();
            if (string.IsNullOrWhiteSpace(input))
            {
                continue;
            }
            if (input.Trim().Equals("EXIT", StringComparison.OrdinalIgnoreCase))
            {
                isComplete = true;
                break;
            }
            if (input.Trim().Equals("Clear", StringComparison.OrdinalIgnoreCase))
            {
                history.Clear();
                history1 = " ";
                Console.WriteLine("已清除聊天記錄");
                continue;
            }
            history.Add(new ChatMessageContent(AuthorRole.User, input));
            history1 += $"User:{input}\n";
            Console.WriteLine();
            // Find the best service to use based on the user's input
            KernelArguments arguments = new(new PromptExecutionSettings()
            {
                ServiceId = router.GetService(input, serviceIds).Result,
                FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
            });
            // Invoke the prompt and print the response
            //await foreach (var chatChunk in kernel.InvokePromptStreamingAsync(userMessage, arguments).ConfigureAwait(false))
            //{
            //    Console.Write(chatChunk);
            //}
           
            var result = await kernel.InvokePromptAsync(history1, arguments).ConfigureAwait(false);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(result);
            Console.WriteLine();
            // Add the message from the agent to the chat history
            history.AddMessage(AuthorRole.Assistant, result.ToString());
            history1 += $"Assistant:{result}\n";
        } while (!isComplete);
    }
}

來看看現(xiàn)在這個簡單的路由規(guī)則:

當你的提問中包含一個ServiceId的時候,就會選擇那個服務ID對應的模型進行回復,如果不包含就選擇第一個服務ID對應的模型進行回復。

實際上這樣使用,很容易讓AI迷惑,因為我們總是要帶上一個ServiceId,如果讓AI根據(jù)用戶的提問,自己決定用哪個模型是更好的。

進階使用,用AI自己來決定

使用一個靠譜的AI模型來做這個事情比較好。

我們輸入你好,那么Prompt就會變成這樣:

AI返回的結(jié)果如下:

再試試其他幾個怎么觸發(fā):

而工具調(diào)用與其他比較容易混淆,因為就算是我們自己,也很難分辨有什么區(qū)別:

這時候或許修改Prompt可以奏效。

修改后的Prompt如下:

 string skPrompt = """
          根據(jù)用戶的輸入,返回最佳服務ID。
          如果用戶需要獲取當前時間與寫郵件,則選擇工具調(diào)用相關(guān)的服務ID。
          用戶輸入:
          {{$input}}
          服務ID列表:
          {{$serviceIds}}
          無需返回任何其他內(nèi)容,只需返回服務ID。              
     """;

效果如下所示:

以上就是本次分享的全部內(nèi)容,希望對你有所幫助。

?轉(zhuǎn)自https://www.cnblogs.com/mingupupu/p/18654982


該文章在 2025/1/9 9:34:20 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務費用、相關(guān)報表等業(yè)務管理,結(jié)合碼頭的業(yè)務特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 精品黄色片 | 一区二区三区毛AAAA片特级 | 在线视频一区二区三区在线播放 | 人妻丰满熟妞av无码区 | 国产成人久久精品麻豆一区 | 国产乱码一区二区三区 | 久久中文字幕久久久久91 | 久久久精品宅男一区二区三区免费 | 狠狠热免费视频 | 丝袜天堂| 熟女人妻久久中文字幕一二区 | 亚洲精品成人专区在线观看 | 欧美日韩极品美女精品 | 国产亚洲精品第一区香蕉 | 国产卡一卡二卡3卡4乱码 | 久久国产精品免费一区六九堂 | 日本欧美成人免费 | 9191精品国产免费久久电影 | 吉泽明步高清无码中文 | 海角国精产品一区一区三区糖心行业总结 | 成人免费网站 | 1024国产精品免费观看 | 久久精品亚洲日本波多野结衣 | 免费看无码午夜福利片 | 中文精品一卡2卡3卡4卡国色 | 国产精品无遮挡又黄又爽免费网站 | 四虎成人免费观看在线网址 | 亚洲无线码一区国产欧美国日产 | 丁香精无码一区二区三区 | 国产福利视频一区二区 | 人妻一区二区三区在线播放 | 亚洲狠狠爱| 成人a毛片在线看免费全部播放 | 永久免费看成人A片在线播放 | 大陆一级毛片免费高清 | 无码精品人妻一区二区三区夜夜嗨 | 老妇FREE性VIDEOSXX | 丰满多毛少妇做爰视频爽爽和R | 精品人妻中文字幕影片 | 国产精品久久久久无码AV1 | 亚洲一区二区免费看 |