Skip to content

Commit

Permalink
#4292 - ollama-based recommender
Browse files Browse the repository at this point in the history
- Added few-shot example
- Fix handling of JSON responses consisting of numbers
  • Loading branch information
reckart committed Nov 28, 2023
1 parent 5162dae commit efb78fa
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ List<Pair<String, String>> extractMentionFromJson(String aResponse)
var fieldEntry = fieldIterator.next();
if (fieldEntry.getValue().isArray()) {
for (var item : fieldEntry.getValue()) {
if (item.isTextual()) {
if (item.isTextual() || item.isNumber() || item.isBoolean()) {
// Looks like this
// "Person": ["John"],
// "Location": ["diner", "Starbucks"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@
{{ text }}</s>
- name: Extract named entities from sentenes (dynamic few-shot)
promptingMode: per-sentence
format: json
extractionMode: mentions-from-json
prompt: |-
Identify all politicians in the following text and return them as JSON.
{% for example in examples %}
Text:
'''
{{ example.getText() }}
'''
Response:
{{ example.getLabelledMentions() | tojson }}
{% endfor %}
Text:
'''
{{ text }}
'''
Response:
- name: Summarize each sentence in a word
promptingMode: per-sentence
extractionMode: response-as-label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ void testPerDocumentUsingMentionsFromJsonList_Numbers() throws Exception

var traits = new OllamaRecommenderTraits();
traits.setModel("mistral");
traits.setPrompt(
"Identify all even numbers in the following list and return them as JSON.\n\n{{ text }}");
traits.setPrompt("""
Identify all even numbers in the following list and return them as JSON.
{{ text }}""");
traits.setFormat(JSON);
traits.setPromptingMode(PER_DOCUMENT);
traits.setExtractionMode(MENTIONS_FROM_JSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,22 @@ class MentionsFromJsonExtractorTest
private MentionsFromJsonExtractor sut = new MentionsFromJsonExtractor();

@Test
void testExtractMentionFromJson_variant1()
void testExtractMentionFromJson_categorizedNumbers()
{
var json = """
{
"even_numbers": [2, 4, 6]
}
""";
assertThat(sut.extractMentionFromJson(json)) //
.containsExactly( //
Pair.of("2", "even_numbers"), //
Pair.of("4", "even_numbers"), //
Pair.of("6", "even_numbers"));
}

@Test
void testExtractMentionFromJson_categorizedStrings()
{
var json = """
{
Expand All @@ -44,7 +59,7 @@ void testExtractMentionFromJson_variant1()
}

@Test
void testExtractMentionFromJson_variant2()
void testExtractMentionFromJson_categorizedObjects()
{
var json = """
{
Expand All @@ -63,7 +78,7 @@ void testExtractMentionFromJson_variant2()
}

@Test
void testExtractMentionFromJson_variant3()
void testExtractMentionFromJson_namedObjects()
{
var json = """
{
Expand All @@ -80,7 +95,7 @@ void testExtractMentionFromJson_variant3()
}

@Test
void testExtractMentionFromJson_variant4()
void testExtractMentionFromJson_keyValue()
{
var json = """
{
Expand All @@ -98,9 +113,9 @@ void testExtractMentionFromJson_variant4()
Pair.of("Don Horny", "politician"));
}

@Disabled("Cannot really tell this one apart from variant 4")
@Disabled("Cannot really tell this one apart from keyValue")
@Test
void testExtractMentionFromJson_variant5()
void testExtractMentionFromJson_valueKey()
{
// We assume that the first item is the most relevant one (the
// mention) so we do not get a bad mention in cases like this:
Expand Down

0 comments on commit efb78fa

Please sign in to comment.