c# OllamaSharp DeepSeek 有上下文可连续对话

 使用 ollama 安装完 deepseek 后, nuget 安装 OllamaSharp ,使用如下代码可实现完整对话,我使用的模型是 DeepSeek-Coder-V2-Lite,以下是核心代码:

public class DeepSeekChat
{
    IChatClient chatClient = new OllamaApiClient("http://127.0.0.1:11434", "DeepSeek-Coder-V2-Lite:latest");
    List<ChatMessage> chatHistory = new();
    CancellationTokenSource cts = null;
    CancellationToken? cancellationToken = null;

    public delegate void PartialResponse(string text);

    public DeepSeekChat() 
    {
        //chatHistory.Add(new ChatMessage(Microsoft.Extensions.AI.ChatRole.System, "你是一个代码助手,请用中文回答所有问题。"));
    }

    public async Task Chat(string prompt, PartialResponse partialResponse)
    {
        chatHistory.Add(new ChatMessage(Microsoft.Extensions.AI.ChatRole.User, prompt));

        try
        {
            var response = string.Empty;
            cts = new CancellationTokenSource();
            cancellationToken = cts.Token;

            await foreach (var item in
                chatClient.GetStreamingResponseAsync(chatHistory,null,cancellationToken.Value))
            {
                // 在此处调用 ThrowIfCancellationRequested
                cancellationToken?.ThrowIfCancellationRequested();

                var content = (item.Text ?? string.Empty).Replace("<think>", "").Replace("</think>", "");

                if (content.Length > 0)
                    partialResponse(content);

                System.Diagnostics.Debug.Print(item.Text);

                response += item.Text;
            }
            chatHistory.Add(new ChatMessage(Microsoft.Extensions.AI.ChatRole.Assistant, response));
        }
        catch (Exception ex)
        {
            chatHistory.RemoveAt(chatHistory.Count - 1);
            partialResponse(Environment.NewLine + $"Error: {ex.Message}" + Environment.NewLine);
        }
    }

    public void Cancel()
    {
        cts?.Cancel();
        cts = null;
    }
}

使用 wpf markdig:MarkdownViewer 来显示输出:
private DeepSeekChat chat = new DeepSeekChat();

async Task startRequest(string question)
{
    PartialResponse pr = delegate (string text)
    {
        Dispatcher.Invoke(async () =>
        {
            await Task.Delay(1);
            if((markdownViewer.Markdown.EndsWith(Environment.NewLine) 
                || markdownViewer.Markdown.EndsWith("\n")) && text.StartsWith(" "))
                // Markdown行首空格会被忽略,所以用零宽空格代替行首第一个空格,以便保留缩进
                markdownViewer.Markdown += "\u200B" + text;
            else
                markdownViewer.Markdown += text;
        });
    };

    Debug.Print("开始请求");

    await chat.Chat(question, pr);

    Debug.Print("结束请求");
}

请求:

Task.Run(async () =>
{
    try
    {
        await startRequest("用C#写一个冒泡排序算法?");
    }
    catch (Exception ex)
    {
        Dispatcher.Invoke(() =>
        {
            markdownViewer.Markdown += "  \n" + ex.Message;
        });
    }
    finally
    {
        Dispatcher.Invoke(() =>
        {
            markdownViewer.Markdown += "  \n";
        });
    }
});

 以下是输出效果截图:

 

posted on 2025-03-06 09:49  空明流光  阅读(264)  评论(0)    收藏  举报

导航