Files
TechHelper/TechHelper.Client/AI/AiService.cs
SpecialX e824c081bf change
2025-05-30 12:46:55 +08:00

58 lines
1.3 KiB
C#

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<string> CallGLM(string userContent, string AnsConfig, AIModelsEnum aIModels/* = AIModelsEnum.GLMZ1Flash*/)
{
string model = aIModels.GetDescription();
var request = new ChatCompletionRequest
{
Model = model,
Messages = new List<Message>
{
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("<think>");
int endIndex = content.IndexOf("</think>");
if (startIndex != -1 && endIndex != -1 && endIndex > startIndex)
{
content = content.Remove(startIndex, endIndex - startIndex + "</think>".Length);
}
return content.Trim();
}
}
}
catch (HttpRequestException ex)
{
throw;
}
catch (Exception ex)
{
throw;
}
return null;
}
}
}