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