添加项目文件。

This commit is contained in:
SpecialX
2025-05-23 19:03:00 +08:00
parent 6fa7679fd3
commit d36fef2bbb
185 changed files with 13413 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
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))
{
// 移除 <think>...</think> 标签及其内容
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)
{
Console.WriteLine($"API 请求错误:{ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"发生未知错误:{ex.Message}");
}
return null;
}
}
}