添加项目文件。
This commit is contained in:
58
TechHelper.Client/AI/AiService.cs
Normal file
58
TechHelper.Client/AI/AiService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user