using Entities.Contracts; using Newtonsoft.Json; namespace TechHelper.Client.AI { public class AiService : IAIService { private readonly GLMZ1Client _glmClient; public AiService() { _glmClient = new GLMZ1Client(AIConfiguration.APIkey); } public async Task CallGLM(string userContent, string AnsConfig, AIModelsEnum aIModels/* = AIModelsEnum.GLMZ1Flash*/) { string model = aIModels.GetDescription(); var request = new ChatCompletionRequest { Model = model, Messages = new List { new UserMessage(AnsConfig + userContent) } }; try { var response = await _glmClient.ChatCompletionsSync(request); if (response?.Choices != null && response.Choices.Count > 0) { string content = response.Choices[0].Message?.Content; if (!string.IsNullOrEmpty(content)) { // 移除 ... 标签及其内容 int startIndex = content.IndexOf(""); int endIndex = content.IndexOf(""); if (startIndex != -1 && endIndex != -1 && endIndex > startIndex) { content = content.Remove(startIndex, endIndex - startIndex + "".Length); } return content.Trim(); } } } catch (HttpRequestException ex) { Console.WriteLine($"API 请求错误:{ex.Message}"); } catch (Exception ex) { Console.WriteLine($"发生未知错误:{ex.Message}"); } return null; } } }