agentrun-inner-test 0.0.46__py3-none-any.whl → 0.0.64__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.
- agentrun/__init__.py +34 -1
- agentrun/agent_runtime/__endpoint_async_template.py +40 -0
- agentrun/agent_runtime/api/control.py +1 -1
- agentrun/agent_runtime/endpoint.py +79 -0
- agentrun/credential/api/control.py +1 -1
- agentrun/integration/agentscope/__init__.py +2 -1
- agentrun/integration/agentscope/builtin.py +23 -0
- agentrun/integration/builtin/__init__.py +2 -0
- agentrun/integration/builtin/knowledgebase.py +137 -0
- agentrun/integration/crewai/__init__.py +2 -1
- agentrun/integration/crewai/builtin.py +23 -0
- agentrun/integration/google_adk/__init__.py +2 -1
- agentrun/integration/google_adk/builtin.py +23 -0
- agentrun/integration/langchain/__init__.py +2 -1
- agentrun/integration/langchain/builtin.py +23 -0
- agentrun/integration/langgraph/__init__.py +2 -1
- agentrun/integration/langgraph/builtin.py +23 -0
- agentrun/integration/pydantic_ai/__init__.py +2 -1
- agentrun/integration/pydantic_ai/builtin.py +23 -0
- agentrun/knowledgebase/__client_async_template.py +173 -0
- agentrun/knowledgebase/__init__.py +53 -0
- agentrun/knowledgebase/__knowledgebase_async_template.py +438 -0
- agentrun/knowledgebase/api/__data_async_template.py +414 -0
- agentrun/knowledgebase/api/__init__.py +19 -0
- agentrun/knowledgebase/api/control.py +606 -0
- agentrun/knowledgebase/api/data.py +624 -0
- agentrun/knowledgebase/client.py +311 -0
- agentrun/knowledgebase/knowledgebase.py +748 -0
- agentrun/knowledgebase/model.py +270 -0
- agentrun/memory_collection/__client_async_template.py +178 -0
- agentrun/memory_collection/__init__.py +37 -0
- agentrun/memory_collection/__memory_collection_async_template.py +457 -0
- agentrun/memory_collection/api/__init__.py +5 -0
- agentrun/memory_collection/api/control.py +610 -0
- agentrun/memory_collection/client.py +323 -0
- agentrun/memory_collection/memory_collection.py +844 -0
- agentrun/memory_collection/model.py +162 -0
- agentrun/model/api/control.py +1 -1
- agentrun/sandbox/aio_sandbox.py +11 -4
- agentrun/sandbox/api/control.py +1 -1
- agentrun/sandbox/browser_sandbox.py +2 -2
- agentrun/sandbox/model.py +0 -13
- agentrun/toolset/api/control.py +1 -1
- agentrun/toolset/toolset.py +1 -0
- agentrun/utils/__data_api_async_template.py +1 -0
- agentrun/utils/config.py +12 -0
- agentrun/utils/control_api.py +27 -0
- agentrun/utils/data_api.py +1 -0
- {agentrun_inner_test-0.0.46.dist-info → agentrun_inner_test-0.0.64.dist-info}/METADATA +4 -2
- {agentrun_inner_test-0.0.46.dist-info → agentrun_inner_test-0.0.64.dist-info}/RECORD +53 -34
- {agentrun_inner_test-0.0.46.dist-info → agentrun_inner_test-0.0.64.dist-info}/WHEEL +0 -0
- {agentrun_inner_test-0.0.46.dist-info → agentrun_inner_test-0.0.64.dist-info}/licenses/LICENSE +0 -0
- {agentrun_inner_test-0.0.46.dist-info → agentrun_inner_test-0.0.64.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,844 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This file is auto generated by the code generation script.
|
|
3
|
+
Do not modify this file manually.
|
|
4
|
+
Use the `make codegen` command to regenerate.
|
|
5
|
+
|
|
6
|
+
当前文件为自动生成的控制 API 客户端代码。请勿手动修改此文件。
|
|
7
|
+
使用 `make codegen` 命令重新生成。
|
|
8
|
+
|
|
9
|
+
source: agentrun/memory_collection/__memory_collection_async_template.py
|
|
10
|
+
|
|
11
|
+
MemoryCollection 高层 API / MemoryCollection High-Level API
|
|
12
|
+
|
|
13
|
+
此模块定义记忆集合资源的高级API。
|
|
14
|
+
This module defines the high-level API for memory collection resources.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
18
|
+
|
|
19
|
+
from agentrun.utils.config import Config
|
|
20
|
+
from agentrun.utils.model import PageableInput
|
|
21
|
+
from agentrun.utils.resource import ResourceBase
|
|
22
|
+
|
|
23
|
+
from .model import (
|
|
24
|
+
MemoryCollectionCreateInput,
|
|
25
|
+
MemoryCollectionImmutableProps,
|
|
26
|
+
MemoryCollectionListInput,
|
|
27
|
+
MemoryCollectionListOutput,
|
|
28
|
+
MemoryCollectionMutableProps,
|
|
29
|
+
MemoryCollectionSystemProps,
|
|
30
|
+
MemoryCollectionUpdateInput,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class MemoryCollection(
|
|
35
|
+
MemoryCollectionMutableProps,
|
|
36
|
+
MemoryCollectionImmutableProps,
|
|
37
|
+
MemoryCollectionSystemProps,
|
|
38
|
+
ResourceBase,
|
|
39
|
+
):
|
|
40
|
+
"""记忆集合资源 / MemoryCollection Resource
|
|
41
|
+
|
|
42
|
+
提供记忆集合的完整生命周期管理,包括创建、删除、更新、查询。
|
|
43
|
+
Provides complete lifecycle management for memory collections, including create, delete, update, and query.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
@classmethod
|
|
47
|
+
def __get_client(cls):
|
|
48
|
+
"""获取客户端实例 / Get client instance
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
MemoryCollectionClient: 客户端实例 / Client instance
|
|
52
|
+
"""
|
|
53
|
+
from .client import MemoryCollectionClient
|
|
54
|
+
|
|
55
|
+
return MemoryCollectionClient()
|
|
56
|
+
|
|
57
|
+
@classmethod
|
|
58
|
+
async def create_async(
|
|
59
|
+
cls, input: MemoryCollectionCreateInput, config: Optional[Config] = None
|
|
60
|
+
):
|
|
61
|
+
"""创建记忆集合(异步)
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
input: 记忆集合输入参数
|
|
65
|
+
config: 配置
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
MemoryCollection: 创建的记忆集合对象
|
|
69
|
+
"""
|
|
70
|
+
return await cls.__get_client().create_async(input, config=config)
|
|
71
|
+
|
|
72
|
+
@classmethod
|
|
73
|
+
def create(
|
|
74
|
+
cls, input: MemoryCollectionCreateInput, config: Optional[Config] = None
|
|
75
|
+
):
|
|
76
|
+
"""创建记忆集合(同步)
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
input: 记忆集合输入参数
|
|
80
|
+
config: 配置
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
MemoryCollection: 创建的记忆集合对象
|
|
84
|
+
"""
|
|
85
|
+
return cls.__get_client().create(input, config=config)
|
|
86
|
+
|
|
87
|
+
@classmethod
|
|
88
|
+
async def delete_by_name_async(
|
|
89
|
+
cls, memory_collection_name: str, config: Optional[Config] = None
|
|
90
|
+
):
|
|
91
|
+
"""根据名称删除记忆集合(异步)
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
memory_collection_name: 记忆集合名称
|
|
95
|
+
config: 配置
|
|
96
|
+
"""
|
|
97
|
+
return await cls.__get_client().delete_async(
|
|
98
|
+
memory_collection_name, config=config
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
@classmethod
|
|
102
|
+
def delete_by_name(
|
|
103
|
+
cls, memory_collection_name: str, config: Optional[Config] = None
|
|
104
|
+
):
|
|
105
|
+
"""根据名称删除记忆集合(同步)
|
|
106
|
+
|
|
107
|
+
Args:
|
|
108
|
+
memory_collection_name: 记忆集合名称
|
|
109
|
+
config: 配置
|
|
110
|
+
"""
|
|
111
|
+
return cls.__get_client().delete(memory_collection_name, config=config)
|
|
112
|
+
|
|
113
|
+
@classmethod
|
|
114
|
+
async def update_by_name_async(
|
|
115
|
+
cls,
|
|
116
|
+
memory_collection_name: str,
|
|
117
|
+
input: MemoryCollectionUpdateInput,
|
|
118
|
+
config: Optional[Config] = None,
|
|
119
|
+
):
|
|
120
|
+
"""根据名称更新记忆集合(异步)
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
memory_collection_name: 记忆集合名称
|
|
124
|
+
input: 记忆集合更新输入参数
|
|
125
|
+
config: 配置
|
|
126
|
+
|
|
127
|
+
Returns:
|
|
128
|
+
MemoryCollection: 更新后的记忆集合对象
|
|
129
|
+
"""
|
|
130
|
+
return await cls.__get_client().update_async(
|
|
131
|
+
memory_collection_name, input, config=config
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
@classmethod
|
|
135
|
+
def update_by_name(
|
|
136
|
+
cls,
|
|
137
|
+
memory_collection_name: str,
|
|
138
|
+
input: MemoryCollectionUpdateInput,
|
|
139
|
+
config: Optional[Config] = None,
|
|
140
|
+
):
|
|
141
|
+
"""根据名称更新记忆集合(同步)
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
memory_collection_name: 记忆集合名称
|
|
145
|
+
input: 记忆集合更新输入参数
|
|
146
|
+
config: 配置
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
MemoryCollection: 更新后的记忆集合对象
|
|
150
|
+
"""
|
|
151
|
+
return cls.__get_client().update(
|
|
152
|
+
memory_collection_name, input, config=config
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
@classmethod
|
|
156
|
+
async def get_by_name_async(
|
|
157
|
+
cls, memory_collection_name: str, config: Optional[Config] = None
|
|
158
|
+
):
|
|
159
|
+
"""根据名称获取记忆集合(异步)
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
memory_collection_name: 记忆集合名称
|
|
163
|
+
config: 配置
|
|
164
|
+
|
|
165
|
+
Returns:
|
|
166
|
+
MemoryCollection: 记忆集合对象
|
|
167
|
+
"""
|
|
168
|
+
return await cls.__get_client().get_async(
|
|
169
|
+
memory_collection_name, config=config
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
@classmethod
|
|
173
|
+
def get_by_name(
|
|
174
|
+
cls, memory_collection_name: str, config: Optional[Config] = None
|
|
175
|
+
):
|
|
176
|
+
"""根据名称获取记忆集合(同步)
|
|
177
|
+
|
|
178
|
+
Args:
|
|
179
|
+
memory_collection_name: 记忆集合名称
|
|
180
|
+
config: 配置
|
|
181
|
+
|
|
182
|
+
Returns:
|
|
183
|
+
MemoryCollection: 记忆集合对象
|
|
184
|
+
"""
|
|
185
|
+
return cls.__get_client().get(memory_collection_name, config=config)
|
|
186
|
+
|
|
187
|
+
@classmethod
|
|
188
|
+
async def _list_page_async(
|
|
189
|
+
cls, page_input: PageableInput, config: Config | None = None, **kwargs
|
|
190
|
+
):
|
|
191
|
+
return await cls.__get_client().list_async(
|
|
192
|
+
input=MemoryCollectionListInput(
|
|
193
|
+
**kwargs,
|
|
194
|
+
**page_input.model_dump(),
|
|
195
|
+
),
|
|
196
|
+
config=config,
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
@classmethod
|
|
200
|
+
def _list_page(
|
|
201
|
+
cls, page_input: PageableInput, config: Config | None = None, **kwargs
|
|
202
|
+
):
|
|
203
|
+
return cls.__get_client().list(
|
|
204
|
+
input=MemoryCollectionListInput(
|
|
205
|
+
**kwargs,
|
|
206
|
+
**page_input.model_dump(),
|
|
207
|
+
),
|
|
208
|
+
config=config,
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
@classmethod
|
|
212
|
+
async def list_all_async(
|
|
213
|
+
cls,
|
|
214
|
+
*,
|
|
215
|
+
memory_collection_name: Optional[str] = None,
|
|
216
|
+
config: Optional[Config] = None,
|
|
217
|
+
) -> List[MemoryCollectionListOutput]:
|
|
218
|
+
"""列出所有记忆集合(异步)
|
|
219
|
+
|
|
220
|
+
Args:
|
|
221
|
+
memory_collection_name: 记忆集合名称(可选)
|
|
222
|
+
config: 配置
|
|
223
|
+
|
|
224
|
+
Returns:
|
|
225
|
+
List[MemoryCollectionListOutput]: 记忆集合列表
|
|
226
|
+
"""
|
|
227
|
+
return await cls._list_all_async(
|
|
228
|
+
lambda mc: mc.memory_collection_id or "",
|
|
229
|
+
config=config,
|
|
230
|
+
memory_collection_name=memory_collection_name,
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
@classmethod
|
|
234
|
+
def list_all(
|
|
235
|
+
cls,
|
|
236
|
+
*,
|
|
237
|
+
memory_collection_name: Optional[str] = None,
|
|
238
|
+
config: Optional[Config] = None,
|
|
239
|
+
) -> List[MemoryCollectionListOutput]:
|
|
240
|
+
"""列出所有记忆集合(同步)
|
|
241
|
+
|
|
242
|
+
Args:
|
|
243
|
+
memory_collection_name: 记忆集合名称(可选)
|
|
244
|
+
config: 配置
|
|
245
|
+
|
|
246
|
+
Returns:
|
|
247
|
+
List[MemoryCollectionListOutput]: 记忆集合列表
|
|
248
|
+
"""
|
|
249
|
+
return cls._list_all(
|
|
250
|
+
lambda mc: mc.memory_collection_id or "",
|
|
251
|
+
config=config,
|
|
252
|
+
memory_collection_name=memory_collection_name,
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
async def update_async(
|
|
256
|
+
self,
|
|
257
|
+
input: MemoryCollectionUpdateInput,
|
|
258
|
+
config: Optional[Config] = None,
|
|
259
|
+
):
|
|
260
|
+
"""更新记忆集合(异步)
|
|
261
|
+
|
|
262
|
+
Args:
|
|
263
|
+
input: 记忆集合更新输入参数
|
|
264
|
+
config: 配置
|
|
265
|
+
|
|
266
|
+
Returns:
|
|
267
|
+
MemoryCollection: 更新后的记忆集合对象
|
|
268
|
+
"""
|
|
269
|
+
if self.memory_collection_name is None:
|
|
270
|
+
raise ValueError(
|
|
271
|
+
"memory_collection_name is required to update a"
|
|
272
|
+
" MemoryCollection"
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
result = await self.update_by_name_async(
|
|
276
|
+
self.memory_collection_name, input, config=config
|
|
277
|
+
)
|
|
278
|
+
self.update_self(result)
|
|
279
|
+
|
|
280
|
+
return self
|
|
281
|
+
|
|
282
|
+
def update(
|
|
283
|
+
self,
|
|
284
|
+
input: MemoryCollectionUpdateInput,
|
|
285
|
+
config: Optional[Config] = None,
|
|
286
|
+
):
|
|
287
|
+
"""更新记忆集合(同步)
|
|
288
|
+
|
|
289
|
+
Args:
|
|
290
|
+
input: 记忆集合更新输入参数
|
|
291
|
+
config: 配置
|
|
292
|
+
|
|
293
|
+
Returns:
|
|
294
|
+
MemoryCollection: 更新后的记忆集合对象
|
|
295
|
+
"""
|
|
296
|
+
if self.memory_collection_name is None:
|
|
297
|
+
raise ValueError(
|
|
298
|
+
"memory_collection_name is required to update a"
|
|
299
|
+
" MemoryCollection"
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
result = self.update_by_name(
|
|
303
|
+
self.memory_collection_name, input, config=config
|
|
304
|
+
)
|
|
305
|
+
self.update_self(result)
|
|
306
|
+
|
|
307
|
+
return self
|
|
308
|
+
|
|
309
|
+
async def delete_async(self, config: Optional[Config] = None):
|
|
310
|
+
"""删除记忆集合(异步)
|
|
311
|
+
|
|
312
|
+
Args:
|
|
313
|
+
config: 配置
|
|
314
|
+
"""
|
|
315
|
+
if self.memory_collection_name is None:
|
|
316
|
+
raise ValueError(
|
|
317
|
+
"memory_collection_name is required to delete a"
|
|
318
|
+
" MemoryCollection"
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
return await self.delete_by_name_async(
|
|
322
|
+
self.memory_collection_name, config=config
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
def delete(self, config: Optional[Config] = None):
|
|
326
|
+
"""删除记忆集合(同步)
|
|
327
|
+
|
|
328
|
+
Args:
|
|
329
|
+
config: 配置
|
|
330
|
+
"""
|
|
331
|
+
if self.memory_collection_name is None:
|
|
332
|
+
raise ValueError(
|
|
333
|
+
"memory_collection_name is required to delete a"
|
|
334
|
+
" MemoryCollection"
|
|
335
|
+
)
|
|
336
|
+
|
|
337
|
+
return self.delete_by_name(self.memory_collection_name, config=config)
|
|
338
|
+
|
|
339
|
+
async def get_async(self, config: Optional[Config] = None):
|
|
340
|
+
"""刷新记忆集合信息(异步)
|
|
341
|
+
|
|
342
|
+
Args:
|
|
343
|
+
config: 配置
|
|
344
|
+
|
|
345
|
+
Returns:
|
|
346
|
+
MemoryCollection: 刷新后的记忆集合对象
|
|
347
|
+
"""
|
|
348
|
+
if self.memory_collection_name is None:
|
|
349
|
+
raise ValueError(
|
|
350
|
+
"memory_collection_name is required to refresh a"
|
|
351
|
+
" MemoryCollection"
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
result = await self.get_by_name_async(
|
|
355
|
+
self.memory_collection_name, config=config
|
|
356
|
+
)
|
|
357
|
+
self.update_self(result)
|
|
358
|
+
|
|
359
|
+
return self
|
|
360
|
+
|
|
361
|
+
def get(self, config: Optional[Config] = None):
|
|
362
|
+
"""刷新记忆集合信息(同步)
|
|
363
|
+
|
|
364
|
+
Args:
|
|
365
|
+
config: 配置
|
|
366
|
+
|
|
367
|
+
Returns:
|
|
368
|
+
MemoryCollection: 刷新后的记忆集合对象
|
|
369
|
+
"""
|
|
370
|
+
if self.memory_collection_name is None:
|
|
371
|
+
raise ValueError(
|
|
372
|
+
"memory_collection_name is required to refresh a"
|
|
373
|
+
" MemoryCollection"
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
result = self.get_by_name(self.memory_collection_name, config=config)
|
|
377
|
+
self.update_self(result)
|
|
378
|
+
|
|
379
|
+
return self
|
|
380
|
+
|
|
381
|
+
async def refresh_async(self, config: Optional[Config] = None):
|
|
382
|
+
"""刷新记忆集合信息(异步)
|
|
383
|
+
|
|
384
|
+
Args:
|
|
385
|
+
config: 配置
|
|
386
|
+
|
|
387
|
+
Returns:
|
|
388
|
+
MemoryCollection: 刷新后的记忆集合对象
|
|
389
|
+
"""
|
|
390
|
+
return await self.get_async(config=config)
|
|
391
|
+
|
|
392
|
+
def refresh(self, config: Optional[Config] = None):
|
|
393
|
+
"""刷新记忆集合信息(同步)
|
|
394
|
+
|
|
395
|
+
Args:
|
|
396
|
+
config: 配置
|
|
397
|
+
|
|
398
|
+
Returns:
|
|
399
|
+
MemoryCollection: 刷新后的记忆集合对象
|
|
400
|
+
"""
|
|
401
|
+
return self.get(config=config)
|
|
402
|
+
|
|
403
|
+
@classmethod
|
|
404
|
+
async def to_mem0_memory_async(
|
|
405
|
+
cls,
|
|
406
|
+
memory_collection_name: str,
|
|
407
|
+
config: Optional[Config] = None,
|
|
408
|
+
history_db_path: Optional[str] = None,
|
|
409
|
+
):
|
|
410
|
+
"""将 MemoryCollection 转换为 agentrun-mem0ai Memory 客户端(异步)
|
|
411
|
+
|
|
412
|
+
Args:
|
|
413
|
+
memory_collection_name: 记忆集合名称
|
|
414
|
+
config: AgentRun 配置
|
|
415
|
+
history_db_path: mem0 历史数据库路径(可选)
|
|
416
|
+
|
|
417
|
+
Returns:
|
|
418
|
+
Memory: agentrun-mem0ai Memory 客户端实例
|
|
419
|
+
|
|
420
|
+
Raises:
|
|
421
|
+
ImportError: 如果未安装 agentrun-mem0ai 包
|
|
422
|
+
ValueError: 如果配置信息不完整
|
|
423
|
+
|
|
424
|
+
Example:
|
|
425
|
+
>>> memory = await MemoryCollection.to_mem0_memory_async(
|
|
426
|
+
... "memoryCollection010901",
|
|
427
|
+
... config=config
|
|
428
|
+
... )
|
|
429
|
+
>>> memory.add("用户喜欢吃苹果", user_id="user123")
|
|
430
|
+
"""
|
|
431
|
+
try:
|
|
432
|
+
from agentrun_mem0 import Memory
|
|
433
|
+
except ImportError as e:
|
|
434
|
+
raise ImportError(
|
|
435
|
+
"agentrun-mem0ai package is required. Install it with: pip"
|
|
436
|
+
" install agentrun-mem0ai"
|
|
437
|
+
) from e
|
|
438
|
+
|
|
439
|
+
# 获取 MemoryCollection 配置
|
|
440
|
+
memory_collection = await cls.get_by_name_async(
|
|
441
|
+
memory_collection_name, config=config
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
# 构建 mem0 配置
|
|
445
|
+
mem0_config = await cls._build_mem0_config_async(
|
|
446
|
+
memory_collection, config, history_db_path
|
|
447
|
+
)
|
|
448
|
+
|
|
449
|
+
# 创建并返回 Memory 实例
|
|
450
|
+
return Memory.from_config(mem0_config)
|
|
451
|
+
|
|
452
|
+
@classmethod
|
|
453
|
+
def to_mem0_memory(
|
|
454
|
+
cls,
|
|
455
|
+
memory_collection_name: str,
|
|
456
|
+
config: Optional[Config] = None,
|
|
457
|
+
history_db_path: Optional[str] = None,
|
|
458
|
+
):
|
|
459
|
+
"""将 MemoryCollection 转换为 agentrun-mem0ai Memory 客户端(同步)
|
|
460
|
+
|
|
461
|
+
Args:
|
|
462
|
+
memory_collection_name: 记忆集合名称
|
|
463
|
+
config: AgentRun 配置
|
|
464
|
+
history_db_path: mem0 历史数据库路径(可选)
|
|
465
|
+
|
|
466
|
+
Returns:
|
|
467
|
+
Memory: agentrun-mem0ai Memory 客户端实例
|
|
468
|
+
|
|
469
|
+
Raises:
|
|
470
|
+
ImportError: 如果未安装 agentrun-mem0ai 包
|
|
471
|
+
ValueError: 如果配置信息不完整
|
|
472
|
+
|
|
473
|
+
Example:
|
|
474
|
+
>>> memory = MemoryCollection.to_mem0_memory(
|
|
475
|
+
... "memoryCollection010901",
|
|
476
|
+
... config=config
|
|
477
|
+
... )
|
|
478
|
+
>>> memory.add("用户喜欢吃苹果", user_id="user123")
|
|
479
|
+
"""
|
|
480
|
+
try:
|
|
481
|
+
from agentrun_mem0 import Memory
|
|
482
|
+
except ImportError as e:
|
|
483
|
+
raise ImportError(
|
|
484
|
+
"agentrun-mem0ai package is required. Install it with: pip"
|
|
485
|
+
" install agentrun-mem0ai"
|
|
486
|
+
) from e
|
|
487
|
+
|
|
488
|
+
# 获取 MemoryCollection 配置
|
|
489
|
+
memory_collection = cls.get_by_name(
|
|
490
|
+
memory_collection_name, config=config
|
|
491
|
+
)
|
|
492
|
+
|
|
493
|
+
# 构建 mem0 配置
|
|
494
|
+
mem0_config = cls._build_mem0_config(
|
|
495
|
+
memory_collection, config, history_db_path
|
|
496
|
+
)
|
|
497
|
+
|
|
498
|
+
# 创建并返回 Memory 实例
|
|
499
|
+
return Memory.from_config(mem0_config)
|
|
500
|
+
|
|
501
|
+
@staticmethod
|
|
502
|
+
def _convert_vpc_endpoint_to_public(endpoint: str) -> str:
|
|
503
|
+
"""将 VPC 内网地址转换为公网地址
|
|
504
|
+
|
|
505
|
+
Args:
|
|
506
|
+
endpoint: 原始 endpoint,可能是 VPC 内网地址
|
|
507
|
+
|
|
508
|
+
Returns:
|
|
509
|
+
str: 公网地址
|
|
510
|
+
|
|
511
|
+
Example:
|
|
512
|
+
>>> _convert_vpc_endpoint_to_public("https://jiuqing.cn-hangzhou.vpc.tablestore.aliyuncs.com")
|
|
513
|
+
"https://jiuqing.cn-hangzhou.ots.aliyuncs.com"
|
|
514
|
+
"""
|
|
515
|
+
if ".vpc.tablestore.aliyuncs.com" in endpoint:
|
|
516
|
+
# 将 .vpc.tablestore.aliyuncs.com 替换为 .ots.aliyuncs.com
|
|
517
|
+
return endpoint.replace(
|
|
518
|
+
".vpc.tablestore.aliyuncs.com", ".ots.aliyuncs.com"
|
|
519
|
+
)
|
|
520
|
+
return endpoint
|
|
521
|
+
|
|
522
|
+
@classmethod
|
|
523
|
+
async def _build_mem0_config_async(
|
|
524
|
+
cls,
|
|
525
|
+
memory_collection: "MemoryCollection",
|
|
526
|
+
config: Optional[Config],
|
|
527
|
+
history_db_path: Optional[str] = None,
|
|
528
|
+
) -> Dict[str, Any]:
|
|
529
|
+
"""构建 mem0 配置字典(异步)
|
|
530
|
+
|
|
531
|
+
Args:
|
|
532
|
+
memory_collection: MemoryCollection 对象
|
|
533
|
+
config: AgentRun 配置
|
|
534
|
+
history_db_path: 历史数据库路径
|
|
535
|
+
|
|
536
|
+
Returns:
|
|
537
|
+
Dict[str, Any]: mem0 配置字典
|
|
538
|
+
"""
|
|
539
|
+
mem0_config: Dict[str, Any] = {}
|
|
540
|
+
|
|
541
|
+
# 构建 vector_store 配置
|
|
542
|
+
if memory_collection.vector_store_config:
|
|
543
|
+
vector_store_config = memory_collection.vector_store_config
|
|
544
|
+
provider = vector_store_config.provider or ""
|
|
545
|
+
|
|
546
|
+
if vector_store_config.config:
|
|
547
|
+
vs_config = vector_store_config.config
|
|
548
|
+
vector_store: Dict[str, Any] = {
|
|
549
|
+
"provider": provider,
|
|
550
|
+
"config": {},
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
# 根据不同的 provider 构建配置
|
|
554
|
+
if provider == "aliyun_tablestore":
|
|
555
|
+
# 获取凭证信息
|
|
556
|
+
effective_config = config or Config()
|
|
557
|
+
# 将 VPC 内网地址转换为公网地址
|
|
558
|
+
public_endpoint = cls._convert_vpc_endpoint_to_public(
|
|
559
|
+
vs_config.endpoint or ""
|
|
560
|
+
)
|
|
561
|
+
vector_store["config"] = {
|
|
562
|
+
"vector_dimension": vs_config.vector_dimension,
|
|
563
|
+
"endpoint": public_endpoint,
|
|
564
|
+
"instance_name": vs_config.instance_name,
|
|
565
|
+
"collection_name": vs_config.collection_name,
|
|
566
|
+
"access_key_id": effective_config.get_access_key_id(),
|
|
567
|
+
"access_key_secret": (
|
|
568
|
+
effective_config.get_access_key_secret()
|
|
569
|
+
),
|
|
570
|
+
}
|
|
571
|
+
# 如果有 security_token,添加它
|
|
572
|
+
security_token = effective_config.get_security_token()
|
|
573
|
+
if security_token:
|
|
574
|
+
vector_store["config"]["sts_token"] = security_token
|
|
575
|
+
else:
|
|
576
|
+
# 其他 provider 的通用配置
|
|
577
|
+
vector_store["config"] = {
|
|
578
|
+
"endpoint": vs_config.endpoint,
|
|
579
|
+
"collection_name": vs_config.collection_name,
|
|
580
|
+
}
|
|
581
|
+
if vs_config.vector_dimension:
|
|
582
|
+
vector_store["config"][
|
|
583
|
+
"vector_dimension"
|
|
584
|
+
] = vs_config.vector_dimension
|
|
585
|
+
|
|
586
|
+
mem0_config["vector_store"] = vector_store
|
|
587
|
+
|
|
588
|
+
# 构建 llm 配置
|
|
589
|
+
if memory_collection.llm_config:
|
|
590
|
+
llm_config = memory_collection.llm_config
|
|
591
|
+
model_service_name = llm_config.model_service_name
|
|
592
|
+
|
|
593
|
+
if model_service_name and llm_config.config:
|
|
594
|
+
# 使用高层 API 获取 ModelService 配置
|
|
595
|
+
base_url, api_key = (
|
|
596
|
+
await cls._resolve_model_service_config_async(
|
|
597
|
+
model_service_name, config
|
|
598
|
+
)
|
|
599
|
+
)
|
|
600
|
+
|
|
601
|
+
mem0_config["llm"] = {
|
|
602
|
+
"provider": "openai", # mem0 使用 openai 兼容接口
|
|
603
|
+
"config": {
|
|
604
|
+
"model": llm_config.config.model,
|
|
605
|
+
"openai_base_url": base_url,
|
|
606
|
+
"api_key": api_key,
|
|
607
|
+
},
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
# 构建 embedder 配置
|
|
611
|
+
if memory_collection.embedder_config:
|
|
612
|
+
embedder_config = memory_collection.embedder_config
|
|
613
|
+
model_service_name = embedder_config.model_service_name
|
|
614
|
+
|
|
615
|
+
if model_service_name and embedder_config.config:
|
|
616
|
+
# 使用高层 API 获取 ModelService 配置
|
|
617
|
+
base_url, api_key = (
|
|
618
|
+
await cls._resolve_model_service_config_async(
|
|
619
|
+
model_service_name, config
|
|
620
|
+
)
|
|
621
|
+
)
|
|
622
|
+
|
|
623
|
+
mem0_config["embedder"] = {
|
|
624
|
+
"provider": "openai", # mem0 使用 openai 兼容接口
|
|
625
|
+
"config": {
|
|
626
|
+
"model": embedder_config.config.model,
|
|
627
|
+
"openai_base_url": base_url,
|
|
628
|
+
"api_key": api_key,
|
|
629
|
+
},
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
# 添加历史数据库路径
|
|
633
|
+
if history_db_path:
|
|
634
|
+
mem0_config["history_db_path"] = history_db_path
|
|
635
|
+
|
|
636
|
+
return mem0_config
|
|
637
|
+
|
|
638
|
+
@classmethod
|
|
639
|
+
def _build_mem0_config(
|
|
640
|
+
cls,
|
|
641
|
+
memory_collection: "MemoryCollection",
|
|
642
|
+
config: Optional[Config],
|
|
643
|
+
history_db_path: Optional[str] = None,
|
|
644
|
+
) -> Dict[str, Any]:
|
|
645
|
+
"""构建 mem0 配置字典(同步)
|
|
646
|
+
|
|
647
|
+
Args:
|
|
648
|
+
memory_collection: MemoryCollection 对象
|
|
649
|
+
config: AgentRun 配置
|
|
650
|
+
history_db_path: 历史数据库路径
|
|
651
|
+
|
|
652
|
+
Returns:
|
|
653
|
+
Dict[str, Any]: mem0 配置字典
|
|
654
|
+
"""
|
|
655
|
+
mem0_config: Dict[str, Any] = {}
|
|
656
|
+
|
|
657
|
+
# 构建 vector_store 配置
|
|
658
|
+
if memory_collection.vector_store_config:
|
|
659
|
+
vector_store_config = memory_collection.vector_store_config
|
|
660
|
+
provider = vector_store_config.provider or ""
|
|
661
|
+
|
|
662
|
+
if vector_store_config.config:
|
|
663
|
+
vs_config = vector_store_config.config
|
|
664
|
+
vector_store: Dict[str, Any] = {
|
|
665
|
+
"provider": provider,
|
|
666
|
+
"config": {},
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
# 根据不同的 provider 构建配置
|
|
670
|
+
if provider == "aliyun_tablestore":
|
|
671
|
+
# 获取凭证信息
|
|
672
|
+
effective_config = config or Config()
|
|
673
|
+
# 将 VPC 内网地址转换为公网地址
|
|
674
|
+
public_endpoint = cls._convert_vpc_endpoint_to_public(
|
|
675
|
+
vs_config.endpoint or ""
|
|
676
|
+
)
|
|
677
|
+
vector_store["config"] = {
|
|
678
|
+
"vector_dimension": vs_config.vector_dimension,
|
|
679
|
+
"endpoint": public_endpoint,
|
|
680
|
+
"instance_name": vs_config.instance_name,
|
|
681
|
+
"collection_name": vs_config.collection_name,
|
|
682
|
+
"access_key_id": effective_config.get_access_key_id(),
|
|
683
|
+
"access_key_secret": (
|
|
684
|
+
effective_config.get_access_key_secret()
|
|
685
|
+
),
|
|
686
|
+
}
|
|
687
|
+
# 如果有 security_token,添加它
|
|
688
|
+
security_token = effective_config.get_security_token()
|
|
689
|
+
if security_token:
|
|
690
|
+
vector_store["config"]["sts_token"] = security_token
|
|
691
|
+
else:
|
|
692
|
+
# 其他 provider 的通用配置
|
|
693
|
+
vector_store["config"] = {
|
|
694
|
+
"endpoint": vs_config.endpoint,
|
|
695
|
+
"collection_name": vs_config.collection_name,
|
|
696
|
+
}
|
|
697
|
+
if vs_config.vector_dimension:
|
|
698
|
+
vector_store["config"][
|
|
699
|
+
"vector_dimension"
|
|
700
|
+
] = vs_config.vector_dimension
|
|
701
|
+
|
|
702
|
+
mem0_config["vector_store"] = vector_store
|
|
703
|
+
|
|
704
|
+
# 构建 llm 配置
|
|
705
|
+
if memory_collection.llm_config:
|
|
706
|
+
llm_config = memory_collection.llm_config
|
|
707
|
+
model_service_name = llm_config.model_service_name
|
|
708
|
+
|
|
709
|
+
if model_service_name and llm_config.config:
|
|
710
|
+
# 使用高层 API 获取 ModelService 配置
|
|
711
|
+
base_url, api_key = cls._resolve_model_service_config(
|
|
712
|
+
model_service_name, config
|
|
713
|
+
)
|
|
714
|
+
|
|
715
|
+
mem0_config["llm"] = {
|
|
716
|
+
"provider": "openai", # mem0 使用 openai 兼容接口
|
|
717
|
+
"config": {
|
|
718
|
+
"model": llm_config.config.model,
|
|
719
|
+
"openai_base_url": base_url,
|
|
720
|
+
"api_key": api_key,
|
|
721
|
+
},
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
# 构建 embedder 配置
|
|
725
|
+
if memory_collection.embedder_config:
|
|
726
|
+
embedder_config = memory_collection.embedder_config
|
|
727
|
+
model_service_name = embedder_config.model_service_name
|
|
728
|
+
|
|
729
|
+
if model_service_name and embedder_config.config:
|
|
730
|
+
# 使用高层 API 获取 ModelService 配置
|
|
731
|
+
base_url, api_key = cls._resolve_model_service_config(
|
|
732
|
+
model_service_name, config
|
|
733
|
+
)
|
|
734
|
+
|
|
735
|
+
mem0_config["embedder"] = {
|
|
736
|
+
"provider": "openai", # mem0 使用 openai 兼容接口
|
|
737
|
+
"config": {
|
|
738
|
+
"model": embedder_config.config.model,
|
|
739
|
+
"openai_base_url": base_url,
|
|
740
|
+
"api_key": api_key,
|
|
741
|
+
},
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
# 添加历史数据库路径
|
|
745
|
+
if history_db_path:
|
|
746
|
+
mem0_config["history_db_path"] = history_db_path
|
|
747
|
+
|
|
748
|
+
return mem0_config
|
|
749
|
+
|
|
750
|
+
@staticmethod
|
|
751
|
+
async def _resolve_model_service_config_async(
|
|
752
|
+
model_service_name: str, config: Optional[Config]
|
|
753
|
+
) -> Tuple[str, str]:
|
|
754
|
+
"""解析 ModelService 配置获取 baseUrl 和 apiKey(异步)
|
|
755
|
+
|
|
756
|
+
Args:
|
|
757
|
+
model_service_name: ModelService 名称
|
|
758
|
+
config: AgentRun 配置
|
|
759
|
+
|
|
760
|
+
Returns:
|
|
761
|
+
Tuple[str, str]: (base_url, api_key)
|
|
762
|
+
|
|
763
|
+
Raises:
|
|
764
|
+
ValueError: 如果配置信息不完整
|
|
765
|
+
"""
|
|
766
|
+
from agentrun.credential import Credential
|
|
767
|
+
from agentrun.model import ModelService
|
|
768
|
+
|
|
769
|
+
# 使用高层 API 获取 ModelService
|
|
770
|
+
model_service = await ModelService.get_by_name_async(
|
|
771
|
+
model_service_name, config=config
|
|
772
|
+
)
|
|
773
|
+
|
|
774
|
+
# 获取 provider_settings
|
|
775
|
+
if not model_service.provider_settings:
|
|
776
|
+
raise ValueError(
|
|
777
|
+
f"ModelService {model_service_name} providerSettings is empty"
|
|
778
|
+
)
|
|
779
|
+
|
|
780
|
+
base_url = model_service.provider_settings.base_url or ""
|
|
781
|
+
api_key = model_service.provider_settings.api_key or ""
|
|
782
|
+
|
|
783
|
+
# 如果有 credentialName,使用高层 API 获取 credential secret
|
|
784
|
+
credential_name = model_service.credential_name
|
|
785
|
+
if credential_name:
|
|
786
|
+
credential = await Credential.get_by_name_async(
|
|
787
|
+
credential_name, config=config
|
|
788
|
+
)
|
|
789
|
+
if credential.credential_secret:
|
|
790
|
+
api_key = credential.credential_secret
|
|
791
|
+
|
|
792
|
+
if not base_url:
|
|
793
|
+
raise ValueError(
|
|
794
|
+
f"ModelService {model_service_name} baseUrl is empty"
|
|
795
|
+
)
|
|
796
|
+
|
|
797
|
+
return base_url, api_key
|
|
798
|
+
|
|
799
|
+
@staticmethod
|
|
800
|
+
def _resolve_model_service_config(
|
|
801
|
+
model_service_name: str, config: Optional[Config]
|
|
802
|
+
) -> Tuple[str, str]:
|
|
803
|
+
"""解析 ModelService 配置获取 baseUrl 和 apiKey(同步)
|
|
804
|
+
|
|
805
|
+
Args:
|
|
806
|
+
model_service_name: ModelService 名称
|
|
807
|
+
config: AgentRun 配置
|
|
808
|
+
|
|
809
|
+
Returns:
|
|
810
|
+
Tuple[str, str]: (base_url, api_key)
|
|
811
|
+
|
|
812
|
+
Raises:
|
|
813
|
+
ValueError: 如果配置信息不完整
|
|
814
|
+
"""
|
|
815
|
+
from agentrun.credential import Credential
|
|
816
|
+
from agentrun.model import ModelService
|
|
817
|
+
|
|
818
|
+
# 使用高层 API 获取 ModelService
|
|
819
|
+
model_service = ModelService.get_by_name(
|
|
820
|
+
model_service_name, config=config
|
|
821
|
+
)
|
|
822
|
+
|
|
823
|
+
# 获取 provider_settings
|
|
824
|
+
if not model_service.provider_settings:
|
|
825
|
+
raise ValueError(
|
|
826
|
+
f"ModelService {model_service_name} providerSettings is empty"
|
|
827
|
+
)
|
|
828
|
+
|
|
829
|
+
base_url = model_service.provider_settings.base_url or ""
|
|
830
|
+
api_key = model_service.provider_settings.api_key or ""
|
|
831
|
+
|
|
832
|
+
# 如果有 credentialName,使用高层 API 获取 credential secret
|
|
833
|
+
credential_name = model_service.credential_name
|
|
834
|
+
if credential_name:
|
|
835
|
+
credential = Credential.get_by_name(credential_name, config=config)
|
|
836
|
+
if credential.credential_secret:
|
|
837
|
+
api_key = credential.credential_secret
|
|
838
|
+
|
|
839
|
+
if not base_url:
|
|
840
|
+
raise ValueError(
|
|
841
|
+
f"ModelService {model_service_name} baseUrl is empty"
|
|
842
|
+
)
|
|
843
|
+
|
|
844
|
+
return base_url, api_key
|