pycoze 0.1.99__py3-none-any.whl → 0.1.101__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,37 +4,43 @@ import importlib
4
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, wrapped_fun
7
+ from .lib import ChangeDirectoryAndPath, ModuleManager, wrapped_func
8
8
 
9
9
 
10
- def import_tools(tool_id):
11
- tool_base_path = "../../tool"
12
- module_path = os.path.join(tool_base_path, tool_id)
10
+ def import_bot(bot_id):
11
+ tool_base_path = "../"
12
+ module_path = os.path.join(tool_base_path, bot_id)
13
13
  module_path = os.path.normpath(os.path.abspath(module_path))
14
14
 
15
15
  if not os.path.exists(module_path):
16
- print(f"Tool {tool_id} not found")
17
- return []
18
-
16
+ print(f"Bot {bot_id} not found")
17
+ return None
18
+
19
19
  try:
20
20
  with ModuleManager(module_path) as manager:
21
- module = importlib.import_module("tool")
22
- export_tools = getattr(module, "export_tools")
23
- valid_tools = []
24
- for tool in export_tools:
25
- assert isinstance(tool, langchain_core.tools.StructuredTool) or isinstance(
26
- tool, types.FunctionType
27
- ), f"Tool is not a StructuredTool or function: {tool}"
28
- if not isinstance(tool, langchain_core.tools.StructuredTool):
29
- tool = to_agent_tool(tool)
30
- valid_tools.append(tool)
31
- export_tools = valid_tools
32
-
33
- except Exception as e:
34
- print(f"Error loading tool {tool_id}: {e}")
35
- return []
21
+ bot_setting = module_path + "/botSetting.json"
22
+ with open(bot_setting, "r", encoding="utf-8") as f:
23
+ bot_setting = json.load(f)
24
+ module = importlib.import_module("bot_function")
25
+ chat_func = getattr(module, "chat")
26
+ random_name = "bot_" + bot_id[-6:]
27
+ # 定义函数的内容
28
+ function_code = f"""
29
+ def {random_name}(command:str) -> str:
30
+ \"\"\"
31
+ 本函数可以让你与虚拟人角色({bot_setting["name"]})进行交互。
32
+ 该函数接收一个指令字符串,并返回虚拟人执行该指令后的结果。
36
33
 
37
- for tool in export_tools:
38
- tool.func = wrapped_fun(tool, module_path)
34
+ Args:
35
+ command: 需要执行的指令字符串。
39
36
 
40
- return export_tools
37
+ Returns:
38
+ str: 虚拟人执行指令后的结果。
39
+ \"\"\"
40
+ return chat_func(command)
41
+ """
42
+
43
+ return wrapped_func(to_agent_tool(eval(random_name)))
44
+ except Exception as e:
45
+ print(f"Error loading bot {bot_id}: {e}")
46
+ return tool
pycoze/access/lib.py CHANGED
@@ -47,7 +47,7 @@ class ModuleManager:
47
47
  del sys.modules[key]
48
48
 
49
49
 
50
- def wrapped_fun(item, module_path):
50
+ def wrapped_func(item, module_path):
51
51
  original_tool_function = item.func
52
52
 
53
53
  def _wrapped(*args, **kwargs):
@@ -57,4 +57,6 @@ def wrapped_fun(item, module_path):
57
57
  print(f"{item.name} 调用完毕,结果为:", result)
58
58
  return result
59
59
 
60
- return _wrapped
60
+ return _wrapped
61
+
62
+
@@ -4,7 +4,7 @@ import importlib
4
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, wrapped_fun
7
+ from .lib import ChangeDirectoryAndPath, ModuleManager, wrapped_func
8
8
 
9
9
 
10
10
  def import_tools(tool_id):
@@ -35,6 +35,6 @@ def import_tools(tool_id):
35
35
  return []
36
36
 
37
37
  for tool in export_tools:
38
- tool.func = wrapped_fun(tool, module_path)
38
+ tool.func = wrapped_func(tool, module_path)
39
39
 
40
40
  return export_tools
pycoze/bot/bot.py CHANGED
@@ -4,6 +4,7 @@ from .agent import run_agent, Runnable, INPUT_MESSAGE, output
4
4
  import asyncio
5
5
  from langchain_core.messages import HumanMessage
6
6
  from pycoze import utils
7
+ from pycoze.access.bot_for_bot import import_bot
7
8
  from pycoze.access.tool_for_bot import import_tools
8
9
  from langchain_core.utils.function_calling import convert_to_openai_tool
9
10
 
@@ -18,19 +19,23 @@ def load_role_setting(bot_setting_file: str):
18
19
  return json.load(f)
19
20
 
20
21
 
21
- def load_tools(bot_setting_file: str):
22
+ def load_abilities(bot_setting_file: str):
22
23
  with open(bot_setting_file, "r", encoding="utf-8") as f:
23
24
  role_setting = json.load(f)
24
25
 
25
- tools = []
26
+ abilities = []
27
+ for bot_id in role_setting["bots"]:
28
+ bot = import_bot(bot_id)
29
+ if bot:
30
+ abilities.append(bot)
26
31
  for tool_id in role_setting["tools"]:
27
- tools.extend(import_tools(tool_id))
28
- return tools
32
+ abilities.extend(import_tools(tool_id))
33
+ return abilities
29
34
 
30
35
 
31
36
  def agent_chat(bot_setting_file, history):
32
37
  role_setting = load_role_setting(bot_setting_file)
33
- tools = load_tools(bot_setting_file)
38
+ abilities = load_abilities(bot_setting_file)
34
39
 
35
40
  chat = ChatOpenAI(
36
41
  api_key=cfg["apiKey"],
@@ -50,7 +55,7 @@ def agent_chat(bot_setting_file, history):
50
55
  if (
51
56
  cfg["model"].startswith("deepseek")
52
57
  or cfg["toolCompatibilityMode"]
53
- and len(tools) > 0
58
+ and len(abilities) > 0
54
59
  ):
55
60
  prompt += """
56
61
  作为一个AI,你如果不确定结果,请务必使用工具查询。
@@ -62,11 +67,11 @@ def agent_chat(bot_setting_file, history):
62
67
  """
63
68
  if cfg["model"].startswith("yi-"):
64
69
  prompt += "\nAvailable functions:\n"
65
- for t in tools:
70
+ for t in abilities:
66
71
  prompt += f"\n```json\n{json.dumps(convert_to_openai_tool(t))}\n```"
67
72
  agent = Runnable(
68
73
  agent_execution_mode="FuncCall",
69
- tools=tools,
74
+ tools=abilities,
70
75
  llm=chat,
71
76
  assistant_message=prompt,
72
77
  tool_compatibility_mode=cfg["toolCompatibilityMode"],
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.99
3
+ Version: 0.1.101
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -1,13 +1,13 @@
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/bot_for_bot.py,sha256=p7BvQk_y4Hh8L9Et39EUnURpy4enRUvsMlBLNfYFb8o,1377
5
- pycoze/access/lib.py,sha256=RTQvEee1kfdbap-tzrcQbh-mZg_Y5ydmak7RRB7EkJ8,1912
6
- pycoze/access/tool_for_bot.py,sha256=F1QrGpGbJ3ucsRYWiJw-My4XiE8QuVb8oj7p9ZMmZXE,1337
4
+ pycoze/access/bot_for_bot.py,sha256=bNFhZAAK8XZnwvkZhUdRGIM9MqBuoMEdkHX3ZQ6yU0w,1544
5
+ pycoze/access/lib.py,sha256=QGBpyHlAJgmxTvlzANi6KR0VwsXYc0ecwFRGz3cUx0c,1919
6
+ pycoze/access/tool_for_bot.py,sha256=mAx3dPbZpU_FWlK6uxoWqHbduuNM_EQU8kBmzEKkccE,1339
7
7
  pycoze/ai/__init__.py,sha256=odM2lgYSnApxw4AzLV_5AyaxL5_MLfydOSB1VmLGFyA,75
8
8
  pycoze/ai/vram_reserve.py,sha256=QbqaA8qv87cnEpOVDMygi0BNMxuhLYwj1UKfR_D5BD4,4340
9
9
  pycoze/bot/__init__.py,sha256=6HHMxDQVOyZM9dtSjQm9tjGnhj4h7CixD0JOvEwTi48,41
10
- pycoze/bot/bot.py,sha256=HV2N6Cfuvv5eCCh8bG8Lv6DpDJZ1FrSVrVk8H-zcyCE,3146
10
+ pycoze/bot/bot.py,sha256=u56BqzZ5fbpG3ZgciusMQMXZ3g1YZRmJxZ7vEsC1rAw,3354
11
11
  pycoze/bot/agent/__init__.py,sha256=YR9vpkEQn1e4937r_xFPJXUCPBEJ0SFzEQDBe2x3-YA,157
12
12
  pycoze/bot/agent/agent.py,sha256=XMTO6s8OJpaOnymT8ZUuJxXx2ICZ3r7Ck0pHJqPPFIs,3346
13
13
  pycoze/bot/agent/assistant.py,sha256=XI4w-rFfbk3qYE0tWcWoya8dz-3cA-QZ0Sanhl3DbKE,1112
@@ -22,8 +22,8 @@ pycoze/ui/ui_def.py,sha256=UhhU_yB3GV9ISbvTWT48hsHPHI250BhMILh6bu5Uioo,4206
22
22
  pycoze/utils/__init__.py,sha256=TNJhFfY7JYdLlzuP9GvgxfNXUtbgH_NUUJSqHXCxJn4,78
23
23
  pycoze/utils/arg.py,sha256=kA3KBQzXc2WlH5XbF8kfikfpqljiKaW7oY_GE4Qyffc,753
24
24
  pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
25
- pycoze-0.1.99.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
26
- pycoze-0.1.99.dist-info/METADATA,sha256=3HaGjTXmeXlkFAK-1coDK6dpZreMIBRXpQlvPyhOW3U,725
27
- pycoze-0.1.99.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
28
- pycoze-0.1.99.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
29
- pycoze-0.1.99.dist-info/RECORD,,
25
+ pycoze-0.1.101.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
26
+ pycoze-0.1.101.dist-info/METADATA,sha256=aOdIZUfpg2FxYOPdg-iQDK5mGMdfkD9rM-tW_f2FlK4,726
27
+ pycoze-0.1.101.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
28
+ pycoze-0.1.101.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
29
+ pycoze-0.1.101.dist-info/RECORD,,