pycoze 0.1.36__tar.gz → 0.1.38__tar.gz
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-0.1.36 → pycoze-0.1.38}/PKG-INFO +1 -1
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/bot/agent/agent.py +18 -3
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/bot/agent/agent_types/openai_func_call_agent.py +1 -2
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/bot/bot.py +13 -4
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze.egg-info/PKG-INFO +1 -1
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze.egg-info/SOURCES.txt +0 -1
- {pycoze-0.1.36 → pycoze-0.1.38}/setup.py +1 -1
- pycoze-0.1.36/pycoze/bot/base.py +0 -88
- {pycoze-0.1.36 → pycoze-0.1.38}/LICENSE +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/README.md +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/__init__.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/bot/__init__.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/bot/agent/__init__.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/bot/agent/agent_types/__init__.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/bot/agent/agent_types/react_agent.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/bot/agent/agent_types/react_prompt.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/bot/agent/assistant.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/bot/agent/chat.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/gpu/__init__.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/gpu/gpu_reserve.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/module.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/ui/__init__.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/ui/base.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/ui/color.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/ui/typ.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/ui/ui_def.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/utils/__init__.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/utils/arg.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze/utils/text_or_file.py +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze.egg-info/dependency_links.txt +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/pycoze.egg-info/top_level.txt +0 -0
- {pycoze-0.1.36 → pycoze-0.1.38}/setup.cfg +0 -0
@@ -13,10 +13,25 @@ async def run_agent(agent, inputs: list):
|
|
13
13
|
content_list = []
|
14
14
|
async for event in agent.astream_events(inputs, version="v2"):
|
15
15
|
kind = event["event"]
|
16
|
-
if kind == "
|
16
|
+
if kind == "on_chain_end":
|
17
|
+
if "data" in event:
|
18
|
+
if (
|
19
|
+
"output" in event["data"]
|
20
|
+
and event["data"]["output"] == "end"
|
21
|
+
and "input" in event["data"]
|
22
|
+
and isinstance(event["data"]["input"], list)
|
23
|
+
):
|
24
|
+
input_list = event["data"]["input"]
|
25
|
+
for msg in input_list:
|
26
|
+
if isinstance(msg, AIMessage) and not isinstance(
|
27
|
+
msg, AIMessageChunk
|
28
|
+
):
|
29
|
+
content = msg.content
|
30
|
+
if content:
|
31
|
+
content_list.append(content)
|
32
|
+
elif kind == "on_chat_model_stream":
|
17
33
|
content = event["data"]["chunk"].content
|
18
34
|
if content:
|
19
|
-
content_list.append(content)
|
20
35
|
info("assistant", content)
|
21
36
|
elif kind == "on_chain_start":
|
22
37
|
data = event["data"]
|
@@ -41,7 +56,7 @@ async def run_agent(agent, inputs: list):
|
|
41
56
|
tool = t["function"]["name"]
|
42
57
|
info("assistant", f"\n[调用工具:{tool}]\n\n")
|
43
58
|
|
44
|
-
return "".join(content_list)
|
59
|
+
return "\n".join(content_list)
|
45
60
|
else:
|
46
61
|
assert agent.agent_execution_mode == "ReAct"
|
47
62
|
inputs_msg = {"input": inputs[-1].content, "chat_history": inputs[:-1]}
|
@@ -59,8 +59,7 @@ def create_openai_func_call_agent_executor(
|
|
59
59
|
# If there is no FuncCall, then we finish
|
60
60
|
last_message = messages[-1]
|
61
61
|
if last_message.content.strip().endswith("```"):
|
62
|
-
|
63
|
-
last_message.content += "\n\n" # 避免影响阅读
|
62
|
+
last_message.content = last_message.content + "\n\n" # 避免影响阅读
|
64
63
|
if not last_message.tool_calls:
|
65
64
|
if (
|
66
65
|
"接下来我将" in last_message.content
|
@@ -1,12 +1,10 @@
|
|
1
1
|
import json
|
2
2
|
from langchain_openai import ChatOpenAI
|
3
|
-
from .base import import_tools
|
4
3
|
from .agent import run_agent, Runnable, INPUT_MESSAGE, output
|
5
4
|
import asyncio
|
6
5
|
from langchain_core.messages import HumanMessage
|
7
6
|
from pycoze import utils
|
8
7
|
|
9
|
-
|
10
8
|
params = utils.arg.read_params()
|
11
9
|
llm_file = params["appPath"] + "/JsonStorage/llm.json"
|
12
10
|
|
@@ -40,15 +38,26 @@ def agent_chat(bot_setting_file, history):
|
|
40
38
|
if cfg["model"].startswith("deepseek")
|
41
39
|
else role_setting["temperature"]
|
42
40
|
),
|
41
|
+
stop_sequences=[
|
42
|
+
"tool▁calls▁end",
|
43
|
+
"tool▁call▁end",
|
44
|
+
], # 停用deepseek的工具调用标记,不然会虚构工具调用过程和结果
|
43
45
|
)
|
44
|
-
|
46
|
+
prompt = role_setting["prompt"]
|
47
|
+
if cfg["model"].startswith("deepseek") and len(tools) > 0:
|
48
|
+
prompt += """
|
49
|
+
如果需要调用工具,请使用以下面json格式进行结尾:
|
50
|
+
```json
|
51
|
+
{"name": 函数名, "parameters": 参数词典}
|
52
|
+
```
|
53
|
+
"""
|
45
54
|
agent = Runnable(
|
46
55
|
agent_execution_mode=(
|
47
56
|
"ReAct" if cfg["model"] in ["command-r"] else "FuncCall"
|
48
57
|
), # 'FuncCall' or 'ReAct',大模型支持FuncCall的话就用FuncCall
|
49
58
|
tools=tools,
|
50
59
|
llm=chat,
|
51
|
-
assistant_message=
|
60
|
+
assistant_message=prompt,
|
52
61
|
)
|
53
62
|
return asyncio.run(run_agent(agent, history))
|
54
63
|
|
pycoze-0.1.36/pycoze/bot/base.py
DELETED
@@ -1,88 +0,0 @@
|
|
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
|
-
|
10
|
-
def wrapped_tool(tool, module_path):
|
11
|
-
old_tool_fun = tool.func
|
12
|
-
|
13
|
-
def _wrapped_tool(*args, **kwargs):
|
14
|
-
print(f"调用了{tool.name}")
|
15
|
-
old_path = os.getcwd()
|
16
|
-
try:
|
17
|
-
sys.path.insert(0, module_path) # 插入到第一个位置
|
18
|
-
os.chdir(module_path)
|
19
|
-
result = old_tool_fun(*args, **kwargs)
|
20
|
-
finally:
|
21
|
-
sys.path.remove(module_path)
|
22
|
-
os.chdir(old_path)
|
23
|
-
print(f"{tool.name}调用完毕,结果为:", result)
|
24
|
-
return result
|
25
|
-
|
26
|
-
return _wrapped_tool
|
27
|
-
|
28
|
-
|
29
|
-
def import_tools(tool_id):
|
30
|
-
tool_path = "../../tool"
|
31
|
-
old_path = os.getcwd()
|
32
|
-
module_path = os.path.join(tool_path, tool_id)
|
33
|
-
module_path = os.path.normpath(os.path.abspath(module_path))
|
34
|
-
|
35
|
-
if not os.path.exists(module_path):
|
36
|
-
print(f"Tool {tool_id} not found")
|
37
|
-
return []
|
38
|
-
|
39
|
-
# 保存当前的 sys.modules 状态
|
40
|
-
original_modules = sys.modules.copy()
|
41
|
-
|
42
|
-
try:
|
43
|
-
sys.path.insert(0, module_path) # 插入到第一个位置
|
44
|
-
os.chdir(module_path)
|
45
|
-
module = importlib.import_module("tool")
|
46
|
-
export_tools = getattr(module, "export_tools")
|
47
|
-
temp_list = []
|
48
|
-
for tool in export_tools:
|
49
|
-
assert isinstance(tool, langchain_core.tools.StructuredTool) or isinstance(
|
50
|
-
tool, types.FunctionType
|
51
|
-
), f"Tool is not a StructuredTool or function: {tool}"
|
52
|
-
if isinstance(tool, types.FunctionType) and not isinstance(
|
53
|
-
tool, langchain_core.tools.StructuredTool
|
54
|
-
):
|
55
|
-
temp_list.append(_tool(tool))
|
56
|
-
export_tools = temp_list
|
57
|
-
|
58
|
-
except Exception as e:
|
59
|
-
print(f"Error loading tool {tool_id}: {e}")
|
60
|
-
sys.path.remove(module_path)
|
61
|
-
os.chdir(old_path)
|
62
|
-
return []
|
63
|
-
|
64
|
-
# 卸载模块并恢复 sys.modules 状态
|
65
|
-
importlib.invalidate_caches()
|
66
|
-
for key in list(sys.modules.keys()):
|
67
|
-
if key not in original_modules:
|
68
|
-
del sys.modules[key]
|
69
|
-
|
70
|
-
sys.path.remove(module_path)
|
71
|
-
os.chdir(old_path)
|
72
|
-
|
73
|
-
for tool in export_tools:
|
74
|
-
tool.func = wrapped_tool(tool, module_path)
|
75
|
-
|
76
|
-
return export_tools
|
77
|
-
|
78
|
-
|
79
|
-
def read_arg(param: str, is_path=False):
|
80
|
-
parser = argparse.ArgumentParser()
|
81
|
-
parser.add_argument(param, nargs="?", help=f"Parameter {param}")
|
82
|
-
args = parser.parse_args()
|
83
|
-
value = getattr(args, param.lstrip("-"))
|
84
|
-
# 如果是路径并且有引号,去掉引号
|
85
|
-
if is_path and value and value.startswith('"') and value.endswith('"'):
|
86
|
-
value = value[1:-1]
|
87
|
-
|
88
|
-
return value
|
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
|