-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interface for VectorStoreService (#510)
* Interface for VectorStoreService * Build fixed * Loading vector-store by demand * Removed shadow var
- Loading branch information
1 parent
122bf81
commit 75dde7b
Showing
10 changed files
with
164 additions
and
109 deletions.
There are no files selected for viewing
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
8 changes: 8 additions & 0 deletions
8
server/src/main/kotlin/com/xebia/functional/xef/server/db/VectorStoreConfig.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,8 @@ | ||
package com.xebia.functional.xef.server.db | ||
|
||
import com.xebia.functional.xef.server.services.VectorStoreService | ||
import org.slf4j.Logger | ||
|
||
interface VectorStoreConfig { | ||
suspend fun getVectorStoreService(logger: Logger): VectorStoreService | ||
} |
16 changes: 16 additions & 0 deletions
16
server/src/main/kotlin/com/xebia/functional/xef/server/db/local/LocalVectorStoreConfig.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,16 @@ | ||
package com.xebia.functional.xef.server.db.local | ||
|
||
import com.xebia.functional.xef.server.db.VectorStoreConfig | ||
import com.xebia.functional.xef.server.services.LocalVectorStoreService | ||
import kotlinx.serialization.Serializable | ||
import org.slf4j.Logger | ||
|
||
@Serializable | ||
class LocalVectorStoreConfig() : VectorStoreConfig { | ||
override suspend fun getVectorStoreService(logger: Logger): LocalVectorStoreService = | ||
LocalVectorStoreService() | ||
|
||
companion object { | ||
fun load(): LocalVectorStoreConfig = LocalVectorStoreConfig() | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
server/src/main/kotlin/com/xebia/functional/xef/server/db/psql/PSQLVectorStoreConfig.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,73 @@ | ||
package com.xebia.functional.xef.server.db.psql | ||
|
||
import com.typesafe.config.Config | ||
import com.typesafe.config.ConfigFactory | ||
import com.xebia.functional.xef.server.db.VectorStoreConfig | ||
import com.xebia.functional.xef.server.services.PostgreSQLXef | ||
import com.xebia.functional.xef.server.services.PostgresVectorStoreService | ||
import com.xebia.functional.xef.server.services.RepositoryService | ||
import com.xebia.functional.xef.store.migrations.PsqlVectorStoreConfig | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.hocon.Hocon | ||
import org.slf4j.Logger | ||
|
||
@Serializable | ||
class PSQLVectorStoreConfig( | ||
val host: String, | ||
val port: Int, | ||
val database: String, | ||
val driver: String, | ||
val user: String, | ||
val password: String, | ||
val collectionName: String, | ||
val vectorSize: Int | ||
) : VectorStoreConfig { | ||
|
||
fun getUrl(): String = "jdbc:postgresql://$host:$port/$database" | ||
|
||
override suspend fun getVectorStoreService(logger: Logger): PostgresVectorStoreService { | ||
val vectorStoreHikariDataSource = | ||
RepositoryService.getHikariDataSource(getUrl(), user, password) | ||
return PostgresVectorStoreService(toPGVectorStoreConfig(), logger, vectorStoreHikariDataSource) | ||
} | ||
|
||
private fun toPGVectorStoreConfig() = | ||
PostgreSQLXef.PGVectorStoreConfig( | ||
dbConfig = | ||
PostgreSQLXef.DBConfig( | ||
host = host, | ||
port = port, | ||
database = database, | ||
user = user, | ||
password = password | ||
), | ||
collectionName = collectionName, | ||
vectorSize = vectorSize | ||
) | ||
|
||
companion object { | ||
@OptIn(ExperimentalSerializationApi::class) | ||
suspend fun load(configNamespace: String, config: Config?): PSQLVectorStoreConfig = | ||
withContext(Dispatchers.IO) { | ||
val rawConfig = config ?: ConfigFactory.load().resolve() | ||
val jdbcConfig = rawConfig.getConfig(configNamespace) | ||
val psqlConfig = Hocon.decodeFromConfig(serializer(), jdbcConfig) | ||
psqlConfig.toPSQLConfig().migrate() | ||
psqlConfig | ||
} | ||
|
||
private fun PSQLVectorStoreConfig.toPSQLConfig(): PsqlVectorStoreConfig = | ||
PsqlVectorStoreConfig( | ||
host = this.host, | ||
port = this.port, | ||
database = this.database, | ||
driver = this.driver, | ||
user = this.user, | ||
password = this.password, | ||
migrationsTable = "migration" | ||
) | ||
} | ||
} |
99 changes: 0 additions & 99 deletions
99
server/src/main/kotlin/com/xebia/functional/xef/server/db/psql/XefVectorStoreConfig.kt
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
server/src/main/kotlin/com/xebia/functional/xef/server/services/LocalVectorStoreService.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,11 @@ | ||
package com.xebia.functional.xef.server.services | ||
|
||
import com.xebia.functional.xef.conversation.llm.openai.OpenAI | ||
import com.xebia.functional.xef.server.http.routes.Provider | ||
import com.xebia.functional.xef.store.LocalVectorStore | ||
import com.xebia.functional.xef.store.VectorStore | ||
|
||
class LocalVectorStoreService : VectorStoreService() { | ||
override fun getVectorStore(provider: Provider, token: String?): VectorStore = | ||
LocalVectorStore(OpenAI().DEFAULT_EMBEDDING) | ||
} |
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
4 changes: 4 additions & 0 deletions
4
server/src/main/kotlin/com/xebia/functional/xef/server/services/TokenRepositoryService.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
40 changes: 37 additions & 3 deletions
40
server/src/main/kotlin/com/xebia/functional/xef/server/services/VectorStoreService.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 |
---|---|---|
@@ -1,11 +1,45 @@ | ||
package com.xebia.functional.xef.server.services | ||
|
||
import com.typesafe.config.Config | ||
import com.typesafe.config.ConfigFactory | ||
import com.xebia.functional.xef.server.db.VectorStoreConfig | ||
import com.xebia.functional.xef.server.db.local.LocalVectorStoreConfig | ||
import com.xebia.functional.xef.server.db.psql.PSQLVectorStoreConfig | ||
import com.xebia.functional.xef.server.http.routes.Provider | ||
import com.xebia.functional.xef.store.VectorStore | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.hocon.Hocon | ||
|
||
abstract class VectorStoreService { | ||
enum class XefVectorStoreType { | ||
PSQL, | ||
LOCAL | ||
} | ||
|
||
abstract fun addCollection(): Unit | ||
abstract class VectorStoreService { | ||
abstract fun getVectorStore( | ||
provider: Provider = Provider.OPENAI, | ||
token: String? = null | ||
): VectorStore | ||
|
||
abstract fun getVectorStore(provider: Provider = Provider.OPENAI, token: String): VectorStore | ||
companion object { | ||
@OptIn(ExperimentalSerializationApi::class) | ||
suspend fun load(configNamespace: String, config: Config?): VectorStoreConfig = | ||
withContext(Dispatchers.IO) { | ||
val rawConfig = config ?: ConfigFactory.load().resolve() | ||
val jdbcConfig = rawConfig.getConfig(configNamespace) | ||
val typeConfig = Hocon.decodeFromConfig(VectorStoreTypeConfig.serializer(), jdbcConfig) | ||
when (typeConfig.type) { | ||
XefVectorStoreType.PSQL -> PSQLVectorStoreConfig.load(configNamespace, rawConfig) | ||
XefVectorStoreType.LOCAL -> LocalVectorStoreConfig.load() | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Serializable | ||
class VectorStoreTypeConfig( | ||
val type: XefVectorStoreType, | ||
) |
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