forked from NYU-ITS/NAGA-open-webui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_worker_ef_fix.py
More file actions
86 lines (61 loc) · 3.03 KB
/
test_worker_ef_fix.py
File metadata and controls
86 lines (61 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
"""
Test that ef=None is handled correctly for Portkey/OpenAI engines.
Run with: conda activate rit4test && python test_worker_ef_fix.py
"""
import os
import sys
backend_dir = os.path.join(os.path.dirname(__file__), "backend")
if backend_dir not in sys.path:
sys.path.insert(0, backend_dir)
os.environ["TZ"] = "America/New_York"
import logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
log = logging.getLogger(__name__)
def test_ef_none_for_portkey():
"""Test that ef=None works for Portkey engine."""
log.info("=" * 80)
log.info("TEST: ef=None for Portkey/OpenAI engines")
log.info("=" * 80)
try:
from open_webui.workers.file_processor import MockState
from open_webui.routers.retrieval import get_ef, get_embedding_function
# Test get_ef for Portkey (should return None)
ef = get_ef("portkey", "@openai-embedding/text-embedding-3-small", False)
log.info(f"get_ef('portkey', ...) = {ef}")
assert ef is None, "For Portkey engine, ef should be None (API-based)"
log.info("✅ get_ef returns None for Portkey (correct)")
# Test that get_embedding_function works with ef=None for Portkey
embedding_func = get_embedding_function(
embedding_engine="portkey",
embedding_model="@openai-embedding/text-embedding-3-small",
embedding_function=None, # ef is None for API engines
url="",
key="test_key",
embedding_batch_size=1,
)
assert embedding_func is not None, "get_embedding_function should work with ef=None for Portkey"
log.info("✅ get_embedding_function works with ef=None for Portkey")
# Test MockState initialization
state = MockState(embedding_api_key="test_key_12345")
log.info(f"MockState.ef = {state.ef}")
log.info(f"MockState.config.RAG_EMBEDDING_ENGINE = {state.config.RAG_EMBEDDING_ENGINE}")
# For Portkey, ef should be None
if state.config.RAG_EMBEDDING_ENGINE in ["portkey", "openai"]:
assert state.ef is None, f"For {state.config.RAG_EMBEDDING_ENGINE}, ef should be None"
log.info(f"✅ ef is None for {state.config.RAG_EMBEDDING_ENGINE} engine (correct)")
# Test initialize_embedding_function with ef=None
state.initialize_embedding_function(embedding_api_key="test_key_12345")
if state.EMBEDDING_FUNCTION is not None:
log.info("✅ EMBEDDING_FUNCTION initialized successfully with ef=None")
else:
log.warning("⚠️ EMBEDDING_FUNCTION is None (may be expected if API key is invalid)")
log.info("=" * 80)
log.info("✅ ALL TESTS PASSED - ef=None handling is correct!")
return True
except Exception as e:
log.error(f"❌ Test failed: {e}", exc_info=True)
return False
if __name__ == "__main__":
success = test_ef_none_for_portkey()
sys.exit(0 if success else 1)