| | | 1 | | using EHonda.KicktippAi.Core; |
| | | 2 | | using OpenAiIntegration; |
| | | 3 | | using Orchestrator.Infrastructure; |
| | | 4 | | using Orchestrator.Infrastructure.Factories; |
| | | 5 | | using Orchestrator.Infrastructure.Langfuse; |
| | | 6 | | using Spectre.Console; |
| | | 7 | | |
| | | 8 | | namespace Orchestrator.Commands.Shared; |
| | | 9 | | |
| | | 10 | | internal static class PredictionServiceCommandSupport |
| | | 11 | | { |
| | | 12 | | public const string WorldCupDevDefaultModel = "gpt-5-nano"; |
| | | 13 | | public const string WorldCupDevDefaultReasoningEffort = "minimal"; |
| | | 14 | | |
| | | 15 | | public static IPredictionService CreatePredictionService( |
| | | 16 | | IOpenAiServiceFactory openAiServiceFactory, |
| | | 17 | | ILangfusePublicApiClient? langfuseClient, |
| | | 18 | | IAnsiConsole console, |
| | | 19 | | string model, |
| | | 20 | | string competition, |
| | | 21 | | string community, |
| | | 22 | | string communityContext, |
| | | 23 | | string? promptSource, |
| | | 24 | | string? langfusePromptName, |
| | | 25 | | string? langfusePromptLabel, |
| | | 26 | | int? langfusePromptVersion, |
| | | 27 | | string? reasoningEffort, |
| | | 28 | | int? maxOutputTokenCount, |
| | | 29 | | bool bonusPrompt) |
| | | 30 | | { |
| | 1 | 31 | | var metadata = CompetitionResolver.ResolveRuntimeMetadata( |
| | 1 | 32 | | competition, |
| | 1 | 33 | | community, |
| | 1 | 34 | | communityContext, |
| | 1 | 35 | | promptSource, |
| | 1 | 36 | | langfusePromptName, |
| | 1 | 37 | | langfusePromptLabel, |
| | 1 | 38 | | bonusPrompt); |
| | | 39 | | |
| | 1 | 40 | | var options = PredictionServiceOptions.FlexProcessingWithStandardFallback with |
| | 1 | 41 | | { |
| | 1 | 42 | | ReasoningEffort = NormalizeReasoningEffort(reasoningEffort), |
| | 1 | 43 | | MaxOutputTokenCount = maxOutputTokenCount ?? PredictionServiceOptions.FlexProcessingWithStandardFallback.Max |
| | 1 | 44 | | }; |
| | | 45 | | |
| | 1 | 46 | | if (!string.Equals(metadata.PromptSource, CompetitionResolver.LangfusePromptSource, StringComparison.OrdinalIgno |
| | | 47 | | { |
| | 1 | 48 | | return openAiServiceFactory.CreatePredictionService(model, options); |
| | | 49 | | } |
| | | 50 | | |
| | 1 | 51 | | if (langfuseClient is null) |
| | | 52 | | { |
| | 0 | 53 | | throw new InvalidOperationException("Langfuse prompt source requires a Langfuse public API client."); |
| | | 54 | | } |
| | | 55 | | |
| | 1 | 56 | | var promptName = metadata.PromptName; |
| | 1 | 57 | | if (string.IsNullOrWhiteSpace(promptName)) |
| | | 58 | | { |
| | 0 | 59 | | throw new InvalidOperationException("--langfuse-prompt-name is required when --prompt-source langfuse is use |
| | | 60 | | } |
| | | 61 | | |
| | 1 | 62 | | var fallbackModel = string.IsNullOrWhiteSpace(metadata.FallbackPromptModel) |
| | 1 | 63 | | ? model |
| | 1 | 64 | | : metadata.FallbackPromptModel; |
| | | 65 | | |
| | 1 | 66 | | var templateProvider = new LangfuseTextPromptTemplateProvider( |
| | 1 | 67 | | langfuseClient, |
| | 1 | 68 | | promptName, |
| | 1 | 69 | | string.IsNullOrWhiteSpace(metadata.PromptLabel) ? null : metadata.PromptLabel, |
| | 1 | 70 | | langfusePromptVersion, |
| | 1 | 71 | | promptKind: bonusPrompt ? LangfusePromptKind.Bonus : LangfusePromptKind.Match, |
| | 1 | 72 | | fallbackTemplateProvider: new InstructionsTemplateProvider(PromptsFileProvider.Create()), |
| | 1 | 73 | | fallbackModel: fallbackModel, |
| | 0 | 74 | | fallbackWarning: message => console.MarkupLine($"[yellow]Warning:[/] {Markup.Escape(message)}")); |
| | | 75 | | |
| | 1 | 76 | | return openAiServiceFactory.CreatePredictionService(model, options, templateProvider); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public static bool UsesLangfusePromptSource( |
| | | 80 | | string competition, |
| | | 81 | | string community, |
| | | 82 | | string communityContext, |
| | | 83 | | string? promptSource, |
| | | 84 | | bool bonusPrompt) |
| | | 85 | | { |
| | 1 | 86 | | var metadata = CompetitionResolver.ResolveRuntimeMetadata( |
| | 1 | 87 | | competition, |
| | 1 | 88 | | community, |
| | 1 | 89 | | communityContext, |
| | 1 | 90 | | promptSource, |
| | 1 | 91 | | langfusePromptName: null, |
| | 1 | 92 | | langfusePromptLabel: null, |
| | 1 | 93 | | bonusPrompt); |
| | | 94 | | |
| | 1 | 95 | | return string.Equals(metadata.PromptSource, CompetitionResolver.LangfusePromptSource, StringComparison.OrdinalIg |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | public static string? NormalizeReasoningEffort(string? reasoningEffort) |
| | | 99 | | { |
| | 1 | 100 | | return PredictionModelConfig.NormalizeReasoningEffort(reasoningEffort); |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | public static PredictionModelConfig CreateModelConfig(string? model, string? reasoningEffort) |
| | | 104 | | { |
| | 1 | 105 | | return PredictionModelConfig.Create(ResolveModel(model), reasoningEffort); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | public static string ResolveModel(string? model) |
| | | 109 | | { |
| | 1 | 110 | | if (!string.IsNullOrWhiteSpace(model)) |
| | | 111 | | { |
| | 1 | 112 | | return model.Trim(); |
| | | 113 | | } |
| | | 114 | | |
| | 1 | 115 | | throw new ArgumentException("MODEL is required.", nameof(model)); |
| | | 116 | | } |
| | | 117 | | } |