距離上次用 Azure OpenAI SDK 整合 ChatGPT 功能已是一年多前的事。(ChatGPT 聊天程式練習 - 使用 .NET + Azure OpenAI API)

最近的 Side Project 又有整合 ChatGPT 需求,修改過程發現上次用的 .NET 版 Azure.AI.OpenAI 程式庫 (Azure OpenAI client library for .NET),剛在 10/1 發佈 2.0 正式版,去年用的版本是 1.0.0-beta.8,今年五月更新到 beta.17 後沒出 1.0.0 正式版就停更了,直接另開 2.0.0 主線任務。

這代表什麼呢?代表 1.0.0 Beta 沒轉正就被放生,其寫法在 2.0.0 有很大的機率不適用。不過既然要嚐鮮,在 Beta 階段搶先玩,遇到這種狀況都只是剛好而已,沒什麼好靠北。而實測 1.0.0-beta.8 仍與目前的 OpenAI API 相容,舊程式不必改也能繼續跑得好好的。但這回起新專案,就來改用 2.0.0 正式版吧。

2.0 改掉了不少型別名稱,但我個人覺得有更簡潔好寫、針對 OpenAI 共用及 Azure 專屬型別也做了較明確的區隔,是朝好的方向修正,參考官方網站範例我順利完成升級,這篇簡單整理新舊版聊天程式的差異。

以下是 1.0.0-beta 的寫法:

    OpenAIClient client;
    public GptChatService(string apiUrl, string apiKey, string deployName)
    {
        this.apiUrl = apiUrl;
        this.apiKey = apiKey;
        this.deployName = deployName;
        client = new OpenAIClient(
            new Uri(apiUrl),
            new AzureKeyCredential(apiKey));
    }
    List<ChatMessage> history = new List<ChatMessage>();    
    public async Task<string> Complete(string sysPropmt, string msg, bool includeHistory = false)
    {
        var chatMessages = new List<ChatMessage>() {
                new ChatMessage(ChatRole.System, sysPropmt)
        };
        if (includeHistory)
            chatMessages.AddRange(history);
        chatMessages.Add(new ChatMessage(ChatRole.User, msg));
        Response<ChatCompletions> response = await client.GetChatCompletionsAsync(
        deploymentOrModelName: deployName,
        new ChatCompletionsOptions(chatMessages)
        {
            Temperature = Temperature,
            MaxTokens = MaxTokens,
            NucleusSamplingFactor = NucleusSamplingFactor,
            FrequencyPenalty = FrequencyPenalty,
            PresencePenalty = PresencePenalty
        });

        ChatCompletions completions = response.Value;
        var resp = completions.Choices.First().Message.Content;
        if (includeHistory)
        {
            history.Add(new ChatMessage(ChatRole.User, msg));
            history.Add(new ChatMessage(ChatRole.Assistant, resp));
        }
        return resp;
    }

改用 2.0.0 的寫法如下,主要差異我以註解說明:

    // 改為 OpenAI.Chat.ChatClient,
    OpenAI.Chat.ChatClient client; 
    public GptChatService(string apiUrl, string apiKey, string deployName)
    {
        this.apiUrl = apiUrl;
        this.apiKey = apiKey;
        this.deployName = deployName;
        // 改建立 AzureOpenAIClient,其繼承 OpenAI.Chat.ChatClient,內部則實作整合 Azure OpenAI (AOAI)
        // 因此抽換 OpenAI 或 AOAI 服務時,後面的程式不用改
        client = new AzureOpenAIClient(
            new Uri(apiUrl),
            new System.ClientModel.ApiKeyCredential(apiKey))
            .GetChatClient(this.deployName); // 建立 Client 時就指定好 DeployName
    }
    // 記錄交談歷史用 ChatMessage 不變
    List<ChatMessage> history = new List<ChatMessage>();    
    public async Task<string> Complete(string sysPropmt, string msg, bool includeHistory = false)
    {
        var chatMessages = new List<ChatMessage>() {
                // 原本 new ChatMessage(ChatRole.System, sysPropmt),改用 SystemChatMessage
                new SystemChatMessage(sysPropmt) 
        };
        if (includeHistory)
            chatMessages.AddRange(history);
        // new ChatMessage(ChatRole.User, msg) 改 new UserChatMessage(msg)
        chatMessages.Add(new UserChatMessage(msg));
        // 原本呼叫 GetChatCompletionsAsync(),要傳入 deployName 及 ChatCompletionsOptions
        // 簡化為傳入 List<ChatMessage> 即可
        var complete = await client.CompleteChatAsync(chatMessages);
        // 原本是 .Choices.First().Message.Content,2.0 寫 .Value.Content[0].Text
        var resp = complete.Value.Content[0].Text;
        if (includeHistory)
        {
            history.Add(new UserChatMessage(msg));
            history.Add(new AssistantChatMessage(resp));
        }
        return resp;
    }

綜觀下來,2.0.0 更好寫,而出到 2.0.0 正式版,代表這塊已趨於成熟穩定,更可以安心使用了。

A year has passed since using Azure OpenAI SDK for integrating ChatGPT. Recently, I revisited this for a side project and noticed the release of Azure.AI.OpenAI 2.0.0. This version introduces significant changes for improved simplicity and clarity. This post compares the migration from 1.0.0-beta to 2.0.0.


Comments

Be the first to post a comment

Post a comment