pycoze 0.1.93__py3-none-any.whl → 0.1.95__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/lib.py ADDED
@@ -0,0 +1,46 @@
1
+ import os
2
+ import sys
3
+
4
+
5
+ class ChangeDirectoryAndPath:
6
+ """Context manager to change the current working directory and sys.path."""
7
+
8
+ def __init__(self, module_path):
9
+ self.module_path = module_path
10
+ self.old_path = None
11
+
12
+ def __enter__(self):
13
+ self.old_path = os.getcwd()
14
+ sys.path.insert(0, self.module_path)
15
+ os.chdir(self.module_path)
16
+ return self
17
+
18
+ def __exit__(self, exc_type, exc_value, traceback):
19
+ sys.path.remove(self.module_path)
20
+ os.chdir(self.old_path)
21
+
22
+
23
+ class ModuleManager:
24
+ """Context manager for handling module imports and sys.modules state."""
25
+
26
+ def __init__(self, module_path):
27
+ self.module_path = module_path
28
+ self.original_modules = sys.modules.copy()
29
+
30
+ def __enter__(self):
31
+ """Enter the runtime context related to this object."""
32
+ self.change_dir = ChangeDirectoryAndPath(self.module_path)
33
+ self.change_dir.__enter__()
34
+ return self
35
+
36
+ def __exit__(self, exc_type, exc_value, traceback):
37
+ """Exit the runtime context related to this object."""
38
+ self.change_dir.__exit__(exc_type, exc_value, traceback)
39
+ self.cleanup_modules()
40
+
41
+ def cleanup_modules(self):
42
+ """Restore the original sys.modules state."""
43
+ importlib.invalidate_caches()
44
+ for key in list(sys.modules.keys()):
45
+ if key not in self.original_modules:
46
+ del sys.modules[key]
@@ -1,22 +1,12 @@
1
1
  import sys
2
2
  import os
3
3
  import importlib
4
- from langchain.agents import tool as _tool
4
+ from langchain.agents import tool as to_agent_tool
5
5
  import types
6
6
  import langchain_core
7
+ from .lib import ChangeDirectoryAndPath, ModuleManager
7
8
 
8
9
 
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
10
 
21
11
  def wrapped_tool(tool, module_path):
22
12
  """Wrap the tool function to include additional logging and path management."""
@@ -24,12 +14,8 @@ def wrapped_tool(tool, module_path):
24
14
 
25
15
  def _wrapped_tool(*args, **kwargs):
26
16
  print(f"调用了{tool.name}")
27
- old_path = os.getcwd()
28
- try:
29
- change_directory_and_path(module_path)
17
+ with ChangeDirectoryAndPath(module_path):
30
18
  result = original_tool_function(*args, **kwargs)
31
- finally:
32
- restore_directory_and_path(module_path, old_path)
33
19
  print(f"{tool.name}调用完毕,结果为:", result)
34
20
  return result
35
21
 
@@ -39,7 +25,6 @@ def wrapped_tool(tool, module_path):
39
25
  def import_tools(tool_id):
40
26
  """Import tools from a specified tool_id."""
41
27
  tool_base_path = "../../tool"
42
- old_path = os.getcwd()
43
28
  module_path = os.path.join(tool_base_path, tool_id)
44
29
  module_path = os.path.normpath(os.path.abspath(module_path))
45
30
 
@@ -47,37 +32,24 @@ def import_tools(tool_id):
47
32
  print(f"Tool {tool_id} not found")
48
33
  return []
49
34
 
50
- # Save the current sys.modules state
51
- original_modules = sys.modules.copy()
52
-
53
35
  try:
54
- change_directory_and_path(module_path)
55
- module = importlib.import_module("tool")
56
- export_tools = getattr(module, "export_tools")
57
- valid_tools = []
58
- for tool in export_tools:
59
- assert isinstance(tool, langchain_core.tools.StructuredTool) or isinstance(
60
- tool, types.FunctionType
61
- ), f"Tool is not a StructuredTool or function: {tool}"
62
- if isinstance(tool, types.FunctionType) and not isinstance(
63
- tool, langchain_core.tools.StructuredTool
64
- ):
65
- valid_tools.append(_tool(tool))
66
- export_tools = valid_tools
36
+ with ModuleManager(module_path) as manager:
37
+ module = importlib.import_module("tool")
38
+ export_tools = getattr(module, "export_tools")
39
+ valid_tools = []
40
+ for tool in export_tools:
41
+ assert isinstance(tool, langchain_core.tools.StructuredTool) or isinstance(
42
+ tool, types.FunctionType
43
+ ), f"Tool is not a StructuredTool or function: {tool}"
44
+ if not isiisinstance(tool, langchain_core.tools.StructuredTool):
45
+ tool = to_agent_tool(tool)
46
+ valid_tools.append(to_agent_tool(tool))
47
+ export_tools = valid_tools
67
48
 
68
49
  except Exception as e:
69
50
  print(f"Error loading tool {tool_id}: {e}")
70
- restore_directory_and_path(module_path, old_path)
71
51
  return []
72
52
 
73
- # Unload modules and restore sys.modules state
74
- importlib.invalidate_caches()
75
- for key in list(sys.modules.keys()):
76
- if key not in original_modules:
77
- del sys.modules[key]
78
-
79
- restore_directory_and_path(module_path, old_path)
80
-
81
53
  for tool in export_tools:
82
54
  tool.func = wrapped_tool(tool, module_path)
83
55
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.93
3
+ Version: 0.1.95
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -1,7 +1,8 @@
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=Nv6XlxcqaLaMVQNDkpcthlqaSxyfHoOfWiTScQXO22Y,2656
4
+ pycoze/access/lib.py,sha256=TUK-kh09LTjzin_A7Fj-JayxWiB5p_pw2nP8nJU1u5o,1496
5
+ pycoze/access/tool_for_bot.py,sha256=ZVwI3_6gw5eJQ9qyGSwTTcGx9zAoSDPtdWGNzRMdhTU,1855
5
6
  pycoze/ai/__init__.py,sha256=odM2lgYSnApxw4AzLV_5AyaxL5_MLfydOSB1VmLGFyA,75
6
7
  pycoze/ai/vram_reserve.py,sha256=QbqaA8qv87cnEpOVDMygi0BNMxuhLYwj1UKfR_D5BD4,4340
7
8
  pycoze/bot/__init__.py,sha256=6HHMxDQVOyZM9dtSjQm9tjGnhj4h7CixD0JOvEwTi48,41
@@ -20,8 +21,8 @@ pycoze/ui/ui_def.py,sha256=UhhU_yB3GV9ISbvTWT48hsHPHI250BhMILh6bu5Uioo,4206
20
21
  pycoze/utils/__init__.py,sha256=TNJhFfY7JYdLlzuP9GvgxfNXUtbgH_NUUJSqHXCxJn4,78
21
22
  pycoze/utils/arg.py,sha256=kA3KBQzXc2WlH5XbF8kfikfpqljiKaW7oY_GE4Qyffc,753
22
23
  pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
23
- pycoze-0.1.93.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
24
- pycoze-0.1.93.dist-info/METADATA,sha256=logRR2lLxN2coU_EflW59R7Ra3Nu_HkILqKr7OCwVvU,725
25
- pycoze-0.1.93.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
26
- pycoze-0.1.93.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
27
- pycoze-0.1.93.dist-info/RECORD,,
24
+ pycoze-0.1.95.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
25
+ pycoze-0.1.95.dist-info/METADATA,sha256=Iw8qGRdy903gbAX351_zWRMAxj30592JYtZhp1sBqRw,725
26
+ pycoze-0.1.95.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
27
+ pycoze-0.1.95.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
28
+ pycoze-0.1.95.dist-info/RECORD,,