Просмотр исходного кода

fix: handle case where [query] happens in the RAG context

thiswillbeyourgithub 7 месяцев назад
Родитель
Сommit
9661fee554
1 измененных файлов с 10 добавлено и 2 удалено
  1. 10 2
      backend/open_webui/apps/rag/utils.py

+ 10 - 2
backend/open_webui/apps/rag/utils.py

@@ -1,5 +1,6 @@
 import logging
 import logging
 import os
 import os
+import uuid
 from typing import Optional, Union
 from typing import Optional, Union
 
 
 import requests
 import requests
@@ -197,8 +198,15 @@ def rag_template(template: str, context: str, query: str):
         f"RAG template contains an unexpected number of '[context]' : {count}"
         f"RAG template contains an unexpected number of '[context]' : {count}"
     )
     )
     assert "[context]" in template, "RAG template does not contain '[context]'"
     assert "[context]" in template, "RAG template does not contain '[context]'"
-    template = template.replace("[context]", context)
-    template = template.replace("[query]", query)
+
+    if "[query]" in context:
+        query_placeholder = str(uuid.uuid4())
+        template = template.replace("[QUERY]", query_placeholder)
+        template = template.replace("[context]", context)
+        template = template.replace(query_placeholder, query)
+    else:
+        template = template.replace("[context]", context)
+        template = template.replace("[query]", query)
     return template
     return template