pycoze 0.1.40__py3-none-any.whl → 0.1.41__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.
- pycoze/access/__init__.py +0 -0
- pycoze/access/tool_for_bot.py +75 -0
- {pycoze-0.1.40.dist-info → pycoze-0.1.41.dist-info}/METADATA +1 -1
- {pycoze-0.1.40.dist-info → pycoze-0.1.41.dist-info}/RECORD +7 -5
- {pycoze-0.1.40.dist-info → pycoze-0.1.41.dist-info}/LICENSE +0 -0
- {pycoze-0.1.40.dist-info → pycoze-0.1.41.dist-info}/WHEEL +0 -0
- {pycoze-0.1.40.dist-info → pycoze-0.1.41.dist-info}/top_level.txt +0 -0
File without changes
|
@@ -0,0 +1,75 @@
|
|
1
|
+
import sys
|
2
|
+
import os
|
3
|
+
import importlib
|
4
|
+
from langchain.agents import tool as _tool
|
5
|
+
import types
|
6
|
+
import langchain_core
|
7
|
+
|
8
|
+
|
9
|
+
def wrapped_tool(tool, module_path):
|
10
|
+
old_tool_fun = tool.func
|
11
|
+
|
12
|
+
def _wrapped_tool(*args, **kwargs):
|
13
|
+
print(f"调用了{tool.name}")
|
14
|
+
old_path = os.getcwd()
|
15
|
+
try:
|
16
|
+
sys.path.insert(0, module_path) # 插入到第一个位置
|
17
|
+
os.chdir(module_path)
|
18
|
+
result = old_tool_fun(*args, **kwargs)
|
19
|
+
finally:
|
20
|
+
sys.path.remove(module_path)
|
21
|
+
os.chdir(old_path)
|
22
|
+
print(f"{tool.name}调用完毕,结果为:", result)
|
23
|
+
return result
|
24
|
+
|
25
|
+
return _wrapped_tool
|
26
|
+
|
27
|
+
|
28
|
+
def import_tools(tool_id):
|
29
|
+
tool_path = "../../tool"
|
30
|
+
old_path = os.getcwd()
|
31
|
+
module_path = os.path.join(tool_path, tool_id)
|
32
|
+
module_path = os.path.normpath(os.path.abspath(module_path))
|
33
|
+
|
34
|
+
if not os.path.exists(module_path):
|
35
|
+
print(f"Tool {tool_id} not found")
|
36
|
+
return []
|
37
|
+
|
38
|
+
# 保存当前的 sys.modules 状态
|
39
|
+
original_modules = sys.modules.copy()
|
40
|
+
|
41
|
+
try:
|
42
|
+
sys.path.insert(0, module_path) # 插入到第一个位置
|
43
|
+
os.chdir(module_path)
|
44
|
+
module = importlib.import_module("tool")
|
45
|
+
export_tools = getattr(module, "export_tools")
|
46
|
+
temp_list = []
|
47
|
+
for tool in export_tools:
|
48
|
+
assert isinstance(tool, langchain_core.tools.StructuredTool) or isinstance(
|
49
|
+
tool, types.FunctionType
|
50
|
+
), f"Tool is not a StructuredTool or function: {tool}"
|
51
|
+
if isinstance(tool, types.FunctionType) and not isinstance(
|
52
|
+
tool, langchain_core.tools.StructuredTool
|
53
|
+
):
|
54
|
+
temp_list.append(_tool(tool))
|
55
|
+
export_tools = temp_list
|
56
|
+
|
57
|
+
except Exception as e:
|
58
|
+
print(f"Error loading tool {tool_id}: {e}")
|
59
|
+
sys.path.remove(module_path)
|
60
|
+
os.chdir(old_path)
|
61
|
+
return []
|
62
|
+
|
63
|
+
# 卸载模块并恢复 sys.modules 状态
|
64
|
+
importlib.invalidate_caches()
|
65
|
+
for key in list(sys.modules.keys()):
|
66
|
+
if key not in original_modules:
|
67
|
+
del sys.modules[key]
|
68
|
+
|
69
|
+
sys.path.remove(module_path)
|
70
|
+
os.chdir(old_path)
|
71
|
+
|
72
|
+
for tool in export_tools:
|
73
|
+
tool.func = wrapped_tool(tool, module_path)
|
74
|
+
|
75
|
+
return export_tools
|
@@ -1,5 +1,7 @@
|
|
1
1
|
pycoze/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
2
|
pycoze/module.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
+
pycoze/access/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
|
+
pycoze/access/tool_for_bot.py,sha256=qpRMUznyzjl5BcO4T0Cv-4aRFAT_R5CXmlcZb9ZJ9kY,2262
|
3
5
|
pycoze/bot/__init__.py,sha256=pciDtfcIXda7iFt9uI5Fpm0JKpGBhdXHmJv4966WTVU,21
|
4
6
|
pycoze/bot/bot.py,sha256=ijihiXv6etFFQ-NGxtmi0_J7lkgwZir3jbVGUKdFPZ8,2510
|
5
7
|
pycoze/bot/agent/__init__.py,sha256=IaYqQCJ3uBor92JdOxI_EY4HtYOHgej8lijr3UrN1Vc,161
|
@@ -20,8 +22,8 @@ pycoze/ui/ui_def.py,sha256=CNFYH8NC-WYmbceIPpxsRr9H6O006pMKukx7U-BOE1Q,3744
|
|
20
22
|
pycoze/utils/__init__.py,sha256=KExBkotf23dr2NfTEouWke5nJB1q2IuDXgHrmuyd95k,73
|
21
23
|
pycoze/utils/arg.py,sha256=rRujm1zKc0XlnNlpIJ6JAAaFiTzDGmL_RliIpSc5OD8,724
|
22
24
|
pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
|
23
|
-
pycoze-0.1.
|
24
|
-
pycoze-0.1.
|
25
|
-
pycoze-0.1.
|
26
|
-
pycoze-0.1.
|
27
|
-
pycoze-0.1.
|
25
|
+
pycoze-0.1.41.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
|
26
|
+
pycoze-0.1.41.dist-info/METADATA,sha256=yYdj2mQkG6HhcIj3dtnO5CjOf333vW4nWIfT57RJ6QE,719
|
27
|
+
pycoze-0.1.41.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
|
28
|
+
pycoze-0.1.41.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
|
29
|
+
pycoze-0.1.41.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|