(fix)(common):check embedding exist before add to store (#563)

This commit is contained in:
Scott
2023-12-22 14:41:30 +08:00
committed by GitHub
parent 0a1f446fb8
commit 090abbceed

View File

@@ -237,14 +237,22 @@ public class InMemoryS2EmbeddingStore implements S2EmbeddingStore {
}
public void add(String id, Embedding embedding, Embedded embedded) {
entries.add(new InMemoryEmbeddingStore.Entry<>(id, embedding, embedded));
if (checkEmbeddingNotExists(embedding)) {
entries.add(new InMemoryEmbeddingStore.Entry<>(id, embedding, embedded));
}
}
private boolean checkEmbeddingNotExists(Embedding embedding) {
return entries.stream().noneMatch(entry -> entry.embedding.equals(embedding));
}
@Override
public List<String> addAll(List<Embedding> embeddings) {
List<String> ids = new ArrayList<>();
for (Embedding embedding : embeddings) {
ids.add(add(embedding));
if (checkEmbeddingNotExists(embedding)) {
ids.add(add(embedding));
}
}
return ids;
}
@@ -257,7 +265,9 @@ public class InMemoryS2EmbeddingStore implements S2EmbeddingStore {
List<String> ids = new ArrayList<>();
for (int i = 0; i < embeddings.size(); i++) {
ids.add(add(embeddings.get(i), embedded.get(i)));
if (checkEmbeddingNotExists(embeddings.get(i))) {
ids.add(add(embeddings.get(i), embedded.get(i)));
}
}
return ids;
}