-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathPromptOptimizationController.cs
More file actions
125 lines (114 loc) · 5.04 KB
/
Copy pathPromptOptimizationController.cs
File metadata and controls
125 lines (114 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Senparc.Ncf.Core.AppServices;
using Senparc.Ncf.Core.Exceptions;
using Senparc.Xncf.AgentsManager.Domain.Services;
using Senparc.Xncf.AgentsManager.OHS.Local.AppService;
using Senparc.Xncf.PromptRange.Abstractions.Events;
namespace Senparc.Xncf.AgentsManager.OHS.Remote.Controllers
{
/// <summary>
/// Prompt 优化 API Controller
/// </summary>
[ApiController]
[Route("api/Senparc.Xncf.AgentsManager/PromptOptimizationAppService")]
public class PromptOptimizationController : ControllerBase
{
private readonly PromptOptimizationService _promptOptimizationService;
private readonly ILogger<PromptOptimizationController> _logger;
public PromptOptimizationController(
PromptOptimizationService promptOptimizationService,
ILogger<PromptOptimizationController> logger)
{
_promptOptimizationService = promptOptimizationService;
_logger = logger;
}
/// <summary>
/// 优化指定的 Prompt(包括内容和参数如 Temperature)
/// </summary>
[HttpPost("OptimizeAsync")]
public async Task<IActionResult> OptimizeAsync([FromBody] PromptOptimizationRequestDto request)
{
try
{
_logger.LogInformation("========== 收到 Prompt 优化请求 ==========");
_logger.LogInformation("PromptCode: {PromptCode}, UserRequirement: {UserRequirement}",
request.PromptCode, request.UserRequirement);
// 验证请求参数
if (string.IsNullOrWhiteSpace(request.PromptCode))
{
_logger.LogWarning(" ❌ PromptCode 为空");
return Ok(new AppResponseBase<PromptOptimizationResponseEvent>
{
Success = false,
ErrorMessage = "PromptCode is required"
});
}
if (string.IsNullOrWhiteSpace(request.PromptContent))
{
_logger.LogWarning(" ❌ PromptContent 为空");
return Ok(new AppResponseBase<PromptOptimizationResponseEvent>
{
Success = false,
ErrorMessage = "PromptContent is required"
});
}
if (request.Context == null)
{
_logger.LogWarning(" ❌ Context 为空");
return Ok(new AppResponseBase<PromptOptimizationResponseEvent>
{
Success = false,
ErrorMessage = "Context is required"
});
}
_logger.LogInformation(" 请求参数验证通过");
_logger.LogInformation(" Context: ModelId={ModelId}, Temperature={Temp}, TopP={TopP}, MaxTokens={MaxTokens}",
request.Context.ModelId,
request.Context.CurrentTemperature,
request.Context.CurrentTopP,
request.Context.CurrentMaxTokens);
// 调用优化服务(内部已包含 EnsureInitializedAsync)
_logger.LogInformation(" 开始调用 OptimizePromptAsync...");
var result = await _promptOptimizationService.OptimizePromptAsync(
request.PromptCode,
request.PromptContent,
request.UserRequirement ?? "提高 Prompt 的质量和效果",
request.Context);
_logger.LogInformation(" ✅ 优化完成!NewPromptCode: {NewPromptCode}, Score: {Score}",
result.NewPromptCode, result.Score);
_logger.LogInformation("========== Prompt 优化请求处理完成 ==========");
return Ok(new AppResponseBase<PromptOptimizationResponseEvent>
{
Success = true,
Data = result
});
}
catch (TimeoutException ex)
{
_logger.LogError(ex, "❌ Prompt 优化超时");
return Ok(new AppResponseBase<PromptOptimizationResponseEvent>
{
Success = false,
ErrorMessage = $"优化请求超时: {ex.Message}"
});
}
catch (Exception ex)
{
var errorMessage = ex.Message;
if (ex.InnerException != null)
{
errorMessage += $" | Inner: {ex.InnerException.Message}";
}
_logger.LogError(ex, "❌ Prompt 优化失败: {ErrorMessage}", errorMessage);
return Ok(new AppResponseBase<PromptOptimizationResponseEvent>
{
Success = false,
ErrorMessage = errorMessage
});
}
}
}
}