pycoze 0.1.93__py3-none-any.whl → 0.1.94__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,16 +6,22 @@ import types
6
6
  import langchain_core
7
7
 
8
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)
9
+ class ChangeDirectoryAndPath:
10
+ """Context manager to change the current working directory and sys.path."""
13
11
 
12
+ def __init__(self, module_path):
13
+ self.module_path = module_path
14
+ self.old_path = None
14
15
 
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)
16
+ def __enter__(self):
17
+ self.old_path = os.getcwd()
18
+ sys.path.insert(0, self.module_path)
19
+ os.chdir(self.module_path)
20
+ return self
21
+
22
+ def __exit__(self, exc_type, exc_value, traceback):
23
+ sys.path.remove(self.module_path)
24
+ os.chdir(self.old_path)
19
25
 
20
26
 
21
27
  def wrapped_tool(tool, module_path):
@@ -24,12 +30,8 @@ def wrapped_tool(tool, module_path):
24
30
 
25
31
  def _wrapped_tool(*args, **kwargs):
26
32
  print(f"调用了{tool.name}")
27
- old_path = os.getcwd()
28
- try:
29
- change_directory_and_path(module_path)
33
+ with ChangeDirectoryAndPath(module_path):
30
34
  result = original_tool_function(*args, **kwargs)
31
- finally:
32
- restore_directory_and_path(module_path, old_path)
33
35
  print(f"{tool.name}调用完毕,结果为:", result)
34
36
  return result
35
37
 
@@ -39,7 +41,6 @@ def wrapped_tool(tool, module_path):
39
41
  def import_tools(tool_id):
40
42
  """Import tools from a specified tool_id."""
41
43
  tool_base_path = "../../tool"
42
- old_path = os.getcwd()
43
44
  module_path = os.path.join(tool_base_path, tool_id)
44
45
  module_path = os.path.normpath(os.path.abspath(module_path))
45
46
 
@@ -51,23 +52,22 @@ def import_tools(tool_id):
51
52
  original_modules = sys.modules.copy()
52
53
 
53
54
  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
55
+ with ChangeDirectoryAndPath(module_path):
56
+ module = importlib.import_module("tool")
57
+ export_tools = getattr(module, "export_tools")
58
+ valid_tools = []
59
+ for tool in export_tools:
60
+ assert isinstance(tool, langchain_core.tools.StructuredTool) or isinstance(
61
+ tool, types.FunctionType
62
+ ), f"Tool is not a StructuredTool or function: {tool}"
63
+ if isinstance(tool, types.FunctionType) and not isinstance(
64
+ tool, langchain_core.tools.StructuredTool
65
+ ):
66
+ valid_tools.append(_tool(tool))
67
+ export_tools = valid_tools
67
68
 
68
69
  except Exception as e:
69
70
  print(f"Error loading tool {tool_id}: {e}")
70
- restore_directory_and_path(module_path, old_path)
71
71
  return []
72
72
 
73
73
  # Unload modules and restore sys.modules state
@@ -76,9 +76,7 @@ def import_tools(tool_id):
76
76
  if key not in original_modules:
77
77
  del sys.modules[key]
78
78
 
79
- restore_directory_and_path(module_path, old_path)
80
-
81
79
  for tool in export_tools:
82
80
  tool.func = wrapped_tool(tool, module_path)
83
81
 
84
- return export_tools
82
+ return export_tools
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.93
3
+ Version: 0.1.94
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -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=Nv6XlxcqaLaMVQNDkpcthlqaSxyfHoOfWiTScQXO22Y,2656
4
+ pycoze/access/tool_for_bot.py,sha256=SbAyQJ9BtuLquVrXnmHJl8t_cdID-AYvdwXQvY67u-Y,2608
5
5
  pycoze/ai/__init__.py,sha256=odM2lgYSnApxw4AzLV_5AyaxL5_MLfydOSB1VmLGFyA,75
6
6
  pycoze/ai/vram_reserve.py,sha256=QbqaA8qv87cnEpOVDMygi0BNMxuhLYwj1UKfR_D5BD4,4340
7
7
  pycoze/bot/__init__.py,sha256=6HHMxDQVOyZM9dtSjQm9tjGnhj4h7CixD0JOvEwTi48,41
@@ -20,8 +20,8 @@ pycoze/ui/ui_def.py,sha256=UhhU_yB3GV9ISbvTWT48hsHPHI250BhMILh6bu5Uioo,4206
20
20
  pycoze/utils/__init__.py,sha256=TNJhFfY7JYdLlzuP9GvgxfNXUtbgH_NUUJSqHXCxJn4,78
21
21
  pycoze/utils/arg.py,sha256=kA3KBQzXc2WlH5XbF8kfikfpqljiKaW7oY_GE4Qyffc,753
22
22
  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,,
23
+ pycoze-0.1.94.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
24
+ pycoze-0.1.94.dist-info/METADATA,sha256=jjabhgJm9ZlQmWh0-Y0Xoh9t3BDCBvYRpif09RLi5Vs,725
25
+ pycoze-0.1.94.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
26
+ pycoze-0.1.94.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
27
+ pycoze-0.1.94.dist-info/RECORD,,