sycommon-python-lib 0.1.56b3__tar.gz → 0.1.56b4__tar.gz

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.
Files changed (77) hide show
  1. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/PKG-INFO +1 -1
  2. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/pyproject.toml +1 -1
  3. sycommon_python_lib-0.1.56b4/src/sycommon/llm/get_llm.py +246 -0
  4. sycommon_python_lib-0.1.56b4/src/sycommon/llm/llm_tokens.py +119 -0
  5. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/synacos/nacos_service.py +5 -0
  6. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon_python_lib.egg-info/PKG-INFO +1 -1
  7. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon_python_lib.egg-info/SOURCES.txt +1 -0
  8. sycommon_python_lib-0.1.56b3/src/sycommon/llm/get_llm.py +0 -177
  9. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/README.md +0 -0
  10. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/setup.cfg +0 -0
  11. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/command/cli.py +0 -0
  12. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/__init__.py +0 -0
  13. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/config/Config.py +0 -0
  14. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/config/DatabaseConfig.py +0 -0
  15. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/config/EmbeddingConfig.py +0 -0
  16. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/config/LLMConfig.py +0 -0
  17. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/config/MQConfig.py +0 -0
  18. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/config/RerankerConfig.py +0 -0
  19. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/config/__init__.py +0 -0
  20. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/database/async_base_db_service.py +0 -0
  21. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/database/async_database_service.py +0 -0
  22. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/database/base_db_service.py +0 -0
  23. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/database/database_service.py +0 -0
  24. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/health/__init__.py +0 -0
  25. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/health/health_check.py +0 -0
  26. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/health/metrics.py +0 -0
  27. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/health/ping.py +0 -0
  28. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/llm/__init__.py +0 -0
  29. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/llm/embedding.py +0 -0
  30. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/llm/llm_logger.py +0 -0
  31. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/logging/__init__.py +0 -0
  32. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/logging/async_sql_logger.py +0 -0
  33. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/logging/kafka_log.py +0 -0
  34. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/logging/logger_levels.py +0 -0
  35. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/logging/logger_wrapper.py +0 -0
  36. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/logging/sql_logger.py +0 -0
  37. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/middleware/__init__.py +0 -0
  38. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/middleware/context.py +0 -0
  39. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/middleware/cors.py +0 -0
  40. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/middleware/docs.py +0 -0
  41. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/middleware/exception.py +0 -0
  42. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/middleware/middleware.py +0 -0
  43. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/middleware/monitor_memory.py +0 -0
  44. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/middleware/mq.py +0 -0
  45. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/middleware/timeout.py +0 -0
  46. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/middleware/traceid.py +0 -0
  47. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/models/__init__.py +0 -0
  48. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/models/base_http.py +0 -0
  49. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/models/log.py +0 -0
  50. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/models/mqlistener_config.py +0 -0
  51. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/models/mqmsg_model.py +0 -0
  52. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/models/mqsend_config.py +0 -0
  53. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/models/sso_user.py +0 -0
  54. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/notice/__init__.py +0 -0
  55. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/notice/uvicorn_monitor.py +0 -0
  56. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/rabbitmq/rabbitmq_client.py +0 -0
  57. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/rabbitmq/rabbitmq_pool.py +0 -0
  58. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/rabbitmq/rabbitmq_service.py +0 -0
  59. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/services.py +0 -0
  60. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/sse/__init__.py +0 -0
  61. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/sse/event.py +0 -0
  62. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/sse/sse.py +0 -0
  63. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/synacos/__init__.py +0 -0
  64. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/synacos/example.py +0 -0
  65. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/synacos/example2.py +0 -0
  66. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/synacos/feign.py +0 -0
  67. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/synacos/feign_client.py +0 -0
  68. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/synacos/param.py +0 -0
  69. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/tools/__init__.py +0 -0
  70. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/tools/docs.py +0 -0
  71. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/tools/merge_headers.py +0 -0
  72. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/tools/snowflake.py +0 -0
  73. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon/tools/timing.py +0 -0
  74. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon_python_lib.egg-info/dependency_links.txt +0 -0
  75. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon_python_lib.egg-info/entry_points.txt +0 -0
  76. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon_python_lib.egg-info/requires.txt +0 -0
  77. {sycommon_python_lib-0.1.56b3 → sycommon_python_lib-0.1.56b4}/src/sycommon_python_lib.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sycommon-python-lib
3
- Version: 0.1.56b3
3
+ Version: 0.1.56b4
4
4
  Summary: Add your description here
5
5
  Requires-Python: >=3.11
6
6
  Description-Content-Type: text/markdown
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sycommon-python-lib"
3
- version = "0.1.56-beta3"
3
+ version = "0.1.56-beta4"
4
4
  description = "Add your description here"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
@@ -0,0 +1,246 @@
1
+ from typing import Dict, Type, List, Optional, Callable, Any
2
+ from sycommon.llm.llm_logger import LLMLogger
3
+ from langchain_core.language_models import BaseChatModel
4
+ from langchain_core.runnables import Runnable, RunnableLambda, RunnableConfig
5
+ from langchain_core.output_parsers import PydanticOutputParser
6
+ from langchain_core.messages import BaseMessage, HumanMessage
7
+ from langchain.chat_models import init_chat_model
8
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
9
+ from pydantic import BaseModel, ValidationError, Field
10
+ from sycommon.config.LLMConfig import LLMConfig
11
+ from sycommon.llm.llm_tokens import TokensCallbackHandler
12
+ from sycommon.logging.kafka_log import SYLogger
13
+
14
+
15
+ class StructuredRunnableWithToken(Runnable):
16
+ """带Token统计的Runnable类"""
17
+
18
+ def __init__(self, retry_chain: Runnable):
19
+ super().__init__()
20
+ self.retry_chain = retry_chain
21
+
22
+ def _adapt_input(self, input: Any) -> List[BaseMessage]:
23
+ """适配输入格式"""
24
+ if isinstance(input, list) and all(isinstance(x, BaseMessage) for x in input):
25
+ return input
26
+ elif isinstance(input, BaseMessage):
27
+ return [input]
28
+ elif isinstance(input, str):
29
+ return [HumanMessage(content=input)]
30
+ elif isinstance(input, dict) and "input" in input:
31
+ return [HumanMessage(content=str(input["input"]))]
32
+ else:
33
+ raise ValueError(f"不支持的输入格式:{type(input)}")
34
+
35
+ def _get_callback_config(self, config: Optional[RunnableConfig] = None) -> tuple[RunnableConfig, TokensCallbackHandler]:
36
+ """构建包含Token统计的回调配置"""
37
+ # 每次调用创建新的Token处理器实例
38
+ token_handler = TokensCallbackHandler()
39
+
40
+ # 初始化配置
41
+ if config is None:
42
+ processed_config = {"callbacks": []}
43
+ else:
44
+ processed_config = config.copy()
45
+ if "callbacks" not in processed_config:
46
+ processed_config["callbacks"] = []
47
+
48
+ # 添加回调(去重)
49
+ callbacks = processed_config["callbacks"]
50
+ # 添加LLMLogger(如果不存在)
51
+ if not any(isinstance(cb, LLMLogger) for cb in callbacks):
52
+ callbacks.append(LLMLogger())
53
+ # 添加Token处理器
54
+ callbacks.append(token_handler)
55
+
56
+ # 按类型去重
57
+ callback_types = {}
58
+ unique_callbacks = []
59
+ for cb in callbacks:
60
+ cb_type = type(cb)
61
+ if cb_type not in callback_types:
62
+ callback_types[cb_type] = cb
63
+ unique_callbacks.append(cb)
64
+
65
+ processed_config["callbacks"] = unique_callbacks
66
+
67
+ return processed_config, token_handler
68
+
69
+ # 同步调用
70
+ def invoke(self, input: Any, config: Optional[RunnableConfig] = None, ** kwargs) -> Dict[str, Any]:
71
+ try:
72
+ processed_config, token_handler = self._get_callback_config(
73
+ config)
74
+ adapted_input = self._adapt_input(input)
75
+
76
+ structured_result = self.retry_chain.invoke(
77
+ {"messages": adapted_input},
78
+ config=processed_config,
79
+ **kwargs
80
+ )
81
+
82
+ # 获取Token统计结果
83
+ token_usage = token_handler.usage_metadata
84
+ structured_result._token_usage_ = token_usage
85
+
86
+ return structured_result
87
+
88
+ except Exception as e:
89
+ SYLogger.error(f"同步LLM调用失败: {str(e)}", exc_info=True)
90
+ return None
91
+
92
+ # 异步调用
93
+ async def ainvoke(self, input: Any, config: Optional[RunnableConfig] = None, ** kwargs) -> Dict[str, Any]:
94
+ try:
95
+ processed_config, token_handler = self._get_callback_config(
96
+ config)
97
+ adapted_input = self._adapt_input(input)
98
+
99
+ structured_result = await self.retry_chain.ainvoke(
100
+ {"messages": adapted_input},
101
+ config=processed_config,
102
+ **kwargs
103
+ )
104
+
105
+ token_usage = token_handler.usage_metadata
106
+ structured_result._token_usage_ = token_usage
107
+
108
+ return structured_result
109
+
110
+ except Exception as e:
111
+ SYLogger.error(f"异步LLM调用失败: {str(e)}", exc_info=True)
112
+ return None
113
+
114
+
115
+ class LLMWithAutoTokenUsage(BaseChatModel):
116
+ """自动为结构化调用返回token_usage的LLM包装类"""
117
+ llm: BaseChatModel = Field(default=None)
118
+
119
+ def __init__(self, llm: BaseChatModel, **kwargs):
120
+ super().__init__(llm=llm, ** kwargs)
121
+
122
+ def with_structured_output(
123
+ self,
124
+ output_model: Type[BaseModel],
125
+ max_retries: int = 3,
126
+ is_extract: bool = False,
127
+ override_prompt: ChatPromptTemplate = None,
128
+ custom_processors: Optional[List[Callable[[str], str]]] = None,
129
+ custom_parser: Optional[Callable[[str], BaseModel]] = None
130
+ ) -> Runnable:
131
+ """返回支持自动统计Token的结构化Runnable"""
132
+ parser = PydanticOutputParser(pydantic_object=output_model)
133
+
134
+ # 提示词模板
135
+ accuracy_instructions = """
136
+ 字段值的抽取准确率(0~1之间),评分规则:
137
+ 1.0(完全准确):直接从原文提取,无需任何加工,且格式与原文完全一致
138
+ 0.9(轻微处理):数据来源明确,但需进行格式标准化或冗余信息剔除(不改变原始数值)
139
+ 0.8(有限推断):数据需通过上下文关联或简单计算得出,仍有明确依据
140
+ 0.8以下(不可靠):数据需大量推测、存在歧义或来源不明,处理方式:直接忽略该数据,设置为None
141
+ """
142
+
143
+ if is_extract:
144
+ prompt = ChatPromptTemplate.from_messages([
145
+ MessagesPlaceholder(variable_name="messages"),
146
+ HumanMessage(content=f"""
147
+ 请提取信息并遵循以下规则:
148
+ 1. 准确率要求:{accuracy_instructions.strip()}
149
+ 2. 输出格式:{parser.get_format_instructions()}
150
+ """)
151
+ ])
152
+ else:
153
+ prompt = override_prompt or ChatPromptTemplate.from_messages([
154
+ MessagesPlaceholder(variable_name="messages"),
155
+ HumanMessage(content=f"""
156
+ 输出格式:{parser.get_format_instructions()}
157
+ """)
158
+ ])
159
+
160
+ # 文本处理函数
161
+ def extract_response_content(response: BaseMessage) -> str:
162
+ try:
163
+ return response.content
164
+ except Exception as e:
165
+ raise ValueError(f"提取响应内容失败:{str(e)}") from e
166
+
167
+ def strip_code_block_markers(content: str) -> str:
168
+ try:
169
+ return content.strip("```json").strip("```").strip()
170
+ except Exception as e:
171
+ raise ValueError(f"移除代码块标记失败:{str(e)}") from e
172
+
173
+ def normalize_in_json(content: str) -> str:
174
+ try:
175
+ return content.replace("None", "null").replace("none", "null").replace("NONE", "null").replace("''", '""')
176
+ except Exception as e:
177
+ raise ValueError(f"JSON格式化失败:{str(e)}") from e
178
+
179
+ def default_parse_to_pydantic(content: str) -> BaseModel:
180
+ try:
181
+ return parser.parse(content)
182
+ except (ValidationError, ValueError) as e:
183
+ raise ValueError(f"解析结构化结果失败:{str(e)}") from e
184
+
185
+ # ========== 构建处理链 ==========
186
+ base_chain = prompt | self.llm | RunnableLambda(
187
+ extract_response_content)
188
+
189
+ # 文本处理链
190
+ process_runnables = custom_processors or [
191
+ RunnableLambda(strip_code_block_markers),
192
+ RunnableLambda(normalize_in_json)
193
+ ]
194
+ process_chain = base_chain
195
+ for runnable in process_runnables:
196
+ process_chain = process_chain | runnable
197
+
198
+ # 解析链
199
+ parse_chain = process_chain | RunnableLambda(
200
+ custom_parser or default_parse_to_pydantic)
201
+
202
+ # 重试链
203
+ retry_chain = parse_chain.with_retry(
204
+ retry_if_exception_type=(ValidationError, ValueError),
205
+ stop_after_attempt=max_retries,
206
+ wait_exponential_jitter=True,
207
+ exponential_jitter_params={
208
+ "initial": 0.1, "max": 3.0, "exp_base": 2.0, "jitter": 1.0}
209
+ )
210
+
211
+ return StructuredRunnableWithToken(retry_chain)
212
+
213
+ # ========== 实现BaseChatModel抽象方法 ==========
214
+ def _generate(self, messages, stop=None, run_manager=None, ** kwargs):
215
+ return self.llm._generate(messages, stop=stop, run_manager=run_manager, ** kwargs)
216
+
217
+ @property
218
+ def _llm_type(self) -> str:
219
+ return self.llm._llm_type
220
+
221
+
222
+ def get_llm(
223
+ model: str = None,
224
+ streaming: bool = False
225
+ ) -> LLMWithAutoTokenUsage:
226
+ if not model:
227
+ model = "Qwen2.5-72B"
228
+
229
+ llmConfig = LLMConfig.from_config(model)
230
+ if not llmConfig:
231
+ raise Exception(f"无效的模型配置:{model}")
232
+
233
+ llm = init_chat_model(
234
+ model_provider=llmConfig.provider,
235
+ model=llmConfig.model,
236
+ base_url=llmConfig.baseUrl,
237
+ api_key="-",
238
+ temperature=0.1,
239
+ streaming=streaming,
240
+ callbacks=[LLMLogger()]
241
+ )
242
+
243
+ if llm is None:
244
+ raise Exception(f"初始化原始LLM实例失败:{model}")
245
+
246
+ return LLMWithAutoTokenUsage(llm)
@@ -0,0 +1,119 @@
1
+ from typing import Any
2
+ from langchain_core.callbacks import AsyncCallbackHandler
3
+ from langchain_core.outputs.llm_result import LLMResult
4
+ from sycommon.logging.kafka_log import SYLogger
5
+
6
+
7
+ class TokensCallbackHandler(AsyncCallbackHandler):
8
+ """
9
+ 继承AsyncCallbackHandler的Token统计处理器
10
+ """
11
+
12
+ def __init__(self):
13
+ super().__init__()
14
+ self.input_tokens = 0
15
+ self.output_tokens = 0
16
+ self.total_tokens = 0
17
+ self.usage_metadata = {}
18
+ self.reset()
19
+
20
+ def reset(self):
21
+ """重置Token统计数据"""
22
+ self.input_tokens = 0
23
+ self.output_tokens = 0
24
+ self.total_tokens = 0
25
+ self.usage_metadata = {
26
+ "input_tokens": 0,
27
+ "output_tokens": 0,
28
+ "total_tokens": 0
29
+ }
30
+
31
+ # ========== 同步回调方法(兼容签名) ==========
32
+ def on_llm_end(
33
+ self,
34
+ response: LLMResult,
35
+ **kwargs: Any,
36
+ ) -> None:
37
+ """同步LLM调用结束时的回调"""
38
+ self._parse_token_usage(response)
39
+
40
+ # ========== 异步回调方法(兼容签名) ==========
41
+ async def on_llm_end(
42
+ self,
43
+ response: LLMResult,
44
+ **kwargs: Any,
45
+ ) -> None:
46
+ """异步LLM调用结束时的回调"""
47
+ self._parse_token_usage(response)
48
+
49
+ def _parse_token_usage(self, response: LLMResult) -> None:
50
+ """
51
+ 通用Token解析逻辑,不依赖特定类结构
52
+ 兼容各种LLM响应格式
53
+ """
54
+ try:
55
+ # 情况1: 标准LangChain响应(有llm_output属性)
56
+ if response.llm_output:
57
+ llm_output = response.llm_output
58
+ self._parse_from_llm_output(llm_output)
59
+
60
+ # 情况2: 包含generations的响应
61
+ elif response.generations:
62
+ self._parse_from_generations(response.generations)
63
+
64
+ # 计算总Token
65
+ if self.total_tokens <= 0:
66
+ self.total_tokens = self.input_tokens + self.output_tokens
67
+
68
+ # 更新metadata
69
+ self.usage_metadata = {
70
+ "input_tokens": self.input_tokens,
71
+ "output_tokens": self.output_tokens,
72
+ "total_tokens": self.total_tokens
73
+ }
74
+
75
+ SYLogger.debug(
76
+ f"Token统计成功 - 输入: {self.input_tokens}, 输出: {self.output_tokens}")
77
+
78
+ except Exception as e:
79
+ SYLogger.warning(f"Token解析失败: {str(e)}", exc_info=True)
80
+ self.reset()
81
+
82
+ def _parse_from_llm_output(self, llm_output: dict) -> None:
83
+ """从llm_output字典解析Token信息"""
84
+ if not isinstance(llm_output, dict):
85
+ return
86
+
87
+ # OpenAI标准格式
88
+ if 'token_usage' in llm_output:
89
+ token_usage = llm_output['token_usage']
90
+ self.input_tokens = token_usage.get(
91
+ 'prompt_tokens', token_usage.get('input_tokens', 0))
92
+ self.output_tokens = token_usage.get(
93
+ 'completion_tokens', token_usage.get('output_tokens', 0))
94
+ self.total_tokens = token_usage.get('total_tokens', 0)
95
+
96
+ # 直接包含Token信息
97
+ else:
98
+ self.input_tokens = llm_output.get(
99
+ 'input_tokens', llm_output.get('prompt_tokens', 0))
100
+ self.output_tokens = llm_output.get(
101
+ 'output_tokens', llm_output.get('completion_tokens', 0))
102
+ self.total_tokens = token_usage.get('total_tokens', 0)
103
+
104
+ def _parse_from_generations(self, generations: list) -> None:
105
+ """从generations列表解析Token信息"""
106
+ if not isinstance(generations, list) or len(generations) == 0:
107
+ return
108
+
109
+ # 遍历generation信息
110
+ for gen_group in generations:
111
+ for generation in gen_group:
112
+ if hasattr(generation, 'generation_info') and generation.generation_info:
113
+ gen_info = generation.generation_info
114
+ self.input_tokens = gen_info.get(
115
+ 'input_tokens', gen_info.get('prompt_tokens', 0))
116
+ self.output_tokens = gen_info.get(
117
+ 'output_tokens', gen_info.get('completion_tokens', 0))
118
+ self.total_tokens = gen_info.get('total_tokens', 0)
119
+ return
@@ -817,6 +817,11 @@ class NacosService(metaclass=SingletonMeta):
817
817
  return []
818
818
 
819
819
  all_instances = instances.get('hosts', [])
820
+ # 筛选已上线实例
821
+ all_instances = [
822
+ instance for instance in all_instances
823
+ if instance.get('enabled', True) # 默认True担心阿里变更sdk
824
+ ]
820
825
  SYLogger.info(
821
826
  f"nacos:共发现 {len(all_instances)} 个 {service_name} 服务实例")
822
827
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sycommon-python-lib
3
- Version: 0.1.56b3
3
+ Version: 0.1.56b4
4
4
  Summary: Add your description here
5
5
  Requires-Python: >=3.11
6
6
  Description-Content-Type: text/markdown
@@ -22,6 +22,7 @@ src/sycommon/llm/__init__.py
22
22
  src/sycommon/llm/embedding.py
23
23
  src/sycommon/llm/get_llm.py
24
24
  src/sycommon/llm/llm_logger.py
25
+ src/sycommon/llm/llm_tokens.py
25
26
  src/sycommon/logging/__init__.py
26
27
  src/sycommon/logging/async_sql_logger.py
27
28
  src/sycommon/logging/kafka_log.py
@@ -1,177 +0,0 @@
1
- from typing import Dict, Type, List, Union, Optional, Callable
2
-
3
- from sycommon.llm.llm_logger import LLMLogger
4
- from langchain_core.language_models import BaseChatModel
5
- from langchain_core.runnables import Runnable, RunnableLambda
6
- from langchain_core.output_parsers import PydanticOutputParser
7
- from langchain_core.messages import BaseMessage, SystemMessage, HumanMessage
8
- from langchain.chat_models import init_chat_model
9
- from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
10
- from pydantic import BaseModel, ValidationError
11
- from sycommon.config.LLMConfig import LLMConfig
12
-
13
-
14
- def get_llm(model: str = None, streaming: bool = False) -> BaseChatModel:
15
- if not model:
16
- model = "Qwen2.5-72B"
17
- # model = "SyMid"
18
- llmConfig = LLMConfig.from_config(model)
19
- llm = None
20
- if llmConfig:
21
- llm = init_chat_model(
22
- model_provider=llmConfig.provider,
23
- model=llmConfig.model,
24
- base_url=llmConfig.baseUrl,
25
- api_key="-",
26
- temperature=0.1,
27
- streaming=streaming,
28
- )
29
- else:
30
- raise Exception("Invalid model")
31
-
32
- # 为LLM动态添加with_structured_output方法,官方的with_structured_output方法有概率在qwen2.5中导致模型卡死不返回数据,2.5对functioncall支持不好
33
- def with_structured_output(
34
- self: BaseChatModel,
35
- output_model: Type[BaseModel],
36
- max_retries: int = 3,
37
- is_extract: bool = False,
38
- override_prompt: ChatPromptTemplate = None,
39
- # 自定义处理函数列表(每个函数接收str,返回str)
40
- custom_processors: Optional[List[Callable[[str], str]]] = None,
41
- # 自定义解析函数(接收str,返回BaseModel)
42
- custom_parser: Optional[Callable[[str], BaseModel]] = None
43
- ) -> Runnable[List[BaseMessage], BaseModel]:
44
- parser = PydanticOutputParser(pydantic_object=output_model)
45
-
46
- accuracy_instructions = """
47
- 字段值的抽取准确率(0~1之间),评分规则:
48
- 1.0(完全准确):直接从原文提取,无需任何加工,且格式与原文完全一致
49
- 0.9(轻微处理):数据来源明确,但需进行格式标准化或冗余信息剔除(不改变原始数值)
50
- 0.8(有限推断):数据需通过上下文关联或简单计算得出,仍有明确依据
51
- 0.8以下(不可靠):数据需大量推测、存在歧义或来源不明,处理方式:直接忽略该数据,设置为None
52
- """
53
-
54
- if is_extract:
55
- # 抽取模式下使用固定的抽取专用prompt
56
- prompt = ChatPromptTemplate.from_messages([
57
- MessagesPlaceholder(variable_name="messages"),
58
- HumanMessage(content=f"""
59
- 请提取信息并遵循以下规则:
60
- 1. 准确率要求:{accuracy_instructions.strip()}
61
- 2. 输出格式:{parser.get_format_instructions()}
62
- """)
63
- ])
64
- else:
65
- if override_prompt:
66
- prompt = override_prompt
67
- else:
68
- prompt = ChatPromptTemplate.from_messages([
69
- MessagesPlaceholder(variable_name="messages"),
70
- HumanMessage(content=f"""
71
- 输出格式:{parser.get_format_instructions()}
72
- """)
73
- ])
74
-
75
- # ========== 基础处理函数 ==========
76
- def extract_response_content(response: BaseMessage) -> str:
77
- """提取响应中的文本内容"""
78
- try:
79
- return response.content
80
- except Exception as e:
81
- raise ValueError(f"提取响应内容失败:{str(e)}") from e
82
-
83
- def strip_code_block_markers(content: str) -> str:
84
- """移除JSON代码块标记(```json/```)"""
85
- try:
86
- return content.strip("```json").strip("```").strip()
87
- except Exception as e:
88
- raise ValueError(
89
- f"移除代码块标记失败(内容:{str(content)[:100]}):{str(e)}") from e
90
-
91
- def normalize_in_json(content: str) -> str:
92
- """将None替换为null,确保JSON格式合法"""
93
- try:
94
- cleaned = content.replace("None", "null")
95
- cleaned = cleaned.replace("none", "null")
96
- cleaned = cleaned.replace("NONE", "null")
97
- cleaned = cleaned.replace("''", '""')
98
- return cleaned
99
- except Exception as e:
100
- raise ValueError(
101
- f"替换None为null失败(内容:{str(content)[:100]}):{str(e)}") from e
102
-
103
- def default_parse_to_pydantic(content: str) -> BaseModel:
104
- """默认解析函数:将处理后的文本解析为Pydantic模型"""
105
- try:
106
- return parser.parse(content)
107
- except (ValidationError, ValueError) as e:
108
- raise type(e)(f"解析失败(原始内容:{content[:200]}):{str(e)}") from e
109
-
110
- # ========== 构建处理链条 ==========
111
- # 基础链 prompt → LLM → 提取响应内容
112
- base_chain = (
113
- prompt
114
- | self
115
- | RunnableLambda(extract_response_content)
116
- )
117
-
118
- # 处理函数链 优先使用自定义,否则用默认
119
- if custom_processors:
120
- # 自定义处理函数 → 转为RunnableLambda列表
121
- process_runnables = [RunnableLambda(
122
- func) for func in custom_processors]
123
- else:
124
- # 默认处理函数:移除代码块标记 → 标准化JSON空值
125
- process_runnables = [
126
- RunnableLambda(strip_code_block_markers),
127
- RunnableLambda(normalize_in_json)
128
- ]
129
-
130
- # 拼接处理链
131
- process_chain = base_chain
132
- for runnable in process_runnables:
133
- process_chain = process_chain | runnable
134
-
135
- # 解析函数 优先使用自定义,否则用默认
136
- parse_func = custom_parser if custom_parser else default_parse_to_pydantic
137
- parse_chain = process_chain | RunnableLambda(parse_func)
138
-
139
- retry_chain = parse_chain.with_retry(
140
- retry_if_exception_type=(ValidationError, ValueError),
141
- stop_after_attempt=max_retries,
142
- wait_exponential_jitter=True,
143
- exponential_jitter_params={
144
- "initial": 0.1, # 初始等待时间(秒)
145
- "max": 3.0, # 最大等待时间(秒)
146
- "exp_base": 2.0, # 指数基数(默认2)
147
- "jitter": 1.0 # 随机抖动值(默认1)
148
- }
149
- )
150
-
151
- class StructuredRunnable(Runnable[Union[List[BaseMessage], BaseMessage, str, Dict[str, str]], BaseModel]):
152
- def _adapt_input(self, input: Union[List[BaseMessage], BaseMessage, str, Dict[str, str]]) -> List[BaseMessage]:
153
- """将多种输入格式统一转换为 List[BaseMessage]"""
154
- if isinstance(input, list) and all(isinstance(x, BaseMessage) for x in input):
155
- return input
156
- elif isinstance(input, BaseMessage):
157
- return [input]
158
- elif isinstance(input, str):
159
- return [HumanMessage(content=input)]
160
- elif isinstance(input, dict) and "input" in input:
161
- return [HumanMessage(content=str(input["input"]))]
162
- else:
163
- raise ValueError(
164
- "不支持的输入格式,请使用消息列表、单条消息、文本或 {'input': '文本'}")
165
-
166
- def invoke(self, input: Union[List[BaseMessage], BaseMessage, str, Dict[str, str]], config={"callbacks": [LLMLogger()]}):
167
- adapted_input = self._adapt_input(input)
168
- return retry_chain.invoke({"messages": adapted_input}, config=config)
169
-
170
- async def ainvoke(self, input: Union[List[BaseMessage], BaseMessage, str, Dict[str, str]], config={"callbacks": [LLMLogger()]}):
171
- adapted_input = self._adapt_input(input)
172
- return await retry_chain.ainvoke({"messages": adapted_input}, config=config)
173
-
174
- return StructuredRunnable()
175
-
176
- llm.__class__.with_structured_output = with_structured_output
177
- return llm