pycoze 0.1.10__tar.gz → 0.1.14__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.
Files changed (30) hide show
  1. {pycoze-0.1.10 → pycoze-0.1.14}/PKG-INFO +1 -1
  2. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/bot/agent/agent_types/react_agent.py +3 -3
  3. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/bot/base.py +81 -79
  4. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/bot/bot.py +54 -50
  5. pycoze-0.1.14/pycoze/utils/__init__.py +1 -0
  6. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze.egg-info/PKG-INFO +1 -1
  7. {pycoze-0.1.10 → pycoze-0.1.14}/setup.py +19 -19
  8. pycoze-0.1.10/pycoze/utils/__init__.py +0 -1
  9. {pycoze-0.1.10 → pycoze-0.1.14}/LICENSE +0 -0
  10. {pycoze-0.1.10 → pycoze-0.1.14}/README.md +0 -0
  11. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/__init__.py +0 -0
  12. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/bot/__init__.py +0 -0
  13. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/bot/agent/__init__.py +0 -0
  14. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/bot/agent/agent.py +0 -0
  15. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/bot/agent/agent_types/__init__.py +0 -0
  16. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/bot/agent/agent_types/openai_func_call_agent.py +0 -0
  17. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/bot/agent/agent_types/react_prompt.py +0 -0
  18. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/bot/agent/assistant.py +0 -0
  19. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/bot/agent/chat.py +0 -0
  20. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/module.py +0 -0
  21. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/ui/__init__.py +0 -0
  22. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/ui/base.py +0 -0
  23. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/ui/color.py +0 -0
  24. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/ui/typ.py +0 -0
  25. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/ui/ui_def.py +0 -0
  26. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze/utils/arg.py +0 -0
  27. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze.egg-info/SOURCES.txt +0 -0
  28. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze.egg-info/dependency_links.txt +0 -0
  29. {pycoze-0.1.10 → pycoze-0.1.14}/pycoze.egg-info/top_level.txt +0 -0
  30. {pycoze-0.1.10 → pycoze-0.1.14}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.10
3
+ Version: 0.1.14
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -8,15 +8,15 @@ from langchain_core.language_models import LanguageModelLike
8
8
  from langgraph.graph import END, StateGraph
9
9
  from langgraph.graph.state import CompiledStateGraph
10
10
  from langgraph.prebuilt.tool_executor import ToolExecutor
11
- from langgraph.utils import RunnableCallable
11
+ from langgraph.utils.runnable import RunnableCallable
12
12
  from langchain.agents import create_structured_chat_agent
13
13
  from .react_prompt import react_agent_prompt
14
14
 
15
15
 
16
16
  def create_react_agent_executor(
17
- tools: list[BaseTool],
17
+ tools: list[BaseTool],
18
18
  llm: LanguageModelLike,
19
- system_message: str,
19
+ system_message: str,
20
20
  **kwargs # ignore
21
21
  ):
22
22
  prompt = react_agent_prompt.partial(assistant_message=system_message)
@@ -1,79 +1,81 @@
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
- def wrapped_tool(tool, module_path):
10
- old_tool_fun = tool.func
11
- def _wrapped_tool(*args, **kwargs):
12
- print(f"调用了{tool.name}")
13
- old_path = os.getcwd()
14
- try:
15
- sys.path.insert(0, module_path) # 插入到第一个位置
16
- os.chdir(module_path)
17
- result = old_tool_fun(*args, **kwargs)
18
- finally:
19
- sys.path.remove(module_path)
20
- os.chdir(old_path)
21
- print(f"{tool.name}调用完毕,结果为:", result)
22
- return result
23
- return _wrapped_tool
24
-
25
-
26
- def import_tools(tool_id):
27
- tool_path = "../../tool"
28
- old_path = os.getcwd()
29
- module_path = os.path.join(tool_path, tool_id)
30
- module_path = os.path.normpath(os.path.abspath(module_path))
31
-
32
- if not os.path.exists(module_path):
33
- return []
34
-
35
- # 保存当前的 sys.modules 状态
36
- original_modules = sys.modules.copy()
37
-
38
- try:
39
- sys.path.insert(0, module_path) # 插入到第一个位置
40
- os.chdir(module_path)
41
- module = importlib.import_module("tool")
42
- export_tools = getattr(module, "export_tools")
43
- temp_list = []
44
- for tool in export_tools:
45
- assert isinstance(tool, langchain_core.tools.StructuredTool) or isinstance(tool, types.FunctionType), f"Tool is not a StructuredTool or function: {tool}"
46
- if isinstance(tool, types.FunctionType) and not isinstance(tool, langchain_core.tools.StructuredTool):
47
- temp_list.append(_tool(tool))
48
- export_tools = temp_list
49
-
50
- except Exception as e:
51
- sys.path.remove(module_path)
52
- os.chdir(old_path)
53
- return []
54
-
55
- # 卸载模块并恢复 sys.modules 状态
56
- importlib.invalidate_caches()
57
- for key in list(sys.modules.keys()):
58
- if key not in original_modules:
59
- del sys.modules[key]
60
-
61
- sys.path.remove(module_path)
62
- os.chdir(old_path)
63
-
64
- for tool in export_tools:
65
- tool.func = wrapped_tool(tool, module_path)
66
-
67
- return export_tools
68
-
69
-
70
- def read_arg(param: str, is_path=False):
71
- parser = argparse.ArgumentParser()
72
- parser.add_argument(param, nargs='?', help=f'Parameter {param}')
73
- args = parser.parse_args()
74
- value = getattr(args, param.lstrip('-'))
75
- # 如果是路径并且有引号,去掉引号
76
- if is_path and value and value.startswith('"') and value.endswith('"'):
77
- value = value[1:-1]
78
-
79
- return value
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
+ def wrapped_tool(tool, module_path):
10
+ old_tool_fun = tool.func
11
+ def _wrapped_tool(*args, **kwargs):
12
+ print(f"调用了{tool.name}")
13
+ old_path = os.getcwd()
14
+ try:
15
+ sys.path.insert(0, module_path) # 插入到第一个位置
16
+ os.chdir(module_path)
17
+ result = old_tool_fun(*args, **kwargs)
18
+ finally:
19
+ sys.path.remove(module_path)
20
+ os.chdir(old_path)
21
+ print(f"{tool.name}调用完毕,结果为:", result)
22
+ return result
23
+ return _wrapped_tool
24
+
25
+
26
+ def import_tools(tool_id):
27
+ tool_path = "../../tool"
28
+ old_path = os.getcwd()
29
+ module_path = os.path.join(tool_path, tool_id)
30
+ module_path = os.path.normpath(os.path.abspath(module_path))
31
+
32
+ if not os.path.exists(module_path):
33
+ print(f"Tool {tool_id} not found")
34
+ return []
35
+
36
+ # 保存当前的 sys.modules 状态
37
+ original_modules = sys.modules.copy()
38
+
39
+ try:
40
+ sys.path.insert(0, module_path) # 插入到第一个位置
41
+ os.chdir(module_path)
42
+ module = importlib.import_module("tool")
43
+ export_tools = getattr(module, "export_tools")
44
+ temp_list = []
45
+ for tool in export_tools:
46
+ assert isinstance(tool, langchain_core.tools.StructuredTool) or isinstance(tool, types.FunctionType), f"Tool is not a StructuredTool or function: {tool}"
47
+ if isinstance(tool, types.FunctionType) and not isinstance(tool, langchain_core.tools.StructuredTool):
48
+ temp_list.append(_tool(tool))
49
+ export_tools = temp_list
50
+
51
+ except Exception as e:
52
+ print(f"Error loading tool {tool_id}: {e}")
53
+ sys.path.remove(module_path)
54
+ os.chdir(old_path)
55
+ return []
56
+
57
+ # 卸载模块并恢复 sys.modules 状态
58
+ importlib.invalidate_caches()
59
+ for key in list(sys.modules.keys()):
60
+ if key not in original_modules:
61
+ del sys.modules[key]
62
+
63
+ sys.path.remove(module_path)
64
+ os.chdir(old_path)
65
+
66
+ for tool in export_tools:
67
+ tool.func = wrapped_tool(tool, module_path)
68
+
69
+ return export_tools
70
+
71
+
72
+ def read_arg(param: str, is_path=False):
73
+ parser = argparse.ArgumentParser()
74
+ parser.add_argument(param, nargs='?', help=f'Parameter {param}')
75
+ args = parser.parse_args()
76
+ value = getattr(args, param.lstrip('-'))
77
+ # 如果是路径并且有引号,去掉引号
78
+ if is_path and value and value.startswith('"') and value.endswith('"'):
79
+ value = value[1:-1]
80
+
81
+ return value
@@ -1,50 +1,54 @@
1
- import json
2
- from langchain_openai import ChatOpenAI
3
- from .base import import_tools
4
- from .agent import run_agent, Runnable, INPUT_MESSAGE, output
5
- import asyncio
6
- from langchain_core.messages import HumanMessage
7
-
8
-
9
- def load_role_setting(bot_setting_file:str):
10
- with open(bot_setting_file, "r", encoding="utf-8") as f:
11
- return json.load(f)
12
-
13
- def load_tools(bot_setting_file:str):
14
- with open(bot_setting_file, "r", encoding="utf-8") as f:
15
- role_setting = json.load(f)
16
-
17
- tools = []
18
- for tool_id in role_setting["tools"]:
19
- tools.extend(import_tools(tool_id))
20
- return tools
21
-
22
-
23
-
24
-
25
- def chat(bot_setting_file:str, llm_file:str):
26
- history = []
27
-
28
- while True:
29
- message = input()
30
- role_setting = load_role_setting(bot_setting_file)
31
- tools = load_tools(bot_setting_file)
32
- if not message.startswith(INPUT_MESSAGE):
33
- raise ValueError("Invalid message")
34
- message = json.loads(message[len(INPUT_MESSAGE):])["content"]
35
- print("user:", message)
36
-
37
- with open(llm_file, "r", encoding="utf-8") as f:
38
- cfg = json.load(f)
39
- chat = ChatOpenAI(api_key=cfg["apiKey"], base_url=cfg['baseURL'], model=cfg["model"], temperature=role_setting["temperature"])
40
-
41
-
42
- agent = Runnable(agent_execution_mode='FuncCall', # 'FuncCall' or 'ReAct',大模型支持FuncCall的话就用FuncCall
43
- tools=tools,
44
- llm=chat,
45
- assistant_message=role_setting["prompt"],)
46
-
47
- history += [HumanMessage(content=message)]
48
- result = asyncio.run(run_agent(agent, history))
49
- output("assistant", result, history)
50
-
1
+ import json
2
+ from langchain_openai import ChatOpenAI
3
+ from .base import import_tools
4
+ from .agent import run_agent, Runnable, INPUT_MESSAGE, output
5
+ import asyncio
6
+ from langchain_core.messages import HumanMessage
7
+ from pycoze import utils
8
+
9
+
10
+ params = utils.arg.read_params()
11
+ llm_file = params["appPath"] + "/JsonStorage/llm.json"
12
+
13
+ def load_role_setting(bot_setting_file:str):
14
+ with open(bot_setting_file, "r", encoding="utf-8") as f:
15
+ return json.load(f)
16
+
17
+ def load_tools(bot_setting_file:str):
18
+ with open(bot_setting_file, "r", encoding="utf-8") as f:
19
+ role_setting = json.load(f)
20
+
21
+ tools = []
22
+ for tool_id in role_setting["tools"]:
23
+ tools.extend(import_tools(tool_id))
24
+ return tools
25
+
26
+
27
+
28
+
29
+ def chat(bot_setting_file:str):
30
+ history = []
31
+
32
+ while True:
33
+ message = input()
34
+ role_setting = load_role_setting(bot_setting_file)
35
+ tools = load_tools(bot_setting_file)
36
+ if not message.startswith(INPUT_MESSAGE):
37
+ raise ValueError("Invalid message")
38
+ message = json.loads(message[len(INPUT_MESSAGE):])["content"]
39
+ print("user:", message)
40
+
41
+ with open(llm_file, "r", encoding="utf-8") as f:
42
+ cfg = json.load(f)
43
+ chat = ChatOpenAI(api_key=cfg["apiKey"], base_url=cfg['baseURL'], model=cfg["model"], temperature=role_setting["temperature"])
44
+
45
+
46
+ agent = Runnable(agent_execution_mode='FuncCall', # 'FuncCall' or 'ReAct',大模型支持FuncCall的话就用FuncCall
47
+ tools=tools,
48
+ llm=chat,
49
+ assistant_message=role_setting["prompt"],)
50
+
51
+ history += [HumanMessage(content=message)]
52
+ result = asyncio.run(run_agent(agent, history))
53
+ output("assistant", result, history)
54
+
@@ -0,0 +1 @@
1
+ from .arg import read_arg, read_params
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.10
3
+ Version: 0.1.14
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -1,19 +1,19 @@
1
- from setuptools import setup, find_packages
2
-
3
- setup(
4
- name="pycoze",
5
- version="0.1.10",
6
- packages=find_packages(),
7
- install_requires=[],
8
- author="Yuan Jie Xiong",
9
- author_email="aiqqqqqqq@qq.com",
10
- description="Package for pycoze only!",
11
- long_description=open('README.md', encoding="utf-8").read(),
12
- long_description_content_type='text/markdown',
13
- classifiers=[
14
- "Programming Language :: Python :: 3",
15
- "License :: OSI Approved :: MIT License",
16
- "Operating System :: OS Independent",
17
- ],
18
- python_requires='>=3.6',
19
- )
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name="pycoze",
5
+ version="0.1.14",
6
+ packages=find_packages(),
7
+ install_requires=[],
8
+ author="Yuan Jie Xiong",
9
+ author_email="aiqqqqqqq@qq.com",
10
+ description="Package for pycoze only!",
11
+ long_description=open('README.md', encoding="utf-8").read(),
12
+ long_description_content_type='text/markdown',
13
+ classifiers=[
14
+ "Programming Language :: Python :: 3",
15
+ "License :: OSI Approved :: MIT License",
16
+ "Operating System :: OS Independent",
17
+ ],
18
+ python_requires='>=3.6',
19
+ )
@@ -1 +0,0 @@
1
- from .arg import read_arg
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