Skip to content

Commit 4d2f189

Browse files
committed
feat: add RAG_RERANKING_BATCH_SIZE configuration option
Add configurable reranker batch size (env var RAG_RERANKING_BATCH_SIZE, default 32) following the same pattern as RAG_EMBEDDING_BATCH_SIZE. - config.py: PersistentConfig for RAG_RERANKING_BATCH_SIZE - main.py: import, state init, pass to get_reranking_function - colbert.py: accept batch_size param in predict() (was hardcoded 32) - utils.py: get_reranking_function passes batch_size at call time - retrieval.py: expose in config GET/POST endpoints and ConfigForm - Documents.svelte: add Reranking Batch Size input in admin settings Closes open-webui#23730
1 parent 70a6a24 commit 4d2f189

6 files changed

Lines changed: 40 additions & 5 deletions

File tree

backend/open_webui/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,6 +2965,12 @@ class BannerModel(BaseModel):
29652965
os.environ.get('RAG_RERANKING_MODEL_TRUST_REMOTE_CODE', 'True').lower() == 'true'
29662966
)
29672967

2968+
RAG_RERANKING_BATCH_SIZE = PersistentConfig(
2969+
'RAG_RERANKING_BATCH_SIZE',
2970+
'rag.reranking_batch_size',
2971+
int(os.environ.get('RAG_RERANKING_BATCH_SIZE', '32')),
2972+
)
2973+
29682974
RAG_EXTERNAL_RERANKER_URL = PersistentConfig(
29692975
'RAG_EXTERNAL_RERANKER_URL',
29702976
'rag.external_reranker_url',

backend/open_webui/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
RAG_EXTERNAL_RERANKER_URL,
249249
RAG_EXTERNAL_RERANKER_API_KEY,
250250
RAG_EXTERNAL_RERANKER_TIMEOUT,
251+
RAG_RERANKING_BATCH_SIZE,
251252
RAG_RERANKING_MODEL_AUTO_UPDATE,
252253
RAG_RERANKING_MODEL_TRUST_REMOTE_CODE,
253254
RAG_EMBEDDING_ENGINE,
@@ -1044,6 +1045,7 @@ async def lifespan(app: FastAPI):
10441045
app.state.config.RAG_EXTERNAL_RERANKER_URL = RAG_EXTERNAL_RERANKER_URL
10451046
app.state.config.RAG_EXTERNAL_RERANKER_API_KEY = RAG_EXTERNAL_RERANKER_API_KEY
10461047
app.state.config.RAG_EXTERNAL_RERANKER_TIMEOUT = RAG_EXTERNAL_RERANKER_TIMEOUT
1048+
app.state.config.RAG_RERANKING_BATCH_SIZE = RAG_RERANKING_BATCH_SIZE
10471049

10481050
app.state.config.RAG_TEMPLATE = RAG_TEMPLATE
10491051

@@ -1193,6 +1195,7 @@ async def lifespan(app: FastAPI):
11931195
app.state.config.RAG_RERANKING_ENGINE,
11941196
app.state.config.RAG_RERANKING_MODEL,
11951197
reranking_function=app.state.rf,
1198+
reranking_batch_size=app.state.config.RAG_RERANKING_BATCH_SIZE,
11961199
)
11971200

11981201
########################################

backend/open_webui/retrieval/models/colbert.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ def calculate_similarity_scores(self, query_embeddings, document_embeddings):
5959

6060
return normalized_scores.detach().cpu().numpy().astype(np.float32)
6161

62-
def predict(self, sentences):
62+
def predict(self, sentences, batch_size=32):
6363
query = sentences[0][0]
6464
docs = [i[1] for i in sentences]
6565

6666
# Embedding the documents
67-
embedded_docs = self.ckpt.docFromText(docs, bsize=32)[0]
67+
embedded_docs = self.ckpt.docFromText(docs, bsize=batch_size)[0]
6868
# Embedding the queries
69-
embedded_queries = self.ckpt.queryFromText([query], bsize=32)
69+
embedded_queries = self.ckpt.queryFromText([query], bsize=batch_size)
7070
embedded_query = embedded_queries[0]
7171

7272
# Calculate retrieval scores for the query against all documents

backend/open_webui/retrieval/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ async def generate_embeddings(
919919
return embeddings[0] if isinstance(text, str) else embeddings
920920

921921

922-
def get_reranking_function(reranking_engine, reranking_model, reranking_function):
922+
def get_reranking_function(reranking_engine, reranking_model, reranking_function, reranking_batch_size=32):
923923
if reranking_function is None:
924924
return None
925925
if reranking_engine == 'external':
@@ -928,7 +928,7 @@ def get_reranking_function(reranking_engine, reranking_model, reranking_function
928928
)
929929
else:
930930
return lambda query, documents, user=None: reranking_function.predict(
931-
[(query, doc.page_content) for doc in documents]
931+
[(query, doc.page_content) for doc in documents], batch_size=int(reranking_batch_size)
932932
)
933933

934934

backend/open_webui/routers/retrieval.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ async def get_rag_config(request: Request, user=Depends(get_admin_user)):
487487
# Reranking settings
488488
'RAG_RERANKING_MODEL': request.app.state.config.RAG_RERANKING_MODEL,
489489
'RAG_RERANKING_ENGINE': request.app.state.config.RAG_RERANKING_ENGINE,
490+
'RAG_RERANKING_BATCH_SIZE': request.app.state.config.RAG_RERANKING_BATCH_SIZE,
490491
'RAG_EXTERNAL_RERANKER_URL': request.app.state.config.RAG_EXTERNAL_RERANKER_URL,
491492
'RAG_EXTERNAL_RERANKER_API_KEY': request.app.state.config.RAG_EXTERNAL_RERANKER_API_KEY,
492493
'RAG_EXTERNAL_RERANKER_TIMEOUT': request.app.state.config.RAG_EXTERNAL_RERANKER_TIMEOUT,
@@ -694,6 +695,7 @@ class ConfigForm(BaseModel):
694695
# Reranking settings
695696
RAG_RERANKING_MODEL: Optional[str] = None
696697
RAG_RERANKING_ENGINE: Optional[str] = None
698+
RAG_RERANKING_BATCH_SIZE: Optional[int] = None
697699
RAG_EXTERNAL_RERANKER_URL: Optional[str] = None
698700
RAG_EXTERNAL_RERANKER_API_KEY: Optional[str] = None
699701
RAG_EXTERNAL_RERANKER_TIMEOUT: Optional[str] = None
@@ -940,6 +942,12 @@ async def update_rag_config(request: Request, form_data: ConfigForm, user=Depend
940942
else request.app.state.config.RAG_EXTERNAL_RERANKER_TIMEOUT
941943
)
942944

945+
request.app.state.config.RAG_RERANKING_BATCH_SIZE = (
946+
form_data.RAG_RERANKING_BATCH_SIZE
947+
if form_data.RAG_RERANKING_BATCH_SIZE is not None
948+
else request.app.state.config.RAG_RERANKING_BATCH_SIZE
949+
)
950+
943951
log.info(
944952
f'Updating reranking model: {request.app.state.config.RAG_RERANKING_MODEL} to {form_data.RAG_RERANKING_MODEL}'
945953
)
@@ -967,6 +975,7 @@ async def update_rag_config(request: Request, form_data: ConfigForm, user=Depend
967975
request.app.state.config.RAG_RERANKING_ENGINE,
968976
request.app.state.config.RAG_RERANKING_MODEL,
969977
request.app.state.rf,
978+
reranking_batch_size=request.app.state.config.RAG_RERANKING_BATCH_SIZE,
970979
)
971980
except Exception as e:
972981
log.error(f'Error loading reranking model: {e}')

src/lib/components/admin/Settings/Documents.svelte

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,23 @@
11851185
</div>
11861186
{/if}
11871187

1188+
<div class=" mb-2.5 flex w-full justify-between">
1189+
<div class=" self-center text-xs font-medium">
1190+
{$i18n.t('Reranking Batch Size')}
1191+
</div>
1192+
1193+
<div class="">
1194+
<input
1195+
bind:value={RAGConfig.RAG_RERANKING_BATCH_SIZE}
1196+
type="number"
1197+
class=" bg-transparent text-center w-14 outline-none"
1198+
min="1"
1199+
max="16000"
1200+
step="1"
1201+
/>
1202+
</div>
1203+
</div>
1204+
11881205
<div class=" mb-2.5 flex w-full justify-between">
11891206
<div class=" self-center text-xs font-medium">{$i18n.t('Top K')}</div>
11901207
<div class="flex items-center relative">

0 commit comments

Comments
 (0)