pycoze 0.1.148__py3-none-any.whl → 0.1.150__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/agent_chat.py +78 -0
- pycoze/bot/bot.py +2 -78
- pycoze/reference/lib.py +3 -1
- {pycoze-0.1.148.dist-info → pycoze-0.1.150.dist-info}/METADATA +1 -1
- {pycoze-0.1.148.dist-info → pycoze-0.1.150.dist-info}/RECORD +8 -7
- {pycoze-0.1.148.dist-info → pycoze-0.1.150.dist-info}/LICENSE +0 -0
- {pycoze-0.1.148.dist-info → pycoze-0.1.150.dist-info}/WHEEL +0 -0
- {pycoze-0.1.148.dist-info → pycoze-0.1.150.dist-info}/top_level.txt +0 -0
pycoze/bot/agent_chat.py
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
import json
|
2
|
+
from langchain_openai import ChatOpenAI
|
3
|
+
from .agent import run_agent, Runnable
|
4
|
+
import asyncio
|
5
|
+
from pycoze import utils
|
6
|
+
from pycoze.reference.bot import ref_bot
|
7
|
+
from pycoze.reference.tool import ref_tools
|
8
|
+
from langchain_core.utils.function_calling import convert_to_openai_tool
|
9
|
+
|
10
|
+
params = utils.arg.read_params_file()
|
11
|
+
llm_file = params["appPath"] + "/JsonStorage/llm.json"
|
12
|
+
with open(llm_file, "r", encoding="utf-8") as f:
|
13
|
+
cfg = json.load(f)
|
14
|
+
|
15
|
+
|
16
|
+
def load_role_setting(bot_setting_file: str):
|
17
|
+
with open(bot_setting_file, "r", encoding="utf-8") as f:
|
18
|
+
return json.load(f)
|
19
|
+
|
20
|
+
|
21
|
+
def load_abilities(bot_setting_file: str):
|
22
|
+
with open(bot_setting_file, "r", encoding="utf-8") as f:
|
23
|
+
role_setting = json.load(f)
|
24
|
+
|
25
|
+
abilities = []
|
26
|
+
for bot_id in role_setting["bots"]:
|
27
|
+
bot = ref_bot(bot_id, as_agent_tool=True)
|
28
|
+
if bot:
|
29
|
+
abilities.append(bot)
|
30
|
+
for tool_id in role_setting["tools"]:
|
31
|
+
abilities.extend(ref_tools(tool_id, as_agent_tool=True))
|
32
|
+
return abilities
|
33
|
+
|
34
|
+
|
35
|
+
def agent_chat(bot_setting_file, history):
|
36
|
+
role_setting = load_role_setting(bot_setting_file)
|
37
|
+
abilities = load_abilities(bot_setting_file)
|
38
|
+
|
39
|
+
chat = ChatOpenAI(
|
40
|
+
api_key=cfg["apiKey"],
|
41
|
+
base_url=cfg["baseURL"],
|
42
|
+
model=cfg["model"],
|
43
|
+
temperature=(
|
44
|
+
role_setting["temperature"] * 2
|
45
|
+
if cfg["model"].startswith("deepseek")
|
46
|
+
else role_setting["temperature"]
|
47
|
+
),
|
48
|
+
stop_sequences=[
|
49
|
+
"tool▁calls▁end",
|
50
|
+
"tool▁call▁end",
|
51
|
+
], # 停用deepseek的工具调用标记,不然会虚构工具调用过程和结果
|
52
|
+
)
|
53
|
+
prompt = role_setting["prompt"]
|
54
|
+
if (
|
55
|
+
(cfg["model"].startswith("deepseek")
|
56
|
+
or cfg["toolCompatibilityMode"])
|
57
|
+
and len(abilities) > 0
|
58
|
+
):
|
59
|
+
prompt += """
|
60
|
+
作为一个AI,你如果不确定结果,请务必使用工具查询。
|
61
|
+
你可以通过下面的方式使用工具,并耐心等待工具返回结果。
|
62
|
+
如果你需要调用工具,请使用以正确markdown中的json代码格式进行结尾(务必保证json格式正确,不要出现反斜杠未转义等问题):
|
63
|
+
```json
|
64
|
+
{"name": 函数名, "parameters": 参数词典}
|
65
|
+
```
|
66
|
+
"""
|
67
|
+
if cfg["model"].startswith("yi-"):
|
68
|
+
prompt += "\nAvailable functions:\n"
|
69
|
+
for t in abilities:
|
70
|
+
prompt += f"\n```json\n{json.dumps(convert_to_openai_tool(t))}\n```"
|
71
|
+
agent = Runnable(
|
72
|
+
agent_execution_mode="FuncCall",
|
73
|
+
tools=abilities,
|
74
|
+
llm=chat,
|
75
|
+
assistant_message=prompt,
|
76
|
+
tool_compatibility_mode=cfg["toolCompatibilityMode"],
|
77
|
+
)
|
78
|
+
return asyncio.run(run_agent(agent, history, cfg["toolCompatibilityMode"]))
|
pycoze/bot/bot.py
CHANGED
@@ -1,82 +1,6 @@
|
|
1
|
-
import json
|
2
|
-
from langchain_openai import ChatOpenAI
|
3
|
-
from .agent import run_agent, Runnable, INPUT_MESSAGE, output
|
4
|
-
import asyncio
|
5
1
|
from langchain_core.messages import HumanMessage
|
6
|
-
from
|
7
|
-
from
|
8
|
-
from pycoze.reference.tool import ref_tools
|
9
|
-
from langchain_core.utils.function_calling import convert_to_openai_tool
|
10
|
-
|
11
|
-
params = utils.arg.read_params_file()
|
12
|
-
llm_file = params["appPath"] + "/JsonStorage/llm.json"
|
13
|
-
with open(llm_file, "r", encoding="utf-8") as f:
|
14
|
-
cfg = json.load(f)
|
15
|
-
|
16
|
-
|
17
|
-
def load_role_setting(bot_setting_file: str):
|
18
|
-
with open(bot_setting_file, "r", encoding="utf-8") as f:
|
19
|
-
return json.load(f)
|
20
|
-
|
21
|
-
|
22
|
-
def load_abilities(bot_setting_file: str):
|
23
|
-
with open(bot_setting_file, "r", encoding="utf-8") as f:
|
24
|
-
role_setting = json.load(f)
|
25
|
-
|
26
|
-
abilities = []
|
27
|
-
for bot_id in role_setting["bots"]:
|
28
|
-
bot = ref_bot(bot_id, as_agent_tool=True)
|
29
|
-
if bot:
|
30
|
-
abilities.append(bot)
|
31
|
-
for tool_id in role_setting["tools"]:
|
32
|
-
abilities.extend(ref_tools(tool_id, as_agent_tool=True))
|
33
|
-
return abilities
|
34
|
-
|
35
|
-
|
36
|
-
def agent_chat(bot_setting_file, history):
|
37
|
-
role_setting = load_role_setting(bot_setting_file)
|
38
|
-
abilities = load_abilities(bot_setting_file)
|
39
|
-
|
40
|
-
chat = ChatOpenAI(
|
41
|
-
api_key=cfg["apiKey"],
|
42
|
-
base_url=cfg["baseURL"],
|
43
|
-
model=cfg["model"],
|
44
|
-
temperature=(
|
45
|
-
role_setting["temperature"] * 2
|
46
|
-
if cfg["model"].startswith("deepseek")
|
47
|
-
else role_setting["temperature"]
|
48
|
-
),
|
49
|
-
stop_sequences=[
|
50
|
-
"tool▁calls▁end",
|
51
|
-
"tool▁call▁end",
|
52
|
-
], # 停用deepseek的工具调用标记,不然会虚构工具调用过程和结果
|
53
|
-
)
|
54
|
-
prompt = role_setting["prompt"]
|
55
|
-
if (
|
56
|
-
(cfg["model"].startswith("deepseek")
|
57
|
-
or cfg["toolCompatibilityMode"])
|
58
|
-
and len(abilities) > 0
|
59
|
-
):
|
60
|
-
prompt += """
|
61
|
-
作为一个AI,你如果不确定结果,请务必使用工具查询。
|
62
|
-
你可以通过下面的方式使用工具,并耐心等待工具返回结果。
|
63
|
-
如果你需要调用工具,请使用以正确markdown中的json代码格式进行结尾(务必保证json格式正确,不要出现反斜杠未转义等问题):
|
64
|
-
```json
|
65
|
-
{"name": 函数名, "parameters": 参数词典}
|
66
|
-
```
|
67
|
-
"""
|
68
|
-
if cfg["model"].startswith("yi-"):
|
69
|
-
prompt += "\nAvailable functions:\n"
|
70
|
-
for t in abilities:
|
71
|
-
prompt += f"\n```json\n{json.dumps(convert_to_openai_tool(t))}\n```"
|
72
|
-
agent = Runnable(
|
73
|
-
agent_execution_mode="FuncCall",
|
74
|
-
tools=abilities,
|
75
|
-
llm=chat,
|
76
|
-
assistant_message=prompt,
|
77
|
-
tool_compatibility_mode=cfg["toolCompatibilityMode"],
|
78
|
-
)
|
79
|
-
return asyncio.run(run_agent(agent, history, cfg["toolCompatibilityMode"]))
|
2
|
+
from .agent import INPUT_MESSAGE, output
|
3
|
+
from .agent_chat import agent_chat
|
80
4
|
|
81
5
|
|
82
6
|
def chat(bot_setting_file: str):
|
pycoze/reference/lib.py
CHANGED
@@ -59,7 +59,9 @@ def wrapped_func(item, module_path):
|
|
59
59
|
with ChangeDirectoryAndPath(module_path):
|
60
60
|
result = original_tool_function(*args, **kwargs)
|
61
61
|
try:
|
62
|
-
|
62
|
+
if isinstance(result, types.GeneratorType):
|
63
|
+
result = list(result)
|
64
|
+
print(f"{item.name} 调用完毕,结果为:", result)
|
63
65
|
except:
|
64
66
|
pass
|
65
67
|
return result
|
@@ -3,7 +3,8 @@ pycoze/module.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
3
|
pycoze/ai/__init__.py,sha256=odM2lgYSnApxw4AzLV_5AyaxL5_MLfydOSB1VmLGFyA,75
|
4
4
|
pycoze/ai/vram_reserve.py,sha256=QbqaA8qv87cnEpOVDMygi0BNMxuhLYwj1UKfR_D5BD4,4340
|
5
5
|
pycoze/bot/__init__.py,sha256=6HHMxDQVOyZM9dtSjQm9tjGnhj4h7CixD0JOvEwTi48,41
|
6
|
-
pycoze/bot/
|
6
|
+
pycoze/bot/agent_chat.py,sha256=Ey45GpBHmzILsMee-iXc90mpiQK3VAm_gomd1tSScOI,2808
|
7
|
+
pycoze/bot/bot.py,sha256=XOG47oKoxUiuHjlTbB9-JlT7T22_JhyF1MeAnEPVaiM,725
|
7
8
|
pycoze/bot/agent/__init__.py,sha256=YR9vpkEQn1e4937r_xFPJXUCPBEJ0SFzEQDBe2x3-YA,157
|
8
9
|
pycoze/bot/agent/agent.py,sha256=Aue8nWeW_I7e1jo4o7cUjFFjrsV9NtVUiTX3EQYHmbA,3507
|
9
10
|
pycoze/bot/agent/assistant.py,sha256=3iLxnRvf_ia0cP-FHK5Fv4ylltlnzPq1KscRCFYqjkc,1147
|
@@ -13,7 +14,7 @@ pycoze/bot/agent/agent_types/const.py,sha256=BfUKPrhAHREoMLHuFNG2bCIEkC1-f7K0LEq
|
|
13
14
|
pycoze/bot/agent/agent_types/openai_func_call_agent.py,sha256=SnEm5MODHn2uMsaMNqgzULM_91vqLHC0TU6ovwCOqLU,6675
|
14
15
|
pycoze/reference/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
15
16
|
pycoze/reference/bot.py,sha256=6w8LGBFxLygAMR4UUiItUF3AbClnoyDaSoyPK3V-mIg,2205
|
16
|
-
pycoze/reference/lib.py,sha256=
|
17
|
+
pycoze/reference/lib.py,sha256=0xQJTLTHedGzQBsjuTFNBVqYc4-8Yl65gGCrAhWyOX8,2155
|
17
18
|
pycoze/reference/tool.py,sha256=W-XAa2ppIZET99kJcaU9mgYxsxJEKos9ZSUQA8hbqX0,1526
|
18
19
|
pycoze/ui/__init__.py,sha256=7xAfL2lfG7-jllPJEZUJO89xUE9sNzvo1y0WmBswjBI,458
|
19
20
|
pycoze/ui/base.py,sha256=SCXVDK7PpMaBv6ovvabHcfRq_d2AWM0BRyxpNhuJN5A,1285
|
@@ -23,8 +24,8 @@ pycoze/ui/ui_def.py,sha256=UhhU_yB3GV9ISbvTWT48hsHPHI250BhMILh6bu5Uioo,4206
|
|
23
24
|
pycoze/utils/__init__.py,sha256=ET0W5wzq4zlY3dr1wHVbbeRKlKdC_zqHt9b0jZyHohw,94
|
24
25
|
pycoze/utils/arg.py,sha256=orLVEGw3x2f3l7bZDbBPkkdDPSdqXo-_Rs-4ZhzmrEw,984
|
25
26
|
pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
|
26
|
-
pycoze-0.1.
|
27
|
-
pycoze-0.1.
|
28
|
-
pycoze-0.1.
|
29
|
-
pycoze-0.1.
|
30
|
-
pycoze-0.1.
|
27
|
+
pycoze-0.1.150.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
|
28
|
+
pycoze-0.1.150.dist-info/METADATA,sha256=IODuOadmykc1AGE47pIeaoMYkRLn_PsJ9kRczQkwlKU,726
|
29
|
+
pycoze-0.1.150.dist-info/WHEEL,sha256=bFJAMchF8aTQGUgMZzHJyDDMPTO3ToJ7x23SLJa1SVo,92
|
30
|
+
pycoze-0.1.150.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
|
31
|
+
pycoze-0.1.150.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|