瀏覽代碼

Merge branch 'develop' of http://132.232.92.186:3000/XinXiBu/OA2023 into develop

LEIYI 1 周之前
父節點
當前提交
dfed825540

+ 12 - 1
OASystem/OASystem.Api/OAMethodLib/AutofacRegister.cs

@@ -1,5 +1,7 @@
-using OASystem.Domain.Dtos.Business;
+using Autofac;
+using OASystem.Domain.Dtos.Business;
 using OASystem.Domain.Dtos.Groups;
+using OASystem.Domain.Dtos.KiMi;
 
 namespace OASystem.API.OAMethodLib
 {
@@ -24,6 +26,15 @@ namespace OASystem.API.OAMethodLib
             builder.RegisterInstance<GroupsConfig>(_groupsConfig);
             #endregion
 
+            builder.RegisterInstance(AppSettingsHelper.GetSection("KiMiSetting").Get<KiMiSetting>() ?? new KiMiSetting
+                 {
+                    BaseUrl = AppSettingsHelper.Get("KiMiSetting:BaseUrl"),
+                    Key = AppSettingsHelper.Get("KiMiSetting:Key"),
+                    Model = AppSettingsHelper.Get("KiMiSetting:Model")
+                 })
+                 .As<KiMiSetting>()
+                 .SingleInstance();
+
             #region 团组结束通知短信
             DeleReminderConfig _deleReminderConfig = new DeleReminderConfig();
             _deleReminderConfig.Test = AppSettingsHelper.Get(DeleReminderConfig.KEY, "Test");

+ 15 - 11
OASystem/OASystem.Api/OAMethodLib/KiMiApi/KiMiApi.cs

@@ -1,22 +1,30 @@
 
+using OASystem.Domain.Dtos.Groups;
+using OASystem.Domain.Dtos.KiMi;
 using System.Net.Http.Headers;
 
 namespace OASystem.API.OAMethodLib.KiMiApi
 {
     public class KiMiApiClient
     {
+
+        //读取配置文件
+        private KiMiSetting _kimiSetting { get; set; }
+
         private readonly HttpClient _httpClient;
 
         public KiMiApiClient()
         {
             _httpClient = new HttpClient();
 
+            _kimiSetting = AutofacIocManager.Instance.GetService<KiMiSetting>();
+
             // 设置公共请求头
-            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
+            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _kimiSetting.Key);
         }
 
-        private  string apiKey = "sk-AY1Sv4rLnSUgGGHcC8SGSWYYKzGID7leZJcFfxAYozLC8dIc";
-        private  string baseUrl = "https://api.moonshot.cn/v1";
+        //private  string apiKey = "sk-AY1Sv4rLnSUgGGHcC8SGSWYYKzGID7leZJcFfxAYozLC8dIc";
+        //private  string baseUrl = "https://api.moonshot.cn/v1";
 
         public async Task<string> UploadFileAsync(IFormFile file)
         {
@@ -39,7 +47,7 @@ namespace OASystem.API.OAMethodLib.KiMiApi
                 { new StringContent("file-extract"), "purpose" }
             };
 
-            HttpResponseMessage fileResponse = await _httpClient.PostAsync($"{baseUrl}/files", fileFormData);
+            HttpResponseMessage fileResponse = await _httpClient.PostAsync($"{_kimiSetting.BaseUrl}/files", fileFormData);
             string fileResponseContent = await fileResponse.Content.ReadAsStringAsync();
 
             if (!fileResponse.IsSuccessStatusCode)
@@ -50,7 +58,7 @@ namespace OASystem.API.OAMethodLib.KiMiApi
             dynamic fileObject = JsonConvert.DeserializeObject(fileResponseContent);
             string fileId = fileObject.id;
 
-            HttpResponseMessage contentResponse = await _httpClient.GetAsync($"{baseUrl}/files/{fileId}/content");
+            HttpResponseMessage contentResponse = await _httpClient.GetAsync($"{_kimiSetting.BaseUrl}/files/{fileId}/content");
             string fileContentText = await contentResponse.Content.ReadAsStringAsync();
 
             if (!contentResponse.IsSuccessStatusCode)
@@ -80,13 +88,9 @@ namespace OASystem.API.OAMethodLib.KiMiApi
 
         public async Task<string> SeedMessage(List<SeedMessages> messages)
         {
-
-            //读取配置文件
-            var kimiModel = AppSettingsHelper.Get("KiMiModel");
-
             var completionRequest = new
             {
-                model = kimiModel,
+                model = _kimiSetting.Model,
                 messages = messages.Select(x=> new
                 {
                     Role = StringEnumHelper.ToStringValue(x.Role),
@@ -98,7 +102,7 @@ namespace OASystem.API.OAMethodLib.KiMiApi
             var completionJson = JsonConvert.SerializeObject(completionRequest);
             var completionContent = new StringContent(completionJson, Encoding.UTF8, "application/json");
 
-            HttpResponseMessage completionResponse = await _httpClient.PostAsync($"{baseUrl}/chat/completions", completionContent);
+            HttpResponseMessage completionResponse = await _httpClient.PostAsync($"{_kimiSetting.BaseUrl}/chat/completions", completionContent);
             string completionResponseContent = await completionResponse.Content.ReadAsStringAsync();
 
             if (!completionResponse.IsSuccessStatusCode)

+ 5 - 1
OASystem/OASystem.Api/appsettings.json

@@ -436,5 +436,9 @@
       ]
     }
   ],
-  "KiMiModel": "moonshot-v1-8k"
+  "KiMiSetting": {
+    "Model": "moonshot-v1-8k",
+    "BaseUrl": "https://api.moonshot.cn/v1",
+    "Key": "sk-AY1Sv4rLnSUgGGHcC8SGSWYYKzGID7leZJcFfxAYozLC8dIc"
+  }
 }

+ 26 - 0
OASystem/OASystem.Domain/Dtos/KiMi/KiMiSetting.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace OASystem.Domain.Dtos.KiMi
+{
+    public class KiMiSetting
+    {
+        /// <summary>
+        /// 模型名称
+        /// </summary>
+        public string Model { get; set; }
+
+        /// <summary>
+        /// 基础URL
+        /// </summary>
+        public string BaseUrl { get; set; }
+
+        /// <summary>
+        /// 密钥
+        /// </summary>
+        public string Key { get; set; }
+    }
+}