Procházet zdrojové kódy

KIMI处理客户名单

yuanrf před 1 týdnem
rodič
revize
7e44fad049

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 92 - 0
OASystem/OASystem.Api/Controllers/GroupsController.cs


+ 142 - 0
OASystem/OASystem.Api/OAMethodLib/KiMiApi/KiMiApi.cs

@@ -0,0 +1,142 @@
+
+using System.Net.Http.Headers;
+
+namespace OASystem.API.OAMethodLib.KiMiApi
+{
+    public class KiMiApiClient
+    {
+        private readonly HttpClient _httpClient;
+
+        public KiMiApiClient()
+        {
+            _httpClient = new HttpClient();
+
+            // 设置公共请求头
+            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
+        }
+
+        private  string apiKey = "sk-AY1Sv4rLnSUgGGHcC8SGSWYYKzGID7leZJcFfxAYozLC8dIc";
+        private  string baseUrl = "https://api.moonshot.cn/v1";
+
+        public async Task<string> UploadFileAsync(IFormFile file)
+        {
+            string message = string.Empty;
+
+            byte[] fileBytes = null;
+
+            using (var memoryStream = new MemoryStream())
+            {
+                await file.CopyToAsync(memoryStream);
+                fileBytes = memoryStream.ToArray();
+            }
+
+            // 上传文件
+            var fileContent = new ByteArrayContent(fileBytes);
+
+            var fileFormData = new MultipartFormDataContent
+            {
+                { fileContent, "file", file.FileName },
+                { new StringContent("file-extract"), "purpose" }
+            };
+
+            HttpResponseMessage fileResponse = await _httpClient.PostAsync($"{baseUrl}/files", fileFormData);
+            string fileResponseContent = await fileResponse.Content.ReadAsStringAsync();
+
+            if (!fileResponse.IsSuccessStatusCode)
+            {
+                throw new Exception($"Failed to upload file: {fileResponseContent}");
+            }
+
+            dynamic fileObject = JsonConvert.DeserializeObject(fileResponseContent);
+            string fileId = fileObject.id;
+
+            HttpResponseMessage contentResponse = await _httpClient.GetAsync($"{baseUrl}/files/{fileId}/content");
+            string fileContentText = await contentResponse.Content.ReadAsStringAsync();
+
+            if (!contentResponse.IsSuccessStatusCode)
+            {
+                throw new Exception($"Failed to get file content: {fileContentText}");
+            }
+
+            message = fileContentText;
+
+
+            return message;
+        }
+
+        public async Task<List<SeedMessages>> UploadFilesAsync(List<IFormFile> files)
+        {
+            List<SeedMessages> result = new List<SeedMessages>();
+
+            foreach (IFormFile file in files)
+            {
+                var fileConter = await this.UploadFileAsync(file);
+                result.Add(new SeedMessages() { Role = KimiRole.system , Content = fileConter });
+            }
+
+
+            return result;
+        }
+
+        public async Task<string> SeedMessage(List<SeedMessages> messages)
+        {
+
+            //读取配置文件
+            var kimiModel = AppSettingsHelper.Get("KiMiModel");
+
+            var completionRequest = new
+            {
+                model = kimiModel,
+                messages = messages.Select(x=> new
+                {
+                    Role = StringEnumHelper.ToStringValue(x.Role),
+                    x.Content
+                }),
+                temperature = 1
+            };
+
+            var completionJson = JsonConvert.SerializeObject(completionRequest);
+            var completionContent = new StringContent(completionJson, Encoding.UTF8, "application/json");
+
+            HttpResponseMessage completionResponse = await _httpClient.PostAsync($"{baseUrl}/chat/completions", completionContent);
+            string completionResponseContent = await completionResponse.Content.ReadAsStringAsync();
+
+            if (!completionResponse.IsSuccessStatusCode)
+            {
+                throw new Exception($"Failed to seed message: {completionResponseContent}");
+            }
+
+            // 解析返回的完成结果
+            var completion = JObject.Parse(completionResponseContent);
+            string reply = completion["choices"][0]["message"].ToString();
+            return reply;
+        }
+    }
+
+    public class SeedMessages
+    {
+        public KimiRole Role { get; set; }
+
+        public string Content { get; set; }
+    }
+
+    public enum KimiRole
+    {
+        system,
+        user
+    }
+
+    public static class StringEnumHelper
+    {
+        public static string ToStringValue(KimiRole value)
+        {
+            return value switch
+            {
+                KimiRole.system => "system",
+                KimiRole.user => "user",
+                _ => throw new ArgumentOutOfRangeException(nameof(value), value, null)
+            };
+        }
+    }
+
+}

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

@@ -411,6 +411,10 @@
         {
           "Id": 334,
           "Name": "朱琳"
+        },
+        {
+          "Id": 325,
+          "Name": "陈策"
         }
       ]
     },
@@ -423,5 +427,6 @@
         }
       ]
     }
-  ]
+  ],
+  "KiMiModel": "moonshot-v1-8k"
 }

+ 2 - 1
OASystem/OASystem.Domain/Dtos/Groups/TourClientListDto.cs

@@ -1,4 +1,5 @@
-using Newtonsoft.Json;
+using Microsoft.AspNetCore.Http;
+using Newtonsoft.Json;
 using OASystem.Domain.Attributes;
 using System;
 using System.Collections.Generic;