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,748 @@
|
|
|
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/knowledgebase/__knowledgebase_async_template.py
|
|
10
|
+
|
|
11
|
+
KnowledgeBase 高层 API / KnowledgeBase High-Level API
|
|
12
|
+
|
|
13
|
+
此模块定义知识库资源的高级API。
|
|
14
|
+
This module defines the high-level API for knowledge base resources.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import asyncio
|
|
18
|
+
from typing import Any, Dict, List, Optional
|
|
19
|
+
|
|
20
|
+
from agentrun.utils.config import Config
|
|
21
|
+
from agentrun.utils.log import logger
|
|
22
|
+
from agentrun.utils.model import PageableInput
|
|
23
|
+
from agentrun.utils.resource import ResourceBase
|
|
24
|
+
|
|
25
|
+
from .api.data import get_data_api
|
|
26
|
+
from .model import (
|
|
27
|
+
BailianProviderSettings,
|
|
28
|
+
BailianRetrieveSettings,
|
|
29
|
+
KnowledgeBaseCreateInput,
|
|
30
|
+
KnowledgeBaseImmutableProps,
|
|
31
|
+
KnowledgeBaseListInput,
|
|
32
|
+
KnowledgeBaseListOutput,
|
|
33
|
+
KnowledgeBaseMutableProps,
|
|
34
|
+
KnowledgeBaseProvider,
|
|
35
|
+
KnowledgeBaseSystemProps,
|
|
36
|
+
KnowledgeBaseUpdateInput,
|
|
37
|
+
RagFlowProviderSettings,
|
|
38
|
+
RagFlowRetrieveSettings,
|
|
39
|
+
RetrieveInput,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class KnowledgeBase(
|
|
44
|
+
KnowledgeBaseMutableProps,
|
|
45
|
+
KnowledgeBaseImmutableProps,
|
|
46
|
+
KnowledgeBaseSystemProps,
|
|
47
|
+
ResourceBase,
|
|
48
|
+
):
|
|
49
|
+
"""知识库资源 / KnowledgeBase Resource
|
|
50
|
+
|
|
51
|
+
提供知识库的完整生命周期管理,包括创建、删除、更新、查询。
|
|
52
|
+
Provides complete lifecycle management for knowledge bases, including create, delete, update, and query.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
@classmethod
|
|
56
|
+
def __get_client(cls):
|
|
57
|
+
"""获取客户端实例 / Get client instance
|
|
58
|
+
|
|
59
|
+
Returns:
|
|
60
|
+
KnowledgeBaseClient: 客户端实例 / Client instance
|
|
61
|
+
"""
|
|
62
|
+
from .client import KnowledgeBaseClient
|
|
63
|
+
|
|
64
|
+
return KnowledgeBaseClient()
|
|
65
|
+
|
|
66
|
+
@classmethod
|
|
67
|
+
async def create_async(
|
|
68
|
+
cls, input: KnowledgeBaseCreateInput, config: Optional[Config] = None
|
|
69
|
+
):
|
|
70
|
+
"""创建知识库(异步)/ Create knowledge base asynchronously
|
|
71
|
+
|
|
72
|
+
Args:
|
|
73
|
+
input: 知识库输入参数 / KnowledgeBase input parameters
|
|
74
|
+
config: 配置 / Configuration
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
KnowledgeBase: 创建的知识库对象 / Created knowledge base object
|
|
78
|
+
"""
|
|
79
|
+
return await cls.__get_client().create_async(input, config=config)
|
|
80
|
+
|
|
81
|
+
@classmethod
|
|
82
|
+
def create(
|
|
83
|
+
cls, input: KnowledgeBaseCreateInput, config: Optional[Config] = None
|
|
84
|
+
):
|
|
85
|
+
"""创建知识库(同步)/ Create knowledge base asynchronously
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
input: 知识库输入参数 / KnowledgeBase input parameters
|
|
89
|
+
config: 配置 / Configuration
|
|
90
|
+
|
|
91
|
+
Returns:
|
|
92
|
+
KnowledgeBase: 创建的知识库对象 / Created knowledge base object
|
|
93
|
+
"""
|
|
94
|
+
return cls.__get_client().create(input, config=config)
|
|
95
|
+
|
|
96
|
+
@classmethod
|
|
97
|
+
async def delete_by_name_async(
|
|
98
|
+
cls, knowledge_base_name: str, config: Optional[Config] = None
|
|
99
|
+
):
|
|
100
|
+
"""根据名称删除知识库(异步)/ Delete knowledge base by name asynchronously
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
knowledge_base_name: 知识库名称 / KnowledgeBase name
|
|
104
|
+
config: 配置 / Configuration
|
|
105
|
+
"""
|
|
106
|
+
return await cls.__get_client().delete_async(
|
|
107
|
+
knowledge_base_name, config=config
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
@classmethod
|
|
111
|
+
def delete_by_name(
|
|
112
|
+
cls, knowledge_base_name: str, config: Optional[Config] = None
|
|
113
|
+
):
|
|
114
|
+
"""根据名称删除知识库(同步)/ Delete knowledge base by name asynchronously
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
knowledge_base_name: 知识库名称 / KnowledgeBase name
|
|
118
|
+
config: 配置 / Configuration
|
|
119
|
+
"""
|
|
120
|
+
return cls.__get_client().delete(knowledge_base_name, config=config)
|
|
121
|
+
|
|
122
|
+
@classmethod
|
|
123
|
+
async def update_by_name_async(
|
|
124
|
+
cls,
|
|
125
|
+
knowledge_base_name: str,
|
|
126
|
+
input: KnowledgeBaseUpdateInput,
|
|
127
|
+
config: Optional[Config] = None,
|
|
128
|
+
):
|
|
129
|
+
"""根据名称更新知识库(异步)/ Update knowledge base by name asynchronously
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
knowledge_base_name: 知识库名称 / KnowledgeBase name
|
|
133
|
+
input: 知识库更新输入参数 / KnowledgeBase update input parameters
|
|
134
|
+
config: 配置 / Configuration
|
|
135
|
+
|
|
136
|
+
Returns:
|
|
137
|
+
KnowledgeBase: 更新后的知识库对象 / Updated knowledge base object
|
|
138
|
+
"""
|
|
139
|
+
return await cls.__get_client().update_async(
|
|
140
|
+
knowledge_base_name, input, config=config
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
@classmethod
|
|
144
|
+
def update_by_name(
|
|
145
|
+
cls,
|
|
146
|
+
knowledge_base_name: str,
|
|
147
|
+
input: KnowledgeBaseUpdateInput,
|
|
148
|
+
config: Optional[Config] = None,
|
|
149
|
+
):
|
|
150
|
+
"""根据名称更新知识库(同步)/ Update knowledge base by name asynchronously
|
|
151
|
+
|
|
152
|
+
Args:
|
|
153
|
+
knowledge_base_name: 知识库名称 / KnowledgeBase name
|
|
154
|
+
input: 知识库更新输入参数 / KnowledgeBase update input parameters
|
|
155
|
+
config: 配置 / Configuration
|
|
156
|
+
|
|
157
|
+
Returns:
|
|
158
|
+
KnowledgeBase: 更新后的知识库对象 / Updated knowledge base object
|
|
159
|
+
"""
|
|
160
|
+
return cls.__get_client().update(
|
|
161
|
+
knowledge_base_name, input, config=config
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
@classmethod
|
|
165
|
+
async def get_by_name_async(
|
|
166
|
+
cls, knowledge_base_name: str, config: Optional[Config] = None
|
|
167
|
+
):
|
|
168
|
+
"""根据名称获取知识库(异步)/ Get knowledge base by name asynchronously
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
knowledge_base_name: 知识库名称 / KnowledgeBase name
|
|
172
|
+
config: 配置 / Configuration
|
|
173
|
+
|
|
174
|
+
Returns:
|
|
175
|
+
KnowledgeBase: 知识库对象 / KnowledgeBase object
|
|
176
|
+
"""
|
|
177
|
+
return await cls.__get_client().get_async(
|
|
178
|
+
knowledge_base_name, config=config
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
@classmethod
|
|
182
|
+
def get_by_name(
|
|
183
|
+
cls, knowledge_base_name: str, config: Optional[Config] = None
|
|
184
|
+
):
|
|
185
|
+
"""根据名称获取知识库(同步)/ Get knowledge base by name asynchronously
|
|
186
|
+
|
|
187
|
+
Args:
|
|
188
|
+
knowledge_base_name: 知识库名称 / KnowledgeBase name
|
|
189
|
+
config: 配置 / Configuration
|
|
190
|
+
|
|
191
|
+
Returns:
|
|
192
|
+
KnowledgeBase: 知识库对象 / KnowledgeBase object
|
|
193
|
+
"""
|
|
194
|
+
return cls.__get_client().get(knowledge_base_name, config=config)
|
|
195
|
+
|
|
196
|
+
@classmethod
|
|
197
|
+
async def _list_page_async(
|
|
198
|
+
cls, page_input: PageableInput, config: Config | None = None, **kwargs
|
|
199
|
+
):
|
|
200
|
+
return await cls.__get_client().list_async(
|
|
201
|
+
input=KnowledgeBaseListInput(
|
|
202
|
+
**kwargs,
|
|
203
|
+
**page_input.model_dump(),
|
|
204
|
+
),
|
|
205
|
+
config=config,
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
@classmethod
|
|
209
|
+
def _list_page(
|
|
210
|
+
cls, page_input: PageableInput, config: Config | None = None, **kwargs
|
|
211
|
+
):
|
|
212
|
+
return cls.__get_client().list(
|
|
213
|
+
input=KnowledgeBaseListInput(
|
|
214
|
+
**kwargs,
|
|
215
|
+
**page_input.model_dump(),
|
|
216
|
+
),
|
|
217
|
+
config=config,
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
@classmethod
|
|
221
|
+
async def list_all_async(
|
|
222
|
+
cls,
|
|
223
|
+
*,
|
|
224
|
+
provider: Optional[str] = None,
|
|
225
|
+
config: Optional[Config] = None,
|
|
226
|
+
) -> List[KnowledgeBaseListOutput]:
|
|
227
|
+
"""列出所有知识库(异步)/ List all knowledge bases asynchronously
|
|
228
|
+
|
|
229
|
+
Args:
|
|
230
|
+
provider: 提供商 / Provider
|
|
231
|
+
config: 配置 / Configuration
|
|
232
|
+
|
|
233
|
+
Returns:
|
|
234
|
+
List[KnowledgeBaseListOutput]: 知识库列表 / KnowledgeBase list
|
|
235
|
+
"""
|
|
236
|
+
return await cls._list_all_async(
|
|
237
|
+
lambda kb: kb.knowledge_base_id or "",
|
|
238
|
+
config=config,
|
|
239
|
+
provider=provider,
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
@classmethod
|
|
243
|
+
def list_all(
|
|
244
|
+
cls,
|
|
245
|
+
*,
|
|
246
|
+
provider: Optional[str] = None,
|
|
247
|
+
config: Optional[Config] = None,
|
|
248
|
+
) -> List[KnowledgeBaseListOutput]:
|
|
249
|
+
"""列出所有知识库(同步)/ List all knowledge bases asynchronously
|
|
250
|
+
|
|
251
|
+
Args:
|
|
252
|
+
provider: 提供商 / Provider
|
|
253
|
+
config: 配置 / Configuration
|
|
254
|
+
|
|
255
|
+
Returns:
|
|
256
|
+
List[KnowledgeBaseListOutput]: 知识库列表 / KnowledgeBase list
|
|
257
|
+
"""
|
|
258
|
+
return cls._list_all(
|
|
259
|
+
lambda kb: kb.knowledge_base_id or "",
|
|
260
|
+
config=config,
|
|
261
|
+
provider=provider,
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
async def update_async(
|
|
265
|
+
self, input: KnowledgeBaseUpdateInput, config: Optional[Config] = None
|
|
266
|
+
):
|
|
267
|
+
"""更新知识库(异步)/ Update knowledge base asynchronously
|
|
268
|
+
|
|
269
|
+
Args:
|
|
270
|
+
input: 知识库更新输入参数 / KnowledgeBase update input parameters
|
|
271
|
+
config: 配置 / Configuration
|
|
272
|
+
|
|
273
|
+
Returns:
|
|
274
|
+
KnowledgeBase: 更新后的知识库对象 / Updated knowledge base object
|
|
275
|
+
"""
|
|
276
|
+
if self.knowledge_base_name is None:
|
|
277
|
+
raise ValueError(
|
|
278
|
+
"knowledge_base_name is required to update a KnowledgeBase"
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
result = await self.update_by_name_async(
|
|
282
|
+
self.knowledge_base_name, input, config=config
|
|
283
|
+
)
|
|
284
|
+
self.update_self(result)
|
|
285
|
+
|
|
286
|
+
return self
|
|
287
|
+
|
|
288
|
+
def update(
|
|
289
|
+
self, input: KnowledgeBaseUpdateInput, config: Optional[Config] = None
|
|
290
|
+
):
|
|
291
|
+
"""更新知识库(同步)/ Update knowledge base asynchronously
|
|
292
|
+
|
|
293
|
+
Args:
|
|
294
|
+
input: 知识库更新输入参数 / KnowledgeBase update input parameters
|
|
295
|
+
config: 配置 / Configuration
|
|
296
|
+
|
|
297
|
+
Returns:
|
|
298
|
+
KnowledgeBase: 更新后的知识库对象 / Updated knowledge base object
|
|
299
|
+
"""
|
|
300
|
+
if self.knowledge_base_name is None:
|
|
301
|
+
raise ValueError(
|
|
302
|
+
"knowledge_base_name is required to update a KnowledgeBase"
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
result = self.update_by_name(
|
|
306
|
+
self.knowledge_base_name, input, config=config
|
|
307
|
+
)
|
|
308
|
+
self.update_self(result)
|
|
309
|
+
|
|
310
|
+
return self
|
|
311
|
+
|
|
312
|
+
async def delete_async(self, config: Optional[Config] = None):
|
|
313
|
+
"""删除知识库(异步)/ Delete knowledge base asynchronously
|
|
314
|
+
|
|
315
|
+
Args:
|
|
316
|
+
config: 配置 / Configuration
|
|
317
|
+
"""
|
|
318
|
+
if self.knowledge_base_name is None:
|
|
319
|
+
raise ValueError(
|
|
320
|
+
"knowledge_base_name is required to delete a KnowledgeBase"
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
return await self.delete_by_name_async(
|
|
324
|
+
self.knowledge_base_name, config=config
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
def delete(self, config: Optional[Config] = None):
|
|
328
|
+
"""删除知识库(同步)/ Delete knowledge base asynchronously
|
|
329
|
+
|
|
330
|
+
Args:
|
|
331
|
+
config: 配置 / Configuration
|
|
332
|
+
"""
|
|
333
|
+
if self.knowledge_base_name is None:
|
|
334
|
+
raise ValueError(
|
|
335
|
+
"knowledge_base_name is required to delete a KnowledgeBase"
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
return self.delete_by_name(self.knowledge_base_name, config=config)
|
|
339
|
+
|
|
340
|
+
async def get_async(self, config: Optional[Config] = None):
|
|
341
|
+
"""刷新知识库信息(异步)/ Refresh knowledge base info asynchronously
|
|
342
|
+
|
|
343
|
+
Args:
|
|
344
|
+
config: 配置 / Configuration
|
|
345
|
+
|
|
346
|
+
Returns:
|
|
347
|
+
KnowledgeBase: 刷新后的知识库对象 / Refreshed knowledge base object
|
|
348
|
+
"""
|
|
349
|
+
if self.knowledge_base_name is None:
|
|
350
|
+
raise ValueError(
|
|
351
|
+
"knowledge_base_name is required to refresh a KnowledgeBase"
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
result = await self.get_by_name_async(
|
|
355
|
+
self.knowledge_base_name, config=config
|
|
356
|
+
)
|
|
357
|
+
self.update_self(result)
|
|
358
|
+
|
|
359
|
+
return self
|
|
360
|
+
|
|
361
|
+
def get(self, config: Optional[Config] = None):
|
|
362
|
+
"""刷新知识库信息(同步)/ Refresh knowledge base info asynchronously
|
|
363
|
+
|
|
364
|
+
Args:
|
|
365
|
+
config: 配置 / Configuration
|
|
366
|
+
|
|
367
|
+
Returns:
|
|
368
|
+
KnowledgeBase: 刷新后的知识库对象 / Refreshed knowledge base object
|
|
369
|
+
"""
|
|
370
|
+
if self.knowledge_base_name is None:
|
|
371
|
+
raise ValueError(
|
|
372
|
+
"knowledge_base_name is required to refresh a KnowledgeBase"
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
result = self.get_by_name(self.knowledge_base_name, config=config)
|
|
376
|
+
self.update_self(result)
|
|
377
|
+
|
|
378
|
+
return self
|
|
379
|
+
|
|
380
|
+
async def refresh_async(self, config: Optional[Config] = None):
|
|
381
|
+
"""刷新知识库信息(异步)/ Refresh knowledge base info asynchronously
|
|
382
|
+
|
|
383
|
+
Args:
|
|
384
|
+
config: 配置 / Configuration
|
|
385
|
+
|
|
386
|
+
Returns:
|
|
387
|
+
KnowledgeBase: 刷新后的知识库对象 / Refreshed knowledge base object
|
|
388
|
+
"""
|
|
389
|
+
return await self.get_async(config=config)
|
|
390
|
+
|
|
391
|
+
# =========================================================================
|
|
392
|
+
# 数据链路方法 / Data API Methods
|
|
393
|
+
# =========================================================================
|
|
394
|
+
|
|
395
|
+
def refresh(self, config: Optional[Config] = None):
|
|
396
|
+
"""刷新知识库信息(同步)/ Refresh knowledge base info asynchronously
|
|
397
|
+
|
|
398
|
+
Args:
|
|
399
|
+
config: 配置 / Configuration
|
|
400
|
+
|
|
401
|
+
Returns:
|
|
402
|
+
KnowledgeBase: 刷新后的知识库对象 / Refreshed knowledge base object
|
|
403
|
+
"""
|
|
404
|
+
return self.get(config=config)
|
|
405
|
+
|
|
406
|
+
# =========================================================================
|
|
407
|
+
# 数据链路方法 / Data API Methods
|
|
408
|
+
# =========================================================================
|
|
409
|
+
|
|
410
|
+
def _get_data_api(self, config: Optional[Config] = None):
|
|
411
|
+
"""获取数据链路 API 实例 / Get data API instance
|
|
412
|
+
|
|
413
|
+
根据当前知识库的 provider 类型返回对应的数据链路 API。
|
|
414
|
+
Returns the corresponding data API based on current knowledge base provider type.
|
|
415
|
+
|
|
416
|
+
Args:
|
|
417
|
+
config: 配置 / Configuration
|
|
418
|
+
|
|
419
|
+
Returns:
|
|
420
|
+
KnowledgeBaseDataAPI: 数据链路 API 实例 / Data API instance
|
|
421
|
+
|
|
422
|
+
Raises:
|
|
423
|
+
ValueError: 如果 provider 未设置 / If provider is not set
|
|
424
|
+
"""
|
|
425
|
+
if self.provider is None:
|
|
426
|
+
raise ValueError("provider is required to get data API")
|
|
427
|
+
|
|
428
|
+
provider = (
|
|
429
|
+
self.provider
|
|
430
|
+
if isinstance(self.provider, KnowledgeBaseProvider)
|
|
431
|
+
else KnowledgeBaseProvider(self.provider)
|
|
432
|
+
)
|
|
433
|
+
|
|
434
|
+
# 转换 provider_settings 和 retrieve_settings 为正确的类型
|
|
435
|
+
# Convert provider_settings and retrieve_settings to correct types
|
|
436
|
+
converted_provider_settings = None
|
|
437
|
+
converted_retrieve_settings = None
|
|
438
|
+
|
|
439
|
+
if provider == KnowledgeBaseProvider.BAILIAN:
|
|
440
|
+
# 百炼设置 / Bailian settings
|
|
441
|
+
if self.provider_settings:
|
|
442
|
+
if isinstance(self.provider_settings, BailianProviderSettings):
|
|
443
|
+
converted_provider_settings = self.provider_settings
|
|
444
|
+
elif isinstance(self.provider_settings, dict):
|
|
445
|
+
converted_provider_settings = BailianProviderSettings(
|
|
446
|
+
**self.provider_settings
|
|
447
|
+
)
|
|
448
|
+
|
|
449
|
+
if self.retrieve_settings:
|
|
450
|
+
if isinstance(self.retrieve_settings, BailianRetrieveSettings):
|
|
451
|
+
converted_retrieve_settings = self.retrieve_settings
|
|
452
|
+
elif isinstance(self.retrieve_settings, dict):
|
|
453
|
+
converted_retrieve_settings = BailianRetrieveSettings(
|
|
454
|
+
**self.retrieve_settings
|
|
455
|
+
)
|
|
456
|
+
|
|
457
|
+
elif provider == KnowledgeBaseProvider.RAGFLOW:
|
|
458
|
+
# RagFlow 设置 / RagFlow settings
|
|
459
|
+
if self.provider_settings:
|
|
460
|
+
if isinstance(self.provider_settings, RagFlowProviderSettings):
|
|
461
|
+
converted_provider_settings = self.provider_settings
|
|
462
|
+
elif isinstance(self.provider_settings, dict):
|
|
463
|
+
converted_provider_settings = RagFlowProviderSettings(
|
|
464
|
+
**self.provider_settings
|
|
465
|
+
)
|
|
466
|
+
|
|
467
|
+
if self.retrieve_settings:
|
|
468
|
+
if isinstance(self.retrieve_settings, RagFlowRetrieveSettings):
|
|
469
|
+
converted_retrieve_settings = self.retrieve_settings
|
|
470
|
+
elif isinstance(self.retrieve_settings, dict):
|
|
471
|
+
converted_retrieve_settings = RagFlowRetrieveSettings(
|
|
472
|
+
**self.retrieve_settings
|
|
473
|
+
)
|
|
474
|
+
|
|
475
|
+
return get_data_api(
|
|
476
|
+
provider=provider,
|
|
477
|
+
knowledge_base_name=self.knowledge_base_name or "",
|
|
478
|
+
config=config,
|
|
479
|
+
provider_settings=converted_provider_settings,
|
|
480
|
+
retrieve_settings=converted_retrieve_settings,
|
|
481
|
+
credential_name=self.credential_name,
|
|
482
|
+
)
|
|
483
|
+
|
|
484
|
+
async def retrieve_async(
|
|
485
|
+
self,
|
|
486
|
+
query: str,
|
|
487
|
+
config: Optional[Config] = None,
|
|
488
|
+
) -> Dict[str, Any]:
|
|
489
|
+
"""检索知识库(异步)/ Retrieve from knowledge base asynchronously
|
|
490
|
+
|
|
491
|
+
根据当前知识库的 provider 类型和配置执行检索。
|
|
492
|
+
Executes retrieval based on current knowledge base provider type and configuration.
|
|
493
|
+
|
|
494
|
+
Args:
|
|
495
|
+
query: 查询文本 / Query text
|
|
496
|
+
config: 配置 / Configuration
|
|
497
|
+
|
|
498
|
+
Returns:
|
|
499
|
+
Dict[str, Any]: 检索结果 / Retrieval results
|
|
500
|
+
"""
|
|
501
|
+
data_api = self._get_data_api(config)
|
|
502
|
+
return await data_api.retrieve_async(query, config=config)
|
|
503
|
+
|
|
504
|
+
def retrieve(
|
|
505
|
+
self,
|
|
506
|
+
query: str,
|
|
507
|
+
config: Optional[Config] = None,
|
|
508
|
+
) -> Dict[str, Any]:
|
|
509
|
+
"""检索知识库(同步)/ Retrieve from knowledge base asynchronously
|
|
510
|
+
|
|
511
|
+
根据当前知识库的 provider 类型和配置执行检索。
|
|
512
|
+
Executes retrieval based on current knowledge base provider type and configuration.
|
|
513
|
+
|
|
514
|
+
Args:
|
|
515
|
+
query: 查询文本 / Query text
|
|
516
|
+
config: 配置 / Configuration
|
|
517
|
+
|
|
518
|
+
Returns:
|
|
519
|
+
Dict[str, Any]: 检索结果 / Retrieval results
|
|
520
|
+
"""
|
|
521
|
+
data_api = self._get_data_api(config)
|
|
522
|
+
return data_api.retrieve(query, config=config)
|
|
523
|
+
|
|
524
|
+
@classmethod
|
|
525
|
+
async def _safe_get_kb_async(
|
|
526
|
+
cls,
|
|
527
|
+
kb_name: str,
|
|
528
|
+
config: Optional[Config] = None,
|
|
529
|
+
) -> Any:
|
|
530
|
+
"""安全获取知识库(异步)/ Safely get knowledge base asynchronously
|
|
531
|
+
|
|
532
|
+
Args:
|
|
533
|
+
kb_name: 知识库名称 / Knowledge base name
|
|
534
|
+
config: 配置 / Configuration
|
|
535
|
+
|
|
536
|
+
Returns:
|
|
537
|
+
Any: 知识库对象或异常 / Knowledge base object or exception
|
|
538
|
+
"""
|
|
539
|
+
try:
|
|
540
|
+
return await cls.get_by_name_async(kb_name, config=config)
|
|
541
|
+
except Exception as e:
|
|
542
|
+
return e
|
|
543
|
+
|
|
544
|
+
@classmethod
|
|
545
|
+
def _safe_get_kb(
|
|
546
|
+
cls,
|
|
547
|
+
kb_name: str,
|
|
548
|
+
config: Optional[Config] = None,
|
|
549
|
+
) -> Any:
|
|
550
|
+
"""安全获取知识库(同步)/ Safely get knowledge base asynchronously
|
|
551
|
+
|
|
552
|
+
Args:
|
|
553
|
+
kb_name: 知识库名称 / Knowledge base name
|
|
554
|
+
config: 配置 / Configuration
|
|
555
|
+
|
|
556
|
+
Returns:
|
|
557
|
+
Any: 知识库对象或异常 / Knowledge base object or exception
|
|
558
|
+
"""
|
|
559
|
+
try:
|
|
560
|
+
return cls.get_by_name(kb_name, config=config)
|
|
561
|
+
except Exception as e:
|
|
562
|
+
return e
|
|
563
|
+
|
|
564
|
+
@classmethod
|
|
565
|
+
async def _safe_retrieve_kb_async(
|
|
566
|
+
cls,
|
|
567
|
+
kb_name: str,
|
|
568
|
+
kb_or_error: Any,
|
|
569
|
+
query: str,
|
|
570
|
+
config: Optional[Config] = None,
|
|
571
|
+
) -> Dict[str, Any]:
|
|
572
|
+
"""安全执行知识库检索(异步)/ Safely retrieve from knowledge base asynchronously
|
|
573
|
+
|
|
574
|
+
Args:
|
|
575
|
+
kb_name: 知识库名称 / Knowledge base name
|
|
576
|
+
kb_or_error: 知识库对象或异常 / Knowledge base object or exception
|
|
577
|
+
query: 查询文本 / Query text
|
|
578
|
+
config: 配置 / Configuration
|
|
579
|
+
|
|
580
|
+
Returns:
|
|
581
|
+
Dict[str, Any]: 检索结果 / Retrieval results
|
|
582
|
+
"""
|
|
583
|
+
if isinstance(kb_or_error, Exception):
|
|
584
|
+
logger.warning(
|
|
585
|
+
f"Failed to get knowledge base '{kb_name}': {kb_or_error}"
|
|
586
|
+
)
|
|
587
|
+
return {
|
|
588
|
+
"data": f"Failed to retrieve: {kb_or_error}",
|
|
589
|
+
"query": query,
|
|
590
|
+
"knowledge_base_name": kb_name,
|
|
591
|
+
"error": True,
|
|
592
|
+
}
|
|
593
|
+
try:
|
|
594
|
+
return await kb_or_error.retrieve_async(query, config=config)
|
|
595
|
+
except Exception as e:
|
|
596
|
+
logger.warning(
|
|
597
|
+
f"Failed to retrieve from knowledge base '{kb_name}': {e}"
|
|
598
|
+
)
|
|
599
|
+
return {
|
|
600
|
+
"data": f"Failed to retrieve: {e}",
|
|
601
|
+
"query": query,
|
|
602
|
+
"knowledge_base_name": kb_name,
|
|
603
|
+
"error": True,
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
@classmethod
|
|
607
|
+
def _safe_retrieve_kb(
|
|
608
|
+
cls,
|
|
609
|
+
kb_name: str,
|
|
610
|
+
kb_or_error: Any,
|
|
611
|
+
query: str,
|
|
612
|
+
config: Optional[Config] = None,
|
|
613
|
+
) -> Dict[str, Any]:
|
|
614
|
+
"""安全执行知识库检索(同步)/ Safely retrieve from knowledge base asynchronously
|
|
615
|
+
|
|
616
|
+
Args:
|
|
617
|
+
kb_name: 知识库名称 / Knowledge base name
|
|
618
|
+
kb_or_error: 知识库对象或异常 / Knowledge base object or exception
|
|
619
|
+
query: 查询文本 / Query text
|
|
620
|
+
config: 配置 / Configuration
|
|
621
|
+
|
|
622
|
+
Returns:
|
|
623
|
+
Dict[str, Any]: 检索结果 / Retrieval results
|
|
624
|
+
"""
|
|
625
|
+
if isinstance(kb_or_error, Exception):
|
|
626
|
+
logger.warning(
|
|
627
|
+
f"Failed to get knowledge base '{kb_name}': {kb_or_error}"
|
|
628
|
+
)
|
|
629
|
+
return {
|
|
630
|
+
"data": f"Failed to retrieve: {kb_or_error}",
|
|
631
|
+
"query": query,
|
|
632
|
+
"knowledge_base_name": kb_name,
|
|
633
|
+
"error": True,
|
|
634
|
+
}
|
|
635
|
+
try:
|
|
636
|
+
return kb_or_error.retrieve(query, config=config)
|
|
637
|
+
except Exception as e:
|
|
638
|
+
logger.warning(
|
|
639
|
+
f"Failed to retrieve from knowledge base '{kb_name}': {e}"
|
|
640
|
+
)
|
|
641
|
+
return {
|
|
642
|
+
"data": f"Failed to retrieve: {e}",
|
|
643
|
+
"query": query,
|
|
644
|
+
"knowledge_base_name": kb_name,
|
|
645
|
+
"error": True,
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
@classmethod
|
|
649
|
+
async def multi_retrieve_async(
|
|
650
|
+
cls,
|
|
651
|
+
query: str,
|
|
652
|
+
knowledge_base_names: List[str],
|
|
653
|
+
config: Optional[Config] = None,
|
|
654
|
+
) -> Dict[str, Any]:
|
|
655
|
+
"""多知识库检索(异步)/ Multi knowledge base retrieval asynchronously
|
|
656
|
+
|
|
657
|
+
根据知识库名称列表进行检索,自动获取各知识库的配置并执行检索。
|
|
658
|
+
如果某个知识库查询失败,不影响其他知识库的查询。
|
|
659
|
+
Retrieves from multiple knowledge bases by name list, automatically fetching
|
|
660
|
+
configuration for each knowledge base and executing retrieval.
|
|
661
|
+
If one knowledge base fails, it won't affect other knowledge bases.
|
|
662
|
+
|
|
663
|
+
Args:
|
|
664
|
+
query: 查询文本 / Query text
|
|
665
|
+
knowledge_base_names: 知识库名称列表 / List of knowledge base names
|
|
666
|
+
config: 配置 / Configuration
|
|
667
|
+
|
|
668
|
+
Returns:
|
|
669
|
+
Dict[str, Any]: 检索结果,按知识库名称分组 / Retrieval results grouped by knowledge base name
|
|
670
|
+
"""
|
|
671
|
+
# 1. 根据 knowledge_base_names 并发获取各知识库配置(安全方式)
|
|
672
|
+
# Fetch all knowledge bases concurrently by name (safely)
|
|
673
|
+
knowledge_base_results = await asyncio.gather(*[
|
|
674
|
+
cls._safe_get_kb_async(name, config=config)
|
|
675
|
+
for name in knowledge_base_names
|
|
676
|
+
])
|
|
677
|
+
|
|
678
|
+
# 2. 并发执行各知识库的检索(安全方式)
|
|
679
|
+
# Execute retrieval for each knowledge base concurrently (safely)
|
|
680
|
+
retrieve_results = await asyncio.gather(*[
|
|
681
|
+
cls._safe_retrieve_kb_async(
|
|
682
|
+
kb_name, kb_or_error, query, config=config
|
|
683
|
+
)
|
|
684
|
+
for kb_name, kb_or_error in zip(
|
|
685
|
+
knowledge_base_names, knowledge_base_results
|
|
686
|
+
)
|
|
687
|
+
])
|
|
688
|
+
|
|
689
|
+
# 3. 合并返回结果,按知识库名称分组
|
|
690
|
+
# Merge results, grouped by knowledge base name
|
|
691
|
+
results: Dict[str, Any] = {}
|
|
692
|
+
for kb_name, result in zip(knowledge_base_names, retrieve_results):
|
|
693
|
+
results[kb_name] = result
|
|
694
|
+
|
|
695
|
+
return {
|
|
696
|
+
"results": results,
|
|
697
|
+
"query": query,
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
@classmethod
|
|
701
|
+
def multi_retrieve(
|
|
702
|
+
cls,
|
|
703
|
+
query: str,
|
|
704
|
+
knowledge_base_names: List[str],
|
|
705
|
+
config: Optional[Config] = None,
|
|
706
|
+
) -> Dict[str, Any]:
|
|
707
|
+
"""多知识库检索(同步)/ Multi knowledge base retrieval asynchronously
|
|
708
|
+
|
|
709
|
+
根据知识库名称列表进行检索,自动获取各知识库的配置并执行检索。
|
|
710
|
+
如果某个知识库查询失败,不影响其他知识库的查询。
|
|
711
|
+
Retrieves from multiple knowledge bases by name list, automatically fetching
|
|
712
|
+
configuration for each knowledge base and executing retrieval.
|
|
713
|
+
If one knowledge base fails, it won't affect other knowledge bases.
|
|
714
|
+
|
|
715
|
+
Args:
|
|
716
|
+
query: 查询文本 / Query text
|
|
717
|
+
knowledge_base_names: 知识库名称列表 / List of knowledge base names
|
|
718
|
+
config: 配置 / Configuration
|
|
719
|
+
|
|
720
|
+
Returns:
|
|
721
|
+
Dict[str, Any]: 检索结果,按知识库名称分组 / Retrieval results grouped by knowledge base name
|
|
722
|
+
"""
|
|
723
|
+
# 1. 根据 knowledge_base_names 并发获取各知识库配置(安全方式)
|
|
724
|
+
# Fetch all knowledge bases concurrently by name (safely)
|
|
725
|
+
knowledge_base_results = [
|
|
726
|
+
cls._safe_get_kb(name, config=config)
|
|
727
|
+
for name in knowledge_base_names
|
|
728
|
+
]
|
|
729
|
+
|
|
730
|
+
# 2. 并发执行各知识库的检索(安全方式)
|
|
731
|
+
# Execute retrieval for each knowledge base concurrently (safely)
|
|
732
|
+
retrieve_results = [
|
|
733
|
+
cls._safe_retrieve_kb(kb_name, kb_or_error, query, config=config)
|
|
734
|
+
for kb_name, kb_or_error in zip(
|
|
735
|
+
knowledge_base_names, knowledge_base_results
|
|
736
|
+
)
|
|
737
|
+
]
|
|
738
|
+
|
|
739
|
+
# 3. 合并返回结果,按知识库名称分组
|
|
740
|
+
# Merge results, grouped by knowledge base name
|
|
741
|
+
results: Dict[str, Any] = {}
|
|
742
|
+
for kb_name, result in zip(knowledge_base_names, retrieve_results):
|
|
743
|
+
results[kb_name] = result
|
|
744
|
+
|
|
745
|
+
return {
|
|
746
|
+
"results": results,
|
|
747
|
+
"query": query,
|
|
748
|
+
}
|