Skip to content

Commit 745d9e9

Browse files
committed
rag with pictures (2)
1 parent 53c6a72 commit 745d9e9

File tree

1 file changed

+17
-10
lines changed
  • src/main/kotlin/dev/storozhenko/familybot/feature/talking/services/rag

1 file changed

+17
-10
lines changed

src/main/kotlin/dev/storozhenko/familybot/feature/talking/services/rag/RagService.kt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,39 @@ class RagService(
3232
private val log = KotlinLogging.logger {}
3333

3434
suspend fun add(context: ExecutorContext) {
35+
if (context.message.messageId == null) return
36+
3537
try {
3638
val imageDesc = aiService.getImageDescription(context)
39+
3740
if (imageDesc != null) {
3841
log.info { "Adding image to rag" }
42+
val text = if (isTextValid(context)) "с подписью <${context.message.text}>" else ""
3943
ragRepository.add(
4044
context,
4145
getEmbedding(imageDesc).first(),
42-
textOverride = "Пользователь прислал изображение: [$imageDesc]"
46+
textOverride = "Пользователь прислал изображение $text: [$imageDesc]"
4347
)
48+
} else {
49+
if (!isTextValid(context)) return
50+
val embeddings = getEmbedding(context.message.text)
51+
log.info { "Adding new message to rag" }
52+
ragRepository.add(context, embeddings.first())
4453
}
4554

46-
val text = context.message.text ?: return
47-
if (text.isBlank()) return
48-
if (text.length <= 3) return
49-
if (text.startsWith("/")) return
50-
if (context.message.messageId == null) return
51-
val embeddings = getEmbedding(text)
52-
log.info { "Adding new message to rag" }
53-
ragRepository.add(context, embeddings.first())
54-
5555
} catch (e: Exception) {
5656
log.error(e) { "Rag adding failed" }
5757
}
5858

5959
}
6060

61+
private fun isTextValid(context: ExecutorContext): Boolean {
62+
val text = context.message.text ?: return false
63+
if (text.isBlank()) return false
64+
if (text.length <= 3) return false
65+
if (text.startsWith("/")) return false
66+
return true
67+
}
6168

6269
suspend fun getContext(context: ExecutorContext): String {
6370
try {

0 commit comments

Comments
 (0)