forked from theopenconversationkit/tock
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBotRAGConfigurationDTO.kt
123 lines (115 loc) · 5 KB
/
BotRAGConfigurationDTO.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* 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.rag.BotRAGConfiguration
import ai.tock.bot.admin.service.VectorStoreService
import ai.tock.genai.orchestratorclient.requests.PromptTemplate
import ai.tock.genai.orchestratorcore.mappers.EMSettingMapper
import ai.tock.genai.orchestratorcore.mappers.LLMSettingMapper
import ai.tock.genai.orchestratorcore.models.Constants
import ai.tock.genai.orchestratorcore.models.em.EMSettingDTO
import ai.tock.genai.orchestratorcore.models.em.toDTO
import ai.tock.genai.orchestratorcore.models.llm.LLMSettingDTO
import ai.tock.genai.orchestratorcore.models.llm.toDTO
import ai.tock.genai.orchestratorcore.utils.VectorStoreUtils
import org.litote.kmongo.newId
import org.litote.kmongo.toId
data class BotRAGConfigurationDTO(
val id: String? = null,
val namespace: String,
val botId: String,
val enabled: Boolean = false,
val questionCondensingLlmSetting: LLMSettingDTO? = null,
val questionCondensingPrompt: PromptTemplate? = null,
val questionAnsweringLlmSetting: LLMSettingDTO,
val questionAnsweringPrompt: PromptTemplate,
val emSetting: EMSettingDTO,
val indexSessionId: String? = null,
val indexName: String? = null,
val noAnswerSentence: String,
val noAnswerStoryId: String? = null,
val documentsRequired: Boolean = true,
val debugEnabled: Boolean,
val maxDocumentsRetrieved: Int,
val maxMessagesFromHistory: Int,
) {
constructor(configuration: BotRAGConfiguration) : this(
id = configuration._id.toString(),
namespace = configuration.namespace,
botId = configuration.botId,
enabled = configuration.enabled,
questionCondensingLlmSetting = configuration.questionCondensingLlmSetting?.toDTO(),
questionCondensingPrompt = configuration.questionCondensingPrompt,
questionAnsweringLlmSetting = configuration.getQuestionAnsweringLLMSetting().toDTO(),
questionAnsweringPrompt = configuration.questionAnsweringPrompt
?: configuration.initQuestionAnsweringPrompt(),
emSetting = configuration.emSetting.toDTO(),
indexSessionId = configuration.indexSessionId,
indexName = configuration.generateIndexName(),
noAnswerSentence = configuration.noAnswerSentence,
noAnswerStoryId = configuration.noAnswerStoryId,
documentsRequired = configuration.documentsRequired,
debugEnabled = configuration.debugEnabled,
maxDocumentsRetrieved = configuration.maxDocumentsRetrieved,
maxMessagesFromHistory = configuration.maxMessagesFromHistory,
)
fun toBotRAGConfiguration(): BotRAGConfiguration =
BotRAGConfiguration(
_id = id?.toId() ?: newId(),
namespace = namespace,
botId = botId,
enabled = enabled,
questionCondensingLlmSetting = LLMSettingMapper.toEntity(
namespace = namespace,
botId = botId,
feature = Constants.GEN_AI_RAG_QUESTION_CONDENSING,
dto = questionCondensingLlmSetting!!
),
questionCondensingPrompt = questionCondensingPrompt,
questionAnsweringLlmSetting = LLMSettingMapper.toEntity(
namespace = namespace,
botId = botId,
feature = Constants.GEN_AI_RAG_QUESTION_ANSWERING,
dto = questionAnsweringLlmSetting
),
questionAnsweringPrompt = questionAnsweringPrompt,
emSetting = EMSettingMapper.toEntity(
namespace = namespace,
botId = botId,
feature = Constants.GEN_AI_RAG_EMBEDDING_QUESTION,
dto = emSetting
),
indexSessionId = indexSessionId,
noAnswerSentence = noAnswerSentence,
noAnswerStoryId = noAnswerStoryId,
documentsRequired = documentsRequired,
debugEnabled = debugEnabled,
maxDocumentsRetrieved = maxDocumentsRetrieved,
maxMessagesFromHistory = maxMessagesFromHistory,
)
}
private fun BotRAGConfiguration.generateIndexName(): String? {
return indexSessionId?.takeIf { it.isNotBlank() }?.let {
VectorStoreUtils.getVectorStoreElements(
namespace,
botId,
it,
maxDocumentsRetrieved,
VectorStoreService.getVectorStoreConfiguration(namespace, botId, enabled = true)
?.setting
).second
}
}