Spaces:
Paused
Paused
Create router.py
Browse files
router.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from llm_clients import call_llama, call_gemini
|
| 2 |
+
from search_tool import search_web
|
| 3 |
+
from rag_engine import rag_response
|
| 4 |
+
|
| 5 |
+
def is_reasoning(prompt):
|
| 6 |
+
keywords = ["solve", "explain deeply", "why", "logic", "proof"]
|
| 7 |
+
return any(k in prompt.lower() for k in keywords)
|
| 8 |
+
|
| 9 |
+
def is_live_data(prompt):
|
| 10 |
+
keywords = ["today", "latest", "current", "news", "price", "weather"]
|
| 11 |
+
return any(k in prompt.lower() for k in keywords)
|
| 12 |
+
|
| 13 |
+
def route_request(prompt, image=None):
|
| 14 |
+
|
| 15 |
+
# RAG first
|
| 16 |
+
rag = rag_response(prompt)
|
| 17 |
+
if rag:
|
| 18 |
+
return rag
|
| 19 |
+
|
| 20 |
+
# Image case
|
| 21 |
+
if image:
|
| 22 |
+
return call_gemini(prompt)
|
| 23 |
+
|
| 24 |
+
# Reasoning
|
| 25 |
+
if is_reasoning(prompt):
|
| 26 |
+
return call_gemini(prompt)
|
| 27 |
+
|
| 28 |
+
# Live data
|
| 29 |
+
if is_live_data(prompt):
|
| 30 |
+
search_data = search_web(prompt)
|
| 31 |
+
llama_answer = call_llama(prompt + "\n\nWeb Data:\n" + search_data)
|
| 32 |
+
gemini_answer = call_gemini(prompt + "\n\nWeb Data:\n" + search_data)
|
| 33 |
+
|
| 34 |
+
# Simple judge (can improve later)
|
| 35 |
+
if len(gemini_answer) > len(llama_answer):
|
| 36 |
+
return gemini_answer
|
| 37 |
+
return llama_answer
|
| 38 |
+
|
| 39 |
+
# Default
|
| 40 |
+
return call_llama(prompt)
|
| 41 |
+
|