fix: truncate rwfx 3q context
This commit is contained in:
1 parent
daff70c34f
commit
7e7d3c9846
2 files changed
+48
-4
No files matched your search
+14
-2
@@ -17,6 +17,17 @@ from langchain_core.messages import HumanMessage
|
||||
|
||||
_CONTEXT_KEY = "task_external_context"
|
||||
_TAG = "task_external_context"
|
||||
_MAX_CONTEXT_CHARS = 18000
|
||||
_TAIL_CONTEXT_CHARS = 2500
|
||||
|
||||
|
||||
def _truncate_middle(text: str, max_chars: int = _MAX_CONTEXT_CHARS) -> str:
|
||||
if len(text) <= max_chars:
|
||||
return text
|
||||
marker = "\n\n...[隐藏任务上下文过长,已截断中间部分]...\n\n"
|
||||
tail_chars = min(_TAIL_CONTEXT_CHARS, max_chars // 4)
|
||||
head_chars = max(0, max_chars - len(marker) - tail_chars)
|
||||
return f"{text[:head_chars]}{marker}{text[-tail_chars:]}"
|
||||
|
||||
|
||||
def _render_context(value: Any) -> str:
|
||||
@@ -25,9 +36,10 @@ def _render_context(value: Any) -> str:
|
||||
if isinstance(value, str):
|
||||
return value.strip()
|
||||
try:
|
||||
return json.dumps(value, ensure_ascii=False, indent=2, default=str).strip()
|
||||
text = json.dumps(value, ensure_ascii=False, indent=2, default=str).strip()
|
||||
except Exception:
|
||||
return str(value).strip()
|
||||
text = str(value).strip()
|
||||
return _truncate_middle(text)
|
||||
|
||||
|
||||
def _task_context_from_request(request: Any) -> Any:
|
||||
|
||||
Reference in new issue
Block a user