| | | 1 | | using EHonda.KicktippAi.Core; |
| | | 2 | | |
| | | 3 | | namespace OpenAiIntegration; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Reconstructs historical match prompt inputs from stored prediction metadata and versioned context documents. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class MatchPromptReconstructionService : IMatchPromptReconstructionService |
| | | 9 | | { |
| | | 10 | | private readonly IPredictionRepository _predictionRepository; |
| | | 11 | | private readonly IContextRepository _contextRepository; |
| | | 12 | | private readonly IInstructionsTemplateProvider _templateProvider; |
| | | 13 | | |
| | 1 | 14 | | public MatchPromptReconstructionService( |
| | 1 | 15 | | IPredictionRepository predictionRepository, |
| | 1 | 16 | | IContextRepository contextRepository, |
| | 1 | 17 | | IInstructionsTemplateProvider templateProvider) |
| | | 18 | | { |
| | 1 | 19 | | _predictionRepository = predictionRepository ?? throw new ArgumentNullException(nameof(predictionRepository)); |
| | 1 | 20 | | _contextRepository = contextRepository ?? throw new ArgumentNullException(nameof(contextRepository)); |
| | 1 | 21 | | _templateProvider = templateProvider ?? throw new ArgumentNullException(nameof(templateProvider)); |
| | 1 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task<ReconstructedMatchPredictionPrompt?> ReconstructMatchPredictionPromptAsync( |
| | | 25 | | Match match, |
| | | 26 | | string model, |
| | | 27 | | string communityContext, |
| | | 28 | | bool includeJustification = false, |
| | | 29 | | CancellationToken cancellationToken = default) |
| | | 30 | | { |
| | 1 | 31 | | return await ReconstructMatchPredictionPromptAsync( |
| | 1 | 32 | | match, |
| | 1 | 33 | | PredictionModelConfig.Create(model), |
| | 1 | 34 | | communityContext, |
| | 1 | 35 | | includeJustification, |
| | 1 | 36 | | cancellationToken); |
| | 1 | 37 | | } |
| | | 38 | | |
| | | 39 | | public async Task<ReconstructedMatchPredictionPrompt?> ReconstructMatchPredictionPromptAsync( |
| | | 40 | | Match match, |
| | | 41 | | PredictionModelConfig modelConfig, |
| | | 42 | | string communityContext, |
| | | 43 | | bool includeJustification = false, |
| | | 44 | | CancellationToken cancellationToken = default) |
| | | 45 | | { |
| | 1 | 46 | | ArgumentNullException.ThrowIfNull(match); |
| | 1 | 47 | | ArgumentNullException.ThrowIfNull(modelConfig); |
| | 1 | 48 | | ArgumentException.ThrowIfNullOrWhiteSpace(communityContext); |
| | | 49 | | |
| | 1 | 50 | | var predictionMetadata = await _predictionRepository.GetPredictionMetadataAsync( |
| | 1 | 51 | | match, |
| | 1 | 52 | | modelConfig, |
| | 1 | 53 | | communityContext, |
| | 1 | 54 | | cancellationToken); |
| | | 55 | | |
| | 1 | 56 | | if (predictionMetadata is null) |
| | | 57 | | { |
| | 1 | 58 | | return null; |
| | | 59 | | } |
| | | 60 | | |
| | 1 | 61 | | return await ReconstructMatchPredictionPromptAtTimestampAsync( |
| | 1 | 62 | | match, |
| | 1 | 63 | | modelConfig.Model, |
| | 1 | 64 | | communityContext, |
| | 1 | 65 | | predictionMetadata.CreatedAt, |
| | 1 | 66 | | predictionMetadata.ContextDocumentNames, |
| | 1 | 67 | | includeJustification: includeJustification, |
| | 1 | 68 | | cancellationToken: cancellationToken); |
| | 1 | 69 | | } |
| | | 70 | | |
| | | 71 | | public async Task<ReconstructedMatchPredictionPrompt> ReconstructMatchPredictionPromptAtTimestampAsync( |
| | | 72 | | Match match, |
| | | 73 | | string model, |
| | | 74 | | string communityContext, |
| | | 75 | | DateTimeOffset promptTimestamp, |
| | | 76 | | IReadOnlyList<string> requiredContextDocumentNames, |
| | | 77 | | IReadOnlyList<string>? optionalContextDocumentNames = null, |
| | | 78 | | bool includeJustification = false, |
| | | 79 | | CancellationToken cancellationToken = default) |
| | | 80 | | { |
| | 1 | 81 | | ArgumentNullException.ThrowIfNull(match); |
| | 1 | 82 | | ArgumentException.ThrowIfNullOrWhiteSpace(model); |
| | 1 | 83 | | ArgumentException.ThrowIfNullOrWhiteSpace(communityContext); |
| | 1 | 84 | | ArgumentNullException.ThrowIfNull(requiredContextDocumentNames); |
| | | 85 | | |
| | 1 | 86 | | var resolvedContextDocuments = await ResolveContextDocumentsAsync( |
| | 1 | 87 | | match, |
| | 1 | 88 | | communityContext, |
| | 1 | 89 | | promptTimestamp, |
| | 1 | 90 | | requiredContextDocumentNames, |
| | 1 | 91 | | optionalContextDocumentNames ?? [], |
| | 1 | 92 | | cancellationToken); |
| | | 93 | | |
| | 1 | 94 | | var (template, templatePath) = _templateProvider.LoadMatchTemplate(model, includeJustification); |
| | 1 | 95 | | var systemPrompt = PredictionPromptComposer.BuildSystemPrompt( |
| | 1 | 96 | | template, |
| | 1 | 97 | | resolvedContextDocuments.Select(document => new DocumentContext(document.DocumentName, document.Content))); |
| | | 98 | | |
| | 1 | 99 | | return new ReconstructedMatchPredictionPrompt( |
| | 1 | 100 | | match, |
| | 1 | 101 | | model, |
| | 1 | 102 | | communityContext, |
| | 1 | 103 | | includeJustification, |
| | 1 | 104 | | promptTimestamp, |
| | 1 | 105 | | templatePath, |
| | 1 | 106 | | systemPrompt, |
| | 1 | 107 | | PredictionPromptComposer.CreateMatchJson(match), |
| | 1 | 108 | | resolvedContextDocuments.Select(document => document.DocumentName).ToArray(), |
| | 1 | 109 | | resolvedContextDocuments); |
| | 1 | 110 | | } |
| | | 111 | | |
| | | 112 | | private async Task<IReadOnlyList<ResolvedContextDocumentVersion>> ResolveContextDocumentsAsync( |
| | | 113 | | Match match, |
| | | 114 | | string communityContext, |
| | | 115 | | DateTimeOffset promptTimestamp, |
| | | 116 | | IReadOnlyList<string> requiredContextDocumentNames, |
| | | 117 | | IReadOnlyList<string> optionalContextDocumentNames, |
| | | 118 | | CancellationToken cancellationToken) |
| | | 119 | | { |
| | 1 | 120 | | var resolvedContextDocuments = new List<ResolvedContextDocumentVersion>(); |
| | | 121 | | |
| | 1 | 122 | | foreach (var documentName in requiredContextDocumentNames) |
| | | 123 | | { |
| | 1 | 124 | | var contextDocument = await _contextRepository.GetContextDocumentByTimestampAsync( |
| | 1 | 125 | | documentName, |
| | 1 | 126 | | promptTimestamp, |
| | 1 | 127 | | communityContext, |
| | 1 | 128 | | cancellationToken); |
| | | 129 | | |
| | 1 | 130 | | if (contextDocument is null) |
| | | 131 | | { |
| | 0 | 132 | | throw new InvalidOperationException( |
| | 0 | 133 | | $"Failed to reconstruct prompt for {match.HomeTeam} vs {match.AwayTeam}: " + |
| | 0 | 134 | | $"document '{documentName}' had no version at or before {promptTimestamp:O}."); |
| | | 135 | | } |
| | | 136 | | |
| | 1 | 137 | | resolvedContextDocuments.Add(new ResolvedContextDocumentVersion( |
| | 1 | 138 | | contextDocument.DocumentName, |
| | 1 | 139 | | contextDocument.Version, |
| | 1 | 140 | | contextDocument.CreatedAt, |
| | 1 | 141 | | contextDocument.Content)); |
| | 1 | 142 | | } |
| | | 143 | | |
| | 1 | 144 | | foreach (var documentName in optionalContextDocumentNames) |
| | | 145 | | { |
| | 1 | 146 | | var contextDocument = await _contextRepository.GetContextDocumentByTimestampAsync( |
| | 1 | 147 | | documentName, |
| | 1 | 148 | | promptTimestamp, |
| | 1 | 149 | | communityContext, |
| | 1 | 150 | | cancellationToken); |
| | | 151 | | |
| | 1 | 152 | | if (contextDocument is null) |
| | | 153 | | { |
| | | 154 | | continue; |
| | | 155 | | } |
| | | 156 | | |
| | 1 | 157 | | resolvedContextDocuments.Add(new ResolvedContextDocumentVersion( |
| | 1 | 158 | | contextDocument.DocumentName, |
| | 1 | 159 | | contextDocument.Version, |
| | 1 | 160 | | contextDocument.CreatedAt, |
| | 1 | 161 | | contextDocument.Content)); |
| | | 162 | | } |
| | | 163 | | |
| | 1 | 164 | | return resolvedContextDocuments; |
| | 1 | 165 | | } |
| | | 166 | | } |