pycoze 0.1.85__tar.gz → 0.1.87__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. {pycoze-0.1.85 → pycoze-0.1.87}/PKG-INFO +1 -1
  2. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/bot/agent/agent.py +1 -0
  3. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/bot/agent/agent_types/openai_func_call_agent.py +19 -26
  4. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/bot/agent/assistant.py +2 -1
  5. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/bot/bot.py +21 -18
  6. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze.egg-info/PKG-INFO +1 -1
  7. {pycoze-0.1.85 → pycoze-0.1.87}/setup.py +1 -1
  8. {pycoze-0.1.85 → pycoze-0.1.87}/LICENSE +0 -0
  9. {pycoze-0.1.85 → pycoze-0.1.87}/README.md +0 -0
  10. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/__init__.py +0 -0
  11. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/access/__init__.py +0 -0
  12. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/access/tool_for_bot.py +0 -0
  13. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/ai/__init__.py +0 -0
  14. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/ai/comfyui.py +0 -0
  15. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/ai/vram_reserve.py +0 -0
  16. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/bot/__init__.py +0 -0
  17. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/bot/agent/__init__.py +0 -0
  18. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/bot/agent/agent_types/__init__.py +0 -0
  19. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/bot/agent/chat.py +0 -0
  20. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/module.py +0 -0
  21. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/ui/__init__.py +0 -0
  22. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/ui/base.py +0 -0
  23. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/ui/color.py +0 -0
  24. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/ui/typ.py +0 -0
  25. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/ui/ui_def.py +0 -0
  26. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/utils/__init__.py +0 -0
  27. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/utils/arg.py +0 -0
  28. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze/utils/text_or_file.py +0 -0
  29. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze.egg-info/SOURCES.txt +0 -0
  30. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze.egg-info/dependency_links.txt +0 -0
  31. {pycoze-0.1.85 → pycoze-0.1.87}/pycoze.egg-info/top_level.txt +0 -0
  32. {pycoze-0.1.85 → pycoze-0.1.87}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.85
3
+ Version: 0.1.87
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -83,6 +83,7 @@ if __name__ == "__main__":
83
83
  tools=[python_tool],
84
84
  llm=chat,
85
85
  assistant_message="请以女友的口吻回答,输出不小于100字,可以随便说点其他的",
86
+ tool_compatibility_mode=False,
86
87
  )
87
88
 
88
89
  inputs = [HumanMessage(content="计算根号7+根号88")]
@@ -4,7 +4,7 @@ import json
4
4
  from langchain.tools import BaseTool
5
5
  from langchain_core.utils.function_calling import convert_to_openai_tool
6
6
  from langchain_core.language_models.base import LanguageModelLike
7
- from langchain_core.messages import SystemMessage, ToolMessage
7
+ from langchain_core.messages import SystemMessage, ToolMessage, HumanMessage
8
8
  from langgraph.graph import END
9
9
  from langgraph.graph.message import MessageGraph
10
10
  from langgraph.prebuilt import ToolExecutor, ToolInvocation
@@ -31,7 +31,11 @@ def get_all_markdown_json(content):
31
31
 
32
32
 
33
33
  def create_openai_func_call_agent_executor(
34
- tools: list[BaseTool], llm: LanguageModelLike, system_message: str, **kwargs
34
+ tools: list[BaseTool],
35
+ llm: LanguageModelLike,
36
+ system_message: str,
37
+ tool_compatibility_mode: str,
38
+ **kwargs
35
39
  ):
36
40
 
37
41
  async def _get_messages(messages):
@@ -58,7 +62,6 @@ def create_openai_func_call_agent_executor(
58
62
  def should_continue(messages):
59
63
  # If there is no FuncCall, then we finish
60
64
  last_message = messages[-1]
61
- print("last_message", last_message)
62
65
  if last_message.content.strip().endswith("```"):
63
66
  last_message.content = last_message.content + "\n\n" # 避免影响阅读
64
67
  # if not last_message.tool_calls:
@@ -128,7 +131,6 @@ def create_openai_func_call_agent_executor(
128
131
  # Based on the continue condition
129
132
  # we know the last message involves a FuncCall
130
133
  last_message = messages[-1]
131
- print("last_message", last_message)
132
134
  for tool_call in last_message.additional_kwargs["tool_calls"]:
133
135
  function = tool_call["function"]
134
136
  function_name = function["name"]
@@ -167,44 +169,35 @@ def create_openai_func_call_agent_executor(
167
169
  additional_kwargs={"name": tool_call["function"]["name"]},
168
170
  )
169
171
  tool_messages.append(message)
172
+ if tool_compatibility_mode:
173
+ # HumanMessage
174
+ tool_msgs_str = repr(tool_messages)
175
+ tool_messages = [
176
+ HumanMessage(
177
+ content="The tool call is done, the result is as follows:\n"
178
+ + tool_msgs_str
179
+ )
180
+ ]
170
181
  return tool_messages
171
182
 
172
183
  workflow = MessageGraph()
173
184
 
174
185
  # Define the two nodes we will cycle between
175
186
  workflow.add_node("agent", agent)
176
- workflow.add_node("action", call_tool)
187
+ workflow.add_node("call_tool", call_tool)
177
188
 
178
- # Set the entrypoint as `agent`
179
- # This means that this node is the first one called
180
189
  workflow.set_entry_point("agent")
181
190
 
182
- # We now add a conditional edge
183
191
  workflow.add_conditional_edges(
184
- # First, we define the start node. We use `agent`.
185
- # This means these are the edges taken after the `agent` node is called.
186
192
  "agent",
187
- # Next, we pass in the function that will determine which node is called next.
188
193
  should_continue,
189
- # Finally we pass in a mapping.
190
- # The keys are strings, and the values are other nodes.
191
- # END is a special node marking that the graph should finish.
192
- # What will happen is we will call `should_continue`, and then the output of that
193
- # will be matched against the keys in this mapping.
194
- # Based on which one it matches, that node will then be called.
195
194
  {
196
- # If `tools`, then we call the tool node.
197
- "continue": "action",
198
- # Otherwise we finish.
195
+ "continue": "call_tool",
199
196
  "end": END,
200
197
  },
201
198
  )
202
199
 
203
- # We now add a normal edge from `tools` to `agent`.
204
- # This means that after `tools` is called, `agent` node is called next.
205
- workflow.add_edge("action", "agent")
200
+ # 调用完工具后,再次调用agent
201
+ workflow.add_edge("call_tool", "agent")
206
202
 
207
- # Finally, we compile it!
208
- # This compiles it into a LangChain Runnable,
209
- # meaning you can use it as you would any other runnable
210
203
  return workflow.compile()
@@ -18,10 +18,11 @@ class Runnable(RunnableBinding):
18
18
  tools: Sequence[BaseTool],
19
19
  llm: LanguageModelLike,
20
20
  assistant_message: str,
21
+ tool_compatibility_mode: bool
21
22
  ) -> None:
22
23
 
23
24
  agent_executor = create_openai_func_call_agent_executor(
24
- tools, llm, assistant_message
25
+ tools, llm, assistant_message, tool_compatibility_mode
25
26
  )
26
27
  agent_executor = agent_executor.with_config({"recursion_limit": 50})
27
28
  super().__init__(
@@ -9,6 +9,8 @@ from langchain_core.utils.function_calling import convert_to_openai_tool
9
9
 
10
10
  params = utils.arg.read_params_file()
11
11
  llm_file = params["appPath"] + "/JsonStorage/llm.json"
12
+ with open(llm_file, "r", encoding="utf-8") as f:
13
+ cfg = json.load(f)
12
14
 
13
15
 
14
16
  def load_role_setting(bot_setting_file: str):
@@ -29,30 +31,30 @@ def load_tools(bot_setting_file: str):
29
31
  def agent_chat(bot_setting_file, history):
30
32
  role_setting = load_role_setting(bot_setting_file)
31
33
  tools = load_tools(bot_setting_file)
32
- with open(llm_file, "r", encoding="utf-8") as f:
33
- cfg = json.load(f)
34
- chat = ChatOpenAI(
35
- api_key=cfg["apiKey"],
36
- base_url=cfg["baseURL"],
37
- model=cfg["model"],
38
- temperature=(
39
- role_setting["temperature"] * 2
40
- if cfg["model"].startswith("deepseek") or cfg["model"].startswith("yi-")
41
- else role_setting["temperature"]
42
- ),
43
- stop_sequences=[
44
- "tool▁calls▁end",
45
- "tool▁call▁end",
46
- ], # 停用deepseek的工具调用标记,不然会虚构工具调用过程和结果
47
- )
34
+
35
+ chat = ChatOpenAI(
36
+ api_key=cfg["apiKey"],
37
+ base_url=cfg["baseURL"],
38
+ model=cfg["model"],
39
+ temperature=(
40
+ role_setting["temperature"] * 2
41
+ if cfg["model"].startswith("deepseek")
42
+ else role_setting["temperature"]
43
+ ),
44
+ stop_sequences=[
45
+ "tool▁calls▁end",
46
+ "tool▁call▁end",
47
+ ], # 停用deepseek的工具调用标记,不然会虚构工具调用过程和结果
48
+ )
48
49
  prompt = role_setting["prompt"]
49
50
  if (
50
51
  cfg["model"].startswith("deepseek")
51
- or cfg["model"].startswith("yi-")
52
+ or cfg["toolCompatibilityMode"]
52
53
  and len(tools) > 0
53
54
  ):
54
55
  prompt += """
55
- 如果需要调用工具,请使用以正确的json格式进行结尾(务必保证json格式正确,不要出现反斜杠未转义等问题):
56
+ 如果不确定结果,请务必使用工具查询。
57
+ 如果需要调用工具,请使用以正确markdown中的json代码格式进行结尾(务必保证json格式正确,不要出现反斜杠未转义等问题):
56
58
  ```json
57
59
  {"name": 函数名, "parameters": 参数词典}
58
60
  ```
@@ -66,6 +68,7 @@ def agent_chat(bot_setting_file, history):
66
68
  tools=tools,
67
69
  llm=chat,
68
70
  assistant_message=prompt,
71
+ tool_compatibility_mode=cfg["toolCompatibilityMode"],
69
72
  )
70
73
  return asyncio.run(run_agent(agent, history))
71
74
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.85
3
+ Version: 0.1.87
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="pycoze",
5
- version="0.1.85",
5
+ version="0.1.87",
6
6
  packages=find_packages(),
7
7
  install_requires=[],
8
8
  author="Yuan Jie Xiong",
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