pycoze 0.1.37__py3-none-any.whl → 0.1.39__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/agent.py CHANGED
@@ -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 == "on_chat_model_stream":
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
- print("添加末尾换行")
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
@@ -159,7 +158,7 @@ def create_openai_func_call_agent_executor(
159
158
  tool_messages = [
160
159
  ToolMessage(
161
160
  tool_call_id=tool_call["id"],
162
- content="\n\n" + response,
161
+ content=response,
163
162
  additional_kwargs={"name": tool_call["function"]["name"]},
164
163
  )
165
164
  for tool_call, response in zip(
pycoze/bot/bot.py CHANGED
@@ -1,11 +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
+ from pycoze.access.
9
8
 
10
9
  params = utils.arg.read_params()
11
10
  llm_file = params["appPath"] + "/JsonStorage/llm.json"
@@ -40,15 +39,26 @@ def agent_chat(bot_setting_file, history):
40
39
  if cfg["model"].startswith("deepseek")
41
40
  else role_setting["temperature"]
42
41
  ),
42
+ stop_sequences=[
43
+ "tool▁calls▁end",
44
+ "tool▁call▁end",
45
+ ], # 停用deepseek的工具调用标记,不然会虚构工具调用过程和结果
43
46
  )
44
-
47
+ prompt = role_setting["prompt"]
48
+ if cfg["model"].startswith("deepseek") and len(tools) > 0:
49
+ prompt += """
50
+ 如果需要调用工具,请使用以下面json格式进行结尾:
51
+ ```json
52
+ {"name": 函数名, "parameters": 参数词典}
53
+ ```
54
+ """
45
55
  agent = Runnable(
46
56
  agent_execution_mode=(
47
57
  "ReAct" if cfg["model"] in ["command-r"] else "FuncCall"
48
58
  ), # 'FuncCall' or 'ReAct',大模型支持FuncCall的话就用FuncCall
49
59
  tools=tools,
50
60
  llm=chat,
51
- assistant_message=role_setting["prompt"],
61
+ assistant_message=prompt,
52
62
  )
53
63
  return asyncio.run(run_agent(agent, history))
54
64
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.37
3
+ Version: 0.1.39
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -1,14 +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/bot/__init__.py,sha256=pciDtfcIXda7iFt9uI5Fpm0JKpGBhdXHmJv4966WTVU,21
4
- pycoze/bot/base.py,sha256=GWYDVGGtiCpk6gv-163cAbDid_IsnMe5jTj7eZUMJQU,2679
5
- pycoze/bot/bot.py,sha256=RPSlKy9YjKrhrTHiaefqvORXD8PTZ6BC-HOw2w5CcEY,2048
4
+ pycoze/bot/bot.py,sha256=Ee7u0fp_pE-dY6HXJO-z6JOVGNCGZ8FKFqMyo4Rd8zs,2478
6
5
  pycoze/bot/agent/__init__.py,sha256=IaYqQCJ3uBor92JdOxI_EY4HtYOHgej8lijr3UrN1Vc,161
7
- pycoze/bot/agent/agent.py,sha256=hxuNNdSrNUhw6FS7xwT4LWFsAS3adOkdspxibL8oOro,3625
6
+ pycoze/bot/agent/agent.py,sha256=uEkPpHX3xODlDjj2Qz90N9bSdCIQzW9wHxOpxvIegzo,4367
8
7
  pycoze/bot/agent/assistant.py,sha256=QLeWaPi415P9jruYOm8qcIbC94cXXAhJYmLTkyC9NTQ,1267
9
8
  pycoze/bot/agent/chat.py,sha256=kc0qgcrBSXdiMy49JwThZTV-0PAvzAhiUvbI5ILiSnU,571
10
9
  pycoze/bot/agent/agent_types/__init__.py,sha256=W2jTNMLqUMqgCMG0Tw0d8n7WpsbsnIonqaPR-YLegLU,210
11
- pycoze/bot/agent/agent_types/openai_func_call_agent.py,sha256=BJZCqNlDvEYcsesMDpfqRLC53ErYqrImtSsFIUNIECE,8234
10
+ pycoze/bot/agent/agent_types/openai_func_call_agent.py,sha256=EaK0CL69teGuKsoYUrvcvDhcSWLLFyJEL0SHTGpze2E,8207
12
11
  pycoze/bot/agent/agent_types/react_agent.py,sha256=AnjHwHXVwLAm77ndglJGi4rQhqDGWaLuUfl46uZVSzM,6749
13
12
  pycoze/bot/agent/agent_types/react_prompt.py,sha256=jyovokGaPzNIe5bvTRvn0gmsWLx5kpDIPmRwmEMCl-M,2142
14
13
  pycoze/gpu/__init__.py,sha256=cuxwDdz2Oo-VcwZ50FtFtEIJXdqoz2el-n0QpSt_NMc,75
@@ -21,8 +20,8 @@ pycoze/ui/ui_def.py,sha256=CNFYH8NC-WYmbceIPpxsRr9H6O006pMKukx7U-BOE1Q,3744
21
20
  pycoze/utils/__init__.py,sha256=KExBkotf23dr2NfTEouWke5nJB1q2IuDXgHrmuyd95k,73
22
21
  pycoze/utils/arg.py,sha256=rRujm1zKc0XlnNlpIJ6JAAaFiTzDGmL_RliIpSc5OD8,724
23
22
  pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
24
- pycoze-0.1.37.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
25
- pycoze-0.1.37.dist-info/METADATA,sha256=y1TqI51EycK-8nChh0Koi7v-7yEcDLjkzF9P1p4pvLw,719
26
- pycoze-0.1.37.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
27
- pycoze-0.1.37.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
28
- pycoze-0.1.37.dist-info/RECORD,,
23
+ pycoze-0.1.39.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
24
+ pycoze-0.1.39.dist-info/METADATA,sha256=61sziXkjvfW6k9mcCOpZptP_SHDkMcoiaZzsS60CHyY,719
25
+ pycoze-0.1.39.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
26
+ pycoze-0.1.39.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
27
+ pycoze-0.1.39.dist-info/RECORD,,
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