Spaces:
Sleeping
Sleeping
Upload appCodex.py
Browse files- appCodex.py +45 -13
appCodex.py
CHANGED
|
@@ -87,6 +87,10 @@ CATEGORY_KEYWORDS = {
|
|
| 87 |
"overview": {
|
| 88 |
"overview",
|
| 89 |
"biography",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
"who are you",
|
| 91 |
"about yourself",
|
| 92 |
"about rajan",
|
|
@@ -233,6 +237,8 @@ Notification rules:
|
|
| 233 |
the user to repeat them.
|
| 234 |
- Never send the same notification more than once in a turn.
|
| 235 |
- A knowledge-gap notification must not include unnecessary personal or sensitive data.
|
|
|
|
|
|
|
| 236 |
|
| 237 |
Boundary guidelines:
|
| 238 |
- If a message is offensive, abusive, sexually inappropriate, or contains hate
|
|
@@ -825,8 +831,9 @@ def configured_max_distance() -> float:
|
|
| 825 |
def select_retrieval_results(
|
| 826 |
results: dict[str, Any],
|
| 827 |
max_results: int = RETRIEVAL_RESULTS,
|
|
|
|
| 828 |
) -> list[tuple[str, dict[str, Any], float | None]]:
|
| 829 |
-
"""Keep
|
| 830 |
documents = (results.get("documents") or [[]])[0]
|
| 831 |
metadatas = (results.get("metadatas") or [[]])[0]
|
| 832 |
distances = (results.get("distances") or [[]])[0]
|
|
@@ -840,8 +847,30 @@ def select_retrieval_results(
|
|
| 840 |
continue
|
| 841 |
metadata = metadatas[index] if index < len(metadatas) else {}
|
| 842 |
distance = distances[index] if index < len(distances) else None
|
| 843 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 844 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 845 |
|
| 846 |
text_key = hashlib.sha256(document.strip().encode("utf-8")).hexdigest()
|
| 847 |
if text_key in seen_text:
|
|
@@ -1296,7 +1325,10 @@ def respond_ai(
|
|
| 1296 |
query_arguments["where"] = {"category": category}
|
| 1297 |
|
| 1298 |
results = active_collection.query(**query_arguments)
|
| 1299 |
-
selected_results = select_retrieval_results(
|
|
|
|
|
|
|
|
|
|
| 1300 |
context = format_retrieved_context(selected_results)
|
| 1301 |
retrieval_entry = build_retrieval_history_entry(
|
| 1302 |
retrieval_query,
|
|
@@ -1439,16 +1471,16 @@ HEADER_CSS = """
|
|
| 1439 |
gap: 10px !important;
|
| 1440 |
}
|
| 1441 |
.twin-title-image {
|
| 1442 |
-
flex: 0 0
|
| 1443 |
margin: 0 !important;
|
| 1444 |
-
min-width:
|
| 1445 |
-
width:
|
| 1446 |
}
|
| 1447 |
.twin-title-image img {
|
| 1448 |
-
border-radius:
|
| 1449 |
-
height:
|
| 1450 |
-
object-fit:
|
| 1451 |
-
width:
|
| 1452 |
}
|
| 1453 |
.twin-title-text {
|
| 1454 |
flex: 0 0 auto !important;
|
|
@@ -1478,14 +1510,14 @@ def create_app():
|
|
| 1478 |
):
|
| 1479 |
gr.Image(
|
| 1480 |
value=str(PROFILE_IMAGE),
|
| 1481 |
-
height=
|
| 1482 |
-
width=
|
| 1483 |
show_label=False,
|
| 1484 |
buttons=[],
|
| 1485 |
container=False,
|
| 1486 |
interactive=False,
|
| 1487 |
scale=0,
|
| 1488 |
-
min_width=
|
| 1489 |
elem_classes=["twin-title-image"],
|
| 1490 |
)
|
| 1491 |
gr.Markdown(
|
|
|
|
| 87 |
"overview": {
|
| 88 |
"overview",
|
| 89 |
"biography",
|
| 90 |
+
"what's your background",
|
| 91 |
+
"whats your background",
|
| 92 |
+
"your background",
|
| 93 |
+
"personal background",
|
| 94 |
"who are you",
|
| 95 |
"about yourself",
|
| 96 |
"about rajan",
|
|
|
|
| 237 |
the user to repeat them.
|
| 238 |
- Never send the same notification more than once in a turn.
|
| 239 |
- A knowledge-gap notification must not include unnecessary personal or sensitive data.
|
| 240 |
+
- When a genuine knowledge gap occurs, call send_notification immediately. Do not ask
|
| 241 |
+
the user whether Rajan should be notified and do not request confirmation.
|
| 242 |
|
| 243 |
Boundary guidelines:
|
| 244 |
- If a message is offensive, abusive, sexually inappropriate, or contains hate
|
|
|
|
| 831 |
def select_retrieval_results(
|
| 832 |
results: dict[str, Any],
|
| 833 |
max_results: int = RETRIEVAL_RESULTS,
|
| 834 |
+
category_filter: str | None = None,
|
| 835 |
) -> list[tuple[str, dict[str, Any], float | None]]:
|
| 836 |
+
"""Keep ranked unique chunks, with safe fallback for filtered categories."""
|
| 837 |
documents = (results.get("documents") or [[]])[0]
|
| 838 |
metadatas = (results.get("metadatas") or [[]])[0]
|
| 839 |
distances = (results.get("distances") or [[]])[0]
|
|
|
|
| 847 |
continue
|
| 848 |
metadata = metadatas[index] if index < len(metadatas) else {}
|
| 849 |
distance = distances[index] if index < len(distances) else None
|
| 850 |
+
if (
|
| 851 |
+
category_filter is None
|
| 852 |
+
and distance is not None
|
| 853 |
+
and distance > max_distance
|
| 854 |
+
):
|
| 855 |
+
logger.info(
|
| 856 |
+
"Rejected retrieval candidate category=%s distance=%s cutoff=%s",
|
| 857 |
+
(metadata or {}).get("category"),
|
| 858 |
+
distance,
|
| 859 |
+
max_distance,
|
| 860 |
+
)
|
| 861 |
continue
|
| 862 |
+
if (
|
| 863 |
+
category_filter is not None
|
| 864 |
+
and distance is not None
|
| 865 |
+
and distance > max_distance
|
| 866 |
+
):
|
| 867 |
+
logger.info(
|
| 868 |
+
"Retained category-filtered candidate category=%s "
|
| 869 |
+
"distance=%s above global cutoff=%s",
|
| 870 |
+
category_filter,
|
| 871 |
+
distance,
|
| 872 |
+
max_distance,
|
| 873 |
+
)
|
| 874 |
|
| 875 |
text_key = hashlib.sha256(document.strip().encode("utf-8")).hexdigest()
|
| 876 |
if text_key in seen_text:
|
|
|
|
| 1325 |
query_arguments["where"] = {"category": category}
|
| 1326 |
|
| 1327 |
results = active_collection.query(**query_arguments)
|
| 1328 |
+
selected_results = select_retrieval_results(
|
| 1329 |
+
results,
|
| 1330 |
+
category_filter=category,
|
| 1331 |
+
)
|
| 1332 |
context = format_retrieved_context(selected_results)
|
| 1333 |
retrieval_entry = build_retrieval_history_entry(
|
| 1334 |
retrieval_query,
|
|
|
|
| 1471 |
gap: 10px !important;
|
| 1472 |
}
|
| 1473 |
.twin-title-image {
|
| 1474 |
+
flex: 0 0 60px !important;
|
| 1475 |
margin: 0 !important;
|
| 1476 |
+
min-width: 60px !important;
|
| 1477 |
+
width: 60px !important;
|
| 1478 |
}
|
| 1479 |
.twin-title-image img {
|
| 1480 |
+
border-radius: 8px !important;
|
| 1481 |
+
height: 60px !important;
|
| 1482 |
+
object-fit: contain !important;
|
| 1483 |
+
width: 60px !important;
|
| 1484 |
}
|
| 1485 |
.twin-title-text {
|
| 1486 |
flex: 0 0 auto !important;
|
|
|
|
| 1510 |
):
|
| 1511 |
gr.Image(
|
| 1512 |
value=str(PROFILE_IMAGE),
|
| 1513 |
+
height=60,
|
| 1514 |
+
width=60,
|
| 1515 |
show_label=False,
|
| 1516 |
buttons=[],
|
| 1517 |
container=False,
|
| 1518 |
interactive=False,
|
| 1519 |
scale=0,
|
| 1520 |
+
min_width=60,
|
| 1521 |
elem_classes=["twin-title-image"],
|
| 1522 |
)
|
| 1523 |
gr.Markdown(
|