C# 根据Ollama+DeepSeekR1开发本地AI辅助办公助手

在上一篇《访问DeepSeekR1本地部署API服务搭建自己的AI办公助手》中,我们通过通过Ollama提供的本地API接口用Python实现了一个简易的AI办公助手,但是需要运行Py脚本,还比较麻烦,下面我们用C#依据Ollama提供的API接口开发一个本地AI辅助办公助手.

代码如下:

需要引用Newtonsoft.Json.dll和Winform皮肤插件OwnUI.dll去掉也没什么影响

cs 复制代码
using System;
using System.Net.Http;
using System.Windows.Forms;
using OwnUI;
using Newtonsoft.Json.Linq;

namespace OllamaChat
{
    public partial class Form1 : UIForm
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            uitb_requesturl.Text = "http://127.0.0.1:11434/api/chat";
            uitb_question.Text = uitb_answers.Text = "";
        }
        
        private void uitb_question_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                string json = "{\"model\":\"deepseek-r1:1.5b\",\"messages\": [{\"role\":\"user\",\"content\":\"" + uitb_question.Text + "\"}],\"stream\":false}";
                string restext = post(uitb_requesturl.Text, json);
                JObject obj = JObject.Parse(restext);
                string message = obj["message"].ToString();
                if (string.IsNullOrEmpty(message) == false)
                {
                    obj = JObject.Parse(message);
                    string content = obj["content"].ToString();
                    uitb_answers.Text = content;
                }
            }
        }
        /// <summary>
        /// https提交
        /// </summary>
        /// <param name="url"></param>
        /// <param name="jsonParas"></param>
        /// <returns></returns>
        public static String post(String url, String jsonParas)
        {
            String responseBody = String.Empty;
            using (HttpClient client = new HttpClient())
            {
                HttpContent httpContent = new StringContent(jsonParas);
                httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                HttpResponseMessage response = client.PostAsync(url, httpContent).GetAwaiter().GetResult();
                response.EnsureSuccessStatusCode();
                responseBody = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
            }
            //Console.WriteLine(responseBody);
            return responseBody;
        }
    }
}
相关推荐
虾球xz16 分钟前
CppCon 2018 学习:EFFECTIVE REPLACEMENT OF DYNAMIC POLYMORPHISM WITH std::variant
开发语言·c++·学习
Allen_LVyingbo21 分钟前
Python常用医疗AI库以及案例解析(2025年版、上)
开发语言·人工智能·python·学习·健康医疗
小哈龙25 分钟前
裸仓库 + Git Bash 搭建 本地 Git 服务端与客户端
开发语言·git·bash
唐青枫32 分钟前
C#.NET log4net 详解
c#·.net
G探险者44 分钟前
《如何在 Spring 中实现 MQ 消息的自动重连:监听与发送双通道策略》
java·开发语言·rpc
weixin_437398211 小时前
转Go学习笔记
linux·服务器·开发语言·后端·架构·golang
StrongerIrene2 小时前
rs build 的process.env的值undefined解决方案
开发语言·javascript·ecmascript
风逸hhh2 小时前
python打卡day58@浙大疏锦行
开发语言·python
让我们一起加油好吗2 小时前
【C++】list 简介与模拟实现(详解)
开发语言·c++·visualstudio·stl·list
Q_970956392 小时前
java+vue+SpringBoo足球社区管理系统(程序+数据库+报告+部署教程+答辩指导)
java·开发语言·数据库