feat: 新增支持Prompt

This commit is contained in:
ccnetcore
2025-07-09 23:11:57 +08:00
parent d59f40dfba
commit 17412d7de7

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http; using Dm.util;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using OpenAI.Chat; using OpenAI.Chat;
@@ -36,13 +37,17 @@ public class OpenApiService : ApplicationService
/// <param name="input"></param> /// <param name="input"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
[HttpPost("openApi/v1/chat/completions")] [HttpPost("openApi/v1/chat/completions")]
public async Task ChatCompletionsAsync([FromBody]ChatCompletionsInput input, CancellationToken cancellationToken) public async Task ChatCompletionsAsync([FromBody] ChatCompletionsInput input, CancellationToken cancellationToken)
{ {
//前面都是校验,后面才是真正的调用 //前面都是校验,后面才是真正的调用
var httpContext = this._httpContextAccessor.HttpContext; var httpContext = this._httpContextAccessor.HttpContext;
var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext)); var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext));
var history = new List<ChatMessage>(); var history = new List<ChatMessage>();
if (!string.IsNullOrWhiteSpace(input.Prompt))
{
history.add(ChatMessage.CreateSystemMessage(input.Prompt));
}
foreach (var aiChatContextDto in input.Messages) foreach (var aiChatContextDto in input.Messages)
{ {
if (aiChatContextDto.Role == "assistant") if (aiChatContextDto.Role == "assistant")
@@ -53,7 +58,7 @@ public class OpenApiService : ApplicationService
{ {
history.Add(ChatMessage.CreateUserMessage(aiChatContextDto.ConvertContent())); history.Add(ChatMessage.CreateUserMessage(aiChatContextDto.ConvertContent()));
} }
else if (aiChatContextDto.Role == "system") else if (aiChatContextDto.Role == "system")
{ {
history.Add(ChatMessage.CreateSystemMessage(aiChatContextDto.ConvertContent())); history.Add(ChatMessage.CreateSystemMessage(aiChatContextDto.ConvertContent()));
} }