Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit faa494e

Browse files
author
Vaijanath Rao
committed
removed std print and adding ranking test.
1 parent 06b11a7 commit faa494e

File tree

5 files changed

+97
-10
lines changed

5 files changed

+97
-10
lines changed

src/main/cpp/jllama.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,6 @@ JNIEXPORT jobject JNICALL Java_de_kherud_llama_LlamaModel_rerank(JNIEnv *env, jo
765765

766766
const auto out_res = result->to_json();
767767

768-
std::cout << out_res.dump(4) << std::endl;
769-
770768
if (result->is_stop()) {
771769
for (const int id_task : task_ids) {
772770
ctx_server->queue_results.remove_waiting_task_id(id_task);

src/main/java/de/kherud/llama/LlamaIterator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public LlamaOutput next() {
3535
}
3636
LlamaOutput output = model.receiveCompletion(taskId);
3737
hasNext = !output.stop;
38+
if (output.stop) {
39+
model.releaseTask(taskId);
40+
}
3841
return output;
3942
}
4043

src/main/java/de/kherud/llama/LlamaModel.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
import java.lang.annotation.Native;
77
import java.nio.charset.StandardCharsets;
8+
import java.util.ArrayList;
89
import java.util.List;
10+
import java.util.Map;
911
import java.util.function.BiConsumer;
1012

1113
/**
@@ -131,13 +133,34 @@ public void close() {
131133

132134
private native void delete();
133135

134-
private native void releaseTask(int taskId);
136+
native void releaseTask(int taskId);
135137

136138
private static native byte[] jsonSchemaToGrammarBytes(String schema);
137139

138140
public static String jsonSchemaToGrammar(String schema) {
139141
return new String(jsonSchemaToGrammarBytes(schema), StandardCharsets.UTF_8);
140142
}
141143

144+
public List<Pair<String, Float>> rerank(boolean reRank, String query, String ... documents) {
145+
LlamaOutput output = rerank(query, documents);
146+
147+
Map<String, Float> scoredDocumentMap = output.probabilities;
148+
149+
List<Pair<String, Float>> rankedDocuments = new ArrayList<>();
150+
151+
if (reRank) {
152+
// Sort in descending order based on Float values
153+
scoredDocumentMap.entrySet()
154+
.stream()
155+
.sorted((a, b) -> Float.compare(b.getValue(), a.getValue())) // Descending order
156+
.forEach(entry -> rankedDocuments.add(new Pair<>(entry.getKey(), entry.getValue())));
157+
} else {
158+
// Copy without sorting
159+
scoredDocumentMap.forEach((key, value) -> rankedDocuments.add(new Pair<>(key, value)));
160+
}
161+
162+
return rankedDocuments;
163+
}
164+
142165
public native LlamaOutput rerank(String query, String... documents);
143166
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package de.kherud.llama;
2+
3+
import java.util.Objects;
4+
5+
public class Pair<K, V> {
6+
7+
private final K key;
8+
private final V value;
9+
10+
public Pair(K key, V value) {
11+
this.key = key;
12+
this.value = value;
13+
}
14+
15+
public K getKey() {
16+
return key;
17+
}
18+
19+
public V getValue() {
20+
return value;
21+
}
22+
23+
@Override
24+
public int hashCode() {
25+
return Objects.hash(key, value);
26+
}
27+
28+
@Override
29+
public boolean equals(Object obj) {
30+
if (this == obj)
31+
return true;
32+
if (obj == null)
33+
return false;
34+
if (getClass() != obj.getClass())
35+
return false;
36+
Pair other = (Pair) obj;
37+
return Objects.equals(key, other.key) && Objects.equals(value, other.value);
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return "Pair [key=" + key + ", value=" + value + "]";
43+
}
44+
45+
46+
47+
48+
}

src/test/java/de/kherud/llama/RerankingModelTest.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.kherud.llama;
22

3+
import java.util.List;
34
import java.util.Map;
45

56
import org.junit.AfterClass;
@@ -10,6 +11,13 @@
1011
public class RerankingModelTest {
1112

1213
private static LlamaModel model;
14+
15+
String query = "Machine learning is";
16+
String[] TEST_DOCUMENTS = new String[] {
17+
"A machine is a physical system that uses power to apply forces and control movement to perform an action. The term is commonly applied to artificial devices, such as those employing engines or motors, but also to natural biological macromolecules, such as molecular machines.",
18+
"Learning is the process of acquiring new understanding, knowledge, behaviors, skills, values, attitudes, and preferences. The ability to learn is possessed by humans, non-human animals, and some machines; there is also evidence for some kind of learning in certain plants.",
19+
"Machine learning is a field of study in artificial intelligence concerned with the development and study of statistical algorithms that can learn from data and generalize to unseen data, and thus perform tasks without explicit instructions.",
20+
"Paris, capitale de la France, est une grande ville européenne et un centre mondial de l'art, de la mode, de la gastronomie et de la culture. Son paysage urbain du XIXe siècle est traversé par de larges boulevards et la Seine." };
1321

1422
@BeforeClass
1523
public static void setup() {
@@ -28,12 +36,7 @@ public static void tearDown() {
2836
@Test
2937
public void testReRanking() {
3038

31-
String query = "Machine learning is";
32-
String[] TEST_DOCUMENTS = new String[] {
33-
"A machine is a physical system that uses power to apply forces and control movement to perform an action. The term is commonly applied to artificial devices, such as those employing engines or motors, but also to natural biological macromolecules, such as molecular machines.",
34-
"Learning is the process of acquiring new understanding, knowledge, behaviors, skills, values, attitudes, and preferences. The ability to learn is possessed by humans, non-human animals, and some machines; there is also evidence for some kind of learning in certain plants.",
35-
"Machine learning is a field of study in artificial intelligence concerned with the development and study of statistical algorithms that can learn from data and generalize to unseen data, and thus perform tasks without explicit instructions.",
36-
"Paris, capitale de la France, est une grande ville européenne et un centre mondial de l'art, de la mode, de la gastronomie et de la culture. Son paysage urbain du XIXe siècle est traversé par de larges boulevards et la Seine." };
39+
3740
LlamaOutput llamaOutput = model.rerank(query, TEST_DOCUMENTS[0], TEST_DOCUMENTS[1], TEST_DOCUMENTS[2],
3841
TEST_DOCUMENTS[3]);
3942

@@ -64,5 +67,17 @@ public void testReRanking() {
6467

6568

6669
}
67-
70+
71+
@Test
72+
public void testSortedReRanking() {
73+
List<Pair<String, Float>> rankedDocuments = model.rerank(true, query, TEST_DOCUMENTS);
74+
Assert.assertEquals(rankedDocuments.size(), TEST_DOCUMENTS.length);
75+
76+
// Check the ranking order: each score should be >= the next one
77+
for (int i = 0; i < rankedDocuments.size() - 1; i++) {
78+
float currentScore = rankedDocuments.get(i).getValue();
79+
float nextScore = rankedDocuments.get(i + 1).getValue();
80+
Assert.assertTrue("Ranking order incorrect at index " + i, currentScore >= nextScore);
81+
}
82+
}
6883
}

0 commit comments

Comments
 (0)