Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lucene as vector store #59

Merged
merged 4 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ kotlin {
implementation(libs.jtokk.it)
implementation(libs.scrape.it)
implementation(libs.rss.reader)
implementation(libs.apache.lucene)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.xebia.functional.vectorstores

import com.xebia.functional.embeddings.Embedding
import com.xebia.functional.embeddings.Embeddings
import com.xebia.functional.llm.openai.EmbeddingModel
import com.xebia.functional.llm.openai.RequestConfig
import org.apache.lucene.document.Document
import org.apache.lucene.document.Field
import org.apache.lucene.document.KnnFloatVectorField
import org.apache.lucene.document.TextField
import org.apache.lucene.index.*
import org.apache.lucene.search.FuzzyQuery
import org.apache.lucene.search.IndexSearcher
import org.apache.lucene.search.KnnFloatVectorQuery
import org.apache.lucene.search.Query
import org.apache.lucene.store.Directory
import org.apache.lucene.store.MMapDirectory
import java.nio.file.Path

open class Lucene(
private val writer: IndexWriter,
private val searcher: IndexSearcher,
private val embeddings: Embeddings,
private val similarity: VectorSimilarityFunction = VectorSimilarityFunction.EUCLIDEAN
juanpedromoreno marked this conversation as resolved.
Show resolved Hide resolved
): VectorStore, AutoCloseable {

constructor(
writer: IndexWriter,
embeddings: Embeddings,
similarity: VectorSimilarityFunction = VectorSimilarityFunction.EUCLIDEAN
): this(writer, IndexSearcher(DirectoryReader.open(writer)), embeddings, similarity)

private val requestConfig =
RequestConfig(EmbeddingModel.TextEmbeddingAda002, RequestConfig.Companion.User("user"))

override suspend fun addTexts(texts: List<String>) = texts.forEach {
val embedding = embeddings.embedQuery(it, requestConfig)
val doc = Document().apply {
add(TextField("contents", it, Field.Store.YES))
add(KnnFloatVectorField("embedding", embedding.toFloatArray(), similarity))
}
writer.addDocument(doc)
}

override suspend fun similaritySearch(query: String, limit: Int): List<String> =
search(FuzzyQuery(Term("contents", query)), limit)

override suspend fun similaritySearchByVector(embedding: Embedding, limit: Int): List<String> =
search(KnnFloatVectorQuery("embedding", embedding.data.toFloatArray(), limit), limit)

private fun search(q: Query, limit: Int): List<String> = searcher.search(q, limit).scoreDocs.map {
searcher.storedFields().document(it.doc).get("contents")
}

override fun close() {
writer.close()
}
}

class DirectoryLucene(
private val directory: Directory,
writerConfig: IndexWriterConfig = IndexWriterConfig(),
embeddings: Embeddings,
similarity: VectorSimilarityFunction = VectorSimilarityFunction.EUCLIDEAN
): Lucene(IndexWriter(directory, writerConfig), embeddings, similarity) {
override fun close() {
super.close()
directory.close()
}
}

fun InMemoryLucene(
path: Path,
writerConfig: IndexWriterConfig = IndexWriterConfig(),
embeddings: Embeddings,
similarity: VectorSimilarityFunction = VectorSimilarityFunction.EUCLIDEAN
): DirectoryLucene = DirectoryLucene(MMapDirectory(path), writerConfig, embeddings, similarity)

fun List<Embedding>.toFloatArray(): FloatArray = flatMap { it.data }.toFloatArray()
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ munitCatsEffect = "1.0.5"
scrapeit = "1.1.5"
jtokkit = "0.4.0"
rssreader = "3.4.4"
lucene = "9.6.0"

[libraries]
arrow-core = { module = "io.arrow-kt:arrow-core", version.ref = "arrow" }
Expand Down Expand Up @@ -89,6 +90,7 @@ scrape-it-browser-fetcher = { module = "it.skrape:skrapeit-browser-fetcher", ver
scrape-it-async-fetcher = { module = "it.skrape:skrapeit-asyn-fetcher", version.ref = "scrapeit" }
jtokk-it = { module = "com.knuddels:jtokkit", version.ref = "jtokkit" }
rss-reader = { module = "com.apptasticsoftware:rssreader", version.ref = "rssreader" }
apache-lucene = { module = "org.apache.lucene:lucene-core", version.ref = "lucene" }

[bundles]
arrow = [
Expand Down