sycommon-python-lib 0.2.3a6__py3-none-any.whl → 0.2.3a8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,23 +1,88 @@
1
1
  """上下文压缩 middleware 构建工具。
2
2
 
3
3
  根据 nacos 中配置的模型 maxTokens,用绝对 token 数设置压缩阈值。
4
- 仅创建 SummarizationToolMiddleware(compact_conversation 工具),
5
- 自动压缩由 deepagents 内置的 SummarizationMiddleware 处理(通过 model.profile 获取阈值)。
4
+ 中文场景下 count_tokens_approximately 默认 chars_per_token=4.0 会低估约 2.1 倍,
5
+ 因此传入自定义 token_counter 修正偏差,并按真实 token 数设定阈值。
6
6
  """
7
7
 
8
8
  from __future__ import annotations
9
9
 
10
+ import functools
11
+ import logging
10
12
  from typing import TYPE_CHECKING
11
13
 
12
14
  from deepagents.middleware.summarization import (
13
15
  SummarizationMiddleware,
14
16
  SummarizationToolMiddleware,
15
17
  )
18
+ from langchain_core.messages.utils import count_tokens_approximately
16
19
 
17
20
  if TYPE_CHECKING:
18
21
  from langchain_core.language_models import BaseChatModel
19
22
  from deepagents.backends.protocol import BACKEND_TYPES
20
23
 
24
+ logger = logging.getLogger(__name__)
25
+
26
+ # 中文场景修正:chars_per_token 从 4.0 改为 2.0,使近似计数接近真实 token 数
27
+ _chinese_token_counter = functools.partial(count_tokens_approximately, chars_per_token=2.0)
28
+
29
+
30
+ def _patched_compute_summarization_defaults(model):
31
+ """覆盖 deepagents 默认值,返回中文场景修正后的绝对 token 阈值。
32
+
33
+ 不走 fraction 路径(fraction 依赖 model.profile.max_input_tokens,
34
+ 但内置 middleware 用默认 chars_per_token=4.0 计数,fraction 基于的基数不准)。
35
+ 直接返回修正后的绝对 token 数。
36
+ """
37
+ try:
38
+ from sycommon.config.Config import Config
39
+ model_name = getattr(model, 'model_name', None) or getattr(model, 'model', None)
40
+ if model_name:
41
+ llm_cfg = Config().get_llm_config(model_name)
42
+ max_tokens = llm_cfg.get("maxTokens", 72000)
43
+ else:
44
+ max_tokens = 72000
45
+ except Exception:
46
+ max_tokens = 72000
47
+
48
+ trigger = int(max_tokens * 0.70)
49
+ keep = int(max_tokens * 0.10)
50
+ return {
51
+ "trigger": ("tokens", trigger),
52
+ "keep": ("tokens", keep),
53
+ "truncate_args_settings": {
54
+ "trigger": ("tokens", trigger),
55
+ "keep": ("tokens", keep),
56
+ },
57
+ }
58
+
59
+
60
+ # monkey-patch:替换 deepagents 的默认计算函数
61
+ import deepagents.middleware.summarization as _summ_mod
62
+ _summ_mod.compute_summarization_defaults = _patched_compute_summarization_defaults
63
+
64
+ # monkey-patch:在内置 middleware 的 awrap_model_call 中注入日志
65
+ _OrigDeepAgentsSumm = _summ_mod._DeepAgentsSummarizationMiddleware
66
+ _orig_awrap_model_call = _OrigDeepAgentsSumm.awrap_model_call
67
+
68
+
69
+ async def _patched_awrap_model_call(self, request, handler):
70
+ effective_messages = self._get_effective_messages(request)
71
+ truncated_messages, _ = self._truncate_args(
72
+ effective_messages, request.system_message, request.tools,
73
+ )
74
+ counted_messages = [request.system_message, *truncated_messages] if request.system_message is not None else truncated_messages
75
+ try:
76
+ total_tokens = self.token_counter(counted_messages, tools=request.tools)
77
+ except TypeError:
78
+ total_tokens = self.token_counter(counted_messages)
79
+ should = self._should_summarize(truncated_messages, total_tokens)
80
+ print(f"[TokenCount] tokens={total_tokens} msgs={len(counted_messages)} should_summarize={should}")
81
+ return await _orig_awrap_model_call(self, request, handler)
82
+
83
+
84
+ _OrigDeepAgentsSumm.awrap_model_call = _patched_awrap_model_call
85
+
21
86
 
22
87
  def build_summarization_middleware(
23
88
  model: BaseChatModel,
@@ -64,6 +129,7 @@ def build_summarization_middleware(
64
129
  backend=backend,
65
130
  trigger=("tokens", trigger_tokens),
66
131
  keep=("tokens", keep_tokens),
132
+ token_counter=_chinese_token_counter,
67
133
  trim_tokens_to_summarize=None,
68
134
  truncate_args_settings={
69
135
  "trigger": ("tokens", trigger_tokens),
@@ -71,6 +137,7 @@ def build_summarization_middleware(
71
137
  "max_length": 2000,
72
138
  },
73
139
  )
140
+
74
141
  print(f"[Summarization] compact_conversation 工具配置: model={model_name}, max_tokens={max_tokens}, "
75
142
  f"trigger={trigger_tokens} tokens ({trigger_fraction:.0%}), "
76
143
  f"keep={keep_tokens} tokens ({keep_fraction:.0%})")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sycommon-python-lib
3
- Version: 0.2.3a6
3
+ Version: 0.2.3a8
4
4
  Summary: Add your description here
5
5
  Requires-Python: >=3.11
6
6
  Description-Content-Type: text/markdown
@@ -9,16 +9,16 @@ Requires-Dist: aiohttp>=3.13.5
9
9
  Requires-Dist: aiomysql>=0.3.2
10
10
  Requires-Dist: anyio>=4.12.1
11
11
  Requires-Dist: decorator>=5.2.1
12
- Requires-Dist: deepagents>=0.5.7
12
+ Requires-Dist: deepagents>=0.6.1
13
13
  Requires-Dist: elasticsearch>=9.4.0
14
14
  Requires-Dist: fastapi>=0.136.1
15
15
  Requires-Dist: jinja2>=3.1.6
16
16
  Requires-Dist: kafka-python>=2.3.1
17
- Requires-Dist: langchain>=1.2.17
18
- Requires-Dist: langchain-core>=1.3.3
17
+ Requires-Dist: langchain>=1.3.0
18
+ Requires-Dist: langchain-core>=1.4.0
19
19
  Requires-Dist: langchain-openai>=1.2.1
20
- Requires-Dist: langfuse>=4.5.1
21
- Requires-Dist: langgraph>=1.1.10
20
+ Requires-Dist: langfuse>=4.6.1
21
+ Requires-Dist: langgraph>=1.2.0
22
22
  Requires-Dist: langgraph-checkpoint-redis>=0.4.1
23
23
  Requires-Dist: ldap3>=2.9.1
24
24
  Requires-Dist: loguru>=0.7.3
@@ -28,10 +28,10 @@ Requires-Dist: psutil>=7.2.2
28
28
  Requires-Dist: pyxxl>=0.4.6
29
29
  Requires-Dist: pydantic>=2.13.4
30
30
  Requires-Dist: python-dotenv>=1.2.2
31
- Requires-Dist: python-multipart>=0.0.27
31
+ Requires-Dist: python-multipart>=0.0.28
32
32
  Requires-Dist: pyyaml>=6.0.3
33
33
  Requires-Dist: redis>=7.3.0
34
- Requires-Dist: sentry-sdk[fastapi]>=2.59.0
34
+ Requires-Dist: sentry-sdk[fastapi]>=2.60.0
35
35
  Requires-Dist: sqlalchemy[asyncio]>=2.0.48
36
36
  Requires-Dist: starlette[full]>=1.0.0
37
37
  Requires-Dist: tiktoken>=0.12.0
@@ -131,7 +131,7 @@ sycommon/agent/agent_manager.py,sha256=UhhaekEumT7g4v_Z1UB4jTp13X0n8M8erYaQdkGGW
131
131
  sycommon/agent/chat_events.py,sha256=bWAMWYIZ2L_yqUcn5jq9ius_lQxLHEv4zQLEqX6UaeM,13190
132
132
  sycommon/agent/deep_agent.py,sha256=3RY4ijSxm7-I7NN4wlr-1PcIvdFShjFE39_qNfCVGb4,30500
133
133
  sycommon/agent/multi_agent_team.py,sha256=6Ba4MbdEDVuXe6iBE_Q84kzr5zJYLo_djdpkkwJhShg,26558
134
- sycommon/agent/summarization_utils.py,sha256=7iugXE60wkeJ8AfU2G3S6JePhRRA2uUTau8zJRXTNYk,2909
134
+ sycommon/agent/summarization_utils.py,sha256=vOixzpuDBH-g8_xPAjt8-zPz1e4Kf9fyblvXFpkGUfk,5599
135
135
  sycommon/agent/sandbox/__init__.py,sha256=jR7LlkD4J4Y6QYyRXQClkwmqDBCCPmycV_hQV9p9YHw,4621
136
136
  sycommon/agent/sandbox/file_ops.py,sha256=6ymRMM0WchM7G_YmF1ckrLjf5s_JCh1wrAp2g_-sg8k,23162
137
137
  sycommon/agent/sandbox/http_sandbox_backend.py,sha256=mjiTZnADvUq_rO05ewllo_eGDS4uTdD2e2GGYvBpF-Q,56150
@@ -258,8 +258,8 @@ sycommon/tools/syemail.py,sha256=BDFhgf7WDOQeTcjxJEQdu0dQhnHFPO_p3eI0-Ni3LhQ,561
258
258
  sycommon/tools/timing.py,sha256=OiiE7P07lRoMzX9kzb8sZU9cDb0zNnqIlY5pWqHcnkY,2064
259
259
  sycommon/xxljob/__init__.py,sha256=7eoBlQxv-B39IfRSCY2bkqdGYs1QRe1umAWd88VMEEM,86
260
260
  sycommon/xxljob/xxljob_service.py,sha256=JIEJaGXhqrTLcyxlyynSrsHg9bBnDNzX-D4qIWLRPUE,6815
261
- sycommon_python_lib-0.2.3a6.dist-info/METADATA,sha256=ql7nW3cSs3U-REXMv-nYh1A5eks3i-YkktTX891cZC8,7741
262
- sycommon_python_lib-0.2.3a6.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
263
- sycommon_python_lib-0.2.3a6.dist-info/entry_points.txt,sha256=gsR4SssKxDWjRU8ggidzNcdMXDPRSKRS7UaGyNP84Qg,92
264
- sycommon_python_lib-0.2.3a6.dist-info/top_level.txt,sha256=RgphKrg7nJyZ7irJqbxFr-5H2LUYTvI7ivoWZH2hcD0,29
265
- sycommon_python_lib-0.2.3a6.dist-info/RECORD,,
261
+ sycommon_python_lib-0.2.3a8.dist-info/METADATA,sha256=WCl4mIVh0pZAMUSmzyyHIryCiIxiMz8oxhDlt9DBnBY,7739
262
+ sycommon_python_lib-0.2.3a8.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
263
+ sycommon_python_lib-0.2.3a8.dist-info/entry_points.txt,sha256=gsR4SssKxDWjRU8ggidzNcdMXDPRSKRS7UaGyNP84Qg,92
264
+ sycommon_python_lib-0.2.3a8.dist-info/top_level.txt,sha256=RgphKrg7nJyZ7irJqbxFr-5H2LUYTvI7ivoWZH2hcD0,29
265
+ sycommon_python_lib-0.2.3a8.dist-info/RECORD,,