pycoze 0.1.93__tar.gz → 0.1.95__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {pycoze-0.1.93 → pycoze-0.1.95}/PKG-INFO +1 -1
- pycoze-0.1.95/pycoze/access/lib.py +46 -0
- pycoze-0.1.95/pycoze/access/tool_for_bot.py +56 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze.egg-info/PKG-INFO +1 -1
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze.egg-info/SOURCES.txt +1 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/setup.py +1 -1
- pycoze-0.1.93/pycoze/access/tool_for_bot.py +0 -84
- {pycoze-0.1.93 → pycoze-0.1.95}/LICENSE +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/README.md +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/__init__.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/access/__init__.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/ai/__init__.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/ai/vram_reserve.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/bot/__init__.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/bot/agent/__init__.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/bot/agent/agent.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/bot/agent/agent_types/__init__.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/bot/agent/agent_types/openai_func_call_agent.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/bot/agent/assistant.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/bot/agent/chat.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/bot/bot.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/module.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/ui/__init__.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/ui/base.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/ui/color.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/ui/typ.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/ui/ui_def.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/utils/__init__.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/utils/arg.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze/utils/text_or_file.py +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze.egg-info/dependency_links.txt +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/pycoze.egg-info/top_level.txt +0 -0
- {pycoze-0.1.93 → pycoze-0.1.95}/setup.cfg +0 -0
@@ -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]
|
@@ -0,0 +1,56 @@
|
|
1
|
+
import sys
|
2
|
+
import os
|
3
|
+
import importlib
|
4
|
+
from langchain.agents import tool as to_agent_tool
|
5
|
+
import types
|
6
|
+
import langchain_core
|
7
|
+
from .lib import ChangeDirectoryAndPath, ModuleManager
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
def wrapped_tool(tool, module_path):
|
12
|
+
"""Wrap the tool function to include additional logging and path management."""
|
13
|
+
original_tool_function = tool.func
|
14
|
+
|
15
|
+
def _wrapped_tool(*args, **kwargs):
|
16
|
+
print(f"调用了{tool.name}")
|
17
|
+
with ChangeDirectoryAndPath(module_path):
|
18
|
+
result = original_tool_function(*args, **kwargs)
|
19
|
+
print(f"{tool.name}调用完毕,结果为:", result)
|
20
|
+
return result
|
21
|
+
|
22
|
+
return _wrapped_tool
|
23
|
+
|
24
|
+
|
25
|
+
def import_tools(tool_id):
|
26
|
+
"""Import tools from a specified tool_id."""
|
27
|
+
tool_base_path = "../../tool"
|
28
|
+
module_path = os.path.join(tool_base_path, tool_id)
|
29
|
+
module_path = os.path.normpath(os.path.abspath(module_path))
|
30
|
+
|
31
|
+
if not os.path.exists(module_path):
|
32
|
+
print(f"Tool {tool_id} not found")
|
33
|
+
return []
|
34
|
+
|
35
|
+
try:
|
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
|
48
|
+
|
49
|
+
except Exception as e:
|
50
|
+
print(f"Error loading tool {tool_id}: {e}")
|
51
|
+
return []
|
52
|
+
|
53
|
+
for tool in export_tools:
|
54
|
+
tool.func = wrapped_tool(tool, module_path)
|
55
|
+
|
56
|
+
return export_tools
|
@@ -1,84 +0,0 @@
|
|
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 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
|
-
|
21
|
-
def wrapped_tool(tool, module_path):
|
22
|
-
"""Wrap the tool function to include additional logging and path management."""
|
23
|
-
original_tool_function = tool.func
|
24
|
-
|
25
|
-
def _wrapped_tool(*args, **kwargs):
|
26
|
-
print(f"调用了{tool.name}")
|
27
|
-
old_path = os.getcwd()
|
28
|
-
try:
|
29
|
-
change_directory_and_path(module_path)
|
30
|
-
result = original_tool_function(*args, **kwargs)
|
31
|
-
finally:
|
32
|
-
restore_directory_and_path(module_path, old_path)
|
33
|
-
print(f"{tool.name}调用完毕,结果为:", result)
|
34
|
-
return result
|
35
|
-
|
36
|
-
return _wrapped_tool
|
37
|
-
|
38
|
-
|
39
|
-
def import_tools(tool_id):
|
40
|
-
"""Import tools from a specified tool_id."""
|
41
|
-
tool_base_path = "../../tool"
|
42
|
-
old_path = os.getcwd()
|
43
|
-
module_path = os.path.join(tool_base_path, tool_id)
|
44
|
-
module_path = os.path.normpath(os.path.abspath(module_path))
|
45
|
-
|
46
|
-
if not os.path.exists(module_path):
|
47
|
-
print(f"Tool {tool_id} not found")
|
48
|
-
return []
|
49
|
-
|
50
|
-
# Save the current sys.modules state
|
51
|
-
original_modules = sys.modules.copy()
|
52
|
-
|
53
|
-
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
|
67
|
-
|
68
|
-
except Exception as e:
|
69
|
-
print(f"Error loading tool {tool_id}: {e}")
|
70
|
-
restore_directory_and_path(module_path, old_path)
|
71
|
-
return []
|
72
|
-
|
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
|
-
for tool in export_tools:
|
82
|
-
tool.func = wrapped_tool(tool, module_path)
|
83
|
-
|
84
|
-
return export_tools
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|