agentscope-runtime 0.2.0b1__py3-none-any.whl → 1.0.0__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.
- agentscope_runtime/adapters/__init__.py +0 -0
- agentscope_runtime/adapters/agentscope/__init__.py +0 -0
- agentscope_runtime/adapters/agentscope/long_term_memory/__init__.py +6 -0
- agentscope_runtime/adapters/agentscope/long_term_memory/_long_term_memory_adapter.py +258 -0
- agentscope_runtime/adapters/agentscope/memory/__init__.py +6 -0
- agentscope_runtime/adapters/agentscope/memory/_memory_adapter.py +152 -0
- agentscope_runtime/adapters/agentscope/message.py +535 -0
- agentscope_runtime/adapters/agentscope/stream.py +506 -0
- agentscope_runtime/adapters/agentscope/tool/__init__.py +9 -0
- agentscope_runtime/adapters/agentscope/tool/sandbox_tool.py +69 -0
- agentscope_runtime/adapters/agentscope/tool/tool.py +233 -0
- agentscope_runtime/adapters/autogen/__init__.py +0 -0
- agentscope_runtime/adapters/autogen/tool/__init__.py +7 -0
- agentscope_runtime/adapters/autogen/tool/tool.py +211 -0
- agentscope_runtime/adapters/text/__init__.py +0 -0
- agentscope_runtime/adapters/text/stream.py +29 -0
- agentscope_runtime/common/collections/redis_mapping.py +4 -1
- agentscope_runtime/common/container_clients/fc_client.py +855 -0
- agentscope_runtime/common/container_clients/kubernetes_client.py +6 -13
- agentscope_runtime/common/utils/__init__.py +0 -0
- agentscope_runtime/common/utils/lazy_loader.py +57 -0
- agentscope_runtime/engine/__init__.py +25 -18
- agentscope_runtime/engine/app/agent_app.py +161 -91
- agentscope_runtime/engine/app/base_app.py +4 -118
- agentscope_runtime/engine/constant.py +8 -0
- agentscope_runtime/engine/deployers/__init__.py +8 -0
- agentscope_runtime/engine/deployers/adapter/__init__.py +2 -0
- agentscope_runtime/engine/deployers/adapter/a2a/a2a_adapter_utils.py +0 -21
- agentscope_runtime/engine/deployers/adapter/a2a/a2a_protocol_adapter.py +28 -9
- agentscope_runtime/engine/deployers/adapter/responses/__init__.py +2 -0
- agentscope_runtime/engine/deployers/adapter/responses/response_api_adapter_utils.py +5 -2
- agentscope_runtime/engine/deployers/adapter/responses/response_api_protocol_adapter.py +1 -1
- agentscope_runtime/engine/deployers/agentrun_deployer.py +2541 -0
- agentscope_runtime/engine/deployers/cli_fc_deploy.py +1 -1
- agentscope_runtime/engine/deployers/kubernetes_deployer.py +9 -21
- agentscope_runtime/engine/deployers/local_deployer.py +47 -74
- agentscope_runtime/engine/deployers/modelstudio_deployer.py +216 -50
- agentscope_runtime/engine/deployers/utils/app_runner_utils.py +29 -0
- agentscope_runtime/engine/deployers/utils/detached_app.py +510 -0
- agentscope_runtime/engine/deployers/utils/docker_image_utils/__init__.py +1 -1
- agentscope_runtime/engine/deployers/utils/docker_image_utils/dockerfile_generator.py +1 -1
- agentscope_runtime/engine/deployers/utils/docker_image_utils/{runner_image_factory.py → image_factory.py} +121 -61
- agentscope_runtime/engine/deployers/utils/package.py +693 -0
- agentscope_runtime/engine/deployers/utils/service_utils/__init__.py +0 -5
- agentscope_runtime/engine/deployers/utils/service_utils/fastapi_factory.py +301 -282
- agentscope_runtime/engine/deployers/utils/service_utils/fastapi_templates.py +2 -4
- agentscope_runtime/engine/deployers/utils/service_utils/process_manager.py +23 -1
- agentscope_runtime/engine/deployers/utils/templates/app_main.py.j2 +84 -0
- agentscope_runtime/engine/deployers/utils/templates/runner_main.py.j2 +95 -0
- agentscope_runtime/engine/deployers/utils/{service_utils → templates}/standalone_main.py.j2 +0 -45
- agentscope_runtime/engine/deployers/utils/wheel_packager.py +119 -18
- agentscope_runtime/engine/helpers/runner.py +40 -0
- agentscope_runtime/engine/runner.py +171 -130
- agentscope_runtime/engine/schemas/agent_schemas.py +114 -3
- agentscope_runtime/engine/schemas/modelstudio_llm.py +4 -2
- agentscope_runtime/engine/schemas/oai_llm.py +23 -23
- agentscope_runtime/engine/schemas/response_api.py +65 -0
- agentscope_runtime/engine/schemas/session.py +24 -0
- agentscope_runtime/engine/services/__init__.py +0 -9
- agentscope_runtime/engine/services/agent_state/__init__.py +16 -0
- agentscope_runtime/engine/services/agent_state/redis_state_service.py +113 -0
- agentscope_runtime/engine/services/agent_state/state_service.py +179 -0
- agentscope_runtime/engine/services/memory/__init__.py +24 -0
- agentscope_runtime/engine/services/{mem0_memory_service.py → memory/mem0_memory_service.py} +17 -13
- agentscope_runtime/engine/services/{memory_service.py → memory/memory_service.py} +28 -7
- agentscope_runtime/engine/services/{redis_memory_service.py → memory/redis_memory_service.py} +1 -1
- agentscope_runtime/engine/services/{reme_personal_memory_service.py → memory/reme_personal_memory_service.py} +9 -6
- agentscope_runtime/engine/services/{reme_task_memory_service.py → memory/reme_task_memory_service.py} +2 -2
- agentscope_runtime/engine/services/{tablestore_memory_service.py → memory/tablestore_memory_service.py} +16 -19
- agentscope_runtime/engine/services/sandbox/__init__.py +13 -0
- agentscope_runtime/engine/services/{sandbox_service.py → sandbox/sandbox_service.py} +86 -71
- agentscope_runtime/engine/services/session_history/__init__.py +23 -0
- agentscope_runtime/engine/services/{redis_session_history_service.py → session_history/redis_session_history_service.py} +3 -2
- agentscope_runtime/engine/services/{session_history_service.py → session_history/session_history_service.py} +44 -34
- agentscope_runtime/engine/services/{tablestore_session_history_service.py → session_history/tablestore_session_history_service.py} +14 -19
- agentscope_runtime/engine/services/utils/tablestore_service_utils.py +2 -2
- agentscope_runtime/engine/tracing/base.py +10 -9
- agentscope_runtime/engine/tracing/message_util.py +1 -1
- agentscope_runtime/engine/tracing/tracing_util.py +7 -2
- agentscope_runtime/engine/tracing/wrapper.py +49 -31
- agentscope_runtime/sandbox/__init__.py +10 -2
- agentscope_runtime/sandbox/box/agentbay/__init__.py +4 -0
- agentscope_runtime/sandbox/box/agentbay/agentbay_sandbox.py +559 -0
- agentscope_runtime/sandbox/box/base/base_sandbox.py +12 -0
- agentscope_runtime/sandbox/box/browser/browser_sandbox.py +115 -11
- agentscope_runtime/sandbox/box/cloud/__init__.py +4 -0
- agentscope_runtime/sandbox/box/cloud/cloud_sandbox.py +254 -0
- agentscope_runtime/sandbox/box/filesystem/filesystem_sandbox.py +66 -0
- agentscope_runtime/sandbox/box/gui/gui_sandbox.py +42 -0
- agentscope_runtime/sandbox/box/mobile/__init__.py +4 -0
- agentscope_runtime/sandbox/box/mobile/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/mobile/mobile_sandbox.py +216 -0
- agentscope_runtime/sandbox/box/training_box/training_box.py +2 -44
- agentscope_runtime/sandbox/client/http_client.py +1 -0
- agentscope_runtime/sandbox/enums.py +2 -1
- agentscope_runtime/sandbox/manager/sandbox_manager.py +15 -2
- agentscope_runtime/sandbox/manager/server/app.py +12 -0
- agentscope_runtime/sandbox/manager/server/config.py +19 -0
- agentscope_runtime/sandbox/model/manager_config.py +79 -2
- agentscope_runtime/sandbox/utils.py +0 -18
- agentscope_runtime/tools/RAGs/__init__.py +0 -0
- agentscope_runtime/tools/RAGs/modelstudio_rag.py +377 -0
- agentscope_runtime/tools/RAGs/modelstudio_rag_lite.py +219 -0
- agentscope_runtime/tools/__init__.py +119 -0
- agentscope_runtime/tools/_constants.py +18 -0
- agentscope_runtime/tools/alipay/__init__.py +4 -0
- agentscope_runtime/tools/alipay/base.py +334 -0
- agentscope_runtime/tools/alipay/payment.py +835 -0
- agentscope_runtime/tools/alipay/subscribe.py +551 -0
- agentscope_runtime/tools/base.py +264 -0
- agentscope_runtime/tools/cli/__init__.py +0 -0
- agentscope_runtime/tools/cli/modelstudio_mcp_server.py +78 -0
- agentscope_runtime/tools/generations/__init__.py +75 -0
- agentscope_runtime/tools/generations/async_image_to_video.py +350 -0
- agentscope_runtime/tools/generations/async_image_to_video_wan25.py +366 -0
- agentscope_runtime/tools/generations/async_speech_to_video.py +422 -0
- agentscope_runtime/tools/generations/async_text_to_video.py +320 -0
- agentscope_runtime/tools/generations/async_text_to_video_wan25.py +334 -0
- agentscope_runtime/tools/generations/image_edit.py +208 -0
- agentscope_runtime/tools/generations/image_edit_wan25.py +193 -0
- agentscope_runtime/tools/generations/image_generation.py +202 -0
- agentscope_runtime/tools/generations/image_generation_wan25.py +201 -0
- agentscope_runtime/tools/generations/image_style_repaint.py +208 -0
- agentscope_runtime/tools/generations/image_to_video.py +233 -0
- agentscope_runtime/tools/generations/qwen_image_edit.py +205 -0
- agentscope_runtime/tools/generations/qwen_image_generation.py +214 -0
- agentscope_runtime/tools/generations/qwen_text_to_speech.py +154 -0
- agentscope_runtime/tools/generations/speech_to_text.py +260 -0
- agentscope_runtime/tools/generations/speech_to_video.py +314 -0
- agentscope_runtime/tools/generations/text_to_video.py +221 -0
- agentscope_runtime/tools/mcp_wrapper.py +215 -0
- agentscope_runtime/tools/realtime_clients/__init__.py +13 -0
- agentscope_runtime/tools/realtime_clients/asr_client.py +27 -0
- agentscope_runtime/tools/realtime_clients/azure_asr_client.py +195 -0
- agentscope_runtime/tools/realtime_clients/azure_tts_client.py +383 -0
- agentscope_runtime/tools/realtime_clients/modelstudio_asr_client.py +151 -0
- agentscope_runtime/tools/realtime_clients/modelstudio_tts_client.py +199 -0
- agentscope_runtime/tools/realtime_clients/realtime_tool.py +55 -0
- agentscope_runtime/tools/realtime_clients/tts_client.py +33 -0
- agentscope_runtime/tools/searches/__init__.py +3 -0
- agentscope_runtime/tools/searches/modelstudio_search.py +877 -0
- agentscope_runtime/tools/searches/modelstudio_search_lite.py +310 -0
- agentscope_runtime/tools/utils/__init__.py +0 -0
- agentscope_runtime/tools/utils/api_key_util.py +45 -0
- agentscope_runtime/tools/utils/crypto_utils.py +99 -0
- agentscope_runtime/tools/utils/mcp_util.py +35 -0
- agentscope_runtime/version.py +1 -1
- {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/METADATA +244 -168
- agentscope_runtime-1.0.0.dist-info/RECORD +240 -0
- {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/entry_points.txt +1 -0
- agentscope_runtime/engine/agents/__init__.py +0 -2
- agentscope_runtime/engine/agents/agentscope_agent.py +0 -488
- agentscope_runtime/engine/agents/agno_agent.py +0 -222
- agentscope_runtime/engine/agents/autogen_agent.py +0 -250
- agentscope_runtime/engine/agents/base_agent.py +0 -29
- agentscope_runtime/engine/agents/langgraph_agent.py +0 -59
- agentscope_runtime/engine/agents/utils.py +0 -53
- agentscope_runtime/engine/deployers/utils/package_project_utils.py +0 -1163
- agentscope_runtime/engine/deployers/utils/service_utils/service_config.py +0 -75
- agentscope_runtime/engine/deployers/utils/service_utils/service_factory.py +0 -220
- agentscope_runtime/engine/helpers/helper.py +0 -179
- agentscope_runtime/engine/schemas/context.py +0 -54
- agentscope_runtime/engine/services/context_manager.py +0 -164
- agentscope_runtime/engine/services/environment_manager.py +0 -50
- agentscope_runtime/engine/services/manager.py +0 -174
- agentscope_runtime/engine/services/rag_service.py +0 -195
- agentscope_runtime/engine/services/tablestore_rag_service.py +0 -143
- agentscope_runtime/sandbox/tools/__init__.py +0 -12
- agentscope_runtime/sandbox/tools/base/__init__.py +0 -8
- agentscope_runtime/sandbox/tools/base/tool.py +0 -52
- agentscope_runtime/sandbox/tools/browser/__init__.py +0 -57
- agentscope_runtime/sandbox/tools/browser/tool.py +0 -597
- agentscope_runtime/sandbox/tools/filesystem/__init__.py +0 -32
- agentscope_runtime/sandbox/tools/filesystem/tool.py +0 -319
- agentscope_runtime/sandbox/tools/function_tool.py +0 -321
- agentscope_runtime/sandbox/tools/gui/__init__.py +0 -7
- agentscope_runtime/sandbox/tools/gui/tool.py +0 -77
- agentscope_runtime/sandbox/tools/mcp_tool.py +0 -195
- agentscope_runtime/sandbox/tools/sandbox_tool.py +0 -104
- agentscope_runtime/sandbox/tools/tool.py +0 -238
- agentscope_runtime/sandbox/tools/utils.py +0 -68
- agentscope_runtime-0.2.0b1.dist-info/RECORD +0 -183
- {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/WHEEL +0 -0
- {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/licenses/LICENSE +0 -0
- {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/top_level.txt +0 -0
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
from typing import List
|
|
3
|
-
from contextlib import asynccontextmanager
|
|
4
|
-
|
|
5
|
-
from .manager import ServiceManager
|
|
6
|
-
from .sandbox_service import SandboxService
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class EnvironmentManager(ServiceManager):
|
|
10
|
-
"""
|
|
11
|
-
The EnvironmentManager class for managing environment-related services.
|
|
12
|
-
"""
|
|
13
|
-
|
|
14
|
-
def __init__(self, sandbox_service: SandboxService = None):
|
|
15
|
-
self._sandbox_service = sandbox_service
|
|
16
|
-
super().__init__()
|
|
17
|
-
|
|
18
|
-
def _register_default_services(self):
|
|
19
|
-
"""Register default services for environment management."""
|
|
20
|
-
self._sandbox_service = self._sandbox_service or SandboxService()
|
|
21
|
-
self.register_service("sandbox", self._sandbox_service)
|
|
22
|
-
|
|
23
|
-
def connect_sandbox(
|
|
24
|
-
self,
|
|
25
|
-
session_id,
|
|
26
|
-
user_id,
|
|
27
|
-
env_types=None,
|
|
28
|
-
tools=None,
|
|
29
|
-
) -> List:
|
|
30
|
-
return self._sandbox_service.connect(
|
|
31
|
-
session_id,
|
|
32
|
-
user_id,
|
|
33
|
-
env_types=env_types,
|
|
34
|
-
tools=tools,
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
def release_sandbox(self, session_id, user_id):
|
|
38
|
-
return self._sandbox_service.release(session_id, user_id)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
@asynccontextmanager
|
|
42
|
-
async def create_environment_manager(
|
|
43
|
-
sandbox_service: SandboxService = None,
|
|
44
|
-
):
|
|
45
|
-
manager = EnvironmentManager(
|
|
46
|
-
sandbox_service=sandbox_service,
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
async with manager:
|
|
50
|
-
yield manager
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
import logging
|
|
3
|
-
from contextlib import AsyncExitStack
|
|
4
|
-
from typing import Dict, Any, Type, List
|
|
5
|
-
from abc import ABC, abstractmethod
|
|
6
|
-
|
|
7
|
-
from agentscope_runtime.engine import Service
|
|
8
|
-
|
|
9
|
-
logger = logging.getLogger(__name__)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class ServiceManager(ABC):
|
|
13
|
-
"""
|
|
14
|
-
Abstract base class for service managers.
|
|
15
|
-
Provides common functionality for service registration and lifecycle
|
|
16
|
-
management.
|
|
17
|
-
"""
|
|
18
|
-
|
|
19
|
-
def __init__(self):
|
|
20
|
-
self.services = []
|
|
21
|
-
self.service_instances = {}
|
|
22
|
-
self._exit_stack = AsyncExitStack()
|
|
23
|
-
|
|
24
|
-
# Initialize default services
|
|
25
|
-
self._register_default_services()
|
|
26
|
-
|
|
27
|
-
@abstractmethod
|
|
28
|
-
def _register_default_services(self):
|
|
29
|
-
"""
|
|
30
|
-
Register default services for this manager. Override in
|
|
31
|
-
subclasses.
|
|
32
|
-
"""
|
|
33
|
-
|
|
34
|
-
def register(self, service_class: Type, *args, name: str = None, **kwargs):
|
|
35
|
-
"""
|
|
36
|
-
Register a service.
|
|
37
|
-
|
|
38
|
-
Args:
|
|
39
|
-
service_class: The class of the service to register.
|
|
40
|
-
*args: Positional arguments for service initialization.
|
|
41
|
-
name: Optional service name. Defaults to class name without
|
|
42
|
-
'Service' suffix and converted to lowercase.
|
|
43
|
-
**kwargs: Keyword arguments for service initialization.
|
|
44
|
-
|
|
45
|
-
Returns:
|
|
46
|
-
self: For method chaining
|
|
47
|
-
"""
|
|
48
|
-
if name is None:
|
|
49
|
-
name = service_class.__name__.replace("Service", "").lower()
|
|
50
|
-
|
|
51
|
-
# Check if service name already exists
|
|
52
|
-
if any(service[3] == name for service in self.services):
|
|
53
|
-
raise ValueError(
|
|
54
|
-
f"Service with name '{name}' is already registered",
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
self.services.append((service_class, args, kwargs, name))
|
|
58
|
-
logger.debug(f"Registered service: {name} ({service_class.__name__})")
|
|
59
|
-
return self
|
|
60
|
-
|
|
61
|
-
def register_service(self, name: str, service: Service):
|
|
62
|
-
"""Register an already instantiated service.
|
|
63
|
-
|
|
64
|
-
Args:
|
|
65
|
-
name: Service name
|
|
66
|
-
service: Service instance
|
|
67
|
-
|
|
68
|
-
Returns:
|
|
69
|
-
self: For method chaining
|
|
70
|
-
"""
|
|
71
|
-
if name in self.service_instances:
|
|
72
|
-
raise ValueError(
|
|
73
|
-
f"Service with name '{name}' is already registered",
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
self.service_instances[name] = service
|
|
77
|
-
logger.debug(f"Registered service instance: {name}")
|
|
78
|
-
return self
|
|
79
|
-
|
|
80
|
-
async def __aenter__(self):
|
|
81
|
-
"""Start all registered services using AsyncExitStack."""
|
|
82
|
-
try:
|
|
83
|
-
# Track services that were registered with register() to avoid
|
|
84
|
-
# duplicate processing
|
|
85
|
-
registered_names = set()
|
|
86
|
-
|
|
87
|
-
# Start services that were registered with register()
|
|
88
|
-
for service_class, args, kwargs, name in self.services:
|
|
89
|
-
logger.debug(f"Starting service: {name}")
|
|
90
|
-
instance = service_class(*args, **kwargs)
|
|
91
|
-
|
|
92
|
-
# Use AsyncExitStack to manage the context
|
|
93
|
-
await self._exit_stack.enter_async_context(instance)
|
|
94
|
-
self.service_instances[name] = instance
|
|
95
|
-
registered_names.add(name) # Track this service as processed
|
|
96
|
-
logger.debug(f"Successfully started service: {name}")
|
|
97
|
-
|
|
98
|
-
# Start services that were registered with register_service()
|
|
99
|
-
# These services are already instantiated, just need to enter
|
|
100
|
-
# their context
|
|
101
|
-
for name, service in list(self.service_instances.items()):
|
|
102
|
-
if (
|
|
103
|
-
name not in registered_names
|
|
104
|
-
): # Only process services not from register() method
|
|
105
|
-
logger.debug(f"Starting pre-instantiated service: {name}")
|
|
106
|
-
await self._exit_stack.enter_async_context(service)
|
|
107
|
-
logger.debug(
|
|
108
|
-
f"Successfully started pre-instantiated service:"
|
|
109
|
-
f" {name}",
|
|
110
|
-
)
|
|
111
|
-
|
|
112
|
-
return self
|
|
113
|
-
|
|
114
|
-
except Exception as e:
|
|
115
|
-
logger.error(f"Failed to start services: {e}")
|
|
116
|
-
# Ensure proper cleanup if initialization fails
|
|
117
|
-
await self._exit_stack.aclose()
|
|
118
|
-
raise
|
|
119
|
-
|
|
120
|
-
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
121
|
-
"""Close all services using AsyncExitStack."""
|
|
122
|
-
logger.debug("Stopping all services")
|
|
123
|
-
await self._exit_stack.aclose()
|
|
124
|
-
self.service_instances.clear()
|
|
125
|
-
logger.debug("All services stopped")
|
|
126
|
-
return False
|
|
127
|
-
|
|
128
|
-
def __getattr__(self, name: str):
|
|
129
|
-
"""
|
|
130
|
-
Enable attribute access for services, e.g., manager.env,
|
|
131
|
-
manager.session.
|
|
132
|
-
"""
|
|
133
|
-
if name in self.service_instances:
|
|
134
|
-
return self.service_instances[name]
|
|
135
|
-
raise AttributeError(f"Service '{name}' not found")
|
|
136
|
-
|
|
137
|
-
def __getitem__(self, name: str):
|
|
138
|
-
"""Enable dictionary-style access for services."""
|
|
139
|
-
if name in self.service_instances:
|
|
140
|
-
return self.service_instances[name]
|
|
141
|
-
raise KeyError(f"Service '{name}' not found")
|
|
142
|
-
|
|
143
|
-
def get(self, name: str, default=None):
|
|
144
|
-
"""Explicitly retrieve a service instance with optional default."""
|
|
145
|
-
return self.service_instances.get(name, default)
|
|
146
|
-
|
|
147
|
-
def has_service(self, name: str) -> bool:
|
|
148
|
-
"""Check if a service exists."""
|
|
149
|
-
return name in self.service_instances
|
|
150
|
-
|
|
151
|
-
def list_services(self) -> List[str]:
|
|
152
|
-
"""List all registered service names."""
|
|
153
|
-
return list(self.service_instances.keys())
|
|
154
|
-
|
|
155
|
-
@property
|
|
156
|
-
def all_services(self) -> Dict[str, Any]:
|
|
157
|
-
"""Retrieve all service instances."""
|
|
158
|
-
return self.service_instances.copy()
|
|
159
|
-
|
|
160
|
-
async def health_check(self) -> Dict[str, bool]:
|
|
161
|
-
"""Check health of all services."""
|
|
162
|
-
health_status = {}
|
|
163
|
-
for name, service in self.service_instances.items():
|
|
164
|
-
try:
|
|
165
|
-
if hasattr(service, "health"):
|
|
166
|
-
health_status[name] = await service.health()
|
|
167
|
-
else:
|
|
168
|
-
health_status[
|
|
169
|
-
name
|
|
170
|
-
] = True # Assume healthy if no health method
|
|
171
|
-
except Exception as e:
|
|
172
|
-
logger.error(f"Health check failed for service {name}: {e}")
|
|
173
|
-
health_status[name] = False
|
|
174
|
-
return health_status
|
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
from .base import ServiceWithLifecycleManager
|
|
3
|
-
from ..schemas.agent_schemas import Message, MessageType
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
class RAGService(ServiceWithLifecycleManager):
|
|
7
|
-
"""
|
|
8
|
-
RAG Service
|
|
9
|
-
"""
|
|
10
|
-
|
|
11
|
-
async def get_query_text(self, message: Message) -> str:
|
|
12
|
-
"""
|
|
13
|
-
Gets the query text from the messages.
|
|
14
|
-
|
|
15
|
-
Args:
|
|
16
|
-
message: A list of messages.
|
|
17
|
-
|
|
18
|
-
Returns:
|
|
19
|
-
The query text.
|
|
20
|
-
"""
|
|
21
|
-
if message:
|
|
22
|
-
if message.type == MessageType.MESSAGE:
|
|
23
|
-
for content in message.content:
|
|
24
|
-
if content.type == "text":
|
|
25
|
-
return content.text
|
|
26
|
-
return ""
|
|
27
|
-
|
|
28
|
-
async def retrieve(self, query: str, k: int = 1) -> list[str]:
|
|
29
|
-
"""
|
|
30
|
-
Retrieves similar documents based on the given query.
|
|
31
|
-
|
|
32
|
-
Args:
|
|
33
|
-
query (str): The query string to search for similar documents.
|
|
34
|
-
k (int, optional): The number of similar documents to retrieve.
|
|
35
|
-
Defaults to 1.
|
|
36
|
-
|
|
37
|
-
Returns:
|
|
38
|
-
list[str]: A list of document contents that are similar to
|
|
39
|
-
the query.
|
|
40
|
-
"""
|
|
41
|
-
raise NotImplementedError
|
|
42
|
-
|
|
43
|
-
async def start(self) -> None:
|
|
44
|
-
"""Starts the service."""
|
|
45
|
-
|
|
46
|
-
async def stop(self) -> None:
|
|
47
|
-
"""Stops the service."""
|
|
48
|
-
|
|
49
|
-
async def health(self) -> bool:
|
|
50
|
-
"""Checks the health of the service."""
|
|
51
|
-
return True
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
DEFAULT_URI = "milvus_demo.db"
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
class LangChainRAGService(RAGService):
|
|
58
|
-
"""
|
|
59
|
-
RAG Service using LangChain
|
|
60
|
-
"""
|
|
61
|
-
|
|
62
|
-
def __init__(
|
|
63
|
-
self,
|
|
64
|
-
vectorstore=None,
|
|
65
|
-
embedding=None,
|
|
66
|
-
):
|
|
67
|
-
# set default embedding alg.
|
|
68
|
-
if embedding is None:
|
|
69
|
-
from langchain_community.embeddings import DashScopeEmbeddings
|
|
70
|
-
|
|
71
|
-
self.embeddings = DashScopeEmbeddings()
|
|
72
|
-
else:
|
|
73
|
-
self.embeddings = embedding
|
|
74
|
-
|
|
75
|
-
# set default vectorstore class.
|
|
76
|
-
if vectorstore is None:
|
|
77
|
-
from langchain_milvus import Milvus
|
|
78
|
-
|
|
79
|
-
self.vectorstore = Milvus.from_documents(
|
|
80
|
-
[],
|
|
81
|
-
embedding=self.embeddings,
|
|
82
|
-
connection_args={
|
|
83
|
-
"uri": DEFAULT_URI,
|
|
84
|
-
},
|
|
85
|
-
drop_old=False,
|
|
86
|
-
)
|
|
87
|
-
else:
|
|
88
|
-
self.vectorstore = vectorstore
|
|
89
|
-
|
|
90
|
-
async def retrieve(self, query: str, k: int = 1) -> list[str]:
|
|
91
|
-
"""
|
|
92
|
-
Retrieves similar documents based on the given query using LangChain.
|
|
93
|
-
|
|
94
|
-
Args:
|
|
95
|
-
query (str): The query string to search for similar documents.
|
|
96
|
-
k (int, optional): The number of similar documents to retrieve.
|
|
97
|
-
Defaults to 1.
|
|
98
|
-
|
|
99
|
-
Returns:
|
|
100
|
-
list[str]: A list of document contents that are similar to the
|
|
101
|
-
query.
|
|
102
|
-
|
|
103
|
-
Raises:
|
|
104
|
-
ValueError: If the vector store is not initialized.
|
|
105
|
-
"""
|
|
106
|
-
if self.vectorstore is None:
|
|
107
|
-
raise ValueError(
|
|
108
|
-
"Vector store not initialized. Call build_index first.",
|
|
109
|
-
)
|
|
110
|
-
docs = self.vectorstore.similarity_search(query, k=k)
|
|
111
|
-
return [doc.page_content for doc in docs]
|
|
112
|
-
|
|
113
|
-
async def start(self) -> None:
|
|
114
|
-
"""Starts the service."""
|
|
115
|
-
|
|
116
|
-
async def stop(self) -> None:
|
|
117
|
-
"""Stops the service."""
|
|
118
|
-
|
|
119
|
-
async def health(self) -> bool:
|
|
120
|
-
"""Checks the health of the service."""
|
|
121
|
-
return True
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
class LlamaIndexRAGService(RAGService):
|
|
125
|
-
"""
|
|
126
|
-
RAG Service using LlamaIndex
|
|
127
|
-
"""
|
|
128
|
-
|
|
129
|
-
def __init__(
|
|
130
|
-
self,
|
|
131
|
-
vectorstore=None,
|
|
132
|
-
embedding=None,
|
|
133
|
-
):
|
|
134
|
-
# set default embedding alg.
|
|
135
|
-
if embedding is None:
|
|
136
|
-
from langchain_community.embeddings import DashScopeEmbeddings
|
|
137
|
-
|
|
138
|
-
self.embeddings = DashScopeEmbeddings()
|
|
139
|
-
else:
|
|
140
|
-
self.embeddings = embedding
|
|
141
|
-
|
|
142
|
-
# set default vectorstore.
|
|
143
|
-
if vectorstore is None:
|
|
144
|
-
from llama_index.core import VectorStoreIndex
|
|
145
|
-
from llama_index.core.schema import Document
|
|
146
|
-
from llama_index.vector_stores.milvus import MilvusVectorStore
|
|
147
|
-
|
|
148
|
-
# Create empty documents list for initialization
|
|
149
|
-
documents = [Document(text="")]
|
|
150
|
-
|
|
151
|
-
# Initialize Milvus vector store
|
|
152
|
-
self.vector_store = MilvusVectorStore(
|
|
153
|
-
uri=DEFAULT_URI,
|
|
154
|
-
overwrite=False,
|
|
155
|
-
)
|
|
156
|
-
|
|
157
|
-
# Create index
|
|
158
|
-
self.index = VectorStoreIndex.from_documents(
|
|
159
|
-
documents=documents,
|
|
160
|
-
embed_model=self.embeddings,
|
|
161
|
-
vector_store=self.vector_store,
|
|
162
|
-
)
|
|
163
|
-
else:
|
|
164
|
-
self.index = vectorstore
|
|
165
|
-
|
|
166
|
-
async def retrieve(self, query: str, k: int = 1) -> list[str]:
|
|
167
|
-
"""
|
|
168
|
-
Retrieves similar documents based on the given query using LlamaIndex.
|
|
169
|
-
|
|
170
|
-
Args:
|
|
171
|
-
query (str): The query string to search for similar documents.
|
|
172
|
-
k (int, optional): The number of similar documents to retrieve.
|
|
173
|
-
Defaults to 1.
|
|
174
|
-
|
|
175
|
-
Returns:
|
|
176
|
-
list[str]: A list of document contents that are similar to the
|
|
177
|
-
query.
|
|
178
|
-
|
|
179
|
-
Raises:
|
|
180
|
-
ValueError: If the index is not initialized.
|
|
181
|
-
"""
|
|
182
|
-
if self.index is None:
|
|
183
|
-
raise ValueError(
|
|
184
|
-
"Index not initialized.",
|
|
185
|
-
)
|
|
186
|
-
|
|
187
|
-
# Create query engine and query
|
|
188
|
-
query_engine = self.index.as_retriever(similarity_top_k=k)
|
|
189
|
-
response = query_engine.retrieve(query)
|
|
190
|
-
|
|
191
|
-
# Extract text from nodes
|
|
192
|
-
if len(response) > 0:
|
|
193
|
-
return [node.node.get_content() for node in response]
|
|
194
|
-
else:
|
|
195
|
-
return [""]
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
# pylint: disable=redefined-outer-name
|
|
3
|
-
import asyncio
|
|
4
|
-
import uuid
|
|
5
|
-
from typing import Any, List, Optional, Union
|
|
6
|
-
|
|
7
|
-
try:
|
|
8
|
-
from langchain_community.embeddings import DashScopeEmbeddings
|
|
9
|
-
from langchain_core.documents import Document
|
|
10
|
-
from langchain_core.embeddings import Embeddings
|
|
11
|
-
from tablestore import AsyncOTSClient as AsyncTablestoreClient
|
|
12
|
-
from tablestore import VectorMetricType
|
|
13
|
-
from tablestore_for_agent_memory.base.base_knowledge_store import (
|
|
14
|
-
Document as TablestoreDocument,
|
|
15
|
-
)
|
|
16
|
-
from tablestore_for_agent_memory.knowledge.async_knowledge_store import (
|
|
17
|
-
AsyncKnowledgeStore,
|
|
18
|
-
)
|
|
19
|
-
except ImportError as e:
|
|
20
|
-
raise ImportError(
|
|
21
|
-
"aliyun_tablestore is not available. "
|
|
22
|
-
"Please run pip install agentscope-runtime[aliyun_tablestore_ext]",
|
|
23
|
-
) from e
|
|
24
|
-
|
|
25
|
-
from .rag_service import RAGService
|
|
26
|
-
from .utils.tablestore_service_utils import tablestore_log
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
class TablestoreRAGService(RAGService):
|
|
30
|
-
"""
|
|
31
|
-
RAG Service using Tablestore(aliyun tablestore)
|
|
32
|
-
based on tablestore_for_agent_memory
|
|
33
|
-
(https://github.com/aliyun/
|
|
34
|
-
alibabacloud-tablestore-for-agent-memory/blob/main/python/docs/knowledge_store_tutorial.ipynb).
|
|
35
|
-
"""
|
|
36
|
-
|
|
37
|
-
_SEARCH_INDEX_NAME = "agentscope_runtime_knowledge_search_index_name"
|
|
38
|
-
_DEFAULT_SESSION_ID = "default"
|
|
39
|
-
|
|
40
|
-
def __init__(
|
|
41
|
-
self,
|
|
42
|
-
tablestore_client: AsyncTablestoreClient,
|
|
43
|
-
embedding_model: Optional[Embeddings] = None,
|
|
44
|
-
vector_dimension: int = 1536,
|
|
45
|
-
table_name: Optional[str] = "agentscope_runtime_rag",
|
|
46
|
-
text_field: Optional[str] = "text",
|
|
47
|
-
embedding_field: Optional[str] = "embedding",
|
|
48
|
-
vector_metric_type: VectorMetricType = VectorMetricType.VM_COSINE,
|
|
49
|
-
**kwargs: Any,
|
|
50
|
-
):
|
|
51
|
-
self._embedding_model = (
|
|
52
|
-
embedding_model if embedding_model else DashScopeEmbeddings()
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
self._tablestore_client = tablestore_client
|
|
56
|
-
self._vector_dimension = vector_dimension
|
|
57
|
-
self._table_name = table_name
|
|
58
|
-
self._text_field = text_field
|
|
59
|
-
self._embedding_field = embedding_field
|
|
60
|
-
self._vector_metric_type = vector_metric_type
|
|
61
|
-
self._knowledge_store: Optional[AsyncKnowledgeStore] = None
|
|
62
|
-
self._knowledge_store_init_parameter_kwargs = kwargs
|
|
63
|
-
|
|
64
|
-
async def _init_knowledge_store(self) -> None:
|
|
65
|
-
self._knowledge_store = AsyncKnowledgeStore(
|
|
66
|
-
tablestore_client=self._tablestore_client,
|
|
67
|
-
vector_dimension=self._vector_dimension,
|
|
68
|
-
enable_multi_tenant=False,
|
|
69
|
-
table_name=self._table_name,
|
|
70
|
-
search_index_name=TablestoreRAGService._SEARCH_INDEX_NAME,
|
|
71
|
-
text_field=self._text_field,
|
|
72
|
-
embedding_field=self._embedding_field,
|
|
73
|
-
vector_metric_type=self._vector_metric_type,
|
|
74
|
-
**self._knowledge_store_init_parameter_kwargs,
|
|
75
|
-
)
|
|
76
|
-
|
|
77
|
-
await self._knowledge_store.init_table()
|
|
78
|
-
|
|
79
|
-
async def start(self) -> None:
|
|
80
|
-
"""Start the tablestore service"""
|
|
81
|
-
if self._knowledge_store:
|
|
82
|
-
return
|
|
83
|
-
await self._init_knowledge_store()
|
|
84
|
-
|
|
85
|
-
async def stop(self) -> None:
|
|
86
|
-
"""Close the tablestore service"""
|
|
87
|
-
if self._knowledge_store is None:
|
|
88
|
-
return
|
|
89
|
-
knowledge_store = self._knowledge_store
|
|
90
|
-
self._knowledge_store = None
|
|
91
|
-
await knowledge_store.close()
|
|
92
|
-
|
|
93
|
-
async def health(self) -> bool:
|
|
94
|
-
"""Checks the health of the service."""
|
|
95
|
-
if self._knowledge_store is None:
|
|
96
|
-
tablestore_log("Tablestore rag service is not started.")
|
|
97
|
-
return False
|
|
98
|
-
|
|
99
|
-
try:
|
|
100
|
-
async for _ in await self._knowledge_store.get_all_documents():
|
|
101
|
-
return True
|
|
102
|
-
return True
|
|
103
|
-
except Exception as e:
|
|
104
|
-
tablestore_log(
|
|
105
|
-
f"Tablestore rag service "
|
|
106
|
-
f"cannot access Tablestore, error: {str(e)}.",
|
|
107
|
-
)
|
|
108
|
-
return False
|
|
109
|
-
|
|
110
|
-
async def add_docs(self, docs: Union[Document, List[Document]]):
|
|
111
|
-
if not isinstance(docs, List):
|
|
112
|
-
docs = [docs]
|
|
113
|
-
|
|
114
|
-
contents = [doc.page_content for doc in docs]
|
|
115
|
-
# Encode in batches to reduce time consumption.
|
|
116
|
-
embeddings = self._embedding_model.embed_documents(contents)
|
|
117
|
-
|
|
118
|
-
put_tasks = [
|
|
119
|
-
# The conversion logic here is simple,
|
|
120
|
-
# so no separate conversion function is defined.
|
|
121
|
-
self._knowledge_store.put_document(
|
|
122
|
-
document=TablestoreDocument(
|
|
123
|
-
document_id=f"document_{uuid.uuid4()}",
|
|
124
|
-
text=doc.page_content,
|
|
125
|
-
embedding=embedding,
|
|
126
|
-
metadata=doc.metadata,
|
|
127
|
-
),
|
|
128
|
-
)
|
|
129
|
-
for doc, embedding in zip(docs, embeddings)
|
|
130
|
-
]
|
|
131
|
-
await asyncio.gather(*put_tasks)
|
|
132
|
-
|
|
133
|
-
async def retrieve(self, query: str, k: int = 1) -> list[str]:
|
|
134
|
-
matched_text = [
|
|
135
|
-
hit.document.text
|
|
136
|
-
for hit in (
|
|
137
|
-
await self._knowledge_store.vector_search(
|
|
138
|
-
query_vector=self._embedding_model.embed_query(query),
|
|
139
|
-
top_k=k,
|
|
140
|
-
)
|
|
141
|
-
).hits
|
|
142
|
-
]
|
|
143
|
-
return matched_text
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
from .sandbox_tool import SandboxTool
|
|
3
|
-
from .mcp_tool import MCPTool
|
|
4
|
-
from .function_tool import FunctionTool, function_tool, create_function_tool
|
|
5
|
-
|
|
6
|
-
__all__ = [
|
|
7
|
-
"SandboxTool",
|
|
8
|
-
"MCPTool",
|
|
9
|
-
"FunctionTool",
|
|
10
|
-
"function_tool",
|
|
11
|
-
"create_function_tool",
|
|
12
|
-
]
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
from typing import Dict
|
|
3
|
-
|
|
4
|
-
from ..sandbox_tool import SandboxTool
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class RunIPythonCellTool(SandboxTool):
|
|
8
|
-
"""Tool for running IPython cells."""
|
|
9
|
-
|
|
10
|
-
_name: str = "run_ipython_cell"
|
|
11
|
-
_sandbox_type: str = "base"
|
|
12
|
-
_tool_type: str = "generic"
|
|
13
|
-
_schema: Dict = {
|
|
14
|
-
"name": "run_ipython_cell",
|
|
15
|
-
"description": "Run an IPython cell.",
|
|
16
|
-
"parameters": {
|
|
17
|
-
"type": "object",
|
|
18
|
-
"properties": {
|
|
19
|
-
"code": {
|
|
20
|
-
"type": "string",
|
|
21
|
-
"description": "IPython code to execute",
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
"required": ["code"],
|
|
25
|
-
},
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
class RunShellCommandTool(SandboxTool):
|
|
30
|
-
"""Tool for running shell commands."""
|
|
31
|
-
|
|
32
|
-
_name: str = "run_shell_command"
|
|
33
|
-
_sandbox_type: str = "base"
|
|
34
|
-
_tool_type: str = "generic"
|
|
35
|
-
_schema: Dict = {
|
|
36
|
-
"name": "run_shell_command",
|
|
37
|
-
"description": "Run a shell command.",
|
|
38
|
-
"parameters": {
|
|
39
|
-
"type": "object",
|
|
40
|
-
"properties": {
|
|
41
|
-
"command": {
|
|
42
|
-
"type": "string",
|
|
43
|
-
"description": "Shell command to execute",
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
"required": ["command"],
|
|
47
|
-
},
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
run_ipython_cell = RunIPythonCellTool()
|
|
52
|
-
run_shell_command = RunShellCommandTool()
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
from .tool import (
|
|
3
|
-
run_shell_command,
|
|
4
|
-
run_ipython_cell,
|
|
5
|
-
browser_close,
|
|
6
|
-
browser_resize,
|
|
7
|
-
browser_console_messages,
|
|
8
|
-
browser_handle_dialog,
|
|
9
|
-
browser_file_upload,
|
|
10
|
-
browser_press_key,
|
|
11
|
-
browser_navigate,
|
|
12
|
-
browser_navigate_back,
|
|
13
|
-
browser_navigate_forward,
|
|
14
|
-
browser_network_requests,
|
|
15
|
-
browser_pdf_save,
|
|
16
|
-
browser_take_screenshot,
|
|
17
|
-
browser_snapshot,
|
|
18
|
-
browser_click,
|
|
19
|
-
browser_drag,
|
|
20
|
-
browser_hover,
|
|
21
|
-
browser_type,
|
|
22
|
-
browser_select_option,
|
|
23
|
-
browser_tab_list,
|
|
24
|
-
browser_tab_new,
|
|
25
|
-
browser_tab_select,
|
|
26
|
-
browser_tab_close,
|
|
27
|
-
browser_wait_for,
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
__all__ = [
|
|
32
|
-
"run_shell_command",
|
|
33
|
-
"run_ipython_cell",
|
|
34
|
-
"browser_close",
|
|
35
|
-
"browser_resize",
|
|
36
|
-
"browser_console_messages",
|
|
37
|
-
"browser_handle_dialog",
|
|
38
|
-
"browser_file_upload",
|
|
39
|
-
"browser_press_key",
|
|
40
|
-
"browser_navigate",
|
|
41
|
-
"browser_navigate_back",
|
|
42
|
-
"browser_navigate_forward",
|
|
43
|
-
"browser_network_requests",
|
|
44
|
-
"browser_pdf_save",
|
|
45
|
-
"browser_take_screenshot",
|
|
46
|
-
"browser_snapshot",
|
|
47
|
-
"browser_click",
|
|
48
|
-
"browser_drag",
|
|
49
|
-
"browser_hover",
|
|
50
|
-
"browser_type",
|
|
51
|
-
"browser_select_option",
|
|
52
|
-
"browser_tab_list",
|
|
53
|
-
"browser_tab_new",
|
|
54
|
-
"browser_tab_select",
|
|
55
|
-
"browser_tab_close",
|
|
56
|
-
"browser_wait_for",
|
|
57
|
-
]
|