Selaa lähdekoodia

deepseek 配置独立保存不推送
Admin.NET\Admin.NET.Application\Configuration\DeepSeek.json
Web\.env.local

Cyrus Zhou 7 kuukautta sitten
vanhempi
commit
5272b0b84b

+ 1 - 0
.gitignore

@@ -42,3 +42,4 @@ node_modules/
 .DS_Store
 /Admin.NET/Admin.NET.Web.Entry/Admin.NET.db-journal
 /Admin.NET/Admin.NET.sln.DotSettings.user
+Admin.NET/Admin.NET.Application/Configuration/DeepSeek.json

+ 9 - 0
Admin.NET/Admin.NET.Application/Configuration/DeepSeek.example

@@ -0,0 +1,9 @@
+{
+  "$schema": "https://gitee.com/dotnetchina/Furion/raw/v4/schemas/v4/furion-schema.json",
+
+  "DeepSeekSettings": {
+    "SourceLang": "zh-cn",
+    "ApiUrl": "https://api.deepseek.com/v1/chat/completions",
+    "ApiKey": "你的 API KEY"
+  }
+}

+ 23 - 0
Admin.NET/Admin.NET.Core/Option/DeepSeekOptions.cs

@@ -0,0 +1,23 @@
+// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
+//
+// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
+//
+// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
+
+namespace Admin.NET.Core;
+public sealed class DeepSeekOptions: IConfigurableOptions
+{
+    /// <summary>
+    /// 源语言
+    /// </summary>
+    public string SourceLang { get; set; }
+    /// <summary>
+    /// Api地址
+    /// </summary>
+    public string ApiUrl { get; set; }
+    /// <summary>
+    /// API KEY
+    /// </summary>
+    public string ApiKey { get; set; }
+
+}

+ 14 - 11
Admin.NET/Admin.NET.Core/Service/LangText/SysLangTextService.cs

@@ -98,7 +98,7 @@ public partial class SysLangTextService : IDynamicApiController, ITransient
         var entity = input.Adapt<SysLangText>();
         await _sysLangTextRep.AsUpdateable(entity)
         .ExecuteCommandAsync();
-        _sysLangTextCacheService.UpdateCache(entity.EntityName,entity.FieldName,entity.EntityId,entity.LangCode,entity.Content);
+        _sysLangTextCacheService.UpdateCache(entity.EntityName, entity.FieldName, entity.EntityId, entity.LangCode, entity.Content);
     }
 
     /// <summary>
@@ -111,7 +111,7 @@ public partial class SysLangTextService : IDynamicApiController, ITransient
     public async Task Delete(DeleteSysLangTextInput input)
     {
         var entity = await _sysLangTextRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
-        
+
         await _sysLangTextRep.DeleteAsync(entity);   //真删除
         _sysLangTextCacheService.DeleteCache(entity.EntityName, entity.FieldName, entity.EntityId, entity.LangCode);
     }
@@ -282,9 +282,6 @@ public partial class SysLangTextService : IDynamicApiController, ITransient
             return stream;
         }
     }
-    private const string SOURCE_LANG = "zh-cn";
-    private const string API_URL = "https://api.deepseek.com/v1/chat/completions";
-    private const string DEEPSEEK_API_KEY = "你的 API KEY";
     /// <summary>
     /// DEEPSEEK 翻译接口
     /// </summary>
@@ -293,7 +290,13 @@ public partial class SysLangTextService : IDynamicApiController, ITransient
     [ApiDescriptionSettings(Name = "AiTranslateText"), HttpPost]
     public async Task<string> AiTranslateText(AiTranslateTextInput input)
     {
-        if (string.IsNullOrEmpty(DEEPSEEK_API_KEY))
+        // 需要先把DeepSeek.example复制改名为DeepSeek.json文件,添加你的 API KEY
+        var deepSeekOptions = App.GetConfig<DeepSeekOptions>("DeepSeekSettings", true);
+        if (deepSeekOptions == null)
+        {
+            throw new InvalidOperationException("DeepSeek.json文件 未定义");
+        }
+        if (string.IsNullOrEmpty(deepSeekOptions.ApiKey))
         {
             throw new InvalidOperationException("环境变量 DEEPSEEK_API_KEY 未定义");
         }
@@ -301,10 +304,10 @@ public partial class SysLangTextService : IDynamicApiController, ITransient
         using (HttpClient client = new HttpClient())
         {
             // 构建请求头
-            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {DEEPSEEK_API_KEY}");
+            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {deepSeekOptions.ApiKey}");
 
             // 构建系统提示词
-            string systemPrompt = BuildSystemPrompt(input.TargetLang);
+            string systemPrompt = BuildSystemPrompt(deepSeekOptions.SourceLang, input.TargetLang);
 
             // 构建请求体
             var requestBody = new
@@ -324,7 +327,7 @@ public partial class SysLangTextService : IDynamicApiController, ITransient
             var content = new StringContent(json, Encoding.UTF8, "application/json");
 
             // 发送请求
-            HttpResponseMessage response = await client.PostAsync(API_URL, content);
+            HttpResponseMessage response = await client.PostAsync(deepSeekOptions.ApiUrl, content);
 
             // 处理响应
             string responseBody = await response.Content.ReadAsStringAsync();
@@ -379,12 +382,12 @@ public partial class SysLangTextService : IDynamicApiController, ITransient
     /// </summary>
     /// <param name="targetLang"></param>
     /// <returns></returns>
-    private static string BuildSystemPrompt(string targetLang)
+    private static string BuildSystemPrompt(string sourceLang, string targetLang)
     {
         return $@"作为企业软件系统专业翻译,严格遵守以下铁律:
 
 ■ 核心原则
-1. 严格逐符号翻译({SOURCE_LANG}→{targetLang})
+1. 严格逐符号翻译({sourceLang}→{targetLang})
 2. 禁止添加/删除/改写任何内容
 3. 保持批量翻译的编号格式
 

+ 1 - 0
Admin.NET/Admin.NET.Web.Core/ProjectOptions.cs

@@ -39,6 +39,7 @@ public static class ProjectOptions
         services.AddConfigurableOptions<EventBusOptions>();
         services.AddConfigurableOptions<AlipayOptions>();
         services.AddConfigurableOptions<CDConfigOptions>();
+        services.AddConfigurableOptions<DeepSeekOptions>();
         services.Configure<IpRateLimitOptions>(App.Configuration.GetSection("IpRateLimiting"));
         services.Configure<IpRateLimitPolicies>(App.Configuration.GetSection("IpRateLimitPolicies"));
         services.Configure<ClientRateLimitOptions>(App.Configuration.GetSection("ClientRateLimiting"));

+ 2 - 1
Web/.env

@@ -13,4 +13,5 @@ VITE_PUBLIC_PATH =
 # 国密SM公钥
 VITE_SM_PUBLIC_KEY = "0484C7466D950E120E5ECE5DD85D0C90EAA85081A3A2BD7C57AE6DC822EFCCBD66620C67B0103FC8DD280E36C3B282977B722AAEC3C56518EDCEBAFB72C5A05312"
 # 获取API KEY: https://platform.deepseek.com/api_keys
-DEEPSEEK_API_KEY = "你的 API KEY"
+# 可以把 DEEPSEEK_API_KEY变量保存到.env.local文件中避免泄露
+# DEEPSEEK_API_KEY = "你的 API KEY" 

+ 1 - 1
Web/.gitignore

@@ -23,4 +23,4 @@ pnpm-debug.log*
 *.sw?
 
 pnpm-lock.yaml
-package-lock.json
+package-lock.json

+ 2 - 1
Web/scripts/translate.cjs

@@ -1,4 +1,5 @@
-require('dotenv').config();
+require('dotenv').config({ path: ".env" });
+require('dotenv').config({ path: ".env.local" });
 const fs = require('fs');
 const path = require('path');
 const API_URL = 'https://api.deepseek.com/v1/chat/completions';