【大模型部署】在C# Winform中使用文心一言ERNIE-3.5 4K 聊天模型
前言
今天來寫一個簡單的ernie-c#的例子,主要參考了百度智能云的例子,然后自己改了改,學習了ERNIE模型的鑒權方式,數據流的格式和簡單的數據解析,實現了在C#中調用百度智能云的ernie(其實就是文心一言3.5的語言模型)的例子。
步驟
- 注冊百度智能云
- 創建應用
- 開通ERNIE 3.5 4K模型的付費通道(似乎是免費的)
- 獲取AK和SK
- 代碼接入
步驟1-注冊百度智能云
百度智能云
按提示注冊即可
步驟2-創建應用
點擊“應用接入” -> “創建應用”
按下圖操作即可
步驟3-開通ERNIE 3.5 4K模型的付費通道
百度會送20元的優惠券,試用的話是免費的,如果要收費的話,請酌情開通
我的代金券
步驟4-獲取AK和SK
在這里獲取AK和SK,后面代碼里面要用
代碼接入
定義AK和SK
// 您的AccessKey ID
const string API_KEY = "qSXXXXXXXXXXXXXXXXQ";
// 您的AccessKey Secret
const string SECRET_KEY = "Kb8XXXXXXXXXXXXXXXXXX24ZH";
定義發送和回傳的數據結構
public class Message
{[JsonProperty("role")]public string Role { get; set; }[JsonProperty("content")]public string Content { get; set; }
}public class ErnieMessage
{[JsonProperty("messages")]public Message[] Messages { get; set; }[JsonProperty("temperature")]public double Temperature { get; set; }[JsonProperty("top_p")]public double TopP { get; set; }[JsonProperty("penalty_score")]public double PenaltyScore { get; set; }[JsonProperty("disable_search")]public bool DisableSearch { get; set; }[JsonProperty("enable_citation")]public bool EnableCitation { get; set; }
}
ErnieMessage ernieMessage = new ErnieMessage();public class ChatCompletionResult
{[JsonProperty("id")]public string Id { get; set; }[JsonProperty("object")]public string ObjectType { get; set; } // 這里的屬性名可以根據實際需求進行命名,例如避免與.NET的內置Object類沖突 [JsonProperty("created")]public long CreatedTimestamp { get; set; } // 通常時間戳會轉換為DateTime,但這里保持為long [JsonProperty("result")]public string Result { get; set; }[JsonProperty("is_truncated")]public bool IsTruncated { get; set; }[JsonProperty("need_clear_history")]public bool NeedClearHistory { get; set; }[JsonProperty("finish_reason")]public string FinishReason { get; set; }[JsonProperty("usage")]public Usage UsageInfo { get; set; }// 嵌套類來表示Usage對象 public class Usage{[JsonProperty("prompt_tokens")]public int PromptTokens { get; set; }[JsonProperty("completion_tokens")]public int CompletionTokens { get; set; }[JsonProperty("total_tokens")]public int TotalTokens { get; set; }}
}
定義消息池
// 定義消息池
public class MessagePool
{public List<Message> Messages { get; set; }public MessagePool(){Messages = new List<Message>();}public void AddMessage(Message message){Messages.Add(message);}public void ClearMessages(){Messages.Clear();}
}
定義角色
enum eRole { User, Assistant}
string Role(eRole role)
{return role == eRole.User ? "user" : "assistant";
}
定義聊天函數
private string Chat(string strSend, eRole role)
{var client = new RestClient($"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-4k-0205?access_token={GetAccessTokenMethod()}");client.Timeout = -1;var request = new RestRequest(Method.POST);request.AddHeader("Content-Type", "application/json");AddMessageToPool(strSend, role);var body = JsonConvert.SerializeObject(ernieMessage, Formatting.None);request.AddParameter("application/json", body, ParameterType.RequestBody);IRestResponse response = client.Execute(request);AddMessageToPool(response.Content, eRole.Assistant);ChatCompletionResult result = JsonConvert.DeserializeObject<ChatCompletionResult>(response.Content);FillCompletionInfo(result);if (result.NeedClearHistory) ClearHistoryMethod();return result.Result;
}
完整代碼
完整代碼