pycoze 0.1.44__py3-none-any.whl → 0.1.46__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/tool_for_bot.py +28 -102
- {pycoze-0.1.44.dist-info → pycoze-0.1.46.dist-info}/METADATA +1 -1
- {pycoze-0.1.44.dist-info → pycoze-0.1.46.dist-info}/RECORD +6 -6
- {pycoze-0.1.44.dist-info → pycoze-0.1.46.dist-info}/LICENSE +0 -0
- {pycoze-0.1.44.dist-info → pycoze-0.1.46.dist-info}/WHEEL +0 -0
- {pycoze-0.1.44.dist-info → pycoze-0.1.46.dist-info}/top_level.txt +0 -0
pycoze/access/tool_for_bot.py
CHANGED
@@ -1,106 +1,35 @@
|
|
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
|
-
# def change_directory_and_path(module_path):
|
9
|
-
# """Change the current working directory and sys.path."""
|
10
|
-
# sys.path.insert(0, module_path)
|
11
|
-
# os.chdir(module_path)
|
12
|
-
|
13
|
-
# def restore_directory_and_path(module_path, old_path):
|
14
|
-
# """Restore the original working directory and sys.path."""
|
15
|
-
# sys.path.remove(module_path)
|
16
|
-
# os.chdir(old_path)
|
17
|
-
|
18
|
-
# def wrapped_tool(tool, module_path):
|
19
|
-
# """Wrap the tool function to include additional logging and path management."""
|
20
|
-
# original_tool_function = tool.func
|
21
|
-
|
22
|
-
# def _wrapped_tool(*args, **kwargs):
|
23
|
-
# print(f"调用了{tool.name}")
|
24
|
-
# old_path = os.getcwd()
|
25
|
-
# try:
|
26
|
-
# change_directory_and_path(module_path)
|
27
|
-
# result = original_tool_function(*args, **kwargs)
|
28
|
-
# finally:
|
29
|
-
# restore_directory_and_path(module_path, old_path)
|
30
|
-
# print(f"{tool.name}调用完毕,结果为:", result)
|
31
|
-
# return result
|
32
|
-
|
33
|
-
# return _wrapped_tool
|
34
|
-
|
35
|
-
# def import_tools(tool_id):
|
36
|
-
# """Import tools from a specified tool_id."""
|
37
|
-
# tool_base_path = "../../tool"
|
38
|
-
# old_path = os.getcwd()
|
39
|
-
# module_path = os.path.join(tool_base_path, tool_id)
|
40
|
-
# module_path = os.path.normpath(os.path.abspath(module_path))
|
41
|
-
|
42
|
-
# if not os.path.exists(module_path):
|
43
|
-
# print(f"Tool {tool_id} not found")
|
44
|
-
# return []
|
45
|
-
|
46
|
-
# # Save the current sys.modules state
|
47
|
-
# original_modules = sys.modules.copy()
|
48
|
-
|
49
|
-
# try:
|
50
|
-
# change_directory_and_path(module_path)
|
51
|
-
# module = importlib.import_module("tool")
|
52
|
-
# export_tools = getattr(module, "export_tools")
|
53
|
-
# valid_tools = []
|
54
|
-
# for tool in export_tools:
|
55
|
-
# assert isinstance(tool, langchain_core.tools.StructuredTool) or isinstance(
|
56
|
-
# tool, types.FunctionType
|
57
|
-
# ), f"Tool is not a StructuredTool or function: {tool}"
|
58
|
-
# if isinstance(tool, types.FunctionType) and not isinstance(
|
59
|
-
# tool, langchain_core.tools.StructuredTool
|
60
|
-
# ):
|
61
|
-
# valid_tools.append(_tool(tool))
|
62
|
-
# export_tools = valid_tools
|
63
|
-
|
64
|
-
# except Exception as e:
|
65
|
-
# print(f"Error loading tool {tool_id}: {e}")
|
66
|
-
# restore_directory_and_path(module_path, old_path)
|
67
|
-
# return []
|
68
|
-
|
69
|
-
# # Unload modules and restore sys.modules state
|
70
|
-
# importlib.invalidate_caches()
|
71
|
-
# for key in list(sys.modules.keys()):
|
72
|
-
# if key not in original_modules:
|
73
|
-
# del sys.modules[key]
|
74
|
-
|
75
|
-
# restore_directory_and_path(module_path, old_path)
|
76
|
-
|
77
|
-
# for tool in export_tools:
|
78
|
-
# tool.func = wrapped_tool(tool, module_path)
|
79
|
-
|
80
|
-
# return export_tools
|
81
|
-
|
82
1
|
import sys
|
83
2
|
import os
|
84
|
-
import argparse
|
85
3
|
import importlib
|
86
4
|
from langchain.agents import tool as _tool
|
87
5
|
import types
|
88
6
|
import langchain_core
|
89
7
|
|
90
8
|
|
9
|
+
def change_directory_and_path(module_path):
|
10
|
+
"""Change the current working directory and sys.path."""
|
11
|
+
sys.path.insert(0, module_path)
|
12
|
+
os.chdir(module_path)
|
13
|
+
|
14
|
+
|
15
|
+
def restore_directory_and_path(module_path, old_path):
|
16
|
+
"""Restore the original working directory and sys.path."""
|
17
|
+
sys.path.remove(module_path)
|
18
|
+
os.chdir(old_path)
|
19
|
+
|
20
|
+
|
91
21
|
def wrapped_tool(tool, module_path):
|
92
|
-
|
22
|
+
"""Wrap the tool function to include additional logging and path management."""
|
23
|
+
original_tool_function = tool.func
|
93
24
|
|
94
25
|
def _wrapped_tool(*args, **kwargs):
|
95
26
|
print(f"调用了{tool.name}")
|
96
27
|
old_path = os.getcwd()
|
97
28
|
try:
|
98
|
-
|
99
|
-
|
100
|
-
result = old_tool_fun(*args, **kwargs)
|
29
|
+
change_directory_and_path(module_path)
|
30
|
+
result = original_tool_function(*args, **kwargs)
|
101
31
|
finally:
|
102
|
-
|
103
|
-
os.chdir(old_path)
|
32
|
+
restore_directory_and_path(module_path, old_path)
|
104
33
|
print(f"{tool.name}调用完毕,结果为:", result)
|
105
34
|
return result
|
106
35
|
|
@@ -108,25 +37,24 @@ def wrapped_tool(tool, module_path):
|
|
108
37
|
|
109
38
|
|
110
39
|
def import_tools(tool_id):
|
111
|
-
|
112
|
-
|
40
|
+
"""Import tools from a specified tool_id."""
|
41
|
+
tool_base_path = "../../tool"
|
113
42
|
old_path = os.getcwd()
|
114
|
-
module_path = os.path.join(
|
43
|
+
module_path = os.path.join(tool_base_path, tool_id)
|
115
44
|
module_path = os.path.normpath(os.path.abspath(module_path))
|
116
45
|
|
117
46
|
if not os.path.exists(module_path):
|
118
47
|
print(f"Tool {tool_id} not found")
|
119
48
|
return []
|
120
49
|
|
121
|
-
#
|
50
|
+
# Save the current sys.modules state
|
122
51
|
original_modules = sys.modules.copy()
|
123
52
|
|
124
53
|
try:
|
125
|
-
|
126
|
-
os.chdir(module_path)
|
54
|
+
change_directory_and_path(module_path)
|
127
55
|
module = importlib.import_module("tool")
|
128
56
|
export_tools = getattr(module, "export_tools")
|
129
|
-
|
57
|
+
valid_tools = []
|
130
58
|
for tool in export_tools:
|
131
59
|
assert isinstance(tool, langchain_core.tools.StructuredTool) or isinstance(
|
132
60
|
tool, types.FunctionType
|
@@ -134,23 +62,21 @@ def import_tools(tool_id):
|
|
134
62
|
if isinstance(tool, types.FunctionType) and not isinstance(
|
135
63
|
tool, langchain_core.tools.StructuredTool
|
136
64
|
):
|
137
|
-
|
138
|
-
export_tools =
|
65
|
+
valid_tools.append(_tool(tool))
|
66
|
+
export_tools = valid_tools
|
139
67
|
|
140
68
|
except Exception as e:
|
141
69
|
print(f"Error loading tool {tool_id}: {e}")
|
142
|
-
|
143
|
-
os.chdir(old_path)
|
70
|
+
restore_directory_and_path(module_path, old_path)
|
144
71
|
return []
|
145
72
|
|
146
|
-
#
|
73
|
+
# Unload modules and restore sys.modules state
|
147
74
|
importlib.invalidate_caches()
|
148
75
|
for key in list(sys.modules.keys()):
|
149
76
|
if key not in original_modules:
|
150
77
|
del sys.modules[key]
|
151
78
|
|
152
|
-
|
153
|
-
os.chdir(old_path)
|
79
|
+
restore_directory_and_path(module_path, old_path)
|
154
80
|
|
155
81
|
for tool in export_tools:
|
156
82
|
tool.func = wrapped_tool(tool, module_path)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
pycoze/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
2
|
pycoze/module.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
3
|
pycoze/access/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
|
-
pycoze/access/tool_for_bot.py,sha256=
|
4
|
+
pycoze/access/tool_for_bot.py,sha256=Nv6XlxcqaLaMVQNDkpcthlqaSxyfHoOfWiTScQXO22Y,2656
|
5
5
|
pycoze/bot/__init__.py,sha256=pciDtfcIXda7iFt9uI5Fpm0JKpGBhdXHmJv4966WTVU,21
|
6
6
|
pycoze/bot/bot.py,sha256=ijihiXv6etFFQ-NGxtmi0_J7lkgwZir3jbVGUKdFPZ8,2510
|
7
7
|
pycoze/bot/agent/__init__.py,sha256=IaYqQCJ3uBor92JdOxI_EY4HtYOHgej8lijr3UrN1Vc,161
|
@@ -22,8 +22,8 @@ pycoze/ui/ui_def.py,sha256=CNFYH8NC-WYmbceIPpxsRr9H6O006pMKukx7U-BOE1Q,3744
|
|
22
22
|
pycoze/utils/__init__.py,sha256=KExBkotf23dr2NfTEouWke5nJB1q2IuDXgHrmuyd95k,73
|
23
23
|
pycoze/utils/arg.py,sha256=rRujm1zKc0XlnNlpIJ6JAAaFiTzDGmL_RliIpSc5OD8,724
|
24
24
|
pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
|
25
|
-
pycoze-0.1.
|
26
|
-
pycoze-0.1.
|
27
|
-
pycoze-0.1.
|
28
|
-
pycoze-0.1.
|
29
|
-
pycoze-0.1.
|
25
|
+
pycoze-0.1.46.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
|
26
|
+
pycoze-0.1.46.dist-info/METADATA,sha256=7A_aUh8T3SNIps0HECz7GkQCbq6VSBJWfxQOcWByblM,719
|
27
|
+
pycoze-0.1.46.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
|
28
|
+
pycoze-0.1.46.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
|
29
|
+
pycoze-0.1.46.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|