diff --git a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/OllamaRecommender.java b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/OllamaRecommender.java index cb82d80a99a..1415ecf97b5 100644 --- a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/OllamaRecommender.java +++ b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/OllamaRecommender.java @@ -100,7 +100,7 @@ public Range predict(RecommenderContext aContext, CAS aCas, int aBegin, int aEnd private Range predict(PromptBindingsGenerator aGenerator, RecommenderContext aContext, CAS aCas, int aBegin, int aEnd) { - aGenerator.generate(aCas, aBegin, aEnd).forEach(promptContext -> { + aGenerator.generate(this, aCas, aBegin, aEnd).forEach(promptContext -> { try { var prompt = jinjava.render(traits.getPrompt(), promptContext.getBindings()); var response = query(prompt); diff --git a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/OllamaRecommenderFactory.java b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/OllamaRecommenderFactory.java index a162ec386aa..76ee8019800 100644 --- a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/OllamaRecommenderFactory.java +++ b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/OllamaRecommenderFactory.java @@ -20,7 +20,6 @@ import static de.tudarmstadt.ukp.inception.support.WebAnnoConst.SPAN_TYPE; import static org.apache.uima.cas.CAS.TYPE_NAME_STRING; -import java.io.IOException; import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Collections; @@ -121,7 +120,7 @@ private List getPresets() try { return presets.get().get(); } - catch (IOException e) { + catch (Exception e) { LOG.error("Unable to load presets", e); return Collections.emptyList(); } diff --git a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PerAnnotationBindingsGenerator.java b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PerAnnotationBindingsGenerator.java index 7c3a955a149..5fb0af85a33 100644 --- a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PerAnnotationBindingsGenerator.java +++ b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PerAnnotationBindingsGenerator.java @@ -19,30 +19,49 @@ import static de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectOverlapping; +import java.util.LinkedHashMap; import java.util.stream.Stream; import org.apache.uima.cas.CAS; import org.apache.uima.fit.util.CasUtil; +import org.apache.uima.fit.util.FSUtil; import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence; +import de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngine; public class PerAnnotationBindingsGenerator implements PromptBindingsGenerator { - @Override - public Stream generate(CAS aCas, int aBegin, int aEnd) + public Stream generate(RecommendationEngine aEngine, CAS aCas, int aBegin, + int aEnd) { var candidateType = CasUtil.getAnnotationType(aCas, Sentence.class); - return selectOverlapping(aCas, candidateType, aBegin, aEnd).stream().map(candidate -> { + var predictedFeature = aEngine.getPredictedFeature(aCas); + + var candidates = selectOverlapping(aCas, candidateType, aBegin, aEnd); + + var examples = new LinkedHashMap(); + for (var candidate : candidates) { + var text = candidate.getCoveredText(); + var label = FSUtil.getFeature(candidate, predictedFeature, String.class); + + examples.put(text, label); + + if (examples.size() >= 10) { + break; + } + } + + return candidates.stream().map(candidate -> { var sentence = aCas.select(Sentence.class).covering(candidate) // .map(Sentence::getCoveredText) // .findFirst().orElse(""); var context = new PromptContext(candidate); context.set(VAR_TEXT, candidate.getCoveredText()); context.set(VAR_SENTENCE, sentence); + context.set(VAR_EXAMPLES, examples); return context; - }); } } diff --git a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PerDocumentBindingsGenerator.java b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PerDocumentBindingsGenerator.java index 732237c239c..2ea2ee71bec 100644 --- a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PerDocumentBindingsGenerator.java +++ b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PerDocumentBindingsGenerator.java @@ -21,12 +21,15 @@ import org.apache.uima.cas.CAS; +import de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngine; + public class PerDocumentBindingsGenerator implements PromptBindingsGenerator { @Override - public Stream generate(CAS aCas, int aBegin, int aEnd) + public Stream generate(RecommendationEngine aEngine, CAS aCas, int aBegin, + int aEnd) { var candidate = aCas.getDocumentAnnotation(); var context = new PromptContext(candidate); diff --git a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PerSentenceBindingsGenerator.java b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PerSentenceBindingsGenerator.java index 1e32be1ed02..e7bb2b9b82d 100644 --- a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PerSentenceBindingsGenerator.java +++ b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PerSentenceBindingsGenerator.java @@ -25,13 +25,14 @@ import org.apache.uima.fit.util.CasUtil; import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence; +import de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngine; public class PerSentenceBindingsGenerator implements PromptBindingsGenerator { - @Override - public Stream generate(CAS aCas, int aBegin, int aEnd) + public Stream generate(RecommendationEngine aEngine, CAS aCas, int aBegin, + int aEnd) { var candidateType = CasUtil.getAnnotationType(aCas, Sentence.class); return selectOverlapping(aCas, candidateType, aBegin, aEnd).stream().map(candidate -> { diff --git a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PromptBindingsGenerator.java b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PromptBindingsGenerator.java index 705516c2f69..8acd949afdb 100644 --- a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PromptBindingsGenerator.java +++ b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PromptBindingsGenerator.java @@ -21,11 +21,14 @@ import org.apache.uima.cas.CAS; +import de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngine; + public interface PromptBindingsGenerator { static final String VAR_TEXT = "text"; static final String VAR_SENTENCE = "sentence"; static final String VAR_DOCUMENT = "document"; + static final String VAR_EXAMPLES = "examples"; - Stream generate(CAS aCas, int aBegin, int aEnd); + Stream generate(RecommendationEngine aEngine, CAS aCas, int aBegin, int aEnd); } diff --git a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PromptContext.java b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PromptContext.java index 4e9a09211d2..6d7b9701986 100644 --- a/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PromptContext.java +++ b/inception/inception-imls-ollama/src/main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/prompt/PromptContext.java @@ -25,7 +25,7 @@ public class PromptContext { private final AnnotationFS candidate; - private final Map bindings; + private final Map bindings; public PromptContext(AnnotationFS aCandidate) { @@ -38,12 +38,12 @@ public AnnotationFS getCandidate() return candidate; } - public void set(String aKey, String aValue) + public void set(String aKey, Object aValue) { bindings.put(aKey, aValue); } - public Map getBindings() + public Map getBindings() { return bindings; }