Skip to content

Commit

Permalink
Merge pull request #5286 from inception-project/feature/5283-Improve-…
Browse files Browse the repository at this point in the history
…usability-of-interactive-recommender-sidebar

#5283 - Improve usability of interactive recommender sidebar
  • Loading branch information
reckart authored Feb 10, 2025
2 parents 9d92d09 + 23ed288 commit 7bf0054
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,44 @@ public Stream<PromptContext> generate(RecommendationEngine aEngine, CAS aCas, in
});
}

return Stream.iterate(new PromptContext(0, 0, ""), state -> state != null, state -> {
var text = aCas.getDocumentText();
return Stream
.iterate(new PromptContext(0, 0, ""), state -> state != null,
state -> nextContext(aCas, state)) //
.filter(pc -> !pc.getText().isBlank());
}

if (text.substring(state.getRange().getEnd(), aCas.getDocumentText().length())
.isBlank()) {
return null;
}
private PromptContext nextContext(CAS aCas, PromptContext state)
{
var text = aCas.getDocumentText();

var lineBreakSequenceLength = 0;
var i = state.getRange().getEnd();
var seenContent = false;
while (i < text.length()) {
if (text.charAt(i) == '\n') {
lineBreakSequenceLength++;
}
else if (text.charAt(i) != '\r') {
if (lineBreakSequenceLength > 1 && seenContent) {
break;
}
if (text.substring(state.getRange().getEnd(), aCas.getDocumentText().length()).isBlank()) {
return null;
}

lineBreakSequenceLength = 0;
seenContent = true;
var lineBreakSequenceLength = 0;
var i = state.getRange().getEnd();
var seenContent = false;
while (i < text.length()) {
if (text.charAt(i) == '\n') {
lineBreakSequenceLength++;
}
else if (text.charAt(i) != '\r') {
if (lineBreakSequenceLength > 1 && seenContent) {
break;
}

i++;
lineBreakSequenceLength = 0;
seenContent = true;
}

var offset = new int[] { state.getRange().getEnd(), Math.min(i, text.length()) };
i++;
}

var offset = new int[] { state.getRange().getEnd(), Math.min(i, text.length()) };
var contextEnd = offset[1];

TrimUtils.trim(text, offset);
TrimUtils.trim(text, offset);

return new PromptContext(offset[0], offset[1], text.substring(offset[0], offset[1]));
}).skip(1);
return new PromptContext(offset[0], contextEnd, text.substring(offset[0], offset[1]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.uima.cas.CAS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -103,6 +104,10 @@ public Range predict(PredictionContext aContext, CAS aCas, int aBegin, int aEnd)
.generate(this, aCas, aBegin, aEnd, globalBindings);

contexts.forEach(promptContext -> {
if (StringUtils.isBlank(promptContext.getText())) {
return;
}

try {
var messages = new ArrayList<>(staticMessages);
messages.add(new ChatMessage(SYSTEM, "# Context\n\n" + promptContext.getText()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ void test() throws Exception
{
var cas = CasFactory.createCas();
cas.setDocumentText("""
This is paragraph 1.
This is paragraph 2 sentence 1.\r
Expand All @@ -51,11 +53,11 @@ void test() throws Exception
c -> c.getRange().getEnd(), //
PromptContext::getText) //
.containsExactly( //
tuple(0, 20, "This is paragraph 1."), //
tuple(22, 86,
tuple(2, 24, "This is paragraph 1."), //
tuple(24, 91,
"This is paragraph 2 sentence 1.\r\nThis is paragraph 2 sentence 2."), //
tuple(89, 120, "This is paragraph 3 sentence 1."), //
tuple(122, 153, "This is paragraph 4 sentence 1."));
tuple(91, 124, "This is paragraph 3 sentence 1."), //
tuple(124, 156, "This is paragraph 4 sentence 1."));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class OllamaChatRequest
private boolean stream;
private @JsonInclude(Include.NON_NULL) JsonNode format;
private @JsonInclude(Include.NON_DEFAULT) boolean raw;
private @JsonInclude(Include.NON_EMPTY) Map<Option<?>, Object> options = new HashMap<>();
private @JsonInclude(Include.NON_EMPTY) Map<String, Object> options = new HashMap<>();

private OllamaChatRequest(Builder builder)
{
Expand All @@ -49,7 +49,9 @@ private OllamaChatRequest(Builder builder)
format = builder.format;
stream = builder.stream;
raw = builder.raw;
options = builder.options;
for (var opt : builder.options.entrySet()) {
options.put(opt.getKey().getName(), opt.getValue());
}
}

public JsonNode getFormat()
Expand Down Expand Up @@ -77,7 +79,7 @@ public boolean isStream()
return stream;
}

public Map<Option<?>, Object> getOptions()
public Map<String, Object> getOptions()
{
return options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@
</div>
</div>
</div>
<div class="card-footer text-end">
<div class="card-footer d-flex justify-content-between">
<div class="form-check form-switch align-content-center">
<input wicket:id="keepExistingSuggestions" class="form-check-input" type="checkbox"/>
<label wicket:for="keepExistingSuggestions" class="form-check-label">
<wicket:label key="keepExistingSuggestions"/>
</label>
</div>
<button class="btn btn-success" wicket:id="execute">Execute</button>
</div>
</form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package de.tudarmstadt.ukp.inception.recommendation.sidebar.llm;

import static de.tudarmstadt.ukp.inception.recommendation.sidebar.llm.InteractiveRecommenderSidebarPrefs.KEY_INTERACTIVE_RECOMMENDER_SIDEBAR_PREFS;
import static de.tudarmstadt.ukp.inception.recommendation.tasks.PredictionTask.ReconciliationOption.KEEP_EXISTING;
import static de.tudarmstadt.ukp.inception.support.lambda.HtmlElementEvents.CHANGE_EVENT;
import static de.tudarmstadt.ukp.inception.support.lambda.LambdaBehavior.visibleWhen;
import static java.util.Collections.emptyList;
Expand All @@ -28,6 +29,7 @@

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
Expand Down Expand Up @@ -88,6 +90,7 @@ public class InteractiveRecommenderSidebar
private WebMarkupContainer traitsContainer;

private final IModel<Recommender> recommender;
private final IModel<Boolean> keepExisting;

private DropDownChoice<Recommender> recommenderChoice;

Expand All @@ -104,6 +107,7 @@ public InteractiveRecommenderSidebar(String aId, AnnotationActionHandler aAction

sidebarPrefs = new CompoundPropertyModel<>(Model.of(loadSidebarPrefs()));

keepExisting = new Model<>(false);
recommender = new Model<>();
loadLastUsedRecommender(recommender);

Expand Down Expand Up @@ -151,6 +155,8 @@ public InteractiveRecommenderSidebar(String aId, AnnotationActionHandler aAction
form.add(traitsContainer = new WebMarkupContainer(MID_TRAITS_CONTAINER));
traitsContainer.setOutputMarkupPlaceholderTag(true);

form.add(new CheckBox("keepExistingSuggestions", keepExisting).setOutputMarkupId(true));

actionChangeRecommender(null);

var executeButton = new LambdaAjaxButton<Recommender>(MID_EXECUTE, this::execute);
Expand Down Expand Up @@ -402,6 +408,7 @@ private void execute(AjaxRequestTarget aTarget, Form<Recommender> aForm) throws
.withCurrentDocument(document) //
.withDataOwner(dataOwner) //
.withRecommender(rec) //
.withReconciliationOptions(KEEP_EXISTING) //
.build();

schedulingService.enqueue(predictionTask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
# 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.
keepExistingSuggestions=Keep
Loading

0 comments on commit 7bf0054

Please sign in to comment.