C# WPF本地Deepseek部署

发布于:2025-08-14 ⋅ 阅读:(14) ⋅ 点赞:(0)

在这里插入图片描述
在这里插入图片描述
模型下载地址

using LLama;
using LLama.Common;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace YF_Talk
{
    public partial class MainWindow : Window
    {
        private LLamaWeights _model;
        private LLamaContext _context;
        private InteractiveExecutor _executor;
        private bool _isInitialized = false;

        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            await InitializeModelAsync();
        }

        private async Task InitializeModelAsync()
        {
            try
            {
                ChatBox.Text = "正在加载模型,请稍候...\n";

                // 模型路径 - 确保模型文件放在项目Assets文件夹中
                var modelPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets", "C:\\Users\\Administrator\\Downloads\\DeepSeek-R1-Distill-Qwen-7B-Q4_K_M.gguf");

                // 模型参数 - 更新为最新API
                var parameters = new ModelParams(modelPath)
                {
                    ContextSize = 2048, // 上下文大小
                    GpuLayerCount = 20, // 使用GPU层数(根据你的GPU调整)
                };

                // 加载模型
                _model = await Task.Run(() => LLamaWeights.LoadFromFile(parameters));
                _context = await Task.Run(() => new LLamaContext(_model, parameters));
                _executor = new InteractiveExecutor(_context);

                _isInitialized = true;
                ChatBox.Text += "模型加载完成!现在可以开始聊天了。\n\n";
                InputBox.Focus();
            }
            catch (Exception ex)
            {
                ChatBox.Text += $"初始化失败: {ex.Message}\n";
                MessageBox.Show($"模型加载失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private async void SendButton_Click(object sender, RoutedEventArgs e)
        {
            await ProcessInputAsync();
        }

        private async void InputBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                await ProcessInputAsync();
            }
        }

        private async Task ProcessInputAsync()
        {
            if (!_isInitialized)
            {
                ChatBox.Text += "模型尚未加载完成,请稍候...\n";
                return;
            }

            var input = InputBox.Text.Trim();
            if (string.IsNullOrWhiteSpace(input))
                return;

            // 显示用户输入
            ChatBox.Text += $"你: {input}\n";
            InputBox.Clear();

            try
            {
                // 准备推理参数 - 更新为最新API
                var inferenceParams = new InferenceParams
                {
                    //Temperature = 0.6f,
                    AntiPrompts = new[] { "你:", "User:" },
                    MaxTokens = 512,
                };

                // 显示AI正在思考
                ChatBox.Text += "AI: ";

                // 执行推理
                await foreach (var text in _executor.InferAsync(input, inferenceParams))
                {
                    // 逐步显示结果
                    ChatBox.Text += text;
                    ChatBox.ScrollToEnd();
                }

                ChatBox.Text += "\n\n";
                ChatBox.ScrollToEnd();
            }
            catch (Exception ex)
            {
                ChatBox.Text += $"发生错误: {ex.Message}\n";
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            _context?.Dispose();
            _model?.Dispose();
            base.OnClosed(e);
        }
    }
}
<Window x:Class="YF_Talk.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:YF_Talk"
        mc:Ignorable="d"
        Title="YF Talk - DeepSeek Chat" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <ScrollViewer Grid.Row="0" VerticalScrollBarVisibility="Auto">
            <TextBox x:Name="ChatBox" IsReadOnly="True" TextWrapping="Wrap" 
                     VerticalScrollBarVisibility="Auto" FontSize="14" Padding="10"/>
        </ScrollViewer>

        <Grid Grid.Row="1" Margin="5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>

            <TextBox x:Name="InputBox" Grid.Column="0" Margin="0,0,5,0" 
                     VerticalContentAlignment="Center" FontSize="14" KeyDown="InputBox_KeyDown"/>

            <Button x:Name="SendButton" Grid.Column="1" Content="发送" 
                    Width="80" Click="SendButton_Click"/>
        </Grid>
    </Grid>
</Window>

网站公告

今日签到

点亮在社区的每一天
去签到