rootdriver 0.3.0__tar.gz → 0.4.0__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 (22) hide show
  1. {rootdriver-0.3.0/rootdriver.egg-info → rootdriver-0.4.0}/PKG-INFO +60 -14
  2. rootdriver-0.3.0/PKG-INFO → rootdriver-0.4.0/README.md +168 -135
  3. {rootdriver-0.3.0 → rootdriver-0.4.0}/pyproject.toml +2 -2
  4. {rootdriver-0.3.0 → rootdriver-0.4.0}/rootdriver/__init__.py +7 -1
  5. {rootdriver-0.3.0 → rootdriver-0.4.0}/rootdriver/agent.py +13 -4
  6. rootdriver-0.4.0/rootdriver/constants.py +4 -0
  7. {rootdriver-0.3.0 → rootdriver-0.4.0}/rootdriver/conversation.py +13 -2
  8. {rootdriver-0.3.0 → rootdriver-0.4.0}/rootdriver/engine.py +152 -163
  9. {rootdriver-0.3.0 → rootdriver-0.4.0}/rootdriver/exception.py +7 -3
  10. rootdriver-0.3.0/README.md → rootdriver-0.4.0/rootdriver.egg-info/PKG-INFO +181 -122
  11. {rootdriver-0.3.0 → rootdriver-0.4.0}/rootdriver.egg-info/SOURCES.txt +1 -1
  12. rootdriver-0.4.0/tests/test_state.py +230 -0
  13. rootdriver-0.3.0/rootdriver/constants.py +0 -2
  14. rootdriver-0.3.0/rootdriver/state.py +0 -103
  15. {rootdriver-0.3.0 → rootdriver-0.4.0}/rootdriver.egg-info/dependency_links.txt +0 -0
  16. {rootdriver-0.3.0 → rootdriver-0.4.0}/rootdriver.egg-info/requires.txt +0 -0
  17. {rootdriver-0.3.0 → rootdriver-0.4.0}/rootdriver.egg-info/top_level.txt +0 -0
  18. {rootdriver-0.3.0 → rootdriver-0.4.0}/setup.cfg +0 -0
  19. {rootdriver-0.3.0 → rootdriver-0.4.0}/tests/test_base_tool.py +0 -0
  20. {rootdriver-0.3.0 → rootdriver-0.4.0}/tests/test_conversation.py +0 -0
  21. {rootdriver-0.3.0 → rootdriver-0.4.0}/tests/test_tool.py +0 -0
  22. {rootdriver-0.3.0 → rootdriver-0.4.0}/tests/test_tool_async.py +0 -0
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rootdriver
3
- Version: 0.3.0
4
- Summary: Rooted in Origin, Driving All Things
3
+ Version: 0.4.0
4
+ Summary: Start from Root, Driving All Things
5
5
  Author-email: zimvir <zimvir@qq.com>
6
6
  License: MIT
7
7
  Requires-Python: >=3.10
@@ -15,7 +15,13 @@ Requires-Dist: pytest>=7.0.0; extra == "dev"
15
15
 
16
16
  根源出发,驱动万物
17
17
 
18
- 一个轻量级的 Python AI Agent 开发框架,支持单智能体和多智能体应用。
18
+ 一个轻量级的 Python AI Agent 开发框架。
19
+
20
+ **解决痛点:**
21
+
22
+ - 想快速跑一个 Agent?一行代码,不需要编排、不需要懂设计模式
23
+ - 市面框架太重、太复杂、学习成本高?RootDriver 核心代码几千行,不用记几百个 API
24
+ - 需要扩展?按需添加,想加什么加什么,不被框架绑架
19
25
 
20
26
  ## 特性
21
27
 
@@ -24,7 +30,7 @@ Requires-Dist: pytest>=7.0.0; extra == "dev"
24
30
  - **工具调用**:支持 function calling,自动执行工具并返回结果
25
31
  - **状态管理**:支持内存和数据库持久化检查点
26
32
  - **异常体系**:完整的异常类层次结构
27
- - **异步支持**:全面异步支持,并发多 Agent / 工具调用
33
+ - **异步支持**:全面异步支持,并发 Agent / 工具调用
28
34
 
29
35
  ## 安装
30
36
 
@@ -50,19 +56,23 @@ def get_weather(city: str) -> str:
50
56
  ```python
51
57
  from rootdriver import Agent, AgentLLM, OpenAIAdapter
52
58
 
53
- agent = Agent(
54
- agent_llm=AgentLLM(
55
- adapter=OpenAIAdapter(
56
- api_key="YOUR_API_KEY",
57
- base_url="BASE_URL"
58
- ),
59
- model="gpt-4",
59
+ agent_llm = AgentLLM(
60
+ adapter=OpenAIAdapter(
61
+ api_key="YOUR_API_KEY",
62
+ base_url="BASE_URL"
60
63
  ),
64
+ model= "gpt-4",
65
+ temperature=0.7
66
+ )
67
+
68
+
69
+ agent = Agent(
70
+ agent_llm=agent_llm,
61
71
  tools=[get_weather],
62
72
  system_prompt="你是一个有用的助手",
63
73
  )
64
74
 
65
- # 单次对话
75
+ # 单次对话(无 tool)
66
76
  response = agent.talk("北京天气怎么样?")
67
77
  print(response)
68
78
  ```
@@ -82,7 +92,6 @@ import asyncio
82
92
  from rootdriver import Agent, AgentLLM, OpenAIAdapter
83
93
 
84
94
  async def main():
85
- agent = Agent(agent_llm=AgentLLM(...))
86
95
 
87
96
  # 单次异步对话
88
97
  response = await agent.atalk("你好")
@@ -98,6 +107,39 @@ async def main():
98
107
  asyncio.run(main())
99
108
  ```
100
109
 
110
+ ## 记忆持久化
111
+
112
+ Agent 支持对话历史自动持久化到 JSON 文件:
113
+
114
+ ```python
115
+ agent = Agent(
116
+ agent_llm=agent_llm,
117
+ system_prompt="你是一个有用的助手",
118
+ db_path="conversations.json", # 记忆数据库路径
119
+ auto_save=True, # 开启自动保存(默认开启)
120
+ )
121
+
122
+ # 对话自动增量保存到 memory.json
123
+ response = agent.react("我们之前聊了什么?")
124
+
125
+ # 新建 Agent 时可从数据库恢复对话历史
126
+ agent2 = Agent(agent_llm=agent_llm, id="same_user", db_path="memory.json")
127
+ agent2.state.load_from_db("auto_saved") # 恢复对话
128
+ ```
129
+
130
+ ### 手动检查点
131
+
132
+ ```python
133
+ # 内存快照
134
+ agent.state.checkpoint("backup_point")
135
+
136
+ # 保存到数据库
137
+ agent.state.save_from_checkpoints(name="my_backup", checkpoint_name="backup_point")
138
+
139
+ # 从数据库加载
140
+ agent.state.load_from_db("my_backup")
141
+ ```
142
+
101
143
  ## 核心组件
102
144
 
103
145
  | 组件 | 说明 |
@@ -108,6 +150,7 @@ asyncio.run(main())
108
150
  | `LLM` | LLM 调用封装 |
109
151
  | `Tool` | 工具集合,管理所有可调用工具 |
110
152
  | `State` | 状态管理,支持检查点和持久化 |
153
+ | `JsonDB` | JSON 文件数据库封装 |
111
154
 
112
155
  ## 项目结构
113
156
 
@@ -116,7 +159,10 @@ rootdriver/
116
159
  ├── agent.py # Agent 智能体
117
160
  ├── engine.py # 引擎核心
118
161
  ├── conversation.py # 对话管理
119
- ├── state.py # 状态管理
162
+ ├── state/ # 状态管理包
163
+ │ └── state.py # State 实现
164
+ ├── db/ # 数据库封装包
165
+ │ └── json_db.py # JsonDB 实现
120
166
  ├── exception.py # 异常定义
121
167
  ├── llm/
122
168
  │ ├── llm.py # LLM 封装
@@ -1,135 +1,168 @@
1
- Metadata-Version: 2.4
2
- Name: rootdriver
3
- Version: 0.3.0
4
- Summary: Rooted in Origin, Driving All Things
5
- Author-email: zimvir <zimvir@qq.com>
6
- License: MIT
7
- Requires-Python: >=3.10
8
- Description-Content-Type: text/markdown
9
- Requires-Dist: openai>=1.0.0
10
- Requires-Dist: pydantic>=2.0.0
11
- Provides-Extra: dev
12
- Requires-Dist: pytest>=7.0.0; extra == "dev"
13
-
14
- # RootDriver
15
-
16
- 根源出发,驱动万物
17
-
18
- 一个轻量级的 Python AI Agent 开发框架,支持单智能体和多智能体应用。
19
-
20
- ## 特性
21
-
22
- - **简洁易用**:装饰器方式定义工具,快速构建 Agent
23
- - **模块化设计**:LLM 适配器、工具系统、会话管理解耦
24
- - **工具调用**:支持 function calling,自动执行工具并返回结果
25
- - **状态管理**:支持内存和数据库持久化检查点
26
- - **异常体系**:完整的异常类层次结构
27
- - **异步支持**:全面异步支持,并发多 Agent / 工具调用
28
-
29
- ## 安装
30
-
31
- ```bash
32
- pip install rootdriver
33
- ```
34
-
35
- ## 快速开始
36
-
37
- ### 定义工具
38
-
39
- ```python
40
- from rootdriver import tool
41
-
42
- @tool
43
- def get_weather(city: str) -> str:
44
- """获取城市天气"""
45
- return f"{city} 晴天"
46
- ```
47
-
48
- ### 创建 Agent
49
-
50
- ```python
51
- from rootdriver import Agent, AgentLLM, OpenAIAdapter
52
-
53
- agent = Agent(
54
- agent_llm=AgentLLM(
55
- adapter=OpenAIAdapter(
56
- api_key="YOUR_API_KEY",
57
- base_url="BASE_URL"
58
- ),
59
- model="gpt-4",
60
- ),
61
- tools=[get_weather],
62
- system_prompt="你是一个有用的助手",
63
- )
64
-
65
- # 单次对话
66
- response = agent.talk("北京天气怎么样?")
67
- print(response)
68
- ```
69
-
70
- ### 使用工具
71
-
72
- ```python
73
- # 完整对话循环(包含工具调用)
74
- response = agent.react("帮我查下上海天气")
75
- print(response)
76
- ```
77
-
78
- ### 异步用法
79
-
80
- ```python
81
- import asyncio
82
- from rootdriver import Agent, AgentLLM, OpenAIAdapter
83
-
84
- async def main():
85
- agent = Agent(agent_llm=AgentLLM(...))
86
-
87
- # 单次异步对话
88
- response = await agent.atalk("你好")
89
- print(response)
90
-
91
- # 并发多个 Agent
92
- results = await asyncio.gather(
93
- agent.areact("问题1"),
94
- agent.areact("问题2"),
95
- agent.areact("问题3"),
96
- )
97
-
98
- asyncio.run(main())
99
- ```
100
-
101
- ## 核心组件
102
-
103
- | 组件 | 说明 |
104
- |------|------|
105
- | `Agent` | 智能体入口,整合 LLM、工具、会话 |
106
- | `Engine` | 核心引擎,处理对话循环和工具调用 |
107
- | `Conversation` | 会话管理,维护消息历史 |
108
- | `LLM` | LLM 调用封装 |
109
- | `Tool` | 工具集合,管理所有可调用工具 |
110
- | `State` | 状态管理,支持检查点和持久化 |
111
-
112
- ## 项目结构
113
-
114
- ```
115
- rootdriver/
116
- ├── agent.py # Agent 智能体
117
- ├── engine.py # 引擎核心
118
- ├── conversation.py # 对话管理
119
- ├── state.py # 状态管理
120
- ├── exception.py # 异常定义
121
- ├── llm/
122
- │ ├── llm.py # LLM 封装
123
- │ ├── base_adapter.py # 适配器基类
124
- │ └── adapter/
125
- │ └── openai_adapter.py # OpenAI 适配器
126
- ├── tool/
127
- │ ├── base_tool.py # 工具基类
128
- │ └── tools.py # 工具集
129
- ├── types/ # 类型定义
130
- └── utils/ # 工具函数
131
- ```
132
-
133
- ## License
134
-
135
- MIT
1
+ # RootDriver
2
+
3
+ 根源出发,驱动万物
4
+
5
+ 一个轻量级的 Python AI Agent 开发框架。
6
+
7
+ **解决痛点:**
8
+
9
+ - 想快速跑一个 Agent?一行代码,不需要编排、不需要懂设计模式
10
+ - 市面框架太重、太复杂、学习成本高?RootDriver 核心代码几千行,不用记几百个 API
11
+ - 需要扩展?按需添加,想加什么加什么,不被框架绑架
12
+
13
+ ## 特性
14
+
15
+ - **简洁易用**:装饰器方式定义工具,快速构建 Agent
16
+ - **模块化设计**:LLM 适配器、工具系统、会话管理解耦
17
+ - **工具调用**:支持 function calling,自动执行工具并返回结果
18
+ - **状态管理**:支持内存和数据库持久化检查点
19
+ - **异常体系**:完整的异常类层次结构
20
+ - **异步支持**:全面异步支持,并发 Agent / 工具调用
21
+
22
+ ## 安装
23
+
24
+ ```bash
25
+ pip install rootdriver
26
+ ```
27
+
28
+ ## 快速开始
29
+
30
+ ### 定义工具
31
+
32
+ ```python
33
+ from rootdriver import tool
34
+
35
+ @tool
36
+ def get_weather(city: str) -> str:
37
+ """获取城市天气"""
38
+ return f"{city} 晴天"
39
+ ```
40
+
41
+ ### 创建 Agent
42
+
43
+ ```python
44
+ from rootdriver import Agent, AgentLLM, OpenAIAdapter
45
+
46
+ agent_llm = AgentLLM(
47
+ adapter=OpenAIAdapter(
48
+ api_key="YOUR_API_KEY",
49
+ base_url="BASE_URL"
50
+ ),
51
+ model= "gpt-4",
52
+ temperature=0.7
53
+ )
54
+
55
+
56
+ agent = Agent(
57
+ agent_llm=agent_llm,
58
+ tools=[get_weather],
59
+ system_prompt="你是一个有用的助手",
60
+ )
61
+
62
+ # 单次对话(无 tool)
63
+ response = agent.talk("北京天气怎么样?")
64
+ print(response)
65
+ ```
66
+
67
+ ### 使用工具
68
+
69
+ ```python
70
+ # 完整对话循环(包含工具调用)
71
+ response = agent.react("帮我查下上海天气")
72
+ print(response)
73
+ ```
74
+
75
+ ### 异步用法
76
+
77
+ ```python
78
+ import asyncio
79
+ from rootdriver import Agent, AgentLLM, OpenAIAdapter
80
+
81
+ async def main():
82
+
83
+ # 单次异步对话
84
+ response = await agent.atalk("你好")
85
+ print(response)
86
+
87
+ # 并发多个 Agent
88
+ results = await asyncio.gather(
89
+ agent.areact("问题1"),
90
+ agent.areact("问题2"),
91
+ agent.areact("问题3"),
92
+ )
93
+
94
+ asyncio.run(main())
95
+ ```
96
+
97
+ ## 记忆持久化
98
+
99
+ Agent 支持对话历史自动持久化到 JSON 文件:
100
+
101
+ ```python
102
+ agent = Agent(
103
+ agent_llm=agent_llm,
104
+ system_prompt="你是一个有用的助手",
105
+ db_path="conversations.json", # 记忆数据库路径
106
+ auto_save=True, # 开启自动保存(默认开启)
107
+ )
108
+
109
+ # 对话自动增量保存到 memory.json
110
+ response = agent.react("我们之前聊了什么?")
111
+
112
+ # 新建 Agent 时可从数据库恢复对话历史
113
+ agent2 = Agent(agent_llm=agent_llm, id="same_user", db_path="memory.json")
114
+ agent2.state.load_from_db("auto_saved") # 恢复对话
115
+ ```
116
+
117
+ ### 手动检查点
118
+
119
+ ```python
120
+ # 内存快照
121
+ agent.state.checkpoint("backup_point")
122
+
123
+ # 保存到数据库
124
+ agent.state.save_from_checkpoints(name="my_backup", checkpoint_name="backup_point")
125
+
126
+ # 从数据库加载
127
+ agent.state.load_from_db("my_backup")
128
+ ```
129
+
130
+ ## 核心组件
131
+
132
+ | 组件 | 说明 |
133
+ |------|------|
134
+ | `Agent` | 智能体入口,整合 LLM、工具、会话 |
135
+ | `Engine` | 核心引擎,处理对话循环和工具调用 |
136
+ | `Conversation` | 会话管理,维护消息历史 |
137
+ | `LLM` | LLM 调用封装 |
138
+ | `Tool` | 工具集合,管理所有可调用工具 |
139
+ | `State` | 状态管理,支持检查点和持久化 |
140
+ | `JsonDB` | JSON 文件数据库封装 |
141
+
142
+ ## 项目结构
143
+
144
+ ```
145
+ rootdriver/
146
+ ├── agent.py # Agent 智能体
147
+ ├── engine.py # 引擎核心
148
+ ├── conversation.py # 对话管理
149
+ ├── state/ # 状态管理包
150
+ │ └── state.py # State 实现
151
+ ├── db/ # 数据库封装包
152
+ │ └── json_db.py # JsonDB 实现
153
+ ├── exception.py # 异常定义
154
+ ├── llm/
155
+ │ ├── llm.py # LLM 封装
156
+ │ ├── base_adapter.py # 适配器基类
157
+ │ └── adapter/
158
+ │ └── openai_adapter.py # OpenAI 适配器
159
+ ├── tool/
160
+ │ ├── base_tool.py # 工具基类
161
+ │ └── tools.py # 工具集
162
+ ├── types/ # 类型定义
163
+ └── utils/ # 工具函数
164
+ ```
165
+
166
+ ## License
167
+
168
+ MIT
@@ -4,8 +4,8 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "rootdriver"
7
- version = "0.3.0"
8
- description = "Rooted in Origin, Driving All Things"
7
+ version = "0.4.0"
8
+ description = "Start from Root, Driving All Things"
9
9
  readme = "README.md"
10
10
  license = {text = "MIT"}
11
11
  authors = [
@@ -1,4 +1,4 @@
1
- __version__ = "0.3.0"
1
+ __version__ = "0.4.0"
2
2
  __author__ = "zimvir"
3
3
  __email__ = "zimvir@qq.com"
4
4
 
@@ -6,6 +6,7 @@ from .agent import Agent
6
6
  from .engine import Engine
7
7
  from .conversation import Conversation
8
8
  from .state import State
9
+ from . import db
9
10
  from .llm import LLM
10
11
  from .llm.base_adapter import BaseAdapter
11
12
  from .llm.adapter import OpenAIAdapter
@@ -26,6 +27,8 @@ from .exception import (
26
27
  CheckpointNotFoundError,
27
28
  StateSaveError,
28
29
  StateLoadError,
30
+ StateDBNotFoundError,
31
+ DBNotFoundError,
29
32
  )
30
33
 
31
34
  __all__ = [
@@ -33,6 +36,7 @@ __all__ = [
33
36
  "Engine",
34
37
  "Conversation",
35
38
  "State",
39
+ "db",
36
40
  "LLM",
37
41
  "BaseAdapter",
38
42
  "OpenAIAdapter",
@@ -55,4 +59,6 @@ __all__ = [
55
59
  "CheckpointNotFoundError",
56
60
  "StateSaveError",
57
61
  "StateLoadError",
62
+ "StateDBNotFoundError",
63
+ "DBNotFoundError",
58
64
  ]
@@ -7,7 +7,8 @@ from .conversation import Conversation
7
7
  from .tool import Tool, BaseTool
8
8
  from .types.agent import AgentLLM
9
9
  from .state import State
10
- from .utils import build_system_message, build_message, build_tool_message, build_user_message, build_assistant_message
10
+ from .utils import build_user_message, ensure_file_exist
11
+ from .constants import DEFAULT_AGENT_DB_PATH
11
12
 
12
13
  class Agent:
13
14
 
@@ -19,23 +20,31 @@ class Agent:
19
20
  system_prompt: str | None = None,
20
21
 
21
22
  db_path: str| None = None,
23
+ auto_save: bool = True,
22
24
  # llm_retry: int = 3,
23
25
  # timeout: float | None = None,
24
26
  ):
27
+ # 检查信息
28
+ ensure_file_exist(db_path,"{}")
29
+
30
+ # 初始化模块
25
31
  self.id = id if id else uuid4().hex
26
- self.db_path = db_path
32
+ self.db_path = DEFAULT_AGENT_DB_PATH
33
+ self.auto_save = auto_save
27
34
 
28
35
  self.conversation = Conversation(system_prompt)
29
36
  self.tool = Tool(tools if tools else [])
30
- self.state = State(self, self.db_path)
37
+ self.state = State(self, self.db_path, self.auto_save)
31
38
 
32
39
  self.engine = Engine(
33
40
  model=agent_llm.model,
34
41
  llm=LLM(agent_llm.adapter),
35
42
  conversation=self.conversation,
36
43
  tool=self.tool,
44
+ _agent=self,
37
45
  )
38
-
46
+ # 属性初始化
47
+ self.state.update_auto_save_data_saved_length()
39
48
 
40
49
  def react(self, input_prompt:str) -> "str":
41
50
  """一次 react 循环"""
@@ -0,0 +1,4 @@
1
+ ROLE_ENUM = ("system", "user", "assistant", "tool")
2
+
3
+ DEFAULT_AGENT_STATE_AUTO_SAVE_NAME = "auto_saved"
4
+ DEFAULT_AGENT_DB_PATH = "conversations.json"
@@ -54,8 +54,12 @@ class Conversation:
54
54
 
55
55
  def get_messages_in_list(self) -> list[dict]:
56
56
  '''返回 字典加列表组成的message(可转成json用于网络、跨语言传输) 组成的列表'''
57
- return [m.model_dump(exclude_none=True) for m in self.messages]
58
-
57
+ return [m.model_dump(exclude_none=False) for m in self.messages]
58
+ def get_message_by_index(self, index: int=-1) -> Message:
59
+ return self.messages[index]
60
+ def get_message_in_list_by_index(self, index:int=-1) -> list[dict]:
61
+ """据索引返回当前消息列表的"""
62
+ return self.messages[index].model_dump(exclude_none=False)
59
63
  @classmethod
60
64
  def from_dict_list(cls, messages: list[dict]) -> "Conversation":
61
65
  conv = cls()
@@ -73,3 +77,10 @@ class Conversation:
73
77
  self.messages = []
74
78
  return self
75
79
 
80
+ def copy(self) -> "Conversation":
81
+ """返回当前 conversation 的浅拷贝"""
82
+ new_conv = Conversation()
83
+ new_conv.messages = list(self.messages)
84
+ new_conv.system_prompt = self.system_prompt
85
+ return new_conv
86
+