feat: 完成支持functioncall功能

This commit is contained in:
ccnetcore
2025-07-18 23:12:20 +08:00
parent d6836b8bcf
commit 5d7217b775
3 changed files with 25 additions and 24 deletions

View File

@@ -13,9 +13,8 @@ public class ThorChatCompletionsRequest
{ {
Messages = new List<ThorChatMessage>(); Messages = new List<ThorChatMessage>();
} }
[JsonPropertyName("store")] [JsonPropertyName("store")] public bool? Store { get; set; }
public bool? Store { get; set; }
/// <summary> /// <summary>
/// 表示对话中支持的模态类型数组。可以为 null。 /// 表示对话中支持的模态类型数组。可以为 null。
@@ -26,14 +25,15 @@ public class ThorChatCompletionsRequest
/// <summary> /// <summary>
/// 表示对话中的音频请求参数。可以为 null。 /// 表示对话中的音频请求参数。可以为 null。
/// </summary> /// </summary>
[JsonPropertyName("audio")] public ThorChatAudioRequest? Audio { get; set; } [JsonPropertyName("audio")]
public ThorChatAudioRequest? Audio { get; set; }
/// <summary> /// <summary>
/// 包含迄今为止对话的消息列表 /// 包含迄今为止对话的消息列表
/// </summary> /// </summary>
[JsonPropertyName("messages")] [JsonPropertyName("messages")]
public List<ThorChatMessage> Messages { get; set; } public List<ThorChatMessage> Messages { get; set; }
/// <summary> /// <summary>
/// 模型唯一编码值,如 gpt-4gpt-3.5-turbo,moonshot-v1-8k看底层具体平台定义 /// 模型唯一编码值,如 gpt-4gpt-3.5-turbo,moonshot-v1-8k看底层具体平台定义
/// </summary> /// </summary>
@@ -229,18 +229,25 @@ public class ThorChatCompletionsRequest
{ {
if (value is JsonElement jsonElement) if (value is JsonElement jsonElement)
{ {
if (jsonElement.ValueKind == JsonValueKind.String) // if (jsonElement.ValueKind == JsonValueKind.String)
{ // {
ToolChoice = new ThorToolChoice // ToolChoice = new ThorToolChoice
{ // {
Type = jsonElement.GetString() // Type = jsonElement.GetString()
}; // };
} // }
else if (jsonElement.ValueKind == JsonValueKind.Object) if (jsonElement.ValueKind == JsonValueKind.Object)
{ {
ToolChoice = jsonElement.Deserialize<ThorToolChoice>(); ToolChoice = jsonElement.Deserialize<ThorToolChoice>();
} }
} }
else if (value is string text)
{
ToolChoice = new ThorToolChoice
{
Type = text
};
}
else else
{ {
ToolChoice = (ThorToolChoice)value; ToolChoice = (ThorToolChoice)value;

View File

@@ -44,7 +44,7 @@ public class MessageAggregateRoot : FullAuditedAggregateRoot<Guid>
public Guid? SessionId { get; set; } public Guid? SessionId { get; set; }
[SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)] [SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)]
public string Content { get; set; } public string? Content { get; set; }
public string Role { get; set; } public string Role { get; set; }
public string ModelId { get; set; } public string ModelId { get; set; }

View File

@@ -83,7 +83,7 @@ public class AiGateWayManager : DomainService
var modelDescribe = await GetModelAsync(request.Model); var modelDescribe = await GetModelAsync(request.Model);
var chatService = var chatService =
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName); LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
await foreach (var result in chatService.CompleteChatStreamAsync(modelDescribe, request, cancellationToken)) await foreach (var result in chatService.CompleteChatStreamAsync(modelDescribe, request, cancellationToken))
{ {
yield return result; yield return result;
@@ -109,8 +109,7 @@ public class AiGateWayManager : DomainService
_specialCompatible.Compatible(request); _specialCompatible.Compatible(request);
var response = httpContext.Response; var response = httpContext.Response;
// 设置响应头,声明是 json // 设置响应头,声明是 json
response.ContentType = "application/json; charset=UTF-8"; //response.ContentType = "application/json; charset=UTF-8";
await using var writer = new StreamWriter(response.Body, Encoding.UTF8, leaveOpen: true);
var modelDescribe = await GetModelAsync(request.Model); var modelDescribe = await GetModelAsync(request.Model);
var chatService = var chatService =
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName); LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
@@ -136,13 +135,8 @@ public class AiGateWayManager : DomainService
await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, data.Usage.InputTokens ?? 0, await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, data.Usage.InputTokens ?? 0,
data.Usage.OutputTokens ?? 0); data.Usage.OutputTokens ?? 0);
} }
var body = JsonConvert.SerializeObject(data, new JsonSerializerSettings await response.WriteAsJsonAsync(data, cancellationToken);
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
await writer.WriteLineAsync(body);
await writer.FlushAsync(cancellationToken);
} }
/// <summary> /// <summary>