agentrun-inner-test 0.0.62__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.
Potentially problematic release.
This version of agentrun-inner-test might be problematic. Click here for more details.
- agentrun/__init__.py +358 -0
- agentrun/agent_runtime/__client_async_template.py +466 -0
- agentrun/agent_runtime/__endpoint_async_template.py +345 -0
- agentrun/agent_runtime/__init__.py +53 -0
- agentrun/agent_runtime/__runtime_async_template.py +477 -0
- agentrun/agent_runtime/api/__data_async_template.py +58 -0
- agentrun/agent_runtime/api/__init__.py +6 -0
- agentrun/agent_runtime/api/control.py +1362 -0
- agentrun/agent_runtime/api/data.py +98 -0
- agentrun/agent_runtime/client.py +868 -0
- agentrun/agent_runtime/endpoint.py +649 -0
- agentrun/agent_runtime/model.py +362 -0
- agentrun/agent_runtime/runtime.py +904 -0
- agentrun/credential/__client_async_template.py +177 -0
- agentrun/credential/__credential_async_template.py +216 -0
- agentrun/credential/__init__.py +28 -0
- agentrun/credential/api/__init__.py +5 -0
- agentrun/credential/api/control.py +606 -0
- agentrun/credential/client.py +319 -0
- agentrun/credential/credential.py +381 -0
- agentrun/credential/model.py +248 -0
- agentrun/integration/__init__.py +21 -0
- agentrun/integration/agentscope/__init__.py +13 -0
- agentrun/integration/agentscope/adapter.py +17 -0
- agentrun/integration/agentscope/builtin.py +88 -0
- agentrun/integration/agentscope/message_adapter.py +185 -0
- agentrun/integration/agentscope/model_adapter.py +60 -0
- agentrun/integration/agentscope/tool_adapter.py +59 -0
- agentrun/integration/builtin/__init__.py +18 -0
- agentrun/integration/builtin/knowledgebase.py +137 -0
- agentrun/integration/builtin/model.py +93 -0
- agentrun/integration/builtin/sandbox.py +1234 -0
- agentrun/integration/builtin/toolset.py +47 -0
- agentrun/integration/crewai/__init__.py +13 -0
- agentrun/integration/crewai/adapter.py +9 -0
- agentrun/integration/crewai/builtin.py +88 -0
- agentrun/integration/crewai/model_adapter.py +31 -0
- agentrun/integration/crewai/tool_adapter.py +26 -0
- agentrun/integration/google_adk/__init__.py +13 -0
- agentrun/integration/google_adk/adapter.py +15 -0
- agentrun/integration/google_adk/builtin.py +88 -0
- agentrun/integration/google_adk/message_adapter.py +144 -0
- agentrun/integration/google_adk/model_adapter.py +46 -0
- agentrun/integration/google_adk/tool_adapter.py +235 -0
- agentrun/integration/langchain/__init__.py +31 -0
- agentrun/integration/langchain/adapter.py +15 -0
- agentrun/integration/langchain/builtin.py +94 -0
- agentrun/integration/langchain/message_adapter.py +141 -0
- agentrun/integration/langchain/model_adapter.py +37 -0
- agentrun/integration/langchain/tool_adapter.py +50 -0
- agentrun/integration/langgraph/__init__.py +36 -0
- agentrun/integration/langgraph/adapter.py +20 -0
- agentrun/integration/langgraph/agent_converter.py +1073 -0
- agentrun/integration/langgraph/builtin.py +88 -0
- agentrun/integration/pydantic_ai/__init__.py +13 -0
- agentrun/integration/pydantic_ai/adapter.py +13 -0
- agentrun/integration/pydantic_ai/builtin.py +88 -0
- agentrun/integration/pydantic_ai/model_adapter.py +44 -0
- agentrun/integration/pydantic_ai/tool_adapter.py +19 -0
- agentrun/integration/utils/__init__.py +112 -0
- agentrun/integration/utils/adapter.py +560 -0
- agentrun/integration/utils/canonical.py +164 -0
- agentrun/integration/utils/converter.py +134 -0
- agentrun/integration/utils/model.py +110 -0
- agentrun/integration/utils/tool.py +1759 -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/__client_async_template.py +357 -0
- agentrun/model/__init__.py +57 -0
- agentrun/model/__model_proxy_async_template.py +270 -0
- agentrun/model/__model_service_async_template.py +267 -0
- agentrun/model/api/__init__.py +6 -0
- agentrun/model/api/control.py +1173 -0
- agentrun/model/api/data.py +196 -0
- agentrun/model/client.py +674 -0
- agentrun/model/model.py +235 -0
- agentrun/model/model_proxy.py +439 -0
- agentrun/model/model_service.py +438 -0
- agentrun/sandbox/__aio_sandbox_async_template.py +523 -0
- agentrun/sandbox/__browser_sandbox_async_template.py +110 -0
- agentrun/sandbox/__client_async_template.py +491 -0
- agentrun/sandbox/__code_interpreter_sandbox_async_template.py +463 -0
- agentrun/sandbox/__init__.py +69 -0
- agentrun/sandbox/__sandbox_async_template.py +463 -0
- agentrun/sandbox/__template_async_template.py +152 -0
- agentrun/sandbox/aio_sandbox.py +912 -0
- agentrun/sandbox/api/__aio_data_async_template.py +335 -0
- agentrun/sandbox/api/__browser_data_async_template.py +140 -0
- agentrun/sandbox/api/__code_interpreter_data_async_template.py +206 -0
- agentrun/sandbox/api/__init__.py +19 -0
- agentrun/sandbox/api/__sandbox_data_async_template.py +107 -0
- agentrun/sandbox/api/aio_data.py +551 -0
- agentrun/sandbox/api/browser_data.py +172 -0
- agentrun/sandbox/api/code_interpreter_data.py +396 -0
- agentrun/sandbox/api/control.py +1051 -0
- agentrun/sandbox/api/playwright_async.py +492 -0
- agentrun/sandbox/api/playwright_sync.py +492 -0
- agentrun/sandbox/api/sandbox_data.py +154 -0
- agentrun/sandbox/browser_sandbox.py +185 -0
- agentrun/sandbox/client.py +925 -0
- agentrun/sandbox/code_interpreter_sandbox.py +823 -0
- agentrun/sandbox/model.py +384 -0
- agentrun/sandbox/sandbox.py +848 -0
- agentrun/sandbox/template.py +217 -0
- agentrun/server/__init__.py +191 -0
- agentrun/server/agui_normalizer.py +180 -0
- agentrun/server/agui_protocol.py +797 -0
- agentrun/server/invoker.py +309 -0
- agentrun/server/model.py +427 -0
- agentrun/server/openai_protocol.py +535 -0
- agentrun/server/protocol.py +140 -0
- agentrun/server/server.py +208 -0
- agentrun/toolset/__client_async_template.py +62 -0
- agentrun/toolset/__init__.py +51 -0
- agentrun/toolset/__toolset_async_template.py +204 -0
- agentrun/toolset/api/__init__.py +17 -0
- agentrun/toolset/api/control.py +262 -0
- agentrun/toolset/api/mcp.py +100 -0
- agentrun/toolset/api/openapi.py +1251 -0
- agentrun/toolset/client.py +102 -0
- agentrun/toolset/model.py +321 -0
- agentrun/toolset/toolset.py +271 -0
- agentrun/utils/__data_api_async_template.py +721 -0
- agentrun/utils/__init__.py +5 -0
- agentrun/utils/__resource_async_template.py +158 -0
- agentrun/utils/config.py +270 -0
- agentrun/utils/control_api.py +105 -0
- agentrun/utils/data_api.py +1121 -0
- agentrun/utils/exception.py +151 -0
- agentrun/utils/helper.py +108 -0
- agentrun/utils/log.py +77 -0
- agentrun/utils/model.py +168 -0
- agentrun/utils/resource.py +291 -0
- agentrun_inner_test-0.0.62.dist-info/METADATA +265 -0
- agentrun_inner_test-0.0.62.dist-info/RECORD +154 -0
- agentrun_inner_test-0.0.62.dist-info/WHEEL +5 -0
- agentrun_inner_test-0.0.62.dist-info/licenses/LICENSE +201 -0
- agentrun_inner_test-0.0.62.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"""中间格式定义 / Canonical Format Definition
|
|
2
|
+
|
|
3
|
+
提供统一的中间格式(Canonical Format),作为所有框架转换的桥梁。
|
|
4
|
+
Provides a unified canonical format that serves as a bridge for all framework conversions.
|
|
5
|
+
|
|
6
|
+
这样可以最大化代码复用,减少重复的转换逻辑。
|
|
7
|
+
This maximizes code reuse and reduces redundant conversion logic.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
import json
|
|
12
|
+
from typing import Any, Callable, Dict, List, Optional, TYPE_CHECKING
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from typing import Protocol
|
|
16
|
+
else:
|
|
17
|
+
Protocol = object
|
|
18
|
+
|
|
19
|
+
from enum import Enum
|
|
20
|
+
|
|
21
|
+
from agentrun.integration.utils.tool import normalize_tool_name
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class MessageRole(str, Enum):
|
|
25
|
+
"""统一的消息角色枚举"""
|
|
26
|
+
|
|
27
|
+
SYSTEM = "system"
|
|
28
|
+
USER = "user"
|
|
29
|
+
ASSISTANT = "assistant"
|
|
30
|
+
TOOL = "tool"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass
|
|
34
|
+
class CanonicalToolCall:
|
|
35
|
+
"""统一的工具调用格式
|
|
36
|
+
|
|
37
|
+
所有框架的工具调用都转换为这个格式,然后再转换为目标框架格式。
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
id: str
|
|
41
|
+
name: str
|
|
42
|
+
arguments: Dict[str, Any]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass
|
|
46
|
+
class CanonicalMessage:
|
|
47
|
+
"""统一的消息格式
|
|
48
|
+
|
|
49
|
+
这是所有框架消息格式的中间表示。
|
|
50
|
+
转换流程:框架A格式 → CanonicalMessage → 框架B格式
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
role: MessageRole
|
|
54
|
+
content: Optional[str] = None
|
|
55
|
+
name: Optional[str] = None
|
|
56
|
+
tool_calls: Optional[List[CanonicalToolCall]] = None
|
|
57
|
+
tool_call_id: Optional[str] = None
|
|
58
|
+
|
|
59
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
60
|
+
"""转换为字典格式(用于序列化)
|
|
61
|
+
|
|
62
|
+
注意:OpenAI API 要求 tool_calls 中的 arguments 必须是 JSON 字符串,
|
|
63
|
+
而不是字典。这里会自动转换。
|
|
64
|
+
"""
|
|
65
|
+
result = {
|
|
66
|
+
"role": self.role.value,
|
|
67
|
+
}
|
|
68
|
+
if self.content is not None:
|
|
69
|
+
result["content"] = self.content
|
|
70
|
+
if self.name is not None:
|
|
71
|
+
result["name"] = self.name
|
|
72
|
+
if self.tool_calls is not None:
|
|
73
|
+
result["tool_calls"] = [
|
|
74
|
+
{
|
|
75
|
+
"id": call.id,
|
|
76
|
+
"type": "function",
|
|
77
|
+
"function": {
|
|
78
|
+
"name": call.name,
|
|
79
|
+
# arguments 必须是 JSON 字符串(OpenAI API 要求)
|
|
80
|
+
"arguments": (
|
|
81
|
+
json.dumps(call.arguments)
|
|
82
|
+
if isinstance(call.arguments, dict)
|
|
83
|
+
else str(call.arguments)
|
|
84
|
+
),
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
for call in self.tool_calls
|
|
88
|
+
]
|
|
89
|
+
if self.tool_call_id is not None:
|
|
90
|
+
result["tool_call_id"] = self.tool_call_id
|
|
91
|
+
return result
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@dataclass
|
|
95
|
+
class CanonicalTool:
|
|
96
|
+
"""统一的工具格式
|
|
97
|
+
|
|
98
|
+
所有框架的工具都转换为这个格式。
|
|
99
|
+
参数使用 JSON Schema 格式,这是最通用的工具描述格式。
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
name: str
|
|
103
|
+
description: str
|
|
104
|
+
parameters: Dict[str, Any] # JSON Schema 格式
|
|
105
|
+
func: Optional[Callable] = None
|
|
106
|
+
|
|
107
|
+
def __post_init__(self):
|
|
108
|
+
# Normalize canonical tool name to avoid exceeding provider limits
|
|
109
|
+
if self.name:
|
|
110
|
+
self.name = normalize_tool_name(self.name)
|
|
111
|
+
|
|
112
|
+
def to_openai_function(self) -> Dict[str, Any]:
|
|
113
|
+
"""转换为 OpenAI Function Calling 格式"""
|
|
114
|
+
return {
|
|
115
|
+
"name": self.name,
|
|
116
|
+
"description": self.description,
|
|
117
|
+
"parameters": self.parameters,
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
def to_anthropic_tool(self) -> Dict[str, Any]:
|
|
121
|
+
"""转换为 Anthropic Claude Tools 格式"""
|
|
122
|
+
return {
|
|
123
|
+
"name": self.name,
|
|
124
|
+
"description": self.description,
|
|
125
|
+
"input_schema": self.parameters,
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
@dataclass
|
|
130
|
+
class CanonicalModelResponse:
|
|
131
|
+
"""统一的模型响应格式"""
|
|
132
|
+
|
|
133
|
+
content: Optional[str] = None
|
|
134
|
+
tool_calls: Optional[List[CanonicalToolCall]] = None
|
|
135
|
+
usage: Optional[Dict[str, int]] = None
|
|
136
|
+
|
|
137
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
138
|
+
"""转换为字典格式
|
|
139
|
+
|
|
140
|
+
注意:OpenAI API 要求 tool_calls 中的 arguments 必须是 JSON 字符串。
|
|
141
|
+
"""
|
|
142
|
+
result = {}
|
|
143
|
+
if self.content is not None:
|
|
144
|
+
result["content"] = self.content
|
|
145
|
+
if self.tool_calls is not None:
|
|
146
|
+
result["tool_calls"] = [
|
|
147
|
+
{
|
|
148
|
+
"id": call.id,
|
|
149
|
+
"type": "function",
|
|
150
|
+
"function": {
|
|
151
|
+
"name": call.name,
|
|
152
|
+
# arguments 必须是 JSON 字符串(OpenAI API 要求)
|
|
153
|
+
"arguments": (
|
|
154
|
+
json.dumps(call.arguments)
|
|
155
|
+
if isinstance(call.arguments, dict)
|
|
156
|
+
else str(call.arguments)
|
|
157
|
+
),
|
|
158
|
+
},
|
|
159
|
+
}
|
|
160
|
+
for call in self.tool_calls
|
|
161
|
+
]
|
|
162
|
+
if self.usage is not None:
|
|
163
|
+
result["usage"] = self.usage
|
|
164
|
+
return result
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""框架转换器 / Framework Converter
|
|
2
|
+
|
|
3
|
+
提供统一的框架适配器注册中心。
|
|
4
|
+
Provides a unified registry for framework adapters.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Dict, Optional
|
|
8
|
+
|
|
9
|
+
from agentrun.integration.utils.adapter import ModelAdapter, ToolAdapter
|
|
10
|
+
from agentrun.utils.log import logger
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class FrameworkConverter:
|
|
14
|
+
"""框架适配器注册中心
|
|
15
|
+
|
|
16
|
+
管理所有框架的工具和模型适配器。
|
|
17
|
+
MessageAdapter 不再单独注册,而是作为 ModelAdapter 的内部组件。
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(self):
|
|
21
|
+
self._tool_adapters: Dict[str, ToolAdapter] = {}
|
|
22
|
+
self._model_adapters: Dict[str, ModelAdapter] = {}
|
|
23
|
+
|
|
24
|
+
def register_tool_adapter(
|
|
25
|
+
self, framework: str, adapter: ToolAdapter
|
|
26
|
+
) -> None:
|
|
27
|
+
"""注册工具适配器"""
|
|
28
|
+
self._tool_adapters[framework] = adapter
|
|
29
|
+
|
|
30
|
+
def register_model_adapter(
|
|
31
|
+
self, framework: str, adapter: ModelAdapter
|
|
32
|
+
) -> None:
|
|
33
|
+
"""注册模型适配器"""
|
|
34
|
+
self._model_adapters[framework] = adapter
|
|
35
|
+
|
|
36
|
+
def get_model_adapter(self, framework: str) -> Optional[ModelAdapter]:
|
|
37
|
+
"""获取模型适配器"""
|
|
38
|
+
return self._model_adapters.get(framework)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# 全局转换器实例
|
|
42
|
+
_converter = FrameworkConverter()
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def get_converter() -> FrameworkConverter:
|
|
46
|
+
"""获取全局转换器实例"""
|
|
47
|
+
return _converter
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _auto_register_adapters() -> None:
|
|
51
|
+
"""自动注册所有可用的适配器
|
|
52
|
+
|
|
53
|
+
延迟导入,避免循环依赖。
|
|
54
|
+
MessageAdapter 不再单独注册,由 ModelAdapter 内部管理。
|
|
55
|
+
"""
|
|
56
|
+
# LangChain 适配器
|
|
57
|
+
try:
|
|
58
|
+
from agentrun.integration.langchain.adapter import (
|
|
59
|
+
LangChainModelAdapter,
|
|
60
|
+
LangChainToolAdapter,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
_converter.register_tool_adapter("langchain", LangChainToolAdapter())
|
|
64
|
+
_converter.register_model_adapter("langchain", LangChainModelAdapter())
|
|
65
|
+
except (ImportError, AttributeError) as e:
|
|
66
|
+
logger.warning("failed to register LangChain adapters, due to %s", e)
|
|
67
|
+
|
|
68
|
+
# Google ADK 适配器
|
|
69
|
+
try:
|
|
70
|
+
from agentrun.integration.google_adk.adapter import (
|
|
71
|
+
GoogleADKModelAdapter,
|
|
72
|
+
GoogleADKToolAdapter,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
_converter.register_tool_adapter("google_adk", GoogleADKToolAdapter())
|
|
76
|
+
_converter.register_model_adapter("google_adk", GoogleADKModelAdapter())
|
|
77
|
+
except (ImportError, AttributeError) as e:
|
|
78
|
+
logger.warning("failed to register Google ADK adapters, due to %s", e)
|
|
79
|
+
|
|
80
|
+
# AgentScope 适配器
|
|
81
|
+
try:
|
|
82
|
+
from agentrun.integration.agentscope.adapter import (
|
|
83
|
+
AgentScopeModelAdapter,
|
|
84
|
+
AgentScopeToolAdapter,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
_converter.register_tool_adapter("agentscope", AgentScopeToolAdapter())
|
|
88
|
+
_converter.register_model_adapter(
|
|
89
|
+
"agentscope", AgentScopeModelAdapter()
|
|
90
|
+
)
|
|
91
|
+
except (ImportError, AttributeError) as e:
|
|
92
|
+
logger.warning("failed to register AgentScope adapters, due to %s", e)
|
|
93
|
+
|
|
94
|
+
# LangGraph 适配器(复用 LangChain)
|
|
95
|
+
try:
|
|
96
|
+
from agentrun.integration.langgraph.adapter import (
|
|
97
|
+
LangGraphModelAdapter,
|
|
98
|
+
LangGraphToolAdapter,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
_converter.register_tool_adapter("langgraph", LangGraphToolAdapter())
|
|
102
|
+
_converter.register_model_adapter("langgraph", LangGraphModelAdapter())
|
|
103
|
+
except (ImportError, AttributeError) as e:
|
|
104
|
+
logger.warning("failed to register LangGraph adapters, due to %s", e)
|
|
105
|
+
|
|
106
|
+
# CrewAI 适配器(复用 LangChain)
|
|
107
|
+
try:
|
|
108
|
+
from agentrun.integration.crewai.adapter import (
|
|
109
|
+
CrewAIModelAdapter,
|
|
110
|
+
CrewAIToolAdapter,
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
_converter.register_tool_adapter("crewai", CrewAIToolAdapter())
|
|
114
|
+
_converter.register_model_adapter("crewai", CrewAIModelAdapter())
|
|
115
|
+
except (ImportError, AttributeError) as e:
|
|
116
|
+
logger.warning("failed to register CrewAI adapters, due to %s", e)
|
|
117
|
+
|
|
118
|
+
# PydanticAI 适配器
|
|
119
|
+
try:
|
|
120
|
+
from agentrun.integration.pydantic_ai.adapter import (
|
|
121
|
+
PydanticAIModelAdapter,
|
|
122
|
+
PydanticAIToolAdapter,
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
_converter.register_tool_adapter("pydantic_ai", PydanticAIToolAdapter())
|
|
126
|
+
_converter.register_model_adapter(
|
|
127
|
+
"pydantic_ai", PydanticAIModelAdapter()
|
|
128
|
+
)
|
|
129
|
+
except (ImportError, AttributeError) as e:
|
|
130
|
+
logger.warning("failed to register PydanticAI adapters, due to %s", e)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
# 初始化时自动注册
|
|
134
|
+
_auto_register_adapters()
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""通用模型定义和转换模块 / Common Model Definition and Conversion Module
|
|
2
|
+
|
|
3
|
+
提供跨框架的通用模型定义和转换功能。
|
|
4
|
+
Provides cross-framework model definition and conversion capabilities.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Any, Optional, Union
|
|
8
|
+
|
|
9
|
+
from agentrun.model import BackendType, ModelProxy, ModelService
|
|
10
|
+
from agentrun.utils.config import Config
|
|
11
|
+
from agentrun.utils.log import logger
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class CommonModel:
|
|
15
|
+
"""通用模型定义
|
|
16
|
+
|
|
17
|
+
封装 AgentRun 模型,提供跨框架转换能力。
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
model_obj: Union[ModelService, ModelProxy],
|
|
23
|
+
backend_type: Optional[BackendType] = None,
|
|
24
|
+
specific_model: Optional[str] = None,
|
|
25
|
+
config: Optional[Config] = None,
|
|
26
|
+
):
|
|
27
|
+
self.model_obj = model_obj
|
|
28
|
+
self.backend_type = backend_type
|
|
29
|
+
self.specific_model = specific_model
|
|
30
|
+
self.config = config or Config()
|
|
31
|
+
|
|
32
|
+
def completions(self, *args, **kwargs):
|
|
33
|
+
"""调用底层模型的 completions 方法"""
|
|
34
|
+
return self.model_obj.completions(*args, **kwargs)
|
|
35
|
+
|
|
36
|
+
def responses(self, *args, **kwargs):
|
|
37
|
+
"""调用底层模型的 responses 方法"""
|
|
38
|
+
return self.model_obj.responses(*args, **kwargs)
|
|
39
|
+
|
|
40
|
+
def get_model_info(self, config: Optional[Config] = None):
|
|
41
|
+
"""获取模型信息"""
|
|
42
|
+
cfg = Config.with_configs(self.config, config)
|
|
43
|
+
info = self.model_obj.model_info(config=cfg)
|
|
44
|
+
if self.specific_model:
|
|
45
|
+
info.model = self.specific_model
|
|
46
|
+
return info
|
|
47
|
+
|
|
48
|
+
def __convert_model(self, adapter_name: str):
|
|
49
|
+
try:
|
|
50
|
+
from agentrun.integration.utils.converter import get_converter
|
|
51
|
+
|
|
52
|
+
converter = get_converter()
|
|
53
|
+
adapter = converter.get_model_adapter(adapter_name)
|
|
54
|
+
if adapter is not None:
|
|
55
|
+
return adapter.wrap_model(self)
|
|
56
|
+
except (ImportError, AttributeError, KeyError) as e:
|
|
57
|
+
logger.warning("convert model failed, due to %s", e)
|
|
58
|
+
pass
|
|
59
|
+
|
|
60
|
+
logger.warning(
|
|
61
|
+
f"Model adapter '{adapter_name}' not found. Falling back to default"
|
|
62
|
+
" implementation."
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
return ""
|
|
66
|
+
|
|
67
|
+
def to_google_adk(self) -> Any:
|
|
68
|
+
"""转换为 Google ADK BaseLlm
|
|
69
|
+
|
|
70
|
+
优先使用适配器模式,如果适配器未注册则回退到旧实现。
|
|
71
|
+
"""
|
|
72
|
+
# 尝试使用适配器模式
|
|
73
|
+
return self.__convert_model("google_adk")
|
|
74
|
+
|
|
75
|
+
def to_langchain(self) -> Any:
|
|
76
|
+
"""转换为 LangChain ChatModel
|
|
77
|
+
|
|
78
|
+
优先使用适配器模式,如果适配器未注册则回退到旧实现。
|
|
79
|
+
"""
|
|
80
|
+
# 尝试使用适配器模式
|
|
81
|
+
return self.__convert_model("langchain")
|
|
82
|
+
|
|
83
|
+
def __call__(self, messages: list, **kwargs) -> Any:
|
|
84
|
+
"""直接调用模型"""
|
|
85
|
+
return self.completions(messages=messages, **kwargs)
|
|
86
|
+
|
|
87
|
+
def to_langgraph(self) -> Any:
|
|
88
|
+
"""转换为 LangGraph 兼容的模型。
|
|
89
|
+
|
|
90
|
+
LangGraph 与 LangChain 完全兼容,因此使用相同的接口。
|
|
91
|
+
"""
|
|
92
|
+
return self.__convert_model("langgraph")
|
|
93
|
+
|
|
94
|
+
def to_crewai(self) -> Any:
|
|
95
|
+
"""转换为 CrewAI 兼容的模型。
|
|
96
|
+
|
|
97
|
+
CrewAI 内部使用 LangChain,因此使用相同的接口。
|
|
98
|
+
"""
|
|
99
|
+
return self.__convert_model("crewai")
|
|
100
|
+
|
|
101
|
+
def to_pydantic_ai(self) -> Any:
|
|
102
|
+
"""转换为 PydanticAI 兼容的模型。
|
|
103
|
+
|
|
104
|
+
PydanticAI 支持 OpenAI 兼容的接口,返回一个包装对象。
|
|
105
|
+
"""
|
|
106
|
+
return self.__convert_model("pydantic_ai")
|
|
107
|
+
|
|
108
|
+
def to_agentscope(self) -> Any:
|
|
109
|
+
"""转换为 AgentScope ChatModelBase。"""
|
|
110
|
+
return self.__convert_model("agentscope")
|