Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- rag_system/guardrails.py +18 -5
- rag_system/query_engine.py +2 -7
rag_system/guardrails.py
CHANGED
|
@@ -277,16 +277,29 @@ def _check_query_llama_guard(query: str) -> GuardrailResult:
|
|
| 277 |
tokenized = _llama_guard_tokenizer(prompt, return_tensors="pt")
|
| 278 |
|
| 279 |
if isinstance(tokenized, dict):
|
| 280 |
-
input_ids = tokenized
|
| 281 |
attention_mask = tokenized.get("attention_mask")
|
| 282 |
-
if attention_mask is not None:
|
| 283 |
-
attention_mask = attention_mask.to(_llama_guard_model.device)
|
| 284 |
else:
|
| 285 |
-
input_ids = tokenized
|
| 286 |
attention_mask = None
|
| 287 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
if attention_mask is None:
|
| 289 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
prompt_len = input_ids.shape[1]
|
| 292 |
output = _llama_guard_model.generate(
|
|
|
|
| 277 |
tokenized = _llama_guard_tokenizer(prompt, return_tensors="pt")
|
| 278 |
|
| 279 |
if isinstance(tokenized, dict):
|
| 280 |
+
input_ids = tokenized.get("input_ids")
|
| 281 |
attention_mask = tokenized.get("attention_mask")
|
|
|
|
|
|
|
| 282 |
else:
|
| 283 |
+
input_ids = tokenized
|
| 284 |
attention_mask = None
|
| 285 |
|
| 286 |
+
if not hasattr(input_ids, "shape"):
|
| 287 |
+
import torch
|
| 288 |
+
input_ids = torch.as_tensor(input_ids)
|
| 289 |
+
if len(getattr(input_ids, "shape", [])) == 1:
|
| 290 |
+
input_ids = input_ids.unsqueeze(0)
|
| 291 |
+
input_ids = input_ids.to(_llama_guard_model.device)
|
| 292 |
+
|
| 293 |
if attention_mask is None:
|
| 294 |
+
import torch
|
| 295 |
+
attention_mask = torch.ones_like(input_ids)
|
| 296 |
+
else:
|
| 297 |
+
if not hasattr(attention_mask, "shape"):
|
| 298 |
+
import torch
|
| 299 |
+
attention_mask = torch.as_tensor(attention_mask)
|
| 300 |
+
if len(getattr(attention_mask, "shape", [])) == 1:
|
| 301 |
+
attention_mask = attention_mask.unsqueeze(0)
|
| 302 |
+
attention_mask = attention_mask.to(_llama_guard_model.device)
|
| 303 |
|
| 304 |
prompt_len = input_ids.shape[1]
|
| 305 |
output = _llama_guard_model.generate(
|
rag_system/query_engine.py
CHANGED
|
@@ -72,11 +72,6 @@ def _should_preserve_exact_reference(query: str) -> bool:
|
|
| 72 |
return bool(_SECTION_REF_RE.search(query) and _SECTION_HINT_RE.search(query))
|
| 73 |
|
| 74 |
|
| 75 |
-
def _is_try_docs_scope(collections: list[str]) -> bool:
|
| 76 |
-
prefix = settings.try_docs_prefix
|
| 77 |
-
return bool(collections) and all(c.startswith(prefix) for c in collections)
|
| 78 |
-
|
| 79 |
-
|
| 80 |
def _cache_collection_key(collections: list[str]) -> str:
|
| 81 |
raw = "|".join(sorted(collections))
|
| 82 |
return hashlib.sha1(raw.encode()).hexdigest()[:16]
|
|
@@ -254,7 +249,7 @@ async def query(
|
|
| 254 |
collections = request.doc_collections or [request.collection_name]
|
| 255 |
embedding_mode = resolve_embedding_mode_for_collections(collections, request.embedding_mode)
|
| 256 |
mode_val = request.retrieval_mode.value if hasattr(request.retrieval_mode, "value") else str(request.retrieval_mode)
|
| 257 |
-
cache_allowed = settings.cache_enabled
|
| 258 |
cache_collection_key = _cache_collection_key(collections)
|
| 259 |
cache_params_key = _cache_params_key_v2(
|
| 260 |
mode_val,
|
|
@@ -455,7 +450,7 @@ async def pipeline_stream_query(request: QueryRequest) -> AsyncIterator[str]:
|
|
| 455 |
mode_val = request.retrieval_mode.value if hasattr(request.retrieval_mode, "value") else str(request.retrieval_mode)
|
| 456 |
collections = request.doc_collections or [request.collection_name]
|
| 457 |
embedding_mode = resolve_embedding_mode_for_collections(collections, request.embedding_mode)
|
| 458 |
-
cache_allowed = settings.cache_enabled
|
| 459 |
cache_collection_key = _cache_collection_key(collections)
|
| 460 |
cache_params_key = _cache_params_key_v2(
|
| 461 |
mode_val,
|
|
|
|
| 72 |
return bool(_SECTION_REF_RE.search(query) and _SECTION_HINT_RE.search(query))
|
| 73 |
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
def _cache_collection_key(collections: list[str]) -> str:
|
| 76 |
raw = "|".join(sorted(collections))
|
| 77 |
return hashlib.sha1(raw.encode()).hexdigest()[:16]
|
|
|
|
| 249 |
collections = request.doc_collections or [request.collection_name]
|
| 250 |
embedding_mode = resolve_embedding_mode_for_collections(collections, request.embedding_mode)
|
| 251 |
mode_val = request.retrieval_mode.value if hasattr(request.retrieval_mode, "value") else str(request.retrieval_mode)
|
| 252 |
+
cache_allowed = settings.cache_enabled
|
| 253 |
cache_collection_key = _cache_collection_key(collections)
|
| 254 |
cache_params_key = _cache_params_key_v2(
|
| 255 |
mode_val,
|
|
|
|
| 450 |
mode_val = request.retrieval_mode.value if hasattr(request.retrieval_mode, "value") else str(request.retrieval_mode)
|
| 451 |
collections = request.doc_collections or [request.collection_name]
|
| 452 |
embedding_mode = resolve_embedding_mode_for_collections(collections, request.embedding_mode)
|
| 453 |
+
cache_allowed = settings.cache_enabled
|
| 454 |
cache_collection_key = _cache_collection_key(collections)
|
| 455 |
cache_params_key = _cache_params_key_v2(
|
| 456 |
mode_val,
|