pycoze 0.1.42__py3-none-any.whl → 0.1.43__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.
@@ -1,56 +1,131 @@
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
+
1
82
  import sys
2
83
  import os
84
+ import argparse
3
85
  import importlib
4
86
  from langchain.agents import tool as _tool
5
87
  import types
6
88
  import langchain_core
7
89
 
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
90
 
18
91
  def wrapped_tool(tool, module_path):
19
- """Wrap the tool function to include additional logging and path management."""
20
- original_tool_function = tool.func
92
+ old_tool_fun = tool.func
21
93
 
22
94
  def _wrapped_tool(*args, **kwargs):
23
95
  print(f"调用了{tool.name}")
24
96
  old_path = os.getcwd()
25
97
  try:
26
- change_directory_and_path(module_path)
27
- result = original_tool_function(*args, **kwargs)
98
+ sys.path.insert(0, module_path) # 插入到第一个位置
99
+ os.chdir(module_path)
100
+ result = old_tool_fun(*args, **kwargs)
28
101
  finally:
29
- restore_directory_and_path(module_path, old_path)
102
+ sys.path.remove(module_path)
103
+ os.chdir(old_path)
30
104
  print(f"{tool.name}调用完毕,结果为:", result)
31
105
  return result
32
106
 
33
107
  return _wrapped_tool
34
108
 
109
+
35
110
  def import_tools(tool_id):
36
- """Import tools from a specified tool_id."""
37
- tool_base_path = "../../tool"
111
+ tool_path = "../../tool"
38
112
  old_path = os.getcwd()
39
- module_path = os.path.join(tool_base_path, tool_id)
113
+ module_path = os.path.join(tool_path, tool_id)
40
114
  module_path = os.path.normpath(os.path.abspath(module_path))
41
115
 
42
116
  if not os.path.exists(module_path):
43
117
  print(f"Tool {tool_id} not found")
44
118
  return []
45
119
 
46
- # Save the current sys.modules state
120
+ # 保存当前的 sys.modules 状态
47
121
  original_modules = sys.modules.copy()
48
122
 
49
123
  try:
50
- change_directory_and_path(module_path)
124
+ sys.path.insert(0, module_path) # 插入到第一个位置
125
+ os.chdir(module_path)
51
126
  module = importlib.import_module("tool")
52
127
  export_tools = getattr(module, "export_tools")
53
- valid_tools = []
128
+ temp_list = []
54
129
  for tool in export_tools:
55
130
  assert isinstance(tool, langchain_core.tools.StructuredTool) or isinstance(
56
131
  tool, types.FunctionType
@@ -58,21 +133,23 @@ def import_tools(tool_id):
58
133
  if isinstance(tool, types.FunctionType) and not isinstance(
59
134
  tool, langchain_core.tools.StructuredTool
60
135
  ):
61
- valid_tools.append(_tool(tool))
62
- export_tools = valid_tools
136
+ temp_list.append(_tool(tool))
137
+ export_tools = temp_list
63
138
 
64
139
  except Exception as e:
65
140
  print(f"Error loading tool {tool_id}: {e}")
66
- restore_directory_and_path(module_path, old_path)
141
+ sys.path.remove(module_path)
142
+ os.chdir(old_path)
67
143
  return []
68
144
 
69
- # Unload modules and restore sys.modules state
145
+ # 卸载模块并恢复 sys.modules 状态
70
146
  importlib.invalidate_caches()
71
147
  for key in list(sys.modules.keys()):
72
148
  if key not in original_modules:
73
149
  del sys.modules[key]
74
150
 
75
- restore_directory_and_path(module_path, old_path)
151
+ sys.path.remove(module_path)
152
+ os.chdir(old_path)
76
153
 
77
154
  for tool in export_tools:
78
155
  tool.func = wrapped_tool(tool, module_path)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.42
3
+ Version: 0.1.43
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=dmaMEBCHy0vbYzanFrLg_ZLw60ZMkDCnpHK-K_aUHMo,2652
4
+ pycoze/access/tool_for_bot.py,sha256=NWxS9xJKNc477DB7muEwrKQWZmCCpym5lWIOl6E4mOA,5063
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.42.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
26
- pycoze-0.1.42.dist-info/METADATA,sha256=ca4bq5h8DLWoOBCzYXRZvwDm6bGrQQSX4VwmJjI_hU4,719
27
- pycoze-0.1.42.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
28
- pycoze-0.1.42.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
29
- pycoze-0.1.42.dist-info/RECORD,,
25
+ pycoze-0.1.43.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
26
+ pycoze-0.1.43.dist-info/METADATA,sha256=2BWSATPiv2VUTm3YTRd3TRaDFSSGndCsTchK1q811Uw,719
27
+ pycoze-0.1.43.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
28
+ pycoze-0.1.43.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
29
+ pycoze-0.1.43.dist-info/RECORD,,