Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.jobdri.jobdri_api.domain.analysis.service.ai.fewshot.FewShotProperties;
import com.jobdri.jobdri_api.domain.analysis.service.ai.fewshot.FewShotSearchQuery;
import com.jobdri.jobdri_api.domain.analysis.service.ai.fewshot.FewShotSearchService;
import com.jobdri.jobdri_api.domain.analysis.service.ai.fewshot.FewShotSelectionMode;
import com.jobdri.jobdri_api.domain.analysis.service.ai.fewshot.SelectedFewShotCase;
import com.jobdri.jobdri_api.domain.corpus.service.CorpusRetrievalService.RetrievalContext;
import com.jobdri.jobdri_api.domain.corpus.service.CorpusRetrievalService.RetrievedJobPostingReference;
Expand Down Expand Up @@ -615,7 +616,8 @@ private String resolveFewShotPromptBlock(AnalysisPromptInput promptInput) {
);
if (selectedFewShots.isEmpty()) {
log.warn(
"dynamic few-shot selection returned empty result. fallback=fixed, caseId={}, datasetVersion={}",
"dynamic few-shot selection returned empty result. selectionMode={}, caseId={}, datasetVersion={}",
FewShotSelectionMode.STATIC_FALLBACK,
promptInput.caseId(),
fewShotProperties.getDatasetVersion()
);
Expand All @@ -632,7 +634,8 @@ private String resolveFewShotPromptBlock(AnalysisPromptInput promptInput) {
return fewShotPromptProvider.buildPromptBlock(selectedFewShots);
} catch (Exception e) {
log.warn(
"dynamic few-shot selection failed. fallback=fixed, caseId={}, datasetVersion={}, reason={}, message={}",
"dynamic few-shot selection failed. selectionMode={}, caseId={}, datasetVersion={}, reason={}, message={}",
FewShotSelectionMode.STATIC_FALLBACK,
promptInput.caseId(),
fewShotProperties.getDatasetVersion(),
e.getClass().getSimpleName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,33 @@ public List<SelectedFewShotCase> searchRelevantFewShots(FewShotSearchQuery query
String cacheKey = selectionCacheKey(query, requestedTopK, datasetFingerprint);
SelectionCacheEntry cached = readSelectionCache(cacheKey);
if (cached != null) {
log.debug("few-shot selection cache hit. selectedCount={}, datasetVersion={}", cached.selectedCases().size(), properties.getDatasetVersion());
log.debug(
"few-shot selection cache hit. selectionMode={}, selectedCount={}, datasetVersion={}",
cached.selectionMode(),
cached.selectedCases().size(),
properties.getDatasetVersion()
);
return cached.selectedCases();
}

long startedAt = System.nanoTime();
List<FewShotCase> candidates = localPrefilter(activeCases, query);
List<SelectedFewShotCase> selected = selectWithCohere(query, candidates, requestedTopK);
FewShotSelectionMode selectionMode = selected.isEmpty()
? FewShotSelectionMode.STATIC_FALLBACK
: FewShotSelectionMode.EMBEDDING;
if (selected.isEmpty() && properties.isFallbackEnabled()) {
selected = selectLocally(query, candidates, requestedTopK, "local-fallback");
if (!selected.isEmpty()) {
selectionMode = FewShotSelectionMode.LOCAL_FALLBACK;
}
}
if (properties.isCacheEnabled()) {
selectionCache.put(cacheKey, new SelectionCacheEntry(selected, expiresAt()));
selectionCache.put(cacheKey, new SelectionCacheEntry(selected, selectionMode, expiresAt()));
}
log.info(
"dynamic few-shot selection completed. enabled=true, totalCandidates={}, filteredCandidates={}, selectedIds={}, sources={}, scores={}, latencyMs={}",
"dynamic few-shot selection completed. enabled=true, selectionMode={}, totalCandidates={}, filteredCandidates={}, selectedIds={}, sources={}, scores={}, latencyMs={}",
selectionMode,
activeCases.size(),
candidates.size(),
selected.stream().map(item -> item.fewShotCase().id()).toList(),
Expand Down Expand Up @@ -447,7 +459,11 @@ private record PendingDocumentEmbedding(
) {
}

private record SelectionCacheEntry(List<SelectedFewShotCase> selectedCases, Instant expiresAt) {
private record SelectionCacheEntry(
List<SelectedFewShotCase> selectedCases,
FewShotSelectionMode selectionMode,
Instant expiresAt
) {
private SelectionCacheEntry {
selectedCases = selectedCases == null ? List.of() : List.copyOf(selectedCases);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.jobdri.jobdri_api.domain.analysis.service.ai.fewshot;

public enum FewShotSelectionMode {
EMBEDDING,
LOCAL_FALLBACK,
STATIC_FALLBACK
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void excludesSelfReferenceCandidate() {

assertThat(result).extracting(item -> item.fewShotCase().id())
.containsExactly("EV-02");
assertThat(result.getFirst().selectionMethod()).isEqualTo("cohere-embedding");
}

@Test
Expand Down
Loading