Refactor chat memory utilities: Update token limits, simplify score calculation, and remove deprecated expansion logic.
This commit is contained in:
@@ -300,12 +300,12 @@ def safe_delete_chat_memory_for_session(db: Session, session_id: Optional[str])
|
|||||||
print("[chat-memory] session delete failed:", exc)
|
print("[chat-memory] session delete failed:", exc)
|
||||||
|
|
||||||
|
|
||||||
def _query_tokens(prompt: str, limit: int = 16) -> List[str]:
|
def _query_tokens(prompt: str, limit: int = 24) -> List[str]:
|
||||||
tokens: List[str] = []
|
tokens: List[str] = []
|
||||||
seen = set()
|
seen = set()
|
||||||
for raw in _TOKEN_RE.findall(str(prompt or "").casefold()):
|
for raw in _TOKEN_RE.findall(str(prompt or "").casefold()):
|
||||||
token = raw.strip("'_-")
|
token = raw.strip("'_-")
|
||||||
if len(token) < 3 or token in _STOPWORDS or token in seen:
|
if len(token) < 2 or token in seen:
|
||||||
continue
|
continue
|
||||||
seen.add(token)
|
seen.add(token)
|
||||||
tokens.append(token)
|
tokens.append(token)
|
||||||
@@ -314,22 +314,6 @@ def _query_tokens(prompt: str, limit: int = 16) -> List[str]:
|
|||||||
return tokens
|
return tokens
|
||||||
|
|
||||||
|
|
||||||
def _expanded_query_tokens(tokens: Sequence[str], limit: int = 24) -> List[str]:
|
|
||||||
expanded: List[str] = []
|
|
||||||
seen = set()
|
|
||||||
for token in tokens:
|
|
||||||
values = [token, *_QUERY_EXPANSIONS.get(token, [])]
|
|
||||||
for value in values:
|
|
||||||
cleaned = str(value or "").casefold().strip()
|
|
||||||
if not cleaned or cleaned in seen:
|
|
||||||
continue
|
|
||||||
seen.add(cleaned)
|
|
||||||
expanded.append(cleaned)
|
|
||||||
if len(expanded) >= limit:
|
|
||||||
return expanded
|
|
||||||
return expanded
|
|
||||||
|
|
||||||
|
|
||||||
def _fts_match_query(tokens: Sequence[str]) -> str:
|
def _fts_match_query(tokens: Sequence[str]) -> str:
|
||||||
safe_terms = []
|
safe_terms = []
|
||||||
for token in tokens:
|
for token in tokens:
|
||||||
@@ -339,14 +323,6 @@ def _fts_match_query(tokens: Sequence[str]) -> str:
|
|||||||
return " OR ".join(safe_terms)
|
return " OR ".join(safe_terms)
|
||||||
|
|
||||||
|
|
||||||
def _minimum_memory_score(token_count: int) -> float:
|
|
||||||
if token_count <= 1:
|
|
||||||
return 0.85
|
|
||||||
if token_count == 2:
|
|
||||||
return 0.9
|
|
||||||
return 0.65
|
|
||||||
|
|
||||||
|
|
||||||
def _row_mapping(row: Any) -> Dict[str, Any]:
|
def _row_mapping(row: Any) -> Dict[str, Any]:
|
||||||
if isinstance(row, dict):
|
if isinstance(row, dict):
|
||||||
return row
|
return row
|
||||||
@@ -360,8 +336,6 @@ def _score_hit(
|
|||||||
row: Dict[str, Any],
|
row: Dict[str, Any],
|
||||||
tokens: Sequence[str],
|
tokens: Sequence[str],
|
||||||
fts_rank: Optional[float] = None,
|
fts_rank: Optional[float] = None,
|
||||||
*,
|
|
||||||
query_token_count: Optional[int] = None,
|
|
||||||
) -> float:
|
) -> float:
|
||||||
title = str(row.get("session_name") or "").casefold()
|
title = str(row.get("session_name") or "").casefold()
|
||||||
user_text = str(row.get("user_content") or "").casefold()
|
user_text = str(row.get("user_content") or "").casefold()
|
||||||
@@ -371,10 +345,7 @@ def _score_hit(
|
|||||||
hits = [token for token in tokens if token in combined]
|
hits = [token for token in tokens if token in combined]
|
||||||
if not hits:
|
if not hits:
|
||||||
return 0.0
|
return 0.0
|
||||||
core_count = max(1, int(query_token_count or len(tokens) or 1))
|
score = len(hits) / max(1, min(len(tokens), 8))
|
||||||
if core_count >= 2 and len(hits) < 2:
|
|
||||||
return 0.0
|
|
||||||
score = len(hits) / max(1, min(core_count, 4))
|
|
||||||
score += 0.18 * sum(1 for token in hits if token in title)
|
score += 0.18 * sum(1 for token in hits if token in title)
|
||||||
score += 0.10 * sum(1 for token in hits if token in user_text)
|
score += 0.10 * sum(1 for token in hits if token in user_text)
|
||||||
if fts_rank is not None:
|
if fts_rank is not None:
|
||||||
@@ -396,7 +367,6 @@ def _load_fts_candidates(
|
|||||||
*,
|
*,
|
||||||
exclude_session_id: Optional[str],
|
exclude_session_id: Optional[str],
|
||||||
limit: int,
|
limit: int,
|
||||||
query_token_count: int,
|
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
match = _fts_match_query(tokens)
|
match = _fts_match_query(tokens)
|
||||||
if not match:
|
if not match:
|
||||||
@@ -426,7 +396,7 @@ def _load_fts_candidates(
|
|||||||
candidates: List[Dict[str, Any]] = []
|
candidates: List[Dict[str, Any]] = []
|
||||||
for row in rows:
|
for row in rows:
|
||||||
item = _row_mapping(row)
|
item = _row_mapping(row)
|
||||||
item["_score"] = _score_hit(item, tokens, item.get("fts_rank"), query_token_count=query_token_count)
|
item["_score"] = _score_hit(item, tokens, item.get("fts_rank"))
|
||||||
if item["_score"] > 0:
|
if item["_score"] > 0:
|
||||||
candidates.append(item)
|
candidates.append(item)
|
||||||
return candidates
|
return candidates
|
||||||
@@ -438,7 +408,6 @@ def _load_table_candidates(
|
|||||||
*,
|
*,
|
||||||
exclude_session_id: Optional[str],
|
exclude_session_id: Optional[str],
|
||||||
limit: int,
|
limit: int,
|
||||||
query_token_count: int,
|
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
try:
|
try:
|
||||||
rows = db.execute(text(
|
rows = db.execute(text(
|
||||||
@@ -461,7 +430,7 @@ def _load_table_candidates(
|
|||||||
candidates: List[Dict[str, Any]] = []
|
candidates: List[Dict[str, Any]] = []
|
||||||
for row in rows:
|
for row in rows:
|
||||||
item = _row_mapping(row)
|
item = _row_mapping(row)
|
||||||
item["_score"] = _score_hit(item, tokens, query_token_count=query_token_count)
|
item["_score"] = _score_hit(item, tokens)
|
||||||
if item["_score"] > 0:
|
if item["_score"] > 0:
|
||||||
candidates.append(item)
|
candidates.append(item)
|
||||||
return candidates
|
return candidates
|
||||||
@@ -473,7 +442,6 @@ def _load_live_candidates(
|
|||||||
*,
|
*,
|
||||||
exclude_session_id: Optional[str],
|
exclude_session_id: Optional[str],
|
||||||
limit: int,
|
limit: int,
|
||||||
query_token_count: int,
|
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
try:
|
try:
|
||||||
rows = db.execute(text(
|
rows = db.execute(text(
|
||||||
@@ -527,7 +495,7 @@ def _load_live_candidates(
|
|||||||
"attachment_summary": attachments,
|
"attachment_summary": attachments,
|
||||||
"created_at": str(item.get("created_at") or ""),
|
"created_at": str(item.get("created_at") or ""),
|
||||||
}
|
}
|
||||||
turn["_score"] = _score_hit(turn, tokens, query_token_count=query_token_count)
|
turn["_score"] = _score_hit(turn, tokens)
|
||||||
if turn["_score"] > 0:
|
if turn["_score"] > 0:
|
||||||
turns.append(turn)
|
turns.append(turn)
|
||||||
return turns
|
return turns
|
||||||
|
|||||||
Reference in New Issue
Block a user