Skip to content

Commit 5778470

Browse files
committed
refac
1 parent fc98000 commit 5778470

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

backend/open_webui/models/models.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import logging
23
import time
34
from typing import Optional
@@ -319,9 +320,20 @@ async def search_models(
319320

320321
tag = filter.get('tag')
321322
if tag:
322-
like_pattern = f'%"{tag.lower()}"%'
323-
meta_text = func.lower(cast(Model.meta, String))
324-
stmt = stmt.filter(meta_text.like(like_pattern))
323+
# SQLite stores JSON text via json.dumps(ensure_ascii=True),
324+
# so non-ASCII chars are \uXXXX-escaped. PostgreSQL native JSONB
325+
# stores literal Unicode. Use the right pattern for each.
326+
if db.bind.dialect.name == 'sqlite':
327+
if tag.isascii():
328+
meta_text = func.lower(cast(Model.meta, String))
329+
pattern = f'%{json.dumps(tag.lower())}%'
330+
else:
331+
meta_text = cast(Model.meta, String)
332+
pattern = f'%{json.dumps(tag)}%'
333+
else:
334+
meta_text = func.lower(cast(Model.meta, String))
335+
pattern = f'%{json.dumps(tag.lower(), ensure_ascii=False)}%'
336+
stmt = stmt.filter(meta_text.like(pattern))
325337

326338
order_by = filter.get('order_by')
327339
direction = filter.get('direction')

backend/open_webui/models/prompts.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import time
23
import uuid
34
from typing import Optional
@@ -292,10 +293,22 @@ async def search_prompts(
292293

293294
tag = filter.get('tag')
294295
if tag:
295-
# Search for tag in JSON array field
296-
like_pattern = f'%"{tag.lower()}"%'
297-
tags_text = func.lower(cast(Prompt.tags, String))
298-
stmt = stmt.filter(tags_text.like(like_pattern))
296+
# SQLite stores JSON text via json.dumps(ensure_ascii=True),
297+
# so non-ASCII chars are \uXXXX-escaped. PostgreSQL native JSONB
298+
# stores literal Unicode. Use the right pattern for each.
299+
if db.bind.dialect.name == 'sqlite':
300+
if tag.isascii():
301+
tags_text = func.lower(cast(Prompt.tags, String))
302+
pattern = f'%{json.dumps(tag.lower())}%'
303+
else:
304+
# LOWER() is ASCII-only; non-ASCII codepoints would
305+
# produce different \uXXXX escapes when lowered.
306+
tags_text = cast(Prompt.tags, String)
307+
pattern = f'%{json.dumps(tag)}%'
308+
else:
309+
tags_text = func.lower(cast(Prompt.tags, String))
310+
pattern = f'%{json.dumps(tag.lower(), ensure_ascii=False)}%'
311+
stmt = stmt.filter(tags_text.like(pattern))
299312

300313
order_by = filter.get('order_by')
301314
direction = filter.get('direction')

0 commit comments

Comments
 (0)