bep40 commited on
Commit
48b9248
·
verified ·
1 Parent(s): 6b00697

Fix patch: ensure models are added correctly to the list

Browse files
Files changed (1) hide show
  1. patch_models.py +40 -93
patch_models.py CHANGED
@@ -1,118 +1,65 @@
1
- """Build-time patch: Add models to dropdown + fix title generation for OpenRouter."""
2
- import re, os
3
 
4
  AGENT_FILE = "/app/backend/routes/agent.py"
5
  with open(AGENT_FILE, "r") as f:
6
  content = f.read()
7
 
8
- # 1. Add new models to _available_models (INSERT before closing bracket)
9
- # Find the last model entry and add our models before "]"
10
- insert_marker = """ {
11
- "id": "deepseek-ai/DeepSeek-V4-Pro:deepinfra",
12
- "label": "DeepSeek V4 Pro",
13
- "provider": "huggingface",
14
- "tier": "free",
15
- },
16
- ]
17
- return models"""
18
 
19
- new_models_block = """ {
20
- "id": "deepseek-ai/DeepSeek-V4-Pro:deepinfra",
21
- "label": "DeepSeek V4 Pro",
22
- "provider": "huggingface",
23
- "tier": "free",
24
- },
25
- {
26
- "id": "openai/deepseek/deepseek-chat-v3-0324",
27
- "label": "DeepSeek V3 (OpenRouter)",
28
- "provider": "openrouter",
29
- "tier": "free",
30
- "recommended": True,
31
- },
32
- {
33
- "id": "openai/google/gemini-2.0-flash-001",
34
- "label": "Gemini 2.0 Flash (OpenRouter)",
35
- "provider": "openrouter",
36
- "tier": "free",
37
- },
38
- {
39
- "id": "openai/qwen/qwen3-30b-a3b",
40
- "label": "Qwen3 30B (OpenRouter)",
41
- "provider": "openrouter",
42
- "tier": "free",
43
- },
44
- ]
45
- return models"""
46
 
47
- if insert_marker in content:
48
- content = content.replace(insert_marker, new_models_block)
49
- print("✅ Added DeepSeek V3, Gemini 2.0 Flash, Qwen3 30B to models list")
50
- else:
51
- print("⚠️ Could not find insert marker - trying alternative")
52
- # Alternative: find "]\\n return models" and insert before it
53
- content = content.replace(
54
- " ]\n return models",
55
- ''' {
56
- "id": "openai/deepseek/deepseek-chat-v3-0324",
57
- "label": "DeepSeek V3 (OpenRouter)",
58
- "provider": "openrouter",
59
- "tier": "free",
60
- "recommended": True,
61
- },
62
- {
63
- "id": "openai/google/gemini-2.0-flash-001",
64
- "label": "Gemini 2.0 Flash (OpenRouter)",
65
- "provider": "openrouter",
66
- "tier": "free",
67
- },
68
- {
69
- "id": "openai/qwen/qwen3-30b-a3b",
70
- "label": "Qwen3 30B (OpenRouter)",
71
- "provider": "openrouter",
72
- "tier": "free",
73
- },
74
- ]
75
- return models''',
76
- 1 # only first occurrence
77
- )
78
- print("✅ Added models via alternative method")
79
 
80
- # 2. Fix title generation: replace HF Router call with OpenRouter
81
- old_title = '''model="openai/openai/gpt-oss-120b:cerebras",
82
- api_base="https://router.huggingface.co/v1",
83
- api_key=api_key,'''
 
 
 
 
 
 
 
 
 
 
84
 
85
- new_title = '''model="openai/deepseek/deepseek-chat-v3-0324",
86
- api_base=os.environ.get("OPENAI_API_BASE", "https://openrouter.ai/api/v1"),
87
- api_key=os.environ.get("OPENAI_API_KEY", api_key),'''
88
 
89
  if old_title in content:
90
  content = content.replace(old_title, new_title)
91
- # Remove reasoning_effort (not supported by DeepSeek)
92
  content = content.replace(' reasoning_effort="low",\n', '')
93
- print("✅ Title generation patched to use OpenRouter")
94
  else:
95
- print("���️ Title generation pattern not found")
96
-
97
- # 3. Add "import os" if missing
98
- if "\nimport os\n" not in content and "import os," not in content:
99
- content = "import os\n" + content
100
- print("✅ Added import os")
101
 
102
  # Verify syntax
103
  import ast
104
  try:
105
  ast.parse(content)
106
- print("✅ Syntax valid")
107
  except SyntaxError as e:
108
- print(f"❌ Syntax error line {e.lineno}: {e.msg}")
109
  import sys; sys.exit(1)
110
 
111
  with open(AGENT_FILE, "w") as f:
112
  f.write(content)
113
 
114
- # Verify models in file
115
- assert "deepseek-chat-v3-0324" in content, "DeepSeek missing!"
116
- assert "gemini-2.0-flash" in content, "Gemini missing!"
117
- assert "qwen3-30b" in content, "Qwen missing!"
118
- print("\n✅ All patches applied successfully")
 
1
+ """Build-time patch: Add Gemini + Qwen3 to models dropdown."""
2
+ import os
3
 
4
  AGENT_FILE = "/app/backend/routes/agent.py"
5
  with open(AGENT_FILE, "r") as f:
6
  content = f.read()
7
 
8
+ # Find the AVAILABLE_MODELS assignment and add models after it
9
+ # The pattern: AVAILABLE_MODELS = _available_models()
10
+ # We'll append our models directly to the list after it's created
 
 
 
 
 
 
 
11
 
12
+ patch_code = '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ # === PATCHED: Add OpenRouter models ===
15
+ import os as _os
16
+ _extra_models = [
17
+ {"id": "openai/google/gemini-2.0-flash-001", "label": "Gemini 2.0 Flash", "provider": "openrouter", "tier": "free", "recommended": True},
18
+ {"id": "openai/qwen/qwen3-30b-a3b", "label": "Qwen3 30B", "provider": "openrouter", "tier": "free"},
19
+ ]
20
+ AVAILABLE_MODELS.extend(_extra_models)
21
+ # === END PATCH ===
22
+ '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ # Insert after AVAILABLE_MODELS = _available_models()
25
+ target = "AVAILABLE_MODELS = _available_models()\n"
26
+ if target in content:
27
+ content = content.replace(target, target + patch_code)
28
+ print("✅ Added Gemini 2.0 Flash + Qwen3 30B after AVAILABLE_MODELS")
29
+ else:
30
+ # Try without newline
31
+ target2 = "AVAILABLE_MODELS = _available_models()"
32
+ if target2 in content:
33
+ content = content.replace(target2, target2 + "\n" + patch_code)
34
+ print("✅ Added models (alt method)")
35
+ else:
36
+ print("❌ Cannot find AVAILABLE_MODELS assignment!")
37
+ import sys; sys.exit(1)
38
 
39
+ # Fix title generation to use OpenRouter
40
+ old_title = 'model="openai/openai/gpt-oss-120b:cerebras",\n api_base="https://router.huggingface.co/v1",\n api_key=api_key,'
41
+ new_title = 'model="openai/deepseek/deepseek-chat-v3-0324",\n api_base=_os.environ.get("OPENAI_API_BASE", "https://openrouter.ai/api/v1"),\n api_key=_os.environ.get("OPENAI_API_KEY", api_key),'
42
 
43
  if old_title in content:
44
  content = content.replace(old_title, new_title)
 
45
  content = content.replace(' reasoning_effort="low",\n', '')
46
+ print("✅ Title generation fixed for OpenRouter")
47
  else:
48
+ print("⚠️ Title pattern not found (may already be different)")
 
 
 
 
 
49
 
50
  # Verify syntax
51
  import ast
52
  try:
53
  ast.parse(content)
54
+ print("✅ Syntax OK")
55
  except SyntaxError as e:
56
+ print(f"❌ Syntax error: {e}")
57
  import sys; sys.exit(1)
58
 
59
  with open(AGENT_FILE, "w") as f:
60
  f.write(content)
61
 
62
+ # Final check
63
+ assert "gemini-2.0-flash" in content, "Gemini not in file!"
64
+ assert "qwen3-30b" in content, "Qwen3 not in file!"
65
+ print("\n✅ Done - Gemini 2.0 Flash + Qwen3 30B added to dropdown")