-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DERCBOT-1267] Gen AI - Document Compressor
- Loading branch information
Showing
68 changed files
with
1,259 additions
and
158 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
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
51 changes: 51 additions & 0 deletions
51
bot/admin/server/src/main/kotlin/model/BotDocumentCompressorConfigurationDTO.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,51 @@ | ||
/* | ||
* Copyright (C) 2017/2021 e-voyageurs technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package ai.tock.bot.admin.model | ||
|
||
|
||
import ai.tock.bot.admin.bot.compressor.BotDocumentCompressorConfiguration | ||
import ai.tock.genai.orchestratorcore.models.compressor.DocumentCompressorSetting | ||
import org.litote.kmongo.newId | ||
import org.litote.kmongo.toId | ||
|
||
data class BotDocumentCompressorConfigurationDTO( | ||
val id: String? = null, | ||
val namespace: String, | ||
val botId: String, | ||
val enabled: Boolean = false, | ||
val setting: DocumentCompressorSetting, | ||
) { | ||
constructor(configuration: BotDocumentCompressorConfiguration) : this( | ||
id = configuration._id.toString(), | ||
namespace = configuration.namespace, | ||
botId = configuration.botId, | ||
enabled = configuration.enabled, | ||
setting = configuration.setting, | ||
) | ||
|
||
fun toBotDocumentCompressorConfiguration(): BotDocumentCompressorConfiguration = | ||
BotDocumentCompressorConfiguration( | ||
_id = id?.toId() ?: newId(), | ||
namespace = namespace, | ||
botId = botId, | ||
enabled = enabled, | ||
setting = setting, | ||
) | ||
} | ||
|
||
|
||
|
110 changes: 110 additions & 0 deletions
110
bot/admin/server/src/main/kotlin/service/DocumentCompressorService.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,110 @@ | ||
/* | ||
* Copyright (C) 2017/2021 e-voyageurs technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package ai.tock.bot.admin.service | ||
|
||
import ai.tock.bot.admin.BotAdminService | ||
import ai.tock.bot.admin.bot.compressor.BotDocumentCompressorConfiguration | ||
import ai.tock.bot.admin.bot.compressor.BotDocumentCompressorConfigurationDAO | ||
import ai.tock.bot.admin.model.BotDocumentCompressorConfigurationDTO | ||
import ai.tock.shared.exception.rest.BadRequestException | ||
import ai.tock.shared.injector | ||
import ai.tock.shared.provide | ||
import ai.tock.shared.vertx.WebVerticle | ||
import com.mongodb.MongoWriteException | ||
import mu.KLogger | ||
import mu.KotlinLogging | ||
|
||
/** | ||
* Service that manage the document compressor functionality | ||
*/ | ||
object DocumentCompressorService { | ||
|
||
private val logger: KLogger = KotlinLogging.logger {} | ||
private val documentCompressorConfigurationDAO: BotDocumentCompressorConfigurationDAO get() = injector.provide() | ||
/** | ||
* Get the Document Compressor Configuration | ||
* @param namespace: the namespace | ||
* @param botId: the bot ID | ||
*/ | ||
fun getDocumentCompressorConfiguration(namespace: String, botId: String): BotDocumentCompressorConfiguration? { | ||
return documentCompressorConfigurationDAO.findByNamespaceAndBotId(namespace, botId) | ||
} | ||
|
||
/** | ||
* Get the Document Compressor Configuration | ||
* @param namespace: the namespace | ||
* @param botId: the botId | ||
* @param enabled: the document compressor activation (enabled or not) | ||
*/ | ||
fun getDocumentCompressorConfiguration(namespace: String, botId: String, enabled: Boolean): BotDocumentCompressorConfiguration? { | ||
return documentCompressorConfigurationDAO.findByNamespaceAndBotIdAndEnabled(namespace, botId, enabled) | ||
} | ||
|
||
/** | ||
* Deleting the Document Compressor Configuration | ||
* @param namespace: the namespace | ||
* @param botId: the bot ID | ||
*/ | ||
fun deleteConfig(namespace: String, botId: String) { | ||
val documentCompressorConfig = documentCompressorConfigurationDAO.findByNamespaceAndBotId(namespace, botId) | ||
?: WebVerticle.badRequest("No Document Compressor Configuration is defined yet [namespace: $namespace, botId: $botId]") | ||
logger.info { "Deleting the Document Compressor Configuration [namespace: $namespace, botId: $botId]" } | ||
return documentCompressorConfigurationDAO.delete(documentCompressorConfig._id) | ||
} | ||
|
||
/** | ||
* Save Document Compressor Configuration and filter errors | ||
* @param documentCompressorConfig : the document compressor configuration to create or update | ||
* @throws [BadRequestException] if the document compressor configuration is invalid | ||
* @return [BotDocumentCompressorConfiguration] | ||
*/ | ||
fun saveDocumentCompressor( | ||
documentCompressorConfig: BotDocumentCompressorConfigurationDTO | ||
): BotDocumentCompressorConfiguration { | ||
BotAdminService.getBotConfigurationsByNamespaceAndBotId(documentCompressorConfig.namespace, documentCompressorConfig.botId).firstOrNull() | ||
?: WebVerticle.badRequest("No bot configuration is defined yet [namespace: ${documentCompressorConfig.namespace}, botId = ${documentCompressorConfig.botId}]") | ||
return saveDocumentCompressorConfiguration(documentCompressorConfig) | ||
} | ||
|
||
/** | ||
* Save the Document Compressor Configuration | ||
* @param documentCompressorConfiguration [BotDocumentCompressorConfigurationDTO] | ||
*/ | ||
private fun saveDocumentCompressorConfiguration( | ||
documentCompressorConfiguration: BotDocumentCompressorConfigurationDTO | ||
): BotDocumentCompressorConfiguration { | ||
val documentCompressorConfig = documentCompressorConfiguration.toBotDocumentCompressorConfiguration() | ||
|
||
// Check validity of the document compressor configuration | ||
if(documentCompressorConfig.enabled) { | ||
DocumentCompressorValidationService.validate(documentCompressorConfig).let { errors -> | ||
if (errors.isNotEmpty()) { | ||
throw BadRequestException(errors) | ||
} | ||
} | ||
} | ||
|
||
return try { | ||
documentCompressorConfigurationDAO.save(documentCompressorConfig) | ||
} catch (e: MongoWriteException) { | ||
throw BadRequestException(e.message ?: "Document Compressor Configuration: registration failed on mongo ") | ||
} catch (e: Exception) { | ||
throw BadRequestException(e.message ?: "Document Compressor Configuration: registration failed ") | ||
} | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
bot/admin/server/src/main/kotlin/service/DocumentCompressorValidationService.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,49 @@ | ||
/* | ||
* Copyright (C) 2017/2021 e-voyageurs technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package ai.tock.bot.admin.service | ||
|
||
import ai.tock.bot.admin.bot.compressor.BotDocumentCompressorConfiguration | ||
import ai.tock.genai.orchestratorclient.requests.DocumentCompressorProviderSettingStatusQuery | ||
import ai.tock.genai.orchestratorclient.responses.ProviderSettingStatusResponse | ||
import ai.tock.genai.orchestratorclient.services.DocumentCompressorProviderService | ||
import ai.tock.shared.exception.error.ErrorMessage | ||
import ai.tock.shared.injector | ||
import ai.tock.shared.provide | ||
|
||
|
||
object DocumentCompressorValidationService { | ||
|
||
private val documentCompressorProviderService: DocumentCompressorProviderService get() = injector.provide() | ||
|
||
fun validate(config: BotDocumentCompressorConfiguration): Set<ErrorMessage> { | ||
return mutableSetOf<ErrorMessage>().apply { | ||
addAll( | ||
documentCompressorProviderService | ||
.checkSetting( | ||
DocumentCompressorProviderSettingStatusQuery( | ||
setting = config.setting | ||
) | ||
) | ||
.getErrors("Document Compressor setting check failed") | ||
) | ||
} | ||
} | ||
|
||
private fun ProviderSettingStatusResponse?.getErrors(message: String): Set<ErrorMessage> = | ||
this?.errors?.map { ErrorMessage(message = message, params = errors) }?.toSet() ?: emptySet() | ||
|
||
} |
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
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
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
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
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
28 changes: 28 additions & 0 deletions
28
bot/engine/src/main/kotlin/admin/bot/compressor/BotDocumentCompressorConfiguration.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,28 @@ | ||
/* | ||
* Copyright (C) 2017/2021 e-voyageurs technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package ai.tock.bot.admin.bot.compressor | ||
|
||
import ai.tock.genai.orchestratorcore.models.compressor.DocumentCompressorSetting | ||
import org.litote.kmongo.Id | ||
|
||
data class BotDocumentCompressorConfiguration( | ||
val _id: Id<BotDocumentCompressorConfiguration>, | ||
val namespace: String, | ||
val botId: String, | ||
val enabled: Boolean, | ||
val setting: DocumentCompressorSetting, | ||
) |
32 changes: 32 additions & 0 deletions
32
bot/engine/src/main/kotlin/admin/bot/compressor/BotDocumentCompressorConfigurationDAO.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,32 @@ | ||
/* | ||
* Copyright (C) 2017/2021 e-voyageurs technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package ai.tock.bot.admin.bot.compressor | ||
|
||
import org.litote.kmongo.Id | ||
|
||
interface BotDocumentCompressorConfigurationDAO { | ||
|
||
fun listenChanges(listener: () -> Unit) | ||
|
||
fun save(conf: BotDocumentCompressorConfiguration): BotDocumentCompressorConfiguration | ||
|
||
fun findByNamespaceAndBotId(namespace: String, botId: String): BotDocumentCompressorConfiguration? | ||
|
||
fun findByNamespaceAndBotIdAndEnabled(namespace: String, botId: String, enabled: Boolean): BotDocumentCompressorConfiguration? | ||
|
||
fun delete(id: Id<BotDocumentCompressorConfiguration>) | ||
} |
Oops, something went wrong.