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

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

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

代码如下:

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

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;
        }
    }
}


http://www.kler.cn/a/562542.html

相关文章:

  • Node.js 中 fs 模块的高级用法
  • Python入门 — 类
  • SQL: DDL,DML,DCL,DTL,TCL,
  • 如何使用VMware创建虚拟机
  • 解释 React 中的 JSX 语法,如何编译成 React.createElement的过程?
  • Vue.js 测试 Vuex 和 Vue Router
  • 计算机网络模型-TCP/IP协议簇
  • 创建型模式 - 原型模式 (Prototype Pattern)
  • 2025面试Go真题第一场
  • 游戏引擎学习第123天
  • MyBatis简明教程
  • es-head(es库-谷歌浏览器插件)
  • Git系列之Git Reset
  • 量子计算如何改变加密技术:颠覆与变革的前沿
  • MLops:可解释深度神经网络训练平台Neural Designer ®
  • HBase与传统数据库的区别:为什么选择它来处理大数据?
  • MySQL 根据条件多值更新
  • 中兴B863AV3.2-T/B863AV3.1-T2/B863AV3.1-T2K_电信高安_S905L3A-B_安卓9.0_线刷固件包
  • [250224] Yaak 2.0:Git集成、WebSocket支持、OAuth认证等 | Zstandard v1.5.7 发布
  • Redis面试知识点总结