| | | 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 | | ArgumentNullException.ThrowIfNull(match); |
| | 1 | 32 | | ArgumentException.ThrowIfNullOrWhiteSpace(model); |
| | 1 | 33 | | ArgumentException.ThrowIfNullOrWhiteSpace(communityContext); |
| | | 34 | | |
| | 1 | 35 | | var predictionMetadata = await _predictionRepository.GetPredictionMetadataAsync( |
| | 1 | 36 | | match, |
| | 1 | 37 | | model, |
| | 1 | 38 | | communityContext, |
| | 1 | 39 | | cancellationToken); |
| | | 40 | | |
| | 1 | 41 | | if (predictionMetadata is null) |
| | | 42 | | { |
| | 1 | 43 | | return null; |
| | | 44 | | } |
| | | 45 | | |
| | 1 | 46 | | return await ReconstructMatchPredictionPromptAtTimestampAsync( |
| | 1 | 47 | | match, |
| | 1 | 48 | | model, |
| | 1 | 49 | | communityContext, |
| | 1 | 50 | | predictionMetadata.CreatedAt, |
| | 1 | 51 | | predictionMetadata.ContextDocumentNames, |
| | 1 | 52 | | includeJustification: includeJustification, |
| | 1 | 53 | | cancellationToken: cancellationToken); |
| | 1 | 54 | | } |
| | | 55 | | |
| | | 56 | | public async Task<ReconstructedMatchPredictionPrompt> ReconstructMatchPredictionPromptAtTimestampAsync( |
| | | 57 | | Match match, |
| | | 58 | | string model, |
| | | 59 | | string communityContext, |
| | | 60 | | DateTimeOffset promptTimestamp, |
| | | 61 | | IReadOnlyList<string> requiredContextDocumentNames, |
| | | 62 | | IReadOnlyList<string>? optionalContextDocumentNames = null, |
| | | 63 | | bool includeJustification = false, |
| | | 64 | | CancellationToken cancellationToken = default) |
| | | 65 | | { |
| | 1 | 66 | | ArgumentNullException.ThrowIfNull(match); |
| | 1 | 67 | | ArgumentException.ThrowIfNullOrWhiteSpace(model); |
| | 1 | 68 | | ArgumentException.ThrowIfNullOrWhiteSpace(communityContext); |
| | 1 | 69 | | ArgumentNullException.ThrowIfNull(requiredContextDocumentNames); |
| | | 70 | | |
| | 1 | 71 | | var resolvedContextDocuments = await ResolveContextDocumentsAsync( |
| | 1 | 72 | | match, |
| | 1 | 73 | | communityContext, |
| | 1 | 74 | | promptTimestamp, |
| | 1 | 75 | | requiredContextDocumentNames, |
| | 1 | 76 | | optionalContextDocumentNames ?? [], |
| | 1 | 77 | | cancellationToken); |
| | | 78 | | |
| | 1 | 79 | | var (template, templatePath) = _templateProvider.LoadMatchTemplate(model, includeJustification); |
| | 1 | 80 | | var systemPrompt = PredictionPromptComposer.BuildSystemPrompt( |
| | 1 | 81 | | template, |
| | 1 | 82 | | resolvedContextDocuments.Select(document => new DocumentContext(document.DocumentName, document.Content))); |
| | | 83 | | |
| | 1 | 84 | | return new ReconstructedMatchPredictionPrompt( |
| | 1 | 85 | | match, |
| | 1 | 86 | | model, |
| | 1 | 87 | | communityContext, |
| | 1 | 88 | | includeJustification, |
| | 1 | 89 | | promptTimestamp, |
| | 1 | 90 | | templatePath, |
| | 1 | 91 | | systemPrompt, |
| | 1 | 92 | | PredictionPromptComposer.CreateMatchJson(match), |
| | 1 | 93 | | resolvedContextDocuments.Select(document => document.DocumentName).ToArray(), |
| | 1 | 94 | | resolvedContextDocuments); |
| | 1 | 95 | | } |
| | | 96 | | |
| | | 97 | | private async Task<IReadOnlyList<ResolvedContextDocumentVersion>> ResolveContextDocumentsAsync( |
| | | 98 | | Match match, |
| | | 99 | | string communityContext, |
| | | 100 | | DateTimeOffset promptTimestamp, |
| | | 101 | | IReadOnlyList<string> requiredContextDocumentNames, |
| | | 102 | | IReadOnlyList<string> optionalContextDocumentNames, |
| | | 103 | | CancellationToken cancellationToken) |
| | | 104 | | { |
| | 1 | 105 | | var resolvedContextDocuments = new List<ResolvedContextDocumentVersion>(); |
| | | 106 | | |
| | 1 | 107 | | foreach (var documentName in requiredContextDocumentNames) |
| | | 108 | | { |
| | 1 | 109 | | var contextDocument = await _contextRepository.GetContextDocumentByTimestampAsync( |
| | 1 | 110 | | documentName, |
| | 1 | 111 | | promptTimestamp, |
| | 1 | 112 | | communityContext, |
| | 1 | 113 | | cancellationToken); |
| | | 114 | | |
| | 1 | 115 | | if (contextDocument is null) |
| | | 116 | | { |
| | 0 | 117 | | throw new InvalidOperationException( |
| | 0 | 118 | | $"Failed to reconstruct prompt for {match.HomeTeam} vs {match.AwayTeam}: " + |
| | 0 | 119 | | $"document '{documentName}' had no version at or before {promptTimestamp:O}."); |
| | | 120 | | } |
| | | 121 | | |
| | 1 | 122 | | resolvedContextDocuments.Add(new ResolvedContextDocumentVersion( |
| | 1 | 123 | | contextDocument.DocumentName, |
| | 1 | 124 | | contextDocument.Version, |
| | 1 | 125 | | contextDocument.CreatedAt, |
| | 1 | 126 | | contextDocument.Content)); |
| | 1 | 127 | | } |
| | | 128 | | |
| | 1 | 129 | | foreach (var documentName in optionalContextDocumentNames) |
| | | 130 | | { |
| | 1 | 131 | | var contextDocument = await _contextRepository.GetContextDocumentByTimestampAsync( |
| | 1 | 132 | | documentName, |
| | 1 | 133 | | promptTimestamp, |
| | 1 | 134 | | communityContext, |
| | 1 | 135 | | cancellationToken); |
| | | 136 | | |
| | 1 | 137 | | if (contextDocument is null) |
| | | 138 | | { |
| | | 139 | | continue; |
| | | 140 | | } |
| | | 141 | | |
| | 1 | 142 | | resolvedContextDocuments.Add(new ResolvedContextDocumentVersion( |
| | 1 | 143 | | contextDocument.DocumentName, |
| | 1 | 144 | | contextDocument.Version, |
| | 1 | 145 | | contextDocument.CreatedAt, |
| | 1 | 146 | | contextDocument.Content)); |
| | | 147 | | } |
| | | 148 | | |
| | 1 | 149 | | return resolvedContextDocuments; |
| | 1 | 150 | | } |
| | | 151 | | } |