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

Unity 接入本地部署的DeepSeek

接入本地部署的DeepSeek🌵

  • 🍱本地部署DeepSeek
  • 🍝 API 调用

🍱本地部署DeepSeek

参考这里👈

🍝 API 调用

向Ollama发送post请求,接收结果。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
using Newtonsoft.Json;
using System.Linq;
namespace ZYF
{
    public class DeepSeek : MonoBehaviour
    {
        public class Response {
            public string model;
            public string created_at;
            public string response;
            public bool done;
        }
        public static DeepSeek Instance;
        [SerializeField]
        private string baseUrl = "http://localhost:11434/api/generate";
        [SerializeField]
        private string model= "deepseek-r1:7b";
        [SerializeField]
        private string prefix = "推测下下面内容的真实表达内容后再回答,并且回答不要带特殊字符:";
        public UnityEvent<string> onGetResponseEvent = new UnityEvent<string>();
        [SerializeField]
        private string currentRequestContent;

        private void Awake()
        {
            Instance = this;
        }
        
        internal void Request(string content)
        {
            this.currentRequestContent = $"{prefix}{content}";
            StopRequest();
            StartCoroutine(nameof(SendPostRequest));
        }

        public void StopRequest()
        {
            StopCoroutine(nameof(SendPostRequest));
            Debug.Log($"取消请求");
        }

        private IEnumerator SendPostRequest()
        {
            var data = new{model=this.model,prompt=currentRequestContent};
            var json = JsonConvert.SerializeObject(data);
            Debug.Log(json);
            UnityWebRequest request = UnityWebRequest.Post(uri:baseUrl,postData: json, contentType: "application/json");
            yield return request.SendWebRequest();
            if (request.result == UnityWebRequest.Result.Success)
            {
                var res = request.downloadHandler.text;
                res = ToJsonData(res);

                var datas = JsonConvert.DeserializeObject<List<Response>>(res);
                if (datas != null)
                {
                    var result = GetResponseContent(datas);

                    result = RemoveThinkContents(result);

                    onGetResponseEvent?.Invoke(result);
                }
            }
            else {
                Debug.LogError($"request error:{request.error}",gameObject);
            }
        }

        private string ToJsonData(string res)
        {
            res = res.Replace("}", "},");
            res = res.Substring(0, res.Length - 1);
            res = $"[{res}]";
            Debug.Log(res);
            return res;
        }

        private string GetResponseContent(List<Response> datas)
        {
            var result = datas.Where(d => d.done == false).Select(d => d.response).Aggregate((current, next) => current + next);
            Debug.Log(result);
            return result;
        }

        private string RemoveThinkContents(string content)
        {
            int tIndex = content.LastIndexOf("</think>") + 8;
            content = content.Substring(tIndex);
            content = content.Trim();
            Debug.Log($"去掉think内容:{content}");
            return content;
        }
    }
}


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

相关文章:

  • pytest的bug
  • (十 九)趣学设计模式 之 中介者模式!
  • Leetcode 54: 螺旋矩阵
  • 大白话实战docker
  • 计算机基础面试(数据库)
  • 基于springboot+vue3图书借阅管理系统
  • Linux网络相关概念和重要知识(1)(网络协议、网络通信)
  • SEKI —— 基于大型语言模型的自进化与知识启发式神经架构搜索
  • SSM家谱管理系统
  • 蓝桥杯备考:动态规划入门题目之下楼梯问题
  • 华硕电脑开启电池保养模式的方法
  • 用Python+Flask打造可视化武侠人物关系图生成器:从零到一的实战全记录
  • Linux 下使用vmstat监控系统性能
  • Socket是什么接口
  • java2025springboot面试题第二弹
  • C#保存应用启动位置例子 - 开源研究系列文章
  • uniapp笔记-项目中使用iconfont图标
  • vue3之echarts仪表盘
  • leetcode_字典树 140. 单词拆分 II
  • Leetcode 206 -反转链表