当前位置: 首页 > news >正文

C# WPF本地Deepseek部署

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

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");// 模型参数 - 更新为最新APIvar 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{// 准备推理参数 - 更新为最新APIvar 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>
http://www.lryc.cn/news/619837.html

相关文章:

  • WPF 开发的瑞士军刀:Prism 框架从入门到精通指南
  • webrtc弱网-QualityRampUpExperimentHelper类源码分析与算法原理
  • VMD+皮尔逊+降噪+重构(送报告+PPT)Matlab程序
  • 在前端js中使用jsPDF或react-to-pdf生成pdf文件时,不使用默认下载,而是存储到服务器
  • 存量竞争下的破局之道:品牌与IP的双引擎策略|创客匠人
  • 基于elk实现分布式日志
  • ELK开启安全策略
  • web安全开发,在线%射击比赛管理%系统开发demo,基于html,css,jquery,python,django,三层mysql数据库
  • 【微实验】基频提取的MATLAB实现(优化版)
  • 基于MATLAB的图像处理与圆弧拟合技术
  • 深度学习(6):参数初始化
  • Jenkins(集群与流水线配置)
  • 卷积神经网络(CNN):卷积和池化
  • 常用 MaxKB 函数库(HTTP、MYSQL)
  • Day 39: 图像数据与显存
  • 智算赋能:移动云助力“世界一流数据强港”建设之路
  • 深度学习·ExCEL
  • RK3568项目(十五)--linux驱动开发之进阶驱动
  • Spring Boot (v3.2.12) + application.yml + jasypt 数据源加密连接设置实例
  • Java Stream API 中常用方法复习及项目实战示例
  • AR技术赋能风电组装:效率提升30%,错误率降低50%
  • 华为悦盒EC6108V9-1+4G版-盒子有【蓝色USB接口】的特殊刷机说明
  • UniApp开发常见问题及解决办法
  • RabbitMQ面试精讲 Day 21:Spring AMQP核心组件详解
  • FluxApi - 使用Spring进行调用Flux接口
  • 后端Web实战-MySQL数据库
  • 【SpringBoot系列-01】Spring Boot 启动原理深度解析
  • 力扣121:买卖股票的最佳时机
  • 敲响变革的钟声:AI 如何重塑前端开发的基础认知
  • Java毕业设计选题推荐 |基于SpringBoot的水产养殖管理系统 智能水产养殖监测系统 水产养殖小程序