Skip to content

Commit

Permalink
#4292 - ollama-based recommender
Browse files Browse the repository at this point in the history
- Renamed some classes
- Added generation options (not used atm)
  • Loading branch information
reckart committed Nov 14, 2023
1 parent 287b0a5 commit fc42860
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
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.recommendation.imls.ollama.client.OllamaAskRequest;
import de.tudarmstadt.ukp.inception.recommendation.imls.ollama.client.OllamaGenerateRequest;
import de.tudarmstadt.ukp.inception.recommendation.imls.ollama.client.OllamaClient;
import de.tudarmstadt.ukp.inception.rendering.model.Range;

Expand Down Expand Up @@ -326,7 +326,7 @@ private Range predictPerAnnotation(RecommenderContext aContext, CAS aCas, int aB
private String generate(String prompt) throws IOException
{
LOG.trace("Asking ollama [{}]: [{}]", traits.getModel(), prompt);
var request = OllamaAskRequest.builder() //
var request = OllamaGenerateRequest.builder() //
.withModel(traits.getModel()) //
.withPrompt(prompt) //
.withFormat(traits.getFormat()) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import de.tudarmstadt.ukp.inception.recommendation.imls.ollama.client.OllamaResponseFormat;
import de.tudarmstadt.ukp.inception.recommendation.imls.ollama.client.OllamaGenerateResponseFormat;

@JsonIgnoreProperties(ignoreUnknown = true)
public class OllamaRecommenderTraits
Expand All @@ -37,7 +37,7 @@ public class OllamaRecommenderTraits

private boolean raw;

private OllamaResponseFormat format;
private OllamaGenerateResponseFormat format;

private ProcessingMode processingMode = ProcessingMode.PER_ANNOTATION;

Expand Down Expand Up @@ -93,12 +93,12 @@ public void setRaw(boolean aRaw)
raw = aRaw;
}

public OllamaResponseFormat getFormat()
public OllamaGenerateResponseFormat getFormat()
{
return format;
}

public void setFormat(OllamaResponseFormat aFormat)
public void setFormat(OllamaGenerateResponseFormat aFormat)
{
format = aFormat;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ProcessingMode.PER_DOCUMENT=Per document

format=Response format
format.nullValid=Default
OllamaResponseFormat.JSON=JSON
OllamaGenerateResponseFormat.JSON=JSON

extractionMode=Extraction mode
ExtractionMode.RESPONSE_AS_LABEL=Response as label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import org.apache.wicket.markup.html.form.EnumChoiceRenderer;
import org.apache.wicket.model.IModel;

import de.tudarmstadt.ukp.inception.recommendation.imls.ollama.client.OllamaResponseFormat;
import de.tudarmstadt.ukp.inception.recommendation.imls.ollama.client.OllamaGenerateResponseFormat;

public class OllamaResponseFormatSelect
extends DropDownChoice<OllamaResponseFormat>
extends DropDownChoice<OllamaGenerateResponseFormat>
{
private static final long serialVersionUID = 3115872987735239823L;

Expand All @@ -35,7 +35,7 @@ public OllamaResponseFormatSelect(String aId)
super(aId);
}

public OllamaResponseFormatSelect(String aId, IModel<OllamaResponseFormat> aModel)
public OllamaResponseFormatSelect(String aId, IModel<OllamaGenerateResponseFormat> aModel)
{
super(aId);
setModel(aModel);
Expand All @@ -47,7 +47,7 @@ protected void onInitialize()
super.onInitialize();

setChoiceRenderer(new EnumChoiceRenderer<>(this));
setChoices(asList(OllamaResponseFormat.values()));
setChoices(asList(OllamaGenerateResponseFormat.values()));
setNullValid(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import de.tudarmstadt.ukp.inception.recommendation.imls.ollama.client.OllamaResponseFormat;
import de.tudarmstadt.ukp.inception.recommendation.imls.ollama.client.OllamaGenerateResponseFormat;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Preset
Expand All @@ -37,7 +37,7 @@ public class Preset

private boolean raw;

private OllamaResponseFormat format;
private OllamaGenerateResponseFormat format;

private ProcessingMode processingMode = ProcessingMode.PER_ANNOTATION;

Expand Down Expand Up @@ -93,12 +93,12 @@ public void setRaw(boolean aRaw)
raw = aRaw;
}

public OllamaResponseFormat getFormat()
public OllamaGenerateResponseFormat getFormat()
{
return format;
}

public void setFormat(OllamaResponseFormat aFormat)
public void setFormat(OllamaGenerateResponseFormat aFormat)
{
format = aFormat;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@

public interface OllamaClient
{
String generate(String aUrl, OllamaAskRequest aRequest) throws IOException;
String generate(String aUrl, OllamaGenerateRequest aRequest) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected String urlEncodeParameters(Map<String, String> aParameters)
}

@Override
public String generate(String aUrl, OllamaAskRequest aRequest) throws IOException
public String generate(String aUrl, OllamaGenerateRequest aRequest) throws IOException
{
var request = HttpRequest.newBuilder() //
.uri(URI.create(appendIfMissing(aUrl, "/") + "api/generate")) //
Expand All @@ -140,9 +140,9 @@ public String generate(String aUrl, OllamaAskRequest aRequest) throws IOExceptio

var result = new StringBuilder();
try (var is = response.body()) {
var iter = objectMapper.readerFor(OllamaAskResponse.class).readValues(is);
var iter = objectMapper.readerFor(OllamaGenerateResponse.class).readValues(is);
while (iter.hasNext()) {
var chunk = (OllamaAskResponse) iter.nextValue();
var chunk = (OllamaGenerateResponse) iter.nextValue();
result.append(chunk.getResponse());
}
}
Expand Down
Loading

0 comments on commit fc42860

Please sign in to comment.