Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Update llm_router.py
Browse files- llm_router.py +32 -107
llm_router.py
CHANGED
|
@@ -1,109 +1,34 @@
|
|
| 1 |
-
from typing import Optional
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from settings import
|
| 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 |
-
def _retry(fn, attempts=3, backoff=0.8):
|
| 32 |
-
last = None
|
| 33 |
-
for i in range(attempts):
|
| 34 |
-
try:
|
| 35 |
-
return fn()
|
| 36 |
-
except Exception as e:
|
| 37 |
-
last = e
|
| 38 |
-
time.sleep(backoff * (2 ** i))
|
| 39 |
-
raise last if last else RuntimeError("Unknown error")
|
| 40 |
-
|
| 41 |
-
def cohere_chat(prompt: str) -> Optional[str]:
|
| 42 |
-
cli = _co_client()
|
| 43 |
-
if not cli:
|
| 44 |
-
return None
|
| 45 |
-
def _call():
|
| 46 |
-
resp = cli.chat(
|
| 47 |
-
model=COHERE_MODEL_PRIMARY,
|
| 48 |
-
message=prompt,
|
| 49 |
-
temperature=MODEL_SETTINGS["temperature"],
|
| 50 |
-
max_tokens=MODEL_SETTINGS["max_new_tokens"],
|
| 51 |
)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
return resp.text
|
| 55 |
-
if hasattr(resp, "reply") and resp.reply:
|
| 56 |
-
return resp.reply
|
| 57 |
-
if hasattr(resp, "generations") and resp.generations:
|
| 58 |
-
return resp.generations[0].text
|
| 59 |
-
return None
|
| 60 |
-
try:
|
| 61 |
-
return _retry(_call, attempts=3)
|
| 62 |
-
except Exception:
|
| 63 |
-
return None
|
| 64 |
-
|
| 65 |
-
def open_fallback_chat(prompt: str) -> Optional[str]:
|
| 66 |
-
if not USE_OPEN_FALLBACKS or not _HAS_LOCAL:
|
| 67 |
-
return None
|
| 68 |
-
try:
|
| 69 |
-
return LocalLLM().chat(prompt)
|
| 70 |
-
except Exception:
|
| 71 |
-
return None
|
| 72 |
-
|
| 73 |
-
def cohere_embed(texts: List[str]) -> List[List[float]]:
|
| 74 |
-
cli = _co_client()
|
| 75 |
-
if not cli or not texts:
|
| 76 |
-
return []
|
| 77 |
-
def _call():
|
| 78 |
-
resp = cli.embed(texts=texts, model=COHERE_EMBED_MODEL)
|
| 79 |
-
# Newer SDK: resp.embeddings; older: resp.data
|
| 80 |
-
return getattr(resp, "embeddings", None) or getattr(resp, "data", []) or []
|
| 81 |
-
try:
|
| 82 |
-
return _retry(_call, attempts=3)
|
| 83 |
-
except Exception:
|
| 84 |
-
return []
|
| 85 |
-
|
| 86 |
-
def generate_narrative(scenario_text: str, structured_sections_md: str, rag_snippets: List[str]) -> str:
|
| 87 |
-
grounding = "\n\n".join([f"[RAG {i+1}]\n{t}" for i, t in enumerate(rag_snippets or [])])
|
| 88 |
-
prompt = f"""You are a Canadian healthcare operations copilot.
|
| 89 |
-
Follow the scenario's requested deliverables exactly. Use the structured computations provided (already calculated deterministically) and the RAG snippets for grounding.
|
| 90 |
-
|
| 91 |
-
# Scenario
|
| 92 |
-
{scenario_text}
|
| 93 |
-
|
| 94 |
-
# Deterministic Results (already computed)
|
| 95 |
-
{structured_sections_md}
|
| 96 |
-
|
| 97 |
-
# Grounding (Canadian sources, snippets)
|
| 98 |
-
{grounding}
|
| 99 |
-
|
| 100 |
-
Write a concise, decision-ready report tailored to provincial operations leaders.
|
| 101 |
-
Do not invent numbers. If data are missing, say so clearly.
|
| 102 |
-
"""
|
| 103 |
-
out = cohere_chat(prompt)
|
| 104 |
-
if out: return out
|
| 105 |
-
out = open_fallback_chat(prompt)
|
| 106 |
-
if out: return out
|
| 107 |
-
return "Unable to generate narrative at this time."
|
| 108 |
-
|
| 109 |
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 4 |
+
from settings import OPEN_LLM_CANDIDATES, LOCAL_MAX_NEW_TOKENS
|
| 5 |
+
|
| 6 |
+
class LocalLLM:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
self.pipe = None
|
| 9 |
+
self._load_any()
|
| 10 |
+
|
| 11 |
+
def _load_any(self):
|
| 12 |
+
for mid in OPEN_LLM_CANDIDATES:
|
| 13 |
+
try:
|
| 14 |
+
tok = AutoTokenizer.from_pretrained(mid, trust_remote_code=True)
|
| 15 |
+
mdl = AutoModelForCausalLM.from_pretrained(
|
| 16 |
+
mid, device_map="auto",
|
| 17 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 18 |
+
trust_remote_code=True
|
| 19 |
+
)
|
| 20 |
+
self.pipe = pipeline("text-generation", model=mdl, tokenizer=tok)
|
| 21 |
+
return
|
| 22 |
+
except Exception:
|
| 23 |
+
continue
|
| 24 |
+
|
| 25 |
+
def chat(self, prompt: str) -> Optional[str]:
|
| 26 |
+
if not self.pipe: return None
|
| 27 |
+
out = self.pipe(
|
| 28 |
+
prompt, max_new_tokens=LOCAL_MAX_NEW_TOKENS,
|
| 29 |
+
do_sample=True, temperature=0.3, top_p=0.9, repetition_penalty=1.12,
|
| 30 |
+
eos_token_id=self.pipe.tokenizer.eos_token_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
+
text = out[0]["generated_text"]
|
| 33 |
+
return text[len(prompt):].strip() if text.startswith(prompt) else text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|