sycommon-python-lib 0.1.56b11__py3-none-any.whl → 0.1.56b13__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.
- sycommon/llm/embedding.py +78 -23
- sycommon/rabbitmq/rabbitmq_client.py +224 -290
- sycommon/rabbitmq/rabbitmq_pool.py +182 -154
- sycommon/rabbitmq/rabbitmq_service_core.py +2 -2
- {sycommon_python_lib-0.1.56b11.dist-info → sycommon_python_lib-0.1.56b13.dist-info}/METADATA +1 -1
- {sycommon_python_lib-0.1.56b11.dist-info → sycommon_python_lib-0.1.56b13.dist-info}/RECORD +9 -9
- {sycommon_python_lib-0.1.56b11.dist-info → sycommon_python_lib-0.1.56b13.dist-info}/WHEEL +0 -0
- {sycommon_python_lib-0.1.56b11.dist-info → sycommon_python_lib-0.1.56b13.dist-info}/entry_points.txt +0 -0
- {sycommon_python_lib-0.1.56b11.dist-info → sycommon_python_lib-0.1.56b13.dist-info}/top_level.txt +0 -0
sycommon/llm/embedding.py
CHANGED
|
@@ -25,15 +25,21 @@ class Embedding(metaclass=SingletonMeta):
|
|
|
25
25
|
|
|
26
26
|
# 并发信号量
|
|
27
27
|
self.semaphore = asyncio.Semaphore(self.max_concurrency)
|
|
28
|
+
# 全局默认超时:永不超时(None)
|
|
29
|
+
self.default_timeout = aiohttp.ClientTimeout(total=None)
|
|
28
30
|
|
|
29
31
|
async def _get_embeddings_http_async(
|
|
30
32
|
self,
|
|
31
33
|
input: Union[str, List[str]],
|
|
32
34
|
encoding_format: str = None,
|
|
33
35
|
model: str = None,
|
|
36
|
+
timeout: aiohttp.ClientTimeout = None,
|
|
34
37
|
**kwargs
|
|
35
38
|
):
|
|
36
39
|
async with self.semaphore:
|
|
40
|
+
# 优先使用传入的超时,无则用全局默认
|
|
41
|
+
request_timeout = timeout or self.default_timeout
|
|
42
|
+
|
|
37
43
|
# 优先使用传入的模型名,无则用默认值
|
|
38
44
|
target_model = model or self.default_embedding_model
|
|
39
45
|
target_base_url = EmbeddingConfig.from_config(target_model).baseUrl
|
|
@@ -46,14 +52,23 @@ class Embedding(metaclass=SingletonMeta):
|
|
|
46
52
|
}
|
|
47
53
|
request_body.update(kwargs)
|
|
48
54
|
|
|
49
|
-
|
|
50
|
-
async with
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
try:
|
|
56
|
+
async with aiohttp.ClientSession(timeout=request_timeout) as session:
|
|
57
|
+
async with session.post(url, json=request_body) as response:
|
|
58
|
+
if response.status != 200:
|
|
59
|
+
error_detail = await response.text()
|
|
60
|
+
SYLogger.error(
|
|
61
|
+
f"Embedding request failed (model: {target_model}): {error_detail}")
|
|
62
|
+
return None
|
|
63
|
+
return await response.json()
|
|
64
|
+
except asyncio.TimeoutError:
|
|
65
|
+
SYLogger.error(
|
|
66
|
+
f"Embedding request timeout (model: {target_model})")
|
|
67
|
+
return None
|
|
68
|
+
except Exception as e:
|
|
69
|
+
SYLogger.error(
|
|
70
|
+
f"Embedding request unexpected error (model: {target_model}): {str(e)}")
|
|
71
|
+
return None
|
|
57
72
|
|
|
58
73
|
async def _get_reranker_http_async(
|
|
59
74
|
self,
|
|
@@ -64,9 +79,13 @@ class Embedding(metaclass=SingletonMeta):
|
|
|
64
79
|
max_chunks_per_doc: Optional[int] = None,
|
|
65
80
|
return_documents: Optional[bool] = True,
|
|
66
81
|
return_len: Optional[bool] = True,
|
|
82
|
+
timeout: aiohttp.ClientTimeout = None,
|
|
67
83
|
**kwargs
|
|
68
84
|
):
|
|
69
85
|
async with self.semaphore:
|
|
86
|
+
# 优先使用传入的超时,无则用全局默认
|
|
87
|
+
request_timeout = timeout or self.default_timeout
|
|
88
|
+
|
|
70
89
|
# 优先使用传入的模型名,无则用默认值
|
|
71
90
|
target_model = model or self.default_reranker_model
|
|
72
91
|
target_base_url = RerankerConfig.from_config(target_model).baseUrl
|
|
@@ -84,19 +103,29 @@ class Embedding(metaclass=SingletonMeta):
|
|
|
84
103
|
}
|
|
85
104
|
request_body.update(kwargs)
|
|
86
105
|
|
|
87
|
-
|
|
88
|
-
async with
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
106
|
+
try:
|
|
107
|
+
async with aiohttp.ClientSession(timeout=request_timeout) as session:
|
|
108
|
+
async with session.post(url, json=request_body) as response:
|
|
109
|
+
if response.status != 200:
|
|
110
|
+
error_detail = await response.text()
|
|
111
|
+
SYLogger.error(
|
|
112
|
+
f"Rerank request failed (model: {target_model}): {error_detail}")
|
|
113
|
+
return None
|
|
114
|
+
return await response.json()
|
|
115
|
+
except asyncio.TimeoutError:
|
|
116
|
+
SYLogger.error(
|
|
117
|
+
f"Rerank request timeout (model: {target_model})")
|
|
118
|
+
return None
|
|
119
|
+
except Exception as e:
|
|
120
|
+
SYLogger.error(
|
|
121
|
+
f"Rerank request unexpected error (model: {target_model}): {str(e)}")
|
|
122
|
+
return None
|
|
95
123
|
|
|
96
124
|
async def get_embeddings(
|
|
97
125
|
self,
|
|
98
126
|
corpus: List[str],
|
|
99
|
-
model: str = None
|
|
127
|
+
model: str = None,
|
|
128
|
+
timeout: Optional[Union[int, float]] = None
|
|
100
129
|
):
|
|
101
130
|
"""
|
|
102
131
|
获取语料库的嵌入向量,结果顺序与输入语料库顺序一致
|
|
@@ -104,12 +133,24 @@ class Embedding(metaclass=SingletonMeta):
|
|
|
104
133
|
Args:
|
|
105
134
|
corpus: 待生成嵌入向量的文本列表
|
|
106
135
|
model: 可选,指定使用的embedding模型名称,默认使用bge-large-zh-v1.5
|
|
136
|
+
timeout: 可选,超时时间(秒):
|
|
137
|
+
- 传int/float:表示总超时时间(秒)
|
|
138
|
+
- 不传/None:使用默认永不超时配置
|
|
107
139
|
"""
|
|
140
|
+
request_timeout = None
|
|
141
|
+
if timeout is not None:
|
|
142
|
+
if isinstance(timeout, (int, float)):
|
|
143
|
+
request_timeout = aiohttp.ClientTimeout(total=timeout)
|
|
144
|
+
else:
|
|
145
|
+
SYLogger.warning(
|
|
146
|
+
f"Invalid timeout type: {type(timeout)}, must be int/float, use default timeout")
|
|
147
|
+
|
|
108
148
|
SYLogger.info(
|
|
109
|
-
f"Requesting embeddings for corpus: {corpus} (model: {model or self.default_embedding_model}, max_concurrency: {self.max_concurrency})")
|
|
110
|
-
|
|
149
|
+
f"Requesting embeddings for corpus: {corpus} (model: {model or self.default_embedding_model}, max_concurrency: {self.max_concurrency}, timeout: {timeout or 'None'})")
|
|
150
|
+
|
|
151
|
+
# 给每个异步任务传入模型名称和超时配置
|
|
111
152
|
tasks = [self._get_embeddings_http_async(
|
|
112
|
-
text, model=model) for text in corpus]
|
|
153
|
+
text, model=model, timeout=request_timeout) for text in corpus]
|
|
113
154
|
results = await asyncio.gather(*tasks)
|
|
114
155
|
|
|
115
156
|
vectors = []
|
|
@@ -131,7 +172,8 @@ class Embedding(metaclass=SingletonMeta):
|
|
|
131
172
|
self,
|
|
132
173
|
top_results: List[str],
|
|
133
174
|
query: str,
|
|
134
|
-
model: str = None
|
|
175
|
+
model: str = None,
|
|
176
|
+
timeout: Optional[Union[int, float]] = None
|
|
135
177
|
):
|
|
136
178
|
"""
|
|
137
179
|
对搜索结果进行重排序
|
|
@@ -140,10 +182,23 @@ class Embedding(metaclass=SingletonMeta):
|
|
|
140
182
|
top_results: 待重排序的文本列表
|
|
141
183
|
query: 排序参考的查询语句
|
|
142
184
|
model: 可选,指定使用的reranker模型名称,默认使用bge-reranker-large
|
|
185
|
+
timeout: 可选,超时时间(秒):
|
|
186
|
+
- 传int/float:表示总超时时间(秒)
|
|
187
|
+
- 不传/None:使用默认永不超时配置
|
|
143
188
|
"""
|
|
189
|
+
request_timeout = None
|
|
190
|
+
if timeout is not None:
|
|
191
|
+
if isinstance(timeout, (int, float)):
|
|
192
|
+
request_timeout = aiohttp.ClientTimeout(total=timeout)
|
|
193
|
+
else:
|
|
194
|
+
SYLogger.warning(
|
|
195
|
+
f"Invalid timeout type: {type(timeout)}, must be int/float, use default timeout")
|
|
196
|
+
|
|
144
197
|
SYLogger.info(
|
|
145
|
-
f"Requesting reranker for top_results: {top_results} (model: {model or self.default_reranker_model}, max_concurrency: {self.max_concurrency})")
|
|
146
|
-
|
|
198
|
+
f"Requesting reranker for top_results: {top_results} (model: {model or self.default_reranker_model}, max_concurrency: {self.max_concurrency}, timeout: {timeout or 'None'})")
|
|
199
|
+
|
|
200
|
+
data = await self._get_reranker_http_async(
|
|
201
|
+
top_results, query, model=model, timeout=request_timeout)
|
|
147
202
|
SYLogger.info(
|
|
148
203
|
f"Reranker for top_results: {top_results} completed (model: {model or self.default_reranker_model})")
|
|
149
204
|
return data
|