pycoze 0.1.0__py3-none-any.whl → 0.1.3__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/__init__.py CHANGED
@@ -1,2 +1,2 @@
1
- def hello():
2
- return "Hello, World!"
1
+ from . import bot
2
+ from . import utils
pycoze/bot/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from .bot import chat
@@ -0,0 +1,5 @@
1
+ from .agent import run_agent
2
+ from .assistant import Runnable
3
+ from .chat import INPUT_MESSAGE, output
4
+
5
+ __all__ = [run_agent, Runnable, INPUT_MESSAGE, output]
@@ -0,0 +1,67 @@
1
+ import asyncio
2
+ import json
3
+ from langchain_openai import ChatOpenAI
4
+ from .chat import info
5
+ from .assistant import Runnable
6
+ from langchain_core.messages import HumanMessage, AIMessage,AIMessageChunk
7
+ from langchain_core.agents import AgentFinish
8
+
9
+
10
+
11
+ async def run_agent(agent, inputs: list):
12
+ if agent.agent_execution_mode == 'FuncCall':
13
+ content_list = []
14
+ async for event in agent.astream_events(inputs, version="v2"):
15
+ kind = event["event"]
16
+ if kind == "on_chat_model_stream":
17
+ content = event["data"]["chunk"].content
18
+ if content:
19
+ content_list.append(content)
20
+ info("assistant", content)
21
+ elif kind == "on_chain_start":
22
+ data = event["data"]
23
+ if "input" in data:
24
+ input_list = data["input"] if isinstance(data["input"], list) else [data["input"]]
25
+ msg = input_list[-1]
26
+ if isinstance(msg, AIMessage) and not isinstance(msg, AIMessageChunk):
27
+ if "tool_calls" in msg.additional_kwargs:
28
+ tools = [t["function"]["name"] for t in msg.additional_kwargs["tool_calls"]]
29
+ tools_str = ",".join(tools)
30
+ info("assistant", f"(调用工具:{tools_str})")
31
+
32
+ return "".join(content_list)
33
+ else:
34
+ assert agent.agent_execution_mode == 'ReAct'
35
+ inputs_msg = {'input': inputs[-1].content,'chat_history': inputs[:-1]}
36
+ use_tools = []
37
+ async for event in agent.astream_events(inputs_msg, version="v2"):
38
+ kind = event["event"]
39
+ result = None
40
+ if kind == "on_chain_end":
41
+ if 'data' in event:
42
+ if 'output' in event['data']:
43
+ output = event['data']['output']
44
+ if 'agent_outcome' in output and "input" in output:
45
+ outcome = output['agent_outcome']
46
+ if isinstance(outcome, AgentFinish):
47
+ result = outcome.return_values['output']
48
+ elif kind == "on_tool_start":
49
+ use_tools.append(event['name'])
50
+ info("assistant", f"(调用工具:{use_tools})")
51
+ return result
52
+
53
+
54
+ if __name__ == "__main__":
55
+ from langchain_experimental.tools import PythonREPLTool
56
+ llm_file = r"C:\Users\aiqqq\AppData\Roaming\pycoze\JsonStorage\llm.json"
57
+ with open(llm_file, "r", encoding="utf-8") as f:
58
+ cfg = json.load(f)
59
+ chat = ChatOpenAI(api_key=cfg["apiKey"], base_url=cfg['baseURL'], model=cfg["model"], temperature=0)
60
+ python_tool = PythonREPLTool()
61
+ agent = Runnable(agent_execution_mode='FuncCall', # 'FuncCall' or 'ReAct',大模型支持FuncCall的话就用FuncCall
62
+ tools=[python_tool],
63
+ llm=chat,
64
+ assistant_message="请以女友的口吻回答,输出不小于100字,可以随便说点其他的",)
65
+
66
+ inputs = [HumanMessage(content="计算根号7+根号88")]
67
+ print(asyncio.run(run_agent(agent, inputs)))
@@ -0,0 +1,10 @@
1
+ from .openai_func_call_agent import (
2
+ create_openai_func_call_agent_executor
3
+ )
4
+ from .react_agent import create_react_agent_executor
5
+
6
+
7
+ __all__ = [
8
+ create_openai_func_call_agent_executor,
9
+ create_react_agent_executor
10
+ ]
@@ -0,0 +1,113 @@
1
+ # reference:https://github.com/maxtheman/opengpts/blob/d3425b1ba80aec48953a327ecd9a61b80efb0e69/backend/app/agent_types/openai_agent.py
2
+ import json
3
+
4
+ from langchain.tools import BaseTool
5
+ from langchain_core.utils.function_calling import convert_to_openai_tool
6
+ from langchain_core.language_models.base import LanguageModelLike
7
+ from langchain_core.messages import SystemMessage, ToolMessage
8
+ from langgraph.graph import END
9
+ from langgraph.graph.message import MessageGraph
10
+ from langgraph.prebuilt import ToolExecutor, ToolInvocation
11
+ from typing import Any
12
+
13
+
14
+ def create_openai_func_call_agent_executor(tools: list[BaseTool], llm: LanguageModelLike,
15
+ system_message: str, **kwargs):
16
+
17
+ async def _get_messages(messages):
18
+ msgs = []
19
+ for m in messages:
20
+ if isinstance(m, ToolMessage):
21
+ _dict = m.dict()
22
+ _dict['content'] = str(_dict['content'])
23
+ m_c = ToolMessage(**_dict)
24
+ msgs.append(m_c)
25
+ else:
26
+ msgs.append(m)
27
+
28
+ return [SystemMessage(content=system_message)] + msgs
29
+
30
+ if tools:
31
+ llm_with_tools = llm.bind(tools=[convert_to_openai_tool(t) for t in tools])
32
+ else:
33
+ llm_with_tools = llm
34
+ agent = _get_messages | llm_with_tools
35
+ tool_executor = ToolExecutor(tools)
36
+
37
+ # Define the function that determines whether to continue or not
38
+ def should_continue(messages):
39
+ # If there is no FuncCall, then we finish
40
+ last_message = messages[-1]
41
+ if not last_message.tool_calls:
42
+ return "end"
43
+ # Otherwise if there is, we continue
44
+ else:
45
+ return "continue"
46
+
47
+ # Define the function to execute tools
48
+ async def call_tool(messages):
49
+ actions: list[ToolInvocation] = []
50
+ # Based on the continue condition
51
+ # we know the last message involves a FuncCall
52
+ last_message = messages[-1]
53
+ for tool_call in last_message.additional_kwargs['tool_calls']:
54
+ function = tool_call['function']
55
+ function_name = function['name']
56
+ _tool_input = json.loads(function['arguments'] or '{}')
57
+ # We construct an ToolInvocation from the function_call
58
+ actions.append(ToolInvocation(
59
+ tool=function_name,
60
+ tool_input=_tool_input,
61
+ ))
62
+ # We call the tool_executor and get back a response
63
+ responses = await tool_executor.abatch(actions, **kwargs)
64
+ # We use the response to create a ToolMessage
65
+ tool_messages = [
66
+ ToolMessage(
67
+ tool_call_id=tool_call['id'],
68
+ content=response,
69
+ additional_kwargs={'name': tool_call['function']['name']},
70
+ )
71
+ for tool_call, response in zip(last_message.additional_kwargs['tool_calls'], responses)
72
+ ]
73
+ return tool_messages
74
+
75
+ workflow = MessageGraph()
76
+
77
+ # Define the two nodes we will cycle between
78
+ workflow.add_node('agent', agent)
79
+ workflow.add_node('action', call_tool)
80
+
81
+ # Set the entrypoint as `agent`
82
+ # This means that this node is the first one called
83
+ workflow.set_entry_point('agent')
84
+
85
+ # We now add a conditional edge
86
+ workflow.add_conditional_edges(
87
+ # First, we define the start node. We use `agent`.
88
+ # This means these are the edges taken after the `agent` node is called.
89
+ 'agent',
90
+ # Next, we pass in the function that will determine which node is called next.
91
+ should_continue,
92
+ # Finally we pass in a mapping.
93
+ # The keys are strings, and the values are other nodes.
94
+ # END is a special node marking that the graph should finish.
95
+ # What will happen is we will call `should_continue`, and then the output of that
96
+ # will be matched against the keys in this mapping.
97
+ # Based on which one it matches, that node will then be called.
98
+ {
99
+ # If `tools`, then we call the tool node.
100
+ 'continue': 'action',
101
+ # Otherwise we finish.
102
+ 'end': END,
103
+ },
104
+ )
105
+
106
+ # We now add a normal edge from `tools` to `agent`.
107
+ # This means that after `tools` is called, `agent` node is called next.
108
+ workflow.add_edge('action', 'agent')
109
+
110
+ # Finally, we compile it!
111
+ # This compiles it into a LangChain Runnable,
112
+ # meaning you can use it as you would any other runnable
113
+ return workflow.compile()
@@ -0,0 +1,170 @@
1
+ # https://github.com/langchain-ai/langgraph/blob/ea071935fef240d631305df12b6d83e9c363cef3/libs/langgraph/langgraph/prebuilt/agent_executor.py
2
+ import operator
3
+ from typing import Annotated, Sequence, TypedDict, Union
4
+ from langchain.tools import BaseTool
5
+ from langchain_core.agents import AgentAction, AgentFinish
6
+ from langchain_core.messages import BaseMessage
7
+ from langchain_core.language_models import LanguageModelLike
8
+ from langgraph.graph import END, StateGraph
9
+ from langgraph.graph.state import CompiledStateGraph
10
+ from langgraph.prebuilt.tool_executor import ToolExecutor
11
+ from langgraph.utils import RunnableCallable
12
+ from langchain.agents import create_structured_chat_agent
13
+ from .react_prompt import react_agent_prompt
14
+
15
+
16
+ def create_react_agent_executor(
17
+ tools: list[BaseTool],
18
+ llm: LanguageModelLike,
19
+ system_message: str,
20
+ **kwargs # ignore
21
+ ):
22
+ prompt = react_agent_prompt.partial(assistant_message=system_message)
23
+ agent = create_structured_chat_agent(llm, tools, prompt)
24
+ agent_executer = create_agent_executor(agent, tools)
25
+ return agent_executer
26
+
27
+
28
+ def _get_agent_state(input_schema=None):
29
+ if input_schema is None:
30
+
31
+ class AgentState(TypedDict):
32
+ # The input string
33
+ input: str
34
+ # The list of previous messages in the conversation
35
+ chat_history: Sequence[BaseMessage]
36
+ # The outcome of a given call to the agent
37
+ # Needs `None` as a valid type, since this is what this will start as
38
+ agent_outcome: Union[AgentAction, AgentFinish, None]
39
+ # List of actions and corresponding observations
40
+ # Here we annotate this with `operator.add` to indicate that operations to
41
+ # this state should be ADDED to the existing values (not overwrite it)
42
+ intermediate_steps: Annotated[list[tuple[AgentAction, str]], operator.add]
43
+
44
+ else:
45
+
46
+ class AgentState(input_schema):
47
+ # The outcome of a given call to the agent
48
+ # Needs `None` as a valid type, since this is what this will start as
49
+ agent_outcome: Union[AgentAction, AgentFinish, None]
50
+ # List of actions and corresponding observations
51
+ # Here we annotate this with `operator.add` to indicate that operations to
52
+ # this state should be ADDED to the existing values (not overwrite it)
53
+ intermediate_steps: Annotated[list[tuple[AgentAction, str]], operator.add]
54
+
55
+ return AgentState
56
+
57
+
58
+ def create_agent_executor(
59
+ agent_runnable, tools, input_schema=None
60
+ ) -> CompiledStateGraph:
61
+ """This is a helper function for creating a graph that works with LangChain Agents.
62
+
63
+ Args:
64
+ agent_runnable (RunnableLike): The agent runnable.
65
+ tools (list): A list of tools to be used by the agent.
66
+ input_schema (dict, optional): The input schema for the agent. Defaults to None.
67
+
68
+ Returns:
69
+ The `CompiledStateGraph` object.
70
+ """
71
+
72
+ if isinstance(tools, ToolExecutor):
73
+ tool_executor = tools
74
+ else:
75
+ tool_executor = ToolExecutor(tools)
76
+
77
+ state = _get_agent_state(input_schema)
78
+
79
+ # Define logic that will be used to determine which conditional edge to go down
80
+
81
+ def should_continue(data):
82
+ # If the agent outcome is an AgentFinish, then we return `exit` string
83
+ # This will be used when setting up the graph to define the flow
84
+ if isinstance(data["agent_outcome"], AgentFinish):
85
+ return "end"
86
+ # Otherwise, an AgentAction is returned
87
+ # Here we return `continue` string
88
+ # This will be used when setting up the graph to define the flow
89
+ else:
90
+ return "continue"
91
+
92
+ def run_agent(data, config):
93
+ agent_outcome = agent_runnable.invoke(data, config)
94
+ return {"agent_outcome": agent_outcome}
95
+
96
+ async def arun_agent(data, config):
97
+ agent_outcome = await agent_runnable.ainvoke(data, config)
98
+ return {"agent_outcome": agent_outcome}
99
+
100
+ # Define the function to execute tools
101
+ def execute_tools(data, config):
102
+ # Get the most recent agent_outcome - this is the key added in the `agent` above
103
+ agent_action = data["agent_outcome"]
104
+ if not isinstance(agent_action, list):
105
+ agent_action = [agent_action]
106
+ output = tool_executor.batch(agent_action, config, return_exceptions=True)
107
+ return {
108
+ "intermediate_steps": [
109
+ (action, str(out)) for action, out in zip(agent_action, output)
110
+ ]
111
+ }
112
+
113
+ async def aexecute_tools(data, config):
114
+ # Get the most recent agent_outcome - this is the key added in the `agent` above
115
+ agent_action = data["agent_outcome"]
116
+ if not isinstance(agent_action, list):
117
+ agent_action = [agent_action]
118
+ output = await tool_executor.abatch(
119
+ agent_action, config, return_exceptions=True
120
+ )
121
+ return {
122
+ "intermediate_steps": [
123
+ (action, str(out)) for action, out in zip(agent_action, output)
124
+ ]
125
+ }
126
+
127
+ # Define a new graph
128
+ workflow = StateGraph(state)
129
+
130
+ # Define the two nodes we will cycle between
131
+ workflow.add_node("agent", RunnableCallable(run_agent, arun_agent))
132
+ workflow.add_node("tools", RunnableCallable(execute_tools, aexecute_tools))
133
+
134
+ # Set the entrypoint as `agent`
135
+ # This means that this node is the first one called
136
+ workflow.set_entry_point("agent")
137
+
138
+ # We now add a conditional edge
139
+ workflow.add_conditional_edges(
140
+ # First, we define the start node. We use `agent`.
141
+ # This means these are the edges taken after the `agent` node is called.
142
+ "agent",
143
+ # Next, we pass in the function that will determine which node is called next.
144
+ should_continue,
145
+ # Finally we pass in a mapping.
146
+ # The keys are strings, and the values are other nodes.
147
+ # END is a special node marking that the graph should finish.
148
+ # What will happen is we will call `should_continue`, and then the output of that
149
+ # will be matched against the keys in this mapping.
150
+ # Based on which one it matches, that node will then be called.
151
+ {
152
+ # If `tools`, then we call the tool node.
153
+ "continue": "tools",
154
+ # Otherwise we finish.
155
+ "end": END,
156
+ },
157
+ )
158
+
159
+ # We now add a normal edge from `tools` to `agent`.
160
+ # This means that after `tools` is called, `agent` node is called next.
161
+ workflow.add_edge("tools", "agent")
162
+
163
+ # Finally, we compile it!
164
+ # This compiles it into a LangChain Runnable,
165
+ # meaning you can use it as you would any other runnable
166
+ return workflow.compile()
167
+
168
+
169
+ if __name__ == "__main__":
170
+ pass
@@ -0,0 +1,67 @@
1
+ from typing import List, Union
2
+ from langchain_core.prompts.chat import (
3
+ ChatPromptTemplate,
4
+ HumanMessagePromptTemplate,
5
+ SystemMessagePromptTemplate,
6
+ MessagesPlaceholder
7
+ )
8
+ from langchain_core.messages import FunctionMessage, SystemMessage, ToolMessage, AIMessage, HumanMessage, ChatMessage
9
+
10
+
11
+ system_temp = """
12
+ {assistant_message}
13
+
14
+ Respond to the human as helpfully and accurately as possible. You have access to the following tools:
15
+
16
+ {tools}
17
+
18
+ Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).
19
+
20
+ Valid "action" values: "Final Answer" or {tool_names}
21
+
22
+ Provide only ONE action per $JSON_BLOB, as shown:
23
+
24
+ ```
25
+ {{{{
26
+ "action": $TOOL_NAME,
27
+ "action_input": $INPUT
28
+ }}}}
29
+ ```
30
+
31
+ Follow this format:
32
+
33
+ Question: input question to answer
34
+ Thought: consider previous and subsequent steps
35
+ Action:
36
+ ```
37
+ $JSON_BLOB
38
+ ```
39
+ Observation: action result
40
+ ... (repeat Thought/Action/Observation N times)
41
+ Thought: I know what to respond
42
+ Action:
43
+ ```
44
+ {{{{
45
+ "action": "Final Answer",
46
+ "action_input": "Final response to human"
47
+ }}}}
48
+
49
+ Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation
50
+ """
51
+
52
+ human_temp = """Question: {input}
53
+
54
+ Thought: {agent_scratchpad}
55
+ (reminder to respond in a JSON blob no matter what)"""
56
+
57
+
58
+ react_agent_prompt = ChatPromptTemplate(
59
+ input_variables=['agent_scratchpad', 'input', 'tool_names', 'tools', 'assistant_message'],
60
+ optional_variables=['chat_history'],
61
+ input_types={'chat_history': List[Union[AIMessage, HumanMessage, ChatMessage, SystemMessage, FunctionMessage, ToolMessage]]},
62
+ messages=[
63
+ SystemMessagePromptTemplate.from_template(system_temp),
64
+ MessagesPlaceholder(variable_name='chat_history', optional=True),
65
+ HumanMessagePromptTemplate.from_template(human_temp)
66
+ ]
67
+ )
@@ -0,0 +1,35 @@
1
+ from typing import Sequence
2
+ from langchain.tools import BaseTool
3
+ from langchain_core.language_models.base import LanguageModelLike
4
+ from langchain_core.runnables import RunnableBinding
5
+ from .agent_types import create_openai_func_call_agent_executor, create_react_agent_executor
6
+
7
+
8
+ class Runnable(RunnableBinding):
9
+ agent_execution_mode: str
10
+ tools: Sequence[BaseTool]
11
+ llm: LanguageModelLike
12
+ assistant_message: str
13
+
14
+ def __init__(
15
+ self,
16
+ *,
17
+ agent_execution_mode: str,
18
+ tools: Sequence[BaseTool],
19
+ llm: LanguageModelLike,
20
+ assistant_message: str,
21
+ ) -> None:
22
+
23
+ if agent_execution_mode == "FuncCall":
24
+ agent_executor_object = create_openai_func_call_agent_executor
25
+ else:
26
+ agent_executor_object = create_react_agent_executor
27
+ agent_executor = agent_executor_object(tools, llm, assistant_message)
28
+ agent_executor = agent_executor.with_config({"recursion_limit": 50})
29
+ super().__init__(
30
+ tools=tools,
31
+ llm=llm,
32
+ agent_execution_mode=agent_execution_mode,
33
+ assistant_message=assistant_message,
34
+ bound=agent_executor, return_intermediate_steps=True
35
+ )
@@ -0,0 +1,28 @@
1
+ import json
2
+ from langchain_core.messages import HumanMessage, AIMessage
3
+
4
+
5
+ INPUT_MESSAGE = "INPUT_MESSAGE=>"
6
+ _OUTPUT_MESSAGE = "OUTPUT_MESSAGE=>"
7
+ _INFOMATION_MESSAGE = "INFOMATION_MESSAGE=>"
8
+ _LOG = "LOG=>"
9
+
10
+
11
+
12
+ def log(content, *args, end='\n', **kwargs):
13
+ print(_LOG + content, *args, end=end, **kwargs)
14
+
15
+
16
+ def output(role, content, history):
17
+ print(_OUTPUT_MESSAGE + json.dumps({"role": role, "content": content}))
18
+ if role == "assistant":
19
+ history.append(AIMessage(content=content))
20
+
21
+ elif role == "user":
22
+ history.append(HumanMessage(content=content))
23
+ else:
24
+ raise ValueError("Invalid role")
25
+ return history
26
+
27
+ def info(role, content):
28
+ print(_INFOMATION_MESSAGE + json.dumps({"role": role, "content": content}))
pycoze/bot/base.py ADDED
@@ -0,0 +1,79 @@
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
pycoze/bot/bot.py ADDED
@@ -0,0 +1,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
+
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
+
@@ -0,0 +1 @@
1
+ from .arg import read_arg
pycoze/utils/arg.py ADDED
@@ -0,0 +1,13 @@
1
+ import argparse
2
+
3
+
4
+ def read_arg(param: str, is_path=False):
5
+ parser = argparse.ArgumentParser()
6
+ parser.add_argument(param, nargs='?', help=f'Parameter {param}')
7
+ args = parser.parse_args()
8
+ value = getattr(args, param.lstrip('-'))
9
+ # 如果是路径并且有引号,去掉引号
10
+ if is_path and value and value.startswith('"') and value.endswith('"'):
11
+ value = value[1:-1]
12
+
13
+ return value
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.0
3
+ Version: 0.1.3
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -19,5 +19,8 @@ Package for pycoze only!
19
19
 
20
20
  <!-- For author only -->
21
21
  <!-- pip install twine -->
22
+
23
+ <!-- 递增版本 -->
24
+ <!-- 删除build和dist文件夹 -->
22
25
  <!-- python setup.py sdist bdist_wheel -->
23
26
  <!-- twine upload dist/* -->
@@ -0,0 +1,20 @@
1
+ pycoze/__init__.py,sha256=j4jAxl28vO2mAfdZV_p8w01K5XMRxU9IaU_S4oHM7ZE,38
2
+ pycoze/module.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ pycoze/bot/__init__.py,sha256=pciDtfcIXda7iFt9uI5Fpm0JKpGBhdXHmJv4966WTVU,21
4
+ pycoze/bot/base.py,sha256=7sz_OeAqdFSmWsnReaxUku5QU6YEJ3VUFa2BZcVx-uM,2644
5
+ pycoze/bot/bot.py,sha256=h1mEJibPcFV10hRo0idFJC947fCsvCVpOAq0ZBP3Z1g,1802
6
+ pycoze/bot/agent/__init__.py,sha256=IaYqQCJ3uBor92JdOxI_EY4HtYOHgej8lijr3UrN1Vc,161
7
+ pycoze/bot/agent/agent.py,sha256=8vww4POYkXLa5MlKQ5hL6ZIZs5q-Y7FS6SznATrrpjE,3324
8
+ pycoze/bot/agent/assistant.py,sha256=QLeWaPi415P9jruYOm8qcIbC94cXXAhJYmLTkyC9NTQ,1267
9
+ pycoze/bot/agent/chat.py,sha256=C2X0meUcIPbn5FCxvhkhxozldPG7qdb2jVR-WnPqqnQ,791
10
+ pycoze/bot/agent/agent_types/__init__.py,sha256=PoYsSeQld0kdROjejN3BNjC9NsgKNekjNy4FqtWRJqM,227
11
+ pycoze/bot/agent/agent_types/openai_func_call_agent.py,sha256=YkpiMxrLl7aMYZJsOtZraTT2UE0IZrQsfikGRCHx4jM,4467
12
+ pycoze/bot/agent/agent_types/react_agent.py,sha256=LwzIovswxPuc08A0imQwK3DIykhlZXK5eCWXcIAuNgM,6741
13
+ pycoze/bot/agent/agent_types/react_prompt.py,sha256=PdzL3SFb0Ee0dbK4HGqG09bRISrru4bhOj45CXBtqI0,1919
14
+ pycoze/utils/__init__.py,sha256=I69FoCUXvVnlACQzYXoVcR76QyuVAILo9Rv-TUbcRZo,25
15
+ pycoze/utils/arg.py,sha256=NKNSYttFk5y2l2ptk4Fiuk0SHrowYpbvl-xuggYAr4Q,430
16
+ pycoze-0.1.3.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
17
+ pycoze-0.1.3.dist-info/METADATA,sha256=z3r6ghb1WEsMv7FOCqE7XJ-lD9Gq5Ev3_tNA-mrLAgE,618
18
+ pycoze-0.1.3.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
19
+ pycoze-0.1.3.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
20
+ pycoze-0.1.3.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- pycoze/__init__.py,sha256=xhKLVAxqUncda3aCjV2ugkU6nLXvpiKty8n2g_u469g,40
2
- pycoze/module.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- pycoze-0.1.0.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
4
- pycoze-0.1.0.dist-info/METADATA,sha256=scJMwzRvKme7IxVxxJ2C3R5-jb1lbJfloQixR_IXSeo,555
5
- pycoze-0.1.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- pycoze-0.1.0.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
7
- pycoze-0.1.0.dist-info/RECORD,,
File without changes