flexllm 0.3.3__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.
- flexllm/__init__.py +224 -0
- flexllm/__main__.py +1096 -0
- flexllm/async_api/__init__.py +9 -0
- flexllm/async_api/concurrent_call.py +100 -0
- flexllm/async_api/concurrent_executor.py +1036 -0
- flexllm/async_api/core.py +373 -0
- flexllm/async_api/interface.py +12 -0
- flexllm/async_api/progress.py +277 -0
- flexllm/base_client.py +988 -0
- flexllm/batch_tools/__init__.py +16 -0
- flexllm/batch_tools/folder_processor.py +317 -0
- flexllm/batch_tools/table_processor.py +363 -0
- flexllm/cache/__init__.py +10 -0
- flexllm/cache/response_cache.py +293 -0
- flexllm/chain_of_thought_client.py +1120 -0
- flexllm/claudeclient.py +402 -0
- flexllm/client_pool.py +698 -0
- flexllm/geminiclient.py +563 -0
- flexllm/llm_client.py +523 -0
- flexllm/llm_parser.py +60 -0
- flexllm/mllm_client.py +559 -0
- flexllm/msg_processors/__init__.py +174 -0
- flexllm/msg_processors/image_processor.py +729 -0
- flexllm/msg_processors/image_processor_helper.py +485 -0
- flexllm/msg_processors/messages_processor.py +341 -0
- flexllm/msg_processors/unified_processor.py +1404 -0
- flexllm/openaiclient.py +256 -0
- flexllm/pricing/__init__.py +104 -0
- flexllm/pricing/data.json +1201 -0
- flexllm/pricing/updater.py +223 -0
- flexllm/provider_router.py +213 -0
- flexllm/token_counter.py +270 -0
- flexllm/utils/__init__.py +1 -0
- flexllm/utils/core.py +41 -0
- flexllm-0.3.3.dist-info/METADATA +573 -0
- flexllm-0.3.3.dist-info/RECORD +39 -0
- flexllm-0.3.3.dist-info/WHEEL +4 -0
- flexllm-0.3.3.dist-info/entry_points.txt +3 -0
- flexllm-0.3.3.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import time
|
|
3
|
+
from typing import Callable, Any, List, Union, Sequence
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from .progress import ProgressTracker, ProgressBarConfig
|
|
6
|
+
from .interface import RequestResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class FunctionArgs:
|
|
11
|
+
args: tuple = ()
|
|
12
|
+
kwargs: dict = None
|
|
13
|
+
|
|
14
|
+
def __post_init__(self):
|
|
15
|
+
if self.kwargs is None:
|
|
16
|
+
self.kwargs = {}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def concurrent_executor(
|
|
20
|
+
async_func: Callable,
|
|
21
|
+
func_args: Sequence[Union[tuple, dict, FunctionArgs]],
|
|
22
|
+
concurrency_limit: int,
|
|
23
|
+
show_progress: bool = True
|
|
24
|
+
) -> List[Any]:
|
|
25
|
+
"""
|
|
26
|
+
并发执行异步函数的控制器
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
async_func: 要执行的异步函数
|
|
30
|
+
func_args: 函数参数列表,支持多种参数形式:
|
|
31
|
+
- tuple: 按位置传参
|
|
32
|
+
- dict: 按关键字传参
|
|
33
|
+
- FunctionArgs: 同时包含位置参数和关键字参数
|
|
34
|
+
concurrency_limit: 并发上限
|
|
35
|
+
show_progress: 是否显示进度条
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
list: 所有任务的执行结果列表
|
|
39
|
+
"""
|
|
40
|
+
semaphore = asyncio.Semaphore(concurrency_limit)
|
|
41
|
+
|
|
42
|
+
def normalize_args(arg) -> FunctionArgs:
|
|
43
|
+
if isinstance(arg, FunctionArgs):
|
|
44
|
+
return arg
|
|
45
|
+
elif isinstance(arg, tuple):
|
|
46
|
+
return FunctionArgs(args=arg)
|
|
47
|
+
elif isinstance(arg, dict):
|
|
48
|
+
return FunctionArgs(kwargs=arg)
|
|
49
|
+
else:
|
|
50
|
+
return FunctionArgs(args=(arg,))
|
|
51
|
+
|
|
52
|
+
async def wrapped_func(func_arg: FunctionArgs, task_id: int):
|
|
53
|
+
async with semaphore:
|
|
54
|
+
try:
|
|
55
|
+
start_time = time.time()
|
|
56
|
+
result = await async_func(*func_arg.args, **func_arg.kwargs)
|
|
57
|
+
status = 'success'
|
|
58
|
+
except Exception as e:
|
|
59
|
+
result = e
|
|
60
|
+
status = 'error'
|
|
61
|
+
|
|
62
|
+
if progress:
|
|
63
|
+
progress.update(RequestResult(
|
|
64
|
+
request_id=task_id,
|
|
65
|
+
data=result,
|
|
66
|
+
status=status,
|
|
67
|
+
# meta=None,
|
|
68
|
+
latency=time.time() - start_time
|
|
69
|
+
))
|
|
70
|
+
return task_id, result
|
|
71
|
+
|
|
72
|
+
# 标准化所有参数
|
|
73
|
+
normalized_args = [normalize_args(arg) for arg in func_args]
|
|
74
|
+
total_tasks = len(normalized_args)
|
|
75
|
+
|
|
76
|
+
progress = None
|
|
77
|
+
if show_progress:
|
|
78
|
+
progress = ProgressTracker(
|
|
79
|
+
total_tasks,
|
|
80
|
+
concurrency=concurrency_limit,
|
|
81
|
+
config=ProgressBarConfig()
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
# 创建任务列表
|
|
85
|
+
tasks = [
|
|
86
|
+
asyncio.create_task(wrapped_func(arg, i))
|
|
87
|
+
for i, arg in enumerate(normalized_args)
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
# 等待所有任务完成
|
|
91
|
+
completed_results = await asyncio.gather(*tasks)
|
|
92
|
+
|
|
93
|
+
# 按任务ID排序结果
|
|
94
|
+
sorted_results = sorted(completed_results, key=lambda x: x[0])
|
|
95
|
+
results = [result for _, result in sorted_results]
|
|
96
|
+
|
|
97
|
+
if progress:
|
|
98
|
+
progress.summary()
|
|
99
|
+
|
|
100
|
+
return results
|