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

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.mrgr.cn/news/92392.html

相关文章:

  • leetcode_动态规划/递归 509. 斐波那契数
  • 计算机三级网络技术备考
  • Python PyCharm DeepSeek接入
  • Java 大视界 —— Java 大数据在智慧能源微电网能量管理中的关键技术(100)
  • 【生成模型】【ComfyUI(三)】使用WebAPI批量调用ComfyUI
  • 浅析DeepSeek在商业银行的应用
  • 将VsCode变得顺手好用(1
  • 学习笔记05——HashMap实现原理及源码解析(JDK8)
  • 架构思维:架构的演进之路
  • docker-compose install dify(deepseek)
  • Spring boot中的@ConfigurationProperties注解
  • IP------PPP协议
  • Apache DolphinScheduler系列1-单节点部署及测试报告
  • haproxy基本配置详解
  • 网络基础知识-2
  • go基础语法
  • Java常见设计模式(中):结构型模式
  • 最小化重投影误差求解PnP
  • 【蓝桥杯】第十五届省赛大学真题组真题解析
  • SQL注入(order by,limit),seacms的报错注入以及系统库的绕过