forked from theopenconversationkit/tock
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRAGValidationService.kt
106 lines (89 loc) · 4.62 KB
/
RAGValidationService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* 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.rag.BotRAGConfiguration
import ai.tock.genai.orchestratorclient.requests.EMProviderSettingStatusQuery
import ai.tock.genai.orchestratorclient.requests.LLMProviderSettingStatusQuery
import ai.tock.genai.orchestratorclient.requests.VectorStoreProviderSettingStatusQuery
import ai.tock.genai.orchestratorclient.responses.ProviderSettingStatusResponse
import ai.tock.genai.orchestratorclient.services.EMProviderService
import ai.tock.genai.orchestratorclient.services.LLMProviderService
import ai.tock.genai.orchestratorclient.services.VectorStoreProviderService
import ai.tock.genai.orchestratorcore.utils.VectorStoreUtils
import ai.tock.shared.exception.error.ErrorMessage
import ai.tock.shared.injector
import ai.tock.shared.provide
object RAGValidationService {
private val llmProviderService: LLMProviderService get() = injector.provide()
private val emProviderService: EMProviderService get() = injector.provide()
private val vectorStoreProviderService: VectorStoreProviderService get() = injector.provide()
fun validate(ragConfig: BotRAGConfiguration): Set<ErrorMessage> {
val observabilitySetting = ObservabilityService.getObservabilityConfiguration(
ragConfig.namespace, ragConfig.botId, enabled = true
)?.setting
return mutableSetOf<ErrorMessage>().apply {
val questionCondensingLlmErrors = llmProviderService.checkSetting(
LLMProviderSettingStatusQuery(
ragConfig.questionCondensingLlmSetting!!,
observabilitySetting
)
).getErrors("LLM setting check failed (for question condensing)")
val questionAnsweringLlmErrors = llmProviderService.checkSetting(
LLMProviderSettingStatusQuery(
ragConfig.questionAnsweringLlmSetting!!,
observabilitySetting
)
).getErrors("LLM setting check failed (for question answering)")
val embeddingErrors = emProviderService.checkSetting(
EMProviderSettingStatusQuery(ragConfig.emSetting)
).getErrors("Embedding Model setting check failed")
val indexSessionIdErrors = validateIndexSessionId(ragConfig)
val vectorStoreErrors = (indexSessionIdErrors + embeddingErrors).takeIf { it.isEmpty() }?.let {
val vectorStoreSetting = VectorStoreService.getVectorStoreConfiguration(
ragConfig.namespace, ragConfig.botId, enabled = true
)?.setting
val (_, indexName) = VectorStoreUtils.getVectorStoreElements(
ragConfig.namespace,
ragConfig.botId,
ragConfig.indexSessionId!!,
ragConfig.maxDocumentsRetrieved,
vectorStoreSetting
)
vectorStoreProviderService.checkSetting(
VectorStoreProviderSettingStatusQuery(
vectorStoreSetting = vectorStoreSetting,
emSetting = ragConfig.emSetting,
documentIndexName = indexName
)
).getErrors("Vector store setting check failed")
} ?: emptySet()
addAll(questionCondensingLlmErrors + questionAnsweringLlmErrors + embeddingErrors + indexSessionIdErrors + vectorStoreErrors)
}
}
private fun validateIndexSessionId(ragConfig: BotRAGConfiguration): Set<ErrorMessage> {
val errors = mutableSetOf<ErrorMessage>()
if (ragConfig.enabled && ragConfig.indexSessionId.isNullOrBlank()) {
errors.add(
ErrorMessage(
message = "The index session ID is required to enable the RAG feature"
)
)
}
return errors
}
private fun ProviderSettingStatusResponse?.getErrors(message: String): Set<ErrorMessage> =
this?.errors?.map { ErrorMessage(message = message, params = errors) }?.toSet() ?: emptySet()
}