-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Basic ollama-based recommender (highly experimental)
- Loading branch information
Showing
16 changed files
with
727 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Marker file which activates the profile "wicket-module" from the parent POM. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<!-- | ||
Licensed to the Technische Universität Darmstadt under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The Technische Universität Darmstadt | ||
licenses this file to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>de.tudarmstadt.ukp.inception.app</groupId> | ||
<artifactId>inception-app</artifactId> | ||
<version>30.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>inception-imls-ollama</artifactId> | ||
<name>INCEpTION - ML - Ollama</name> | ||
|
||
<repositories> | ||
<repository> | ||
<id>ollama4j-from-ossrh</id> | ||
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.github.amithkoujalgi</groupId> | ||
<artifactId>ollama4j</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.hubspot.jinjava</groupId> | ||
<artifactId>jinjava</artifactId> | ||
<version>2.7.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>de.tudarmstadt.ukp.inception.app</groupId> | ||
<artifactId>inception-recommendation-api</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>de.tudarmstadt.ukp.inception.app</groupId> | ||
<artifactId>inception-model</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>de.tudarmstadt.ukp.inception.app</groupId> | ||
<artifactId>inception-api-render</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>de.tudarmstadt.ukp.inception.app</groupId> | ||
<artifactId>inception-support</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
</dependency> | ||
|
||
<!-- UIMA --> | ||
<dependency> | ||
<groupId>org.apache.uima</groupId> | ||
<artifactId>uimaj-core</artifactId> | ||
</dependency> | ||
|
||
<!-- Spring --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-autoconfigure</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.wicket</groupId> | ||
<artifactId>wicket-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.wicket</groupId> | ||
<artifactId>wicket-spring</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.googlecode.wicket-jquery-ui</groupId> | ||
<artifactId>wicket-kendo-ui</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.googlecode.wicket-jquery-ui</groupId> | ||
<artifactId>wicket-jquery-ui-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.danekja</groupId> | ||
<artifactId>jdk-serializable-functional</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-annotations</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
159 changes: 159 additions & 0 deletions
159
.../main/java/de/tudarmstadt/ukp/inception/recommendation/imls/ollama/OllamaRecommender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Licensed to the Technische Universität Darmstadt under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The Technische Universität Darmstadt | ||
* licenses this file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package de.tudarmstadt.ukp.inception.recommendation.imls.ollama; | ||
|
||
import static de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectOverlapping; | ||
|
||
import java.io.IOException; | ||
import java.lang.invoke.MethodHandles; | ||
import java.nio.charset.Charset; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
import org.apache.uima.cas.CAS; | ||
import org.apache.uima.fit.util.CasUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.hubspot.jinjava.Jinjava; | ||
import com.hubspot.jinjava.JinjavaConfig; | ||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
import com.hubspot.jinjava.loader.ResourceLocator; | ||
import com.hubspot.jinjava.loader.ResourceNotFoundException; | ||
|
||
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence; | ||
import de.tudarmstadt.ukp.inception.recommendation.api.model.Recommender; | ||
import de.tudarmstadt.ukp.inception.recommendation.api.recommender.NonTrainableRecommenderEngineImplBase; | ||
import de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException; | ||
import de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext; | ||
import de.tudarmstadt.ukp.inception.rendering.model.Range; | ||
import io.github.amithkoujalgi.ollama4j.core.OllamaAPI; | ||
import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException; | ||
|
||
public class OllamaRecommender | ||
extends NonTrainableRecommenderEngineImplBase | ||
{ | ||
private static final String VAR_TEXT = "text"; | ||
|
||
private final static Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
|
||
private final OllamaRecommenderTraits traits; | ||
|
||
private final OllamaAPI ollamaApi; | ||
private final Jinjava jinjava; | ||
|
||
public OllamaRecommender(Recommender aRecommender, OllamaRecommenderTraits aTraits) | ||
{ | ||
super(aRecommender); | ||
|
||
traits = aTraits; | ||
|
||
var config = new JinjavaConfig(); | ||
jinjava = new Jinjava(config); | ||
jinjava.setResourceLocator(new ResourceLocator() | ||
{ | ||
@Override | ||
public String getString(String aFullName, Charset aEncoding, | ||
JinjavaInterpreter aInterpreter) | ||
throws IOException | ||
{ | ||
throw new ResourceNotFoundException("Couldn't find resource: " + aFullName); | ||
} | ||
}); | ||
|
||
ollamaApi = new OllamaAPI(traits.getUrl()); | ||
} | ||
|
||
@Override | ||
public Range predict(RecommenderContext aContext, CAS aCas, int aBegin, int aEnd) | ||
throws RecommendationException | ||
{ | ||
switch (traits.getProcessingMode()) { | ||
case PER_ANNOTATION: | ||
return predictPerAnnotation(aContext, aCas, aBegin, aEnd); | ||
case PER_SENTENCE: | ||
return predictPerSentence(aContext, aCas, aBegin, aEnd); | ||
default: | ||
throw new RecommendationException( | ||
"Unsupported mode [" + traits.getProcessingMode() + "]"); | ||
} | ||
} | ||
|
||
private Range predictPerSentence(RecommenderContext aContext, CAS aCas, int aBegin, int aEnd) | ||
{ | ||
var candidateType = CasUtil.getAnnotationType(aCas, Sentence.class); | ||
var predictedType = getPredictedType(aCas); | ||
var predictedFeature = getPredictedFeature(aCas); | ||
var isPredictionFeature = getIsPredictionFeature(aCas); | ||
|
||
for (var candidate : selectOverlapping(aCas, candidateType, aBegin, aEnd)) { | ||
var bindings = Map.of(VAR_TEXT, candidate.getCoveredText()); | ||
var prompt = jinjava.render(traits.getPrompt(), bindings); | ||
|
||
try { | ||
var response = ask(prompt); | ||
|
||
var prediction = aCas.createAnnotation(predictedType, candidate.getBegin(), | ||
candidate.getEnd()); | ||
prediction.setFeatureValueFromString(predictedFeature, response); | ||
prediction.setBooleanValue(isPredictionFeature, true); | ||
aCas.addFsToIndexes(prediction); | ||
} | ||
catch (OllamaBaseException | IOException e) { | ||
LOG.error("Ollama [{}] failed to respond: {}", traits.getModel(), | ||
ExceptionUtils.getRootCauseMessage(e)); | ||
} | ||
} | ||
|
||
return new Range(aBegin, aEnd); | ||
} | ||
|
||
private Range predictPerAnnotation(RecommenderContext aContext, CAS aCas, int aBegin, int aEnd) | ||
{ | ||
var predictedType = getPredictedType(aCas); | ||
var predictedFeature = getPredictedFeature(aCas); | ||
var isPredictionFeature = getIsPredictionFeature(aCas); | ||
|
||
for (var candidate : selectOverlapping(aCas, predictedType, aBegin, aEnd)) { | ||
var bindings = Map.of(VAR_TEXT, candidate.getCoveredText()); | ||
var prompt = jinjava.render(traits.getPrompt(), bindings); | ||
|
||
try { | ||
var response = ask(prompt); | ||
|
||
var prediction = candidate; | ||
prediction.setFeatureValueFromString(predictedFeature, response); | ||
prediction.setBooleanValue(isPredictionFeature, true); | ||
} | ||
catch (OllamaBaseException | IOException e) { | ||
LOG.error("Ollama [{}] failed to respond: {}", traits.getModel(), | ||
ExceptionUtils.getRootCauseMessage(e)); | ||
} | ||
} | ||
|
||
return new Range(aBegin, aEnd); | ||
} | ||
|
||
private String ask(String prompt) throws OllamaBaseException, IOException | ||
{ | ||
LOG.trace("Asking ollama [{}]: [{}]", traits.getModel(), prompt); | ||
var response = ollamaApi.ask(traits.getModel(), prompt).trim(); | ||
LOG.trace("Ollama [{}] responds: [{}]", traits.getModel(), response); | ||
return response; | ||
} | ||
} |
Oops, something went wrong.