C#创建AI项目

文章目录

申请模型

阿里平台申请模型

先登录

创建apikey

这是deepseek所有模型

创建.net8.0的WPF项目

nuget包

Microsoft.SemanticKernel 1.44.0

编写AI类

代码中apikey需要用你自己的

csharp 复制代码
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp1
{
    /// <summary>
    /// ai 类
    /// </summary>
    public class AI
    {
        public AI()
        {
            IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
#pragma warning disable SKEXP0010
            kernelBuilder.AddOpenAIChatCompletion("deepseek-v3", new Uri("https://dashscope.aliyuncs.com/compatible-mode/v1"), "apikey");//阿里平台
            Kernel = kernelBuilder.Build();
            ChatHistory = new ChatHistory();
            ChatHistory.AddSystemMessage("你是一个乐于解答各种问题的助手,你的任务是为用户提供专业、准确、有见地的建议。");
        }
        public Kernel Kernel { get; set; }

        /// <summary>
        /// 聊天记录
        /// </summary>
        public ChatHistory ChatHistory { get; set; }

        /// <summary>
        /// 向AI提问
        /// </summary>
        /// <param name="questions">问题</param>
        /// <param name="action">字符流回调函数(委托中string代表的是AI回答的字符)</param>
        /// <returns></returns>
        public async Task<string> AskAIQuestions(string questions, Action<string> action)
        {
            ChatHistory.AddUserMessage(questions);
            return await AskAIQuestions(ChatHistory, action);
        }
        /// <summary>
        /// 向AI提问
        /// </summary>
        /// <param name="chatHistory">聊天记录</param>
        /// <param name="action">字符流回调函数</param>
        /// <returns></returns>
        public async Task<string> AskAIQuestions(ChatHistory chatHistory, Action<string> action)
        {
            //task.Result可以拿值
            var chatService = Kernel.GetRequiredService<IChatCompletionService>();
            string msg = "";
            await foreach (StreamingChatMessageContent chatUpdate in chatService.GetStreamingChatMessageContentsAsync(chatHistory))
            {
                if (chatUpdate.Content is { Length: > 0 })
                {
                    msg += chatUpdate.Content;
                    action(chatUpdate.Content);
                    Thread.Sleep(10);
                }
            }
            return msg;
        }
    }
}

编写一个简单的交互界面

MainWindow.xaml

xml 复制代码
<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBlock Height="400" x:Name="textBlock1" Text="{Binding Path=Text, Mode=OneWay}" />
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBox x:Name="textBox1" Text="{Binding Path=Text, Mode=TwoWay}" />
            <Button Grid.Column="1" x:Name="button1" Content="发送" Click="button1_Click" />
        </Grid>
    </Grid>
</Window>

MainWindow.xaml.cs

csharp 复制代码
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string text = textBox1.Text;
            textBox1.Text = "";
            textBlock1.Text = "";
            AI aI = new AI();
            aI.AskAIQuestions(text, (string str) =>
            {
                textBlock1.Text += str;
            });
        }
    }
}
相关推荐
咨询QQ2769988536 分钟前
V-REP小车项目+匹配文档,基于V-REP与MATLAB联合仿真,小车能够完成循迹、避障、走...
开发语言
深蓝海拓39 分钟前
opencv的模板匹配(Template Matching)学习笔记
人工智能·opencv·计算机视觉
美林数据Tempodata1 小时前
李飞飞最新论文深度解读:从语言到世界,空间智能将重写AI的未来十年
人工智能·ai·空间智能
东哥说-MES|从入门到精通1 小时前
数字化部分内容 | 十四五年规划和2035年远景目标纲要(新华社正式版)
大数据·人工智能·数字化转型·mes·数字化工厂·2035·十四五规划
小殊小殊1 小时前
DeepSeek为什么这么慢?
人工智能·深度学习
豆沙沙包?1 小时前
2025年--Lc293-784. 字母大小写全排列(回溯)--java版
java·开发语言
极客BIM工作室1 小时前
从静态到动态:Sora与文生图潜在扩散模型的技术同异与AIGC演进逻辑
人工智能·aigc
松果财经1 小时前
长沙的青年友好,五年见“城”心
人工智能
秋邱1 小时前
智启未来:AGI 教育融合 × 跨平台联盟 × 个性化空间,重构教育 AI 新范式开篇:一场 “教育 ×AI” 的范式革命
人工智能·python·重构·推荐算法·agi