mirror of
https://github.com/open-webui/open-webui.git
synced 2026-08-13 17:22:27 -06:00
2d18727ab8
Raising GLOBAL_LOG_LEVEL to WARNING buys quieter output but not less work: 241 INFO call sites interpolate their payload into an f-string before the logging call gets to drop it. The heaviest is get_doc, which logs every chunk id and metadata dict in a collection, so on the full-context retrieval path that is the entire knowledge base, once per chat request.
That one line at WARNING, CPython 3.12:
| knowledge base | payload | before | after |
| -------------- | ------- | -------- | ------- |
| top-k of 3 | 1.2 kB | 3.8 us | 0.07 us |
| 500 chunks | 201 kB | 583.6 us | 0.08 us |
| 5000 chunks | 2.0 MB | 5.8 ms | 0.15 us |
The lazy form log.info('query_doc:result %s %s', result.ids, result.metadatas) hands the payload to record.getMessage(), which the InterceptHandler only reaches once a record has passed the level check. Output at INFO is byte-identical. Two sites that already built their message eagerly, one str concat and one % operator, move to the same lazy form.
70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
import logging
|
|
from typing import List, Optional, Tuple
|
|
from urllib.parse import quote
|
|
|
|
import requests
|
|
from open_webui.env import ENABLE_FORWARD_USER_INFO_HEADERS, REQUESTS_VERIFY
|
|
from open_webui.retrieval.models.base_reranker import BaseReranker
|
|
from open_webui.utils.headers import include_user_info_headers
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class ExternalReranker(BaseReranker):
|
|
def __init__(
|
|
self,
|
|
api_key: str,
|
|
url: str = 'http://localhost:8080/v1/rerank',
|
|
model: str = 'reranker',
|
|
timeout: Optional[int] = None,
|
|
):
|
|
self.api_key = api_key
|
|
self.url = url
|
|
self.model = model
|
|
self.timeout = timeout
|
|
|
|
def predict(self, sentences: List[Tuple[str, str]], user=None) -> Optional[List[float]]:
|
|
query = sentences[0][0]
|
|
docs = [i[1] for i in sentences]
|
|
|
|
payload = {
|
|
'model': self.model,
|
|
'query': query,
|
|
'documents': docs,
|
|
'top_n': len(docs),
|
|
}
|
|
|
|
try:
|
|
log.info('ExternalReranker:predict:model %s', self.model)
|
|
log.info('ExternalReranker:predict:query %s', query)
|
|
|
|
headers = {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': f'Bearer {self.api_key}',
|
|
}
|
|
|
|
if ENABLE_FORWARD_USER_INFO_HEADERS and user:
|
|
headers = include_user_info_headers(headers, user)
|
|
|
|
r = requests.post(
|
|
f'{self.url}',
|
|
headers=headers,
|
|
json=payload,
|
|
timeout=self.timeout,
|
|
verify=REQUESTS_VERIFY,
|
|
)
|
|
|
|
r.raise_for_status()
|
|
data = r.json()
|
|
|
|
if 'results' in data:
|
|
sorted_results = sorted(data['results'], key=lambda x: x['index'])
|
|
return [result['relevance_score'] for result in sorted_results]
|
|
else:
|
|
log.error('No results found in external reranking response')
|
|
return None
|
|
|
|
except Exception as e:
|
|
log.exception(f'Error in external reranking: {e}')
|
|
return None
|