pycoze 0.1.10__py3-none-any.whl → 0.1.13__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/bot/base.py +81 -79
- pycoze/bot/bot.py +54 -50
- pycoze/utils/__init__.py +1 -1
- {pycoze-0.1.10.dist-info → pycoze-0.1.13.dist-info}/METADATA +1 -1
- {pycoze-0.1.10.dist-info → pycoze-0.1.13.dist-info}/RECORD +8 -8
- {pycoze-0.1.10.dist-info → pycoze-0.1.13.dist-info}/LICENSE +0 -0
- {pycoze-0.1.10.dist-info → pycoze-0.1.13.dist-info}/WHEEL +0 -0
- {pycoze-0.1.10.dist-info → pycoze-0.1.13.dist-info}/top_level.txt +0 -0
pycoze/bot/base.py
CHANGED
@@ -1,79 +1,81 @@
|
|
1
|
-
import sys
|
2
|
-
import os
|
3
|
-
import argparse
|
4
|
-
import importlib
|
5
|
-
from langchain.agents import tool as _tool
|
6
|
-
import types
|
7
|
-
import langchain_core
|
8
|
-
|
9
|
-
def wrapped_tool(tool, module_path):
|
10
|
-
old_tool_fun = tool.func
|
11
|
-
def _wrapped_tool(*args, **kwargs):
|
12
|
-
print(f"调用了{tool.name}")
|
13
|
-
old_path = os.getcwd()
|
14
|
-
try:
|
15
|
-
sys.path.insert(0, module_path) # 插入到第一个位置
|
16
|
-
os.chdir(module_path)
|
17
|
-
result = old_tool_fun(*args, **kwargs)
|
18
|
-
finally:
|
19
|
-
sys.path.remove(module_path)
|
20
|
-
os.chdir(old_path)
|
21
|
-
print(f"{tool.name}调用完毕,结果为:", result)
|
22
|
-
return result
|
23
|
-
return _wrapped_tool
|
24
|
-
|
25
|
-
|
26
|
-
def import_tools(tool_id):
|
27
|
-
tool_path = "../../tool"
|
28
|
-
old_path = os.getcwd()
|
29
|
-
module_path = os.path.join(tool_path, tool_id)
|
30
|
-
module_path = os.path.normpath(os.path.abspath(module_path))
|
31
|
-
|
32
|
-
if not os.path.exists(module_path):
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
1
|
+
import sys
|
2
|
+
import os
|
3
|
+
import argparse
|
4
|
+
import importlib
|
5
|
+
from langchain.agents import tool as _tool
|
6
|
+
import types
|
7
|
+
import langchain_core
|
8
|
+
|
9
|
+
def wrapped_tool(tool, module_path):
|
10
|
+
old_tool_fun = tool.func
|
11
|
+
def _wrapped_tool(*args, **kwargs):
|
12
|
+
print(f"调用了{tool.name}")
|
13
|
+
old_path = os.getcwd()
|
14
|
+
try:
|
15
|
+
sys.path.insert(0, module_path) # 插入到第一个位置
|
16
|
+
os.chdir(module_path)
|
17
|
+
result = old_tool_fun(*args, **kwargs)
|
18
|
+
finally:
|
19
|
+
sys.path.remove(module_path)
|
20
|
+
os.chdir(old_path)
|
21
|
+
print(f"{tool.name}调用完毕,结果为:", result)
|
22
|
+
return result
|
23
|
+
return _wrapped_tool
|
24
|
+
|
25
|
+
|
26
|
+
def import_tools(tool_id):
|
27
|
+
tool_path = "../../tool"
|
28
|
+
old_path = os.getcwd()
|
29
|
+
module_path = os.path.join(tool_path, tool_id)
|
30
|
+
module_path = os.path.normpath(os.path.abspath(module_path))
|
31
|
+
|
32
|
+
if not os.path.exists(module_path):
|
33
|
+
print(f"Tool {tool_id} not found")
|
34
|
+
return []
|
35
|
+
|
36
|
+
# 保存当前的 sys.modules 状态
|
37
|
+
original_modules = sys.modules.copy()
|
38
|
+
|
39
|
+
try:
|
40
|
+
sys.path.insert(0, module_path) # 插入到第一个位置
|
41
|
+
os.chdir(module_path)
|
42
|
+
module = importlib.import_module("tool")
|
43
|
+
export_tools = getattr(module, "export_tools")
|
44
|
+
temp_list = []
|
45
|
+
for tool in export_tools:
|
46
|
+
assert isinstance(tool, langchain_core.tools.StructuredTool) or isinstance(tool, types.FunctionType), f"Tool is not a StructuredTool or function: {tool}"
|
47
|
+
if isinstance(tool, types.FunctionType) and not isinstance(tool, langchain_core.tools.StructuredTool):
|
48
|
+
temp_list.append(_tool(tool))
|
49
|
+
export_tools = temp_list
|
50
|
+
|
51
|
+
except Exception as e:
|
52
|
+
print(f"Error loading tool {tool_id}: {e}")
|
53
|
+
sys.path.remove(module_path)
|
54
|
+
os.chdir(old_path)
|
55
|
+
return []
|
56
|
+
|
57
|
+
# 卸载模块并恢复 sys.modules 状态
|
58
|
+
importlib.invalidate_caches()
|
59
|
+
for key in list(sys.modules.keys()):
|
60
|
+
if key not in original_modules:
|
61
|
+
del sys.modules[key]
|
62
|
+
|
63
|
+
sys.path.remove(module_path)
|
64
|
+
os.chdir(old_path)
|
65
|
+
|
66
|
+
for tool in export_tools:
|
67
|
+
tool.func = wrapped_tool(tool, module_path)
|
68
|
+
|
69
|
+
return export_tools
|
70
|
+
|
71
|
+
|
72
|
+
def read_arg(param: str, is_path=False):
|
73
|
+
parser = argparse.ArgumentParser()
|
74
|
+
parser.add_argument(param, nargs='?', help=f'Parameter {param}')
|
75
|
+
args = parser.parse_args()
|
76
|
+
value = getattr(args, param.lstrip('-'))
|
77
|
+
# 如果是路径并且有引号,去掉引号
|
78
|
+
if is_path and value and value.startswith('"') and value.endswith('"'):
|
79
|
+
value = value[1:-1]
|
80
|
+
|
81
|
+
return value
|
pycoze/bot/bot.py
CHANGED
@@ -1,50 +1,54 @@
|
|
1
|
-
import json
|
2
|
-
from langchain_openai import ChatOpenAI
|
3
|
-
from .base import import_tools
|
4
|
-
from .agent import run_agent, Runnable, INPUT_MESSAGE, output
|
5
|
-
import asyncio
|
6
|
-
from langchain_core.messages import HumanMessage
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def
|
14
|
-
with open(bot_setting_file, "r", encoding="utf-8") as f:
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
1
|
+
import json
|
2
|
+
from langchain_openai import ChatOpenAI
|
3
|
+
from .base import import_tools
|
4
|
+
from .agent import run_agent, Runnable, INPUT_MESSAGE, output
|
5
|
+
import asyncio
|
6
|
+
from langchain_core.messages import HumanMessage
|
7
|
+
from pycoze import utils
|
8
|
+
|
9
|
+
|
10
|
+
params = utils.arg.read_params()
|
11
|
+
llm_file = params["appPath"] + "/JsonStorage/llm.json"
|
12
|
+
|
13
|
+
def load_role_setting(bot_setting_file:str):
|
14
|
+
with open(bot_setting_file, "r", encoding="utf-8") as f:
|
15
|
+
return json.load(f)
|
16
|
+
|
17
|
+
def load_tools(bot_setting_file:str):
|
18
|
+
with open(bot_setting_file, "r", encoding="utf-8") as f:
|
19
|
+
role_setting = json.load(f)
|
20
|
+
|
21
|
+
tools = []
|
22
|
+
for tool_id in role_setting["tools"]:
|
23
|
+
tools.extend(import_tools(tool_id))
|
24
|
+
return tools
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
def chat(bot_setting_file:str):
|
30
|
+
history = []
|
31
|
+
|
32
|
+
while True:
|
33
|
+
message = input()
|
34
|
+
role_setting = load_role_setting(bot_setting_file)
|
35
|
+
tools = load_tools(bot_setting_file)
|
36
|
+
if not message.startswith(INPUT_MESSAGE):
|
37
|
+
raise ValueError("Invalid message")
|
38
|
+
message = json.loads(message[len(INPUT_MESSAGE):])["content"]
|
39
|
+
print("user:", message)
|
40
|
+
|
41
|
+
with open(llm_file, "r", encoding="utf-8") as f:
|
42
|
+
cfg = json.load(f)
|
43
|
+
chat = ChatOpenAI(api_key=cfg["apiKey"], base_url=cfg['baseURL'], model=cfg["model"], temperature=role_setting["temperature"])
|
44
|
+
|
45
|
+
|
46
|
+
agent = Runnable(agent_execution_mode='FuncCall', # 'FuncCall' or 'ReAct',大模型支持FuncCall的话就用FuncCall
|
47
|
+
tools=tools,
|
48
|
+
llm=chat,
|
49
|
+
assistant_message=role_setting["prompt"],)
|
50
|
+
|
51
|
+
history += [HumanMessage(content=message)]
|
52
|
+
result = asyncio.run(run_agent(agent, history))
|
53
|
+
output("assistant", result, history)
|
54
|
+
|
pycoze/utils/__init__.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
from .arg import read_arg
|
1
|
+
from .arg import read_arg, read_params
|
@@ -1,8 +1,8 @@
|
|
1
1
|
pycoze/__init__.py,sha256=5UJGG0TRNy3cbXuenCY1Fi7RoXzy4X6HllI7bItZ4l0,56
|
2
2
|
pycoze/module.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
3
|
pycoze/bot/__init__.py,sha256=pciDtfcIXda7iFt9uI5Fpm0JKpGBhdXHmJv4966WTVU,21
|
4
|
-
pycoze/bot/base.py,sha256
|
5
|
-
pycoze/bot/bot.py,sha256=
|
4
|
+
pycoze/bot/base.py,sha256=-2EfIdcPZvFm1cjRyTIhjYsyRuAaSVdhdXJR5_7r4CE,2616
|
5
|
+
pycoze/bot/bot.py,sha256=3BxTejG164_7DFS47lDrEk-C49CrXa2WPnFxgSvZ6Wk,1836
|
6
6
|
pycoze/bot/agent/__init__.py,sha256=IaYqQCJ3uBor92JdOxI_EY4HtYOHgej8lijr3UrN1Vc,161
|
7
7
|
pycoze/bot/agent/agent.py,sha256=8vww4POYkXLa5MlKQ5hL6ZIZs5q-Y7FS6SznATrrpjE,3324
|
8
8
|
pycoze/bot/agent/assistant.py,sha256=QLeWaPi415P9jruYOm8qcIbC94cXXAhJYmLTkyC9NTQ,1267
|
@@ -16,10 +16,10 @@ pycoze/ui/base.py,sha256=nXNXRTZ5Tl1AQp5nfjzLvOVzt_1nLSCn2IOyfxAN_fc,1471
|
|
16
16
|
pycoze/ui/color.py,sha256=cT9Ib8uNzkOKxyW0IwVj46o4LwdB1xgNCj1_Rou9d_4,854
|
17
17
|
pycoze/ui/typ.py,sha256=NpT0FrbHvByOszBZMFtroRp7I7pN-38tYz_zPOPejF4,1723
|
18
18
|
pycoze/ui/ui_def.py,sha256=xCaqtsnRsbAQMqDa4DpbAVupjCnYdWIu1pAiDQdgTwM,3135
|
19
|
-
pycoze/utils/__init__.py,sha256=
|
19
|
+
pycoze/utils/__init__.py,sha256=GMP9EhOsbRM0EfIzS2ll1fXKKSpY3zqpqkm66fPGBaM,39
|
20
20
|
pycoze/utils/arg.py,sha256=VKinJRPiNw0TPvuRFwLg0Vz8u8HXx_ejGfOojfE9b_Y,706
|
21
|
-
pycoze-0.1.
|
22
|
-
pycoze-0.1.
|
23
|
-
pycoze-0.1.
|
24
|
-
pycoze-0.1.
|
25
|
-
pycoze-0.1.
|
21
|
+
pycoze-0.1.13.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
|
22
|
+
pycoze-0.1.13.dist-info/METADATA,sha256=wFhwpl_MzMOLq5Xr2VUOE_FUU_rrA2wtwSIduIM7oQ8,719
|
23
|
+
pycoze-0.1.13.dist-info/WHEEL,sha256=UvcQYKBHoFqaQd6LKyqHw9fxEolWLQnlzP0h_LgJAfI,91
|
24
|
+
pycoze-0.1.13.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
|
25
|
+
pycoze-0.1.13.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|