pycoze 0.1.100__py3-none-any.whl → 0.1.102__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
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, wrapped_func
8
+
9
+
10
+ def import_bot(bot_id):
11
+ tool_base_path = "../"
12
+ module_path = os.path.join(tool_base_path, bot_id)
13
+ module_path = os.path.normpath(os.path.abspath(module_path))
14
+
15
+ if not os.path.exists(module_path):
16
+ print(f"Bot {bot_id} not found")
17
+ return None
18
+
19
+ try:
20
+ with ModuleManager(module_path) as manager:
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
+ 该函数接收一个指令字符串,并返回虚拟人执行该指令后的结果。
33
+
34
+ Args:
35
+ command: 需要执行的指令字符串。
36
+
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 None
pycoze/access/lib.py CHANGED
@@ -57,4 +57,6 @@ def wrapped_func(item, module_path):
57
57
  print(f"{item.name} 调用完毕,结果为:", result)
58
58
  return result
59
59
 
60
- return _wrapped
60
+ return _wrapped
61
+
62
+
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.100
3
+ Version: 0.1.102
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -1,12 +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/lib.py,sha256=OYxBOm-JEJCNntdrBDMlqzwGrJrxiJX2LCmHQ7ULkDM,1913
4
+ pycoze/access/bot_for_bot.py,sha256=YxZopwQuJo3bn6cSl_FZnDe03neSVPYrtmRy-iQLVPU,1544
5
+ pycoze/access/lib.py,sha256=QGBpyHlAJgmxTvlzANi6KR0VwsXYc0ecwFRGz3cUx0c,1919
5
6
  pycoze/access/tool_for_bot.py,sha256=mAx3dPbZpU_FWlK6uxoWqHbduuNM_EQU8kBmzEKkccE,1339
6
7
  pycoze/ai/__init__.py,sha256=odM2lgYSnApxw4AzLV_5AyaxL5_MLfydOSB1VmLGFyA,75
7
8
  pycoze/ai/vram_reserve.py,sha256=QbqaA8qv87cnEpOVDMygi0BNMxuhLYwj1UKfR_D5BD4,4340
8
9
  pycoze/bot/__init__.py,sha256=6HHMxDQVOyZM9dtSjQm9tjGnhj4h7CixD0JOvEwTi48,41
9
- pycoze/bot/bot.py,sha256=HV2N6Cfuvv5eCCh8bG8Lv6DpDJZ1FrSVrVk8H-zcyCE,3146
10
+ pycoze/bot/bot.py,sha256=u56BqzZ5fbpG3ZgciusMQMXZ3g1YZRmJxZ7vEsC1rAw,3354
10
11
  pycoze/bot/agent/__init__.py,sha256=YR9vpkEQn1e4937r_xFPJXUCPBEJ0SFzEQDBe2x3-YA,157
11
12
  pycoze/bot/agent/agent.py,sha256=XMTO6s8OJpaOnymT8ZUuJxXx2ICZ3r7Ck0pHJqPPFIs,3346
12
13
  pycoze/bot/agent/assistant.py,sha256=XI4w-rFfbk3qYE0tWcWoya8dz-3cA-QZ0Sanhl3DbKE,1112
@@ -21,8 +22,8 @@ pycoze/ui/ui_def.py,sha256=UhhU_yB3GV9ISbvTWT48hsHPHI250BhMILh6bu5Uioo,4206
21
22
  pycoze/utils/__init__.py,sha256=TNJhFfY7JYdLlzuP9GvgxfNXUtbgH_NUUJSqHXCxJn4,78
22
23
  pycoze/utils/arg.py,sha256=kA3KBQzXc2WlH5XbF8kfikfpqljiKaW7oY_GE4Qyffc,753
23
24
  pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
24
- pycoze-0.1.100.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
25
- pycoze-0.1.100.dist-info/METADATA,sha256=ryRBHJpYwsLSGBBppguBS-DBqC9ts-NXnUTm3Q16OZc,726
26
- pycoze-0.1.100.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
27
- pycoze-0.1.100.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
28
- pycoze-0.1.100.dist-info/RECORD,,
25
+ pycoze-0.1.102.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
26
+ pycoze-0.1.102.dist-info/METADATA,sha256=R5GyMzQ78TEN6UDH9RgQKOZdefQKQOCXo5loXdWaP9U,726
27
+ pycoze-0.1.102.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
28
+ pycoze-0.1.102.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
29
+ pycoze-0.1.102.dist-info/RECORD,,