? ? ? ? 介于最近deepseek的大火,我就在想能不能用winform也玩一玩本地部署,于是經過查閱資料,然后了解到ollama部署deepseek,最后用ollama sharp NUGet包來實現winform調用ollama 部署的deepseek。
? ? ? ? 本項目使用Vs2022和.net 8.0開發,ollama sharp 使用的是最新版本。也可以使用.net farmwork 4.7.2開發,但是ollama sharp 沒辦法使用最新的,只能使用3.幾的版本,3點幾的版本有問題,因為ollama sharp提供的交互方法不是異步的,這就會導致,大模型如果回復你一個很長的的問題的時候,就會突然中斷,最后我就徹底放棄了,發現最新版本的ollama sharp的交互方法是異步的,最后抱著試一試的心態,果然成功了,讓寫個4000字的論文框架,基本上回答時間在2分鐘左右也不會中斷,(2分鐘是因為我的內存有點少,顯卡還行吧)。效果還是很不錯的,本人使用的deepseek r1 14b的大模型,4060的顯卡,16G的內存,回復速度還是很快的,內存基本上跑80%左右。顯卡40%上下浮動。
展示圖
下載ollama
地址:奧拉馬
下載Windows版本然后進行安裝就好了,安裝完成以后,我們可以在系統環境變量里面添加這兩個
第二個是利用ollama下載的大模型的位置,C盤不夠的可以加這個變量,如果C盤夠多可以忽略,最好設置完以后重啟一下電腦再安裝ollama,安裝好以后可以打開cmd 如圖所示:如果是這樣,說明你已經安裝成功了,
利用ollama安裝deepseek r1 14b
這里我們還是打開ollama網站,打開
如果說內存在32G可以選擇32b的體驗一下,應該會比14b更好用些,最后點擊箭頭所指的地方復制下來打開cmd,直接ctrl+c復制然后回車他就會自動下載,這里有個小技巧:他下載會越來越慢,我們可以按一下ctrl+c,再按一下鍵盤的上方向鍵他就會接著下載,這個時候慢慢就快起來了。
下載完成后我們新打開一個cmd輸入ollama list這個可以查看我們已經下載下來的大模型
補充一點:還可以使用ollama rm 大模型的Name進行刪除
Ollama Sharp
awaescher/OllamaSharp:在 .NET 中使用 Ollama API 的最簡單方法?
上面的是鏈接地址,這是github里面的一個開源項目,使用之前可以看看他的介紹以及使用方法,知其然,知其所以然。
winform 連接大模型
我們打開我們的vs2022。創建新工程,一定要選擇后面不帶括號.netfarmwork的,才會用到8,.0框架
我們進去以后先添加nuget包,找到依賴項,右鍵管理NUGET包,打開以后搜索ollama sharp
這里我已經安裝過了
等待安裝成功以后,我們打開我們窗體的設計器,在左側的工具箱添加一下的控件
listbox主要用來展示安裝的大模型
richtextbox主要用來展示用戶輸入的文字和deepseek回復的文字
textBox讀取用戶輸入的文字
一個發送按鈕一個取消思考按鈕
附上源代碼:
using OllamaSharp.Models;
using OllamaSharp;
using System.Text.RegularExpressions;namespace WinFormsApp1
{public partial class Form1 : Form{private Uri uri;private OllamaApiClient ollama;private List<Model> models;private bool connect;static ManualResetEvent resetEvent = new ManualResetEvent(false);private CancellationTokenSource cancellationTokenSource;int step = 0;private bool mIsCancel = false;public Form1(){InitializeComponent();}private async void Form1_Load(object sender, EventArgs e){richTextBox1.AppendText("稍等,我正在加載模型。。。。。" + Environment.NewLine);uri = new Uri("http://localhost:11434");ollama = new OllamaApiClient(uri);connect = await ollama.IsRunningAsync();models = (await ollama.ListLocalModelsAsync()).ToList();mSelectItem = 0;LoadModles();step = 1;richTextBox1.AppendText("請在上方選擇你要使用的模型,單擊即可" + Environment.NewLine);}/// <summary>/// 流程交互/// </summary>public void WorkFololw(){Task.Run(() =>{while (true){Thread.Sleep(200);string cleanText = "";if (textBox2.Text != ""){cleanText = textBox2.Text;}switch (step){case 1:Thread.Sleep(100);if (models.Count == 0){return;}ollama.SelectedModel = models.ToArray()[mSelectItem].Name; // 選擇模型名稱Log("我已經準備好了小帥哥快來玩呀!", 0, Color.Black);step = 2;break;case 2:if (cleanText.Contains("\r\n")){var prompt = textBox2.Text; // 從文本框讀取提示詞Log(Environment.NewLine + "用戶哥:" + textBox2.Text.TrimEnd('\r', '\n') + Environment.NewLine, 0, Color.Blue);var keepChatting = true;var chat = new Chat(ollama, prompt);Invoke(new Action(() =>{button2.Visible = true;richTextBox1.AppendText("deepSeek-R1>:" + Environment.NewLine);}));BeginSiKao(keepChatting, chat, "");step = 3;}break;case 3:if (cleanText.Contains("\r\n"))step = 2;break;}}});}/// <summary>/// 開始思考/// </summary>/// <param name="keepChatting"></param>/// <param name="chat"></param>public async void BeginSiKao(bool keepChatting, Chat chat, string mImageMsg){//開始聊天await BeginChat(keepChatting, chat, mImageMsg);}/// <summary>/// 加載本地大模型/// </summary>public void LoadModles(){if (models.Any()){foreach (var model in models){if (model.Name.Contains("v2")){Log($"大模型:{model.Name} {model.Size / 1024 / 1024} MB", 1, Color.MediumSeaGreen); // 輸出模型名稱和大小}Invoke(new Action(() =>{listBox1.Items.Add($"大模型:{model.Name} {model.Size / 1024 / 1024} MB");}));}}else{Log("沒有大模型環境,請自行下載大模型", 1, Color.Red);return;}}/// <summary>/// 開始聊天/// </summary>/// <param name="keepChatting"></param>/// <param name="chat"></param>/// <returns></returns>public async Task BeginChat(bool keepChatting, Chat chat, string ImageMsg){cancellationTokenSource = new CancellationTokenSource();var tokenx = cancellationTokenSource.Token;Invoke(new Action(() =>{button1.Text = "思考回答中...";}));string message;message = textBox2.Text.TrimEnd('\r', '\n'); // 從文本框讀取用戶輸入的消息if (message == ""){message = ImageMsg;}Clear(); // 清空文本框以便用戶輸入下一條消息Task sendTask = Task.Run(async () =>{if (string.IsNullOrEmpty(message.Trim())){return;}bool isFirstToken = true;try{string mmsf = "";await foreach (var answerToken in chat.SendAsync(message)){// 如果取消了操作,提前退出if (cancellationTokenSource.Token.IsCancellationRequested){continue;}if (answerToken != "<think>" && answerToken != "</think>"){mmsf += answerToken;// 使用Invoke更新UIrichTextBox1.Invoke(new Action(() =>{if (isFirstToken){richTextBox1.Focus();isFirstToken = false;}richTextBox1.AppendText(answerToken.Trim());}));}}string newmsg = "";if (mmsf.Contains("```sql")) {newmsg= FormatSql(mmsf);// 使用Invoke更新UIrichTextBox1.Invoke(new Action(() =>{if (isFirstToken){richTextBox1.Focus();isFirstToken = false;}richTextBox1.AppendText(newmsg.Trim());}));}}catch (OperationCanceledException){// 處理取消操作時的異常Invoke(new Action(() =>{if (button1.Text == "思考回答中..."){button1.Text = "發送";}}));}catch (Exception ex){if (mIsCancel == true){// 捕獲其他類型的異常并記錄Log(Environment.NewLine + $"用戶哥取消了回答", 0, Color.Red);mIsCancel = false;}else{// 捕獲其他類型的異常并記錄Log(Environment.NewLine + $"哎呦出錯了" + ex, 0, Color.Red);}}});await sendTask;Invoke(new Action(() =>{button2.Visible = false; // 隱藏取消按鈕button1.Text = "發送";textBox2.Focus();richTextBox1.AppendText(Environment.NewLine);}));}/// <summary>/// 清空輸入文本框/// </summary>public void Clear(){Invoke(new Action(() =>{textBox2.Clear();}));}/// <summary>/// 更新控件的一些值或者追加文字/// </summary>/// <param name="message"></param>/// <param name="mtype">0:追加文字,1:大模型使用</param>public void Log(string message, int mtype, Color color){if (mtype == 0){Invoke(new Action(() =>{richTextBox1.AppendText(message + Environment.NewLine);textBox2.Focus();}));}else{Invoke(new Action(() =>{label1.Text = message;label1.ForeColor = color;textBox2.Focus();}));}}/// <summary>/// 發送按鈕/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button1_Click(object sender, EventArgs e){if (models.Count == 0){MessageBox.Show("沒有大模型環境,怎么玩啊!");return;}if (button1.Text == "思考回答中..."){MessageBox.Show("正想著呢,別點了爺們");}else{string mm = textBox2.Text;textBox2.Text = "用戶哥:" + mm + Environment.NewLine;Log(textBox2.Text.TrimEnd('\r', '\n'), 0, Color.Blue);var prompt = mm; // 從文本框讀取提示詞var keepChatting = true;var chat = new Chat(ollama, prompt);Invoke(new Action(() =>{richTextBox1.AppendText("deepSeek-R1>:");}));BeginSiKao(keepChatting, chat, "");step = 3;if (button2.Visible == false){button2.Visible = true;}}}/// <summary>/// 取消回答/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button2_Click(object sender, EventArgs e){mIsCancel = true;StopThinking();}public void StopThinking(){cancellationTokenSource?.Cancel(); // 取消當前的操作Invoke(new Action(() =>{button2.Visible = false; // 隱藏取消按鈕button1.Enabled = true; // 恢復發送按鈕textBox2.Focus(); // 讓用戶可以繼續輸入}));}private int mSelectItem = 99;private void listBox1_SelectedIndexChanged(object sender, EventArgs e){mSelectItem = listBox1.SelectedIndex;WorkFololw();}public static string FormatSql(string input){// 移除開頭的 sql 和多余的空格input = input.Trim();// 用正則表達式找到從 sql 開頭到下一個結束符號的 SQL 代碼string pattern = @"`sql(.*?)```";var match = Regex.Match(input, pattern, RegexOptions.Singleline);if (match.Success){// 獲取 sql 語句部分string sql = match.Groups[1].Value.Trim();// 分析 SQL 的每個部分并格式化return FormatSqlServerCreateTable(sql);}return input;}private static string FormatSqlServerCreateTable(string sql){// 分割 SQL 語句sql = sql.Replace("CREATETABLE", "CREATE TABLE").Replace("NOTNULL", "NOT NULL").Replace("VARCHAR", "VARCHAR").Replace("NVARCHAR", "NVARCHAR").Replace("CHECK", "CHECK").Replace("PRIMARYKEY", "PRIMARY KEY").Replace("UNIQUE", "UNIQUE").Replace("CHAR", "CHAR").Replace("DATENOTNULL", "DATE NOT NULL").Replace("TEXT", "TEXT").Replace("--", "-- "); // 確保注釋有一個空格// 添加換行和縮進string formattedSql = "";int indentationLevel = 0;bool insideComment = false;for (int i = 0; i < sql.Length; i++){char currentChar = sql[i];// 檢查是否進入注釋if (i < sql.Length - 1 && sql.Substring(i, 2) == "--"){insideComment = true;}// 增加縮進處理if (currentChar == '('){formattedSql += " (";indentationLevel++;}else if (currentChar == ')'){formattedSql += "\n" + new string(' ', indentationLevel * 4) + ")";indentationLevel--;}else if (currentChar == ','){formattedSql += ",\n" + new string(' ', indentationLevel * 4);}else{if (insideComment){formattedSql += currentChar;if (currentChar == '\n'){insideComment = false;}}else{formattedSql += currentChar;}}}return formattedSql;}}
}
有些地方有些小bug,比如取消思考沒有進行細節的處理,但是不影響正常的使用,
整體的邏輯就是:窗體啟動時候在線程里面進行一個死循環,只要textBox文本框里面出現回車就根據變量step的值來進行對應的操作。目前無法給deepseek發送圖片讓他進行分析,只支持文字對話。斷網也是可以繼續運行的。
如有更好的想法,歡迎大家評論區暢所欲言!