mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-04 22:13:08 +00:00
The backend import package is now docsgpt, the name it will carry on PyPI; application was far too generic to install into anyone's site-packages. git mv plus a mechanical rewrite of every import, dotted string and path reference: 734 Python files, the compose files, Dockerfile, workflows, docs, setup scripts, devcontainer, k8s manifests, vscode config, pytest and coverage config, .gitignore. Behaviour is unchanged. Kept for one release: - A top-level application package whose meta-path finder resolves application.x.y to the already-imported docsgpt.x.y object, so old imports and entry points (celery -A application.app.celery, uvicorn application.asgi:asgi_app) keep working with a FutureWarning. - Celery registers every application.* task name as an alias of its docsgpt.* task on start-up, so messages queued by the previous release still run. The redbeat key prefix moves to redbeat:docsgpt:v2: so schedule entries the previous release wrote are left unread instead of firing twice. The backend image builds from the repository root (docker build -f docsgpt/Dockerfile .) so it can ship the alias package; a root .dockerignore allow-lists docsgpt/ and application/ and keeps caches, local data, .env files, the sample index files and the Dockerfile out. Compose and the image workflows point at the new context.
112 lines
4.0 KiB
Python
112 lines
4.0 KiB
Python
"""Compression threshold checking logic."""
|
|
|
|
import logging
|
|
from typing import Any, Dict
|
|
|
|
from docsgpt.core.model_utils import get_token_limit
|
|
from docsgpt.core.settings import settings
|
|
from docsgpt.api.answer.services.compression.token_counter import TokenCounter
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class CompressionThresholdChecker:
|
|
"""Determines if compression is needed based on token thresholds."""
|
|
|
|
def __init__(self, threshold_percentage: float = None):
|
|
"""
|
|
Initialize threshold checker.
|
|
|
|
Args:
|
|
threshold_percentage: Percentage of context to use as threshold
|
|
(defaults to settings.COMPRESSION_THRESHOLD_PERCENTAGE)
|
|
"""
|
|
self.threshold_percentage = (
|
|
threshold_percentage or settings.COMPRESSION_THRESHOLD_PERCENTAGE
|
|
)
|
|
|
|
def should_compress(
|
|
self,
|
|
conversation: Dict[str, Any],
|
|
model_id: str,
|
|
current_query_tokens: int = 500,
|
|
user_id: str | None = None,
|
|
) -> bool:
|
|
"""
|
|
Determine if compression is needed.
|
|
|
|
Args:
|
|
conversation: Full conversation document
|
|
model_id: Target model for this request
|
|
current_query_tokens: Estimated tokens for current query
|
|
user_id: Owner — needed so per-user BYOM custom-model UUIDs
|
|
resolve when looking up the context window.
|
|
|
|
Returns:
|
|
True if tokens >= threshold% of context window
|
|
"""
|
|
try:
|
|
# What the next turn will replay: summary + queries after the
|
|
# last compression point, or the raw history when never compressed.
|
|
total_tokens = TokenCounter.count_effective_conversation_tokens(conversation)
|
|
total_tokens += current_query_tokens
|
|
|
|
# Get context window limit for model
|
|
context_limit = get_token_limit(model_id, user_id=user_id)
|
|
|
|
# Calculate threshold
|
|
threshold = int(context_limit * self.threshold_percentage)
|
|
|
|
compression_needed = total_tokens >= threshold
|
|
percentage_used = (total_tokens / context_limit) * 100
|
|
|
|
if compression_needed:
|
|
logger.warning(
|
|
f"COMPRESSION TRIGGERED: {total_tokens} tokens / {context_limit} limit "
|
|
f"({percentage_used:.1f}% used, threshold: {self.threshold_percentage * 100:.0f}%)"
|
|
)
|
|
else:
|
|
logger.info(
|
|
f"Compression check: {total_tokens}/{context_limit} tokens "
|
|
f"({percentage_used:.1f}% used, threshold: {self.threshold_percentage * 100:.0f}%) - No compression needed"
|
|
)
|
|
|
|
return compression_needed
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error checking compression need: {str(e)}", exc_info=True)
|
|
return False
|
|
|
|
def check_message_tokens(
|
|
self, messages: list, model_id: str, user_id: str | None = None
|
|
) -> bool:
|
|
"""
|
|
Check if message list exceeds threshold.
|
|
|
|
Args:
|
|
messages: List of message dicts
|
|
model_id: Target model
|
|
user_id: Owner — needed so per-user BYOM custom-model UUIDs
|
|
resolve when looking up the context window.
|
|
|
|
Returns:
|
|
True if at or above threshold
|
|
"""
|
|
try:
|
|
current_tokens = TokenCounter.count_message_tokens(messages)
|
|
context_limit = get_token_limit(model_id, user_id=user_id)
|
|
threshold = int(context_limit * self.threshold_percentage)
|
|
|
|
if current_tokens >= threshold:
|
|
logger.warning(
|
|
f"Message context limit approaching: {current_tokens}/{context_limit} tokens "
|
|
f"({(current_tokens/context_limit)*100:.1f}%)"
|
|
)
|
|
return True
|
|
|
|
return False
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error checking message tokens: {str(e)}", exc_info=True)
|
|
return False
|