-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
app/src/main/java/org/unitmesh/llmpoc/embedding/CustomEmbedding.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.unitmesh.llmpoc.embedding | ||
|
||
import cc.unitmesh.cf.core.utils.IdUtil | ||
import cc.unitmesh.nlp.embedding.Embedding | ||
import cc.unitmesh.nlp.similarity.CosineSimilarity | ||
import cc.unitmesh.rag.store.EmbeddingMatch | ||
import cc.unitmesh.rag.store.EmbeddingStore | ||
import cc.unitmesh.rag.store.Entry | ||
import java.util.* | ||
|
||
class CustomEmbedding<Embedded> : EmbeddingStore<Embedded> { | ||
private val entries: MutableList<Entry<Embedded>> = ArrayList() | ||
override fun add(embedding: Embedding): String { | ||
val id: String = IdUtil.uuid() | ||
add(id, embedding) | ||
return id | ||
} | ||
|
||
override fun add(id: String, embedding: Embedding) { | ||
add(id, embedding, null) | ||
} | ||
|
||
override fun add(embedding: Embedding, embedded: Embedded): String { | ||
val id: String = IdUtil.uuid() | ||
add(id, embedding, embedded) | ||
return id | ||
} | ||
|
||
private fun add(id: String, embedding: Embedding, embedded: Embedded?) { | ||
entries.add(Entry(id, embedding, embedded)) | ||
} | ||
|
||
override fun addAll(embeddings: List<Embedding>): List<String> { | ||
val ids: MutableList<String> = ArrayList() | ||
for (embedding in embeddings) { | ||
ids.add(add(embedding)) | ||
} | ||
return ids | ||
} | ||
|
||
override fun addAll(embeddings: List<Embedding>, embedded: List<Embedded>): List<String> { | ||
require(embeddings.size == embedded.size) { "The list of embeddings and embedded must have the same size" } | ||
val ids: MutableList<String> = ArrayList() | ||
for (i in embeddings.indices) { | ||
ids.add(add(embeddings[i], embedded[i])) | ||
} | ||
return ids | ||
} | ||
|
||
override fun findRelevant( | ||
referenceEmbedding: Embedding, | ||
maxResults: Int, | ||
minScore: Double, | ||
): List<EmbeddingMatch<Embedded>> { | ||
val comparator = Comparator.comparingDouble(EmbeddingMatch<Embedded>::score) | ||
val matches = PriorityQueue(comparator) | ||
|
||
for (entry in entries) { | ||
val score = CosineSimilarity.between(entry.embedding, referenceEmbedding) | ||
if (score >= minScore) { | ||
matches.add(EmbeddingMatch(score, entry.id, entry.embedding, entry.embedded!!)) | ||
|
||
if (matches.size > maxResults) { | ||
matches.poll() | ||
} | ||
} | ||
} | ||
|
||
val result = ArrayList(matches) | ||
result.sortWith(comparator) | ||
result.reverse() | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters