59 lines
1.4 KiB
C#
59 lines
1.4 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))
|
|
{
|
|
// 移除 <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;
|
|
}
|
|
}
|
|
}
|