// Models/ChatResponseExtensions.cs
namespace OASystem.API.OAMethodLib.TokenHubAI.Models;
///
/// ChatResponse 扩展方法
///
public static class ChatResponseExtensions
{
///
/// 获取第一个选择的消息内容
///
public static string? GetFirstContent(this ChatResponse response)
{
if (response.Choices != null && response.Choices.Count > 0)
{
var choice = response.Choices[0];
return choice.Message?.Content ?? choice.Delta?.Content;
}
return response.Content;
}
///
/// 获取完成原因
///
public static string? GetFinishReason(this ChatResponse response)
{
if (response.Choices != null && response.Choices.Count > 0)
{
return response.Choices[0].FinishReason;
}
return response.FinishReason;
}
///
/// 是否正常结束
///
public static bool IsNormalEnd(this ChatResponse response)
{
var reason = response.GetFinishReason();
return reason is null or "stop";
}
///
/// 是否被内容过滤
///
public static bool IsContentFiltered(this ChatResponse response)
{
return response.GetFinishReason() == "content_filter";
}
///
/// 是否达到长度限制
///
public static bool IsLengthLimit(this ChatResponse response)
{
return response.GetFinishReason() == "length";
}
/////
///// 获取工具调用列表
/////
//public static List? GetToolCalls(this ChatResponse response)
//{
// if (response.Choices != null && response.Choices.Count > 0)
// {
// var message = response.Choices[0].Message;
// return message?.ToolCalls;
// }
// return response.ToolCalls;
//}
}