agentrun-inner-test 0.0.46__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 +325 -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 +12 -0
- agentrun/integration/agentscope/adapter.py +17 -0
- agentrun/integration/agentscope/builtin.py +65 -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 +16 -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 +12 -0
- agentrun/integration/crewai/adapter.py +9 -0
- agentrun/integration/crewai/builtin.py +65 -0
- agentrun/integration/crewai/model_adapter.py +31 -0
- agentrun/integration/crewai/tool_adapter.py +26 -0
- agentrun/integration/google_adk/__init__.py +12 -0
- agentrun/integration/google_adk/adapter.py +15 -0
- agentrun/integration/google_adk/builtin.py +65 -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 +30 -0
- agentrun/integration/langchain/adapter.py +15 -0
- agentrun/integration/langchain/builtin.py +71 -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 +35 -0
- agentrun/integration/langgraph/adapter.py +20 -0
- agentrun/integration/langgraph/agent_converter.py +1073 -0
- agentrun/integration/langgraph/builtin.py +65 -0
- agentrun/integration/pydantic_ai/__init__.py +12 -0
- agentrun/integration/pydantic_ai/adapter.py +13 -0
- agentrun/integration/pydantic_ai/builtin.py +65 -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/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 +905 -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 +397 -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 +270 -0
- agentrun/utils/__data_api_async_template.py +720 -0
- agentrun/utils/__init__.py +5 -0
- agentrun/utils/__resource_async_template.py +158 -0
- agentrun/utils/config.py +258 -0
- agentrun/utils/control_api.py +78 -0
- agentrun/utils/data_api.py +1120 -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.46.dist-info/METADATA +263 -0
- agentrun_inner_test-0.0.46.dist-info/RECORD +135 -0
- agentrun_inner_test-0.0.46.dist-info/WHEEL +5 -0
- agentrun_inner_test-0.0.46.dist-info/licenses/LICENSE +201 -0
- agentrun_inner_test-0.0.46.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""内置工具集集成函数 / Built-in ToolSet Integration Functions
|
|
2
|
+
|
|
3
|
+
提供快速创建通用工具集对象的便捷函数。
|
|
4
|
+
Provides convenient functions for quickly creating common toolset objects.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Optional, Union
|
|
8
|
+
|
|
9
|
+
from agentrun.integration.utils.tool import CommonToolSet
|
|
10
|
+
from agentrun.toolset import ToolSet, ToolSetClient
|
|
11
|
+
from agentrun.utils.config import Config
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def toolset(
|
|
15
|
+
input: Union[str, ToolSet], config: Optional[Config] = None
|
|
16
|
+
) -> CommonToolSet:
|
|
17
|
+
"""将内置工具集封装为通用工具集 / Wrap built-in toolset as CommonToolSet
|
|
18
|
+
|
|
19
|
+
支持从工具集名称或 ToolSet 实例创建通用工具集。
|
|
20
|
+
Supports creating CommonToolSet from toolset name or ToolSet instance.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
input: 工具集名称或 ToolSet 实例 / Toolset name or ToolSet instance
|
|
24
|
+
config: 配置对象 / Configuration object
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
CommonToolSet: 通用工具集实例 / CommonToolSet instance
|
|
28
|
+
|
|
29
|
+
Examples:
|
|
30
|
+
>>> # 从工具集名称创建 / Create from toolset name
|
|
31
|
+
>>> ts = toolset("my-toolset")
|
|
32
|
+
>>>
|
|
33
|
+
>>> # 从 ToolSet 实例创建 / Create from ToolSet instance
|
|
34
|
+
>>> toolset_obj = ToolSetClient().get("my-toolset")
|
|
35
|
+
>>> ts = toolset(toolset_obj)
|
|
36
|
+
>>>
|
|
37
|
+
>>> # 转换为 LangChain 工具 / Convert to LangChain tools
|
|
38
|
+
>>> lc_tools = ts.to_langchain()
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
toolset = (
|
|
42
|
+
input
|
|
43
|
+
if isinstance(input, ToolSet)
|
|
44
|
+
else ToolSetClient().get(name=input, config=config)
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
return CommonToolSet.from_agentrun_toolset(toolset, config=config)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""CrewAI 集成模块。 / CrewAI 集成 Module
|
|
2
|
+
|
|
3
|
+
提供 AgentRun 模型与沙箱工具的 CrewAI 适配入口。 / 提供 AgentRun 模型with沙箱工具的 CrewAI 适配入口。
|
|
4
|
+
CrewAI 与 LangChain 兼容,因此直接复用 LangChain 的转换逻辑。 / CrewAI with LangChain 兼容,因此直接复用 LangChain 的转换逻辑。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from .builtin import model, sandbox_toolset
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"model",
|
|
11
|
+
"sandbox_toolset",
|
|
12
|
+
]
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""CrewAI 内置集成函数 / CrewAI Built-in Integration Functions
|
|
2
|
+
|
|
3
|
+
提供快速创建 CrewAI 兼容模型和工具的便捷函数。
|
|
4
|
+
Provides convenient functions for quickly creating CrewAI-compatible models and tools.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Any, Callable, List, Optional, Union
|
|
8
|
+
|
|
9
|
+
from typing_extensions import Unpack
|
|
10
|
+
|
|
11
|
+
from agentrun.integration.builtin import model as _model
|
|
12
|
+
from agentrun.integration.builtin import ModelArgs
|
|
13
|
+
from agentrun.integration.builtin import sandbox_toolset as _sandbox_toolset
|
|
14
|
+
from agentrun.integration.builtin import toolset as _toolset
|
|
15
|
+
from agentrun.integration.utils.tool import Tool
|
|
16
|
+
from agentrun.model import ModelProxy, ModelService
|
|
17
|
+
from agentrun.sandbox import TemplateType
|
|
18
|
+
from agentrun.toolset import ToolSet
|
|
19
|
+
from agentrun.utils.config import Config
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def model(
|
|
23
|
+
name: Union[str, ModelProxy, ModelService],
|
|
24
|
+
**kwargs: Unpack[ModelArgs],
|
|
25
|
+
):
|
|
26
|
+
"""获取 AgentRun 模型并转换为 LangChain ``BaseChatModel``。 / CrewAI Built-in Integration Functions"""
|
|
27
|
+
|
|
28
|
+
m = _model(input=name, **kwargs) # type: ignore
|
|
29
|
+
return m.to_crewai()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def toolset(
|
|
33
|
+
name: Union[str, ToolSet],
|
|
34
|
+
*,
|
|
35
|
+
prefix: Optional[str] = None,
|
|
36
|
+
modify_tool_name: Optional[Callable[[Tool], Tool]] = None,
|
|
37
|
+
filter_tools_by_name: Optional[Callable[[str], bool]] = None,
|
|
38
|
+
config: Optional[Config] = None,
|
|
39
|
+
) -> List[Any]:
|
|
40
|
+
"""将内置工具集封装为 LangChain ``StructuredTool`` 列表。 / CrewAI Built-in Integration Functions"""
|
|
41
|
+
|
|
42
|
+
ts = _toolset(input=name, config=config)
|
|
43
|
+
return ts.to_crewai(
|
|
44
|
+
prefix=prefix,
|
|
45
|
+
modify_tool_name=modify_tool_name,
|
|
46
|
+
filter_tools_by_name=filter_tools_by_name,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def sandbox_toolset(
|
|
51
|
+
template_name: str,
|
|
52
|
+
*,
|
|
53
|
+
template_type: TemplateType = TemplateType.CODE_INTERPRETER,
|
|
54
|
+
config: Optional[Config] = None,
|
|
55
|
+
sandbox_idle_timeout_seconds: int = 600,
|
|
56
|
+
prefix: Optional[str] = None,
|
|
57
|
+
) -> List[Any]:
|
|
58
|
+
"""将沙箱模板封装为 LangChain ``StructuredTool`` 列表。 / CrewAI Built-in Integration Functions"""
|
|
59
|
+
|
|
60
|
+
return _sandbox_toolset(
|
|
61
|
+
template_name=template_name,
|
|
62
|
+
template_type=template_type,
|
|
63
|
+
config=config,
|
|
64
|
+
sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
|
|
65
|
+
).to_crewai(prefix=prefix)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""LangChain 模型适配器 / CrewAI Model Adapter
|
|
2
|
+
|
|
3
|
+
将 CommonModel 包装为 LangChain BaseChatModel。"""
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from agentrun.integration.utils.adapter import ModelAdapter
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class CrewAIModelAdapter(ModelAdapter):
|
|
11
|
+
"""CrewAI 模型适配器 / CrewAI Model Adapter
|
|
12
|
+
|
|
13
|
+
将 CommonModel 包装为 CrewAI BaseChatModel。"""
|
|
14
|
+
|
|
15
|
+
def wrap_model(self, common_model: Any) -> Any:
|
|
16
|
+
"""包装 CommonModel 为 CrewAI BaseChatModel / CrewAI Model Adapter"""
|
|
17
|
+
from crewai import LLM
|
|
18
|
+
|
|
19
|
+
info = common_model.get_model_info() # 确保模型可用
|
|
20
|
+
|
|
21
|
+
# 注意:不在此处设置 stream_options,因为:
|
|
22
|
+
# 1. CrewAI 内部决定是否使用流式请求
|
|
23
|
+
# 2. 在非流式请求中传递 stream_options 不符合 OpenAI API 规范
|
|
24
|
+
# 3. CrewAI 会自行处理 usage 信息
|
|
25
|
+
return LLM(
|
|
26
|
+
api_key=info.api_key,
|
|
27
|
+
model=f"{info.provider or 'openai'}/{info.model}",
|
|
28
|
+
base_url=info.base_url,
|
|
29
|
+
default_headers=info.headers,
|
|
30
|
+
# async_client=AsyncClient(headers=info.headers),
|
|
31
|
+
)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""LangChain 工具适配器 / CrewAI Tool Adapter
|
|
2
|
+
|
|
3
|
+
将标准工具定义转换为 LangChain StructuredTool 格式。"""
|
|
4
|
+
|
|
5
|
+
from typing import Any, List
|
|
6
|
+
|
|
7
|
+
from agentrun.integration.utils.adapter import ToolAdapter
|
|
8
|
+
from agentrun.integration.utils.canonical import CanonicalTool
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class CrewAIToolAdapter(ToolAdapter):
|
|
12
|
+
"""CrewAI 工具适配器 / CrewAI Tool Adapter
|
|
13
|
+
|
|
14
|
+
实现 CanonicalTool → CrewAI StructuredTool 的转换。"""
|
|
15
|
+
|
|
16
|
+
def from_canonical(self, tools: List[CanonicalTool]) -> Any:
|
|
17
|
+
"""将标准格式转换为 CrewAI StructuredTool / CrewAI Tool Adapter"""
|
|
18
|
+
try:
|
|
19
|
+
from crewai.tools import tool
|
|
20
|
+
except ImportError as e:
|
|
21
|
+
raise ImportError(
|
|
22
|
+
"LangChain is not installed. "
|
|
23
|
+
"Install it with: pip install langchain-core"
|
|
24
|
+
) from e
|
|
25
|
+
|
|
26
|
+
return [tool(t) for t in self.function_tools(tools)]
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Google ADK 集成模块。 / Google ADK 集成 Module
|
|
2
|
+
|
|
3
|
+
提供与 Google Agent Development Kit 的模型与沙箱工具集成。 / 提供with Google Agent Development Kit 的模型with沙箱工具集成。
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from .builtin import model, sandbox_toolset, toolset
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"model",
|
|
10
|
+
"toolset",
|
|
11
|
+
"sandbox_toolset",
|
|
12
|
+
]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""Google ADK 适配器 / Google ADK Adapters
|
|
2
|
+
|
|
3
|
+
提供 Google ADK 框架的消息、工具和模型适配器。"""
|
|
4
|
+
|
|
5
|
+
from agentrun.integration.google_adk.message_adapter import (
|
|
6
|
+
GoogleADKMessageAdapter,
|
|
7
|
+
)
|
|
8
|
+
from agentrun.integration.google_adk.model_adapter import GoogleADKModelAdapter
|
|
9
|
+
from agentrun.integration.google_adk.tool_adapter import GoogleADKToolAdapter
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"GoogleADKMessageAdapter",
|
|
13
|
+
"GoogleADKToolAdapter",
|
|
14
|
+
"GoogleADKModelAdapter",
|
|
15
|
+
]
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""Google ADK 内置集成函数 / Google ADK Built-in Integration Functions
|
|
2
|
+
|
|
3
|
+
提供快速创建 Google ADK 兼容模型和工具的便捷函数。
|
|
4
|
+
Provides convenient functions for quickly creating Google ADK-compatible models and tools.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Any, Callable, List, Optional, Union
|
|
8
|
+
|
|
9
|
+
from typing_extensions import Unpack
|
|
10
|
+
|
|
11
|
+
from agentrun.integration.builtin import model as _model
|
|
12
|
+
from agentrun.integration.builtin import ModelArgs
|
|
13
|
+
from agentrun.integration.builtin import sandbox_toolset as _sandbox_toolset
|
|
14
|
+
from agentrun.integration.builtin import toolset as _toolset
|
|
15
|
+
from agentrun.integration.utils.tool import Tool
|
|
16
|
+
from agentrun.model import ModelProxy, ModelService
|
|
17
|
+
from agentrun.sandbox import TemplateType
|
|
18
|
+
from agentrun.toolset import ToolSet
|
|
19
|
+
from agentrun.utils.config import Config
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def model(
|
|
23
|
+
name: Union[str, ModelProxy, ModelService],
|
|
24
|
+
**kwargs: Unpack[ModelArgs],
|
|
25
|
+
):
|
|
26
|
+
"""获取 AgentRun 模型并转换为 LangChain ``BaseChatModel``。 / Google ADK Built-in Integration Functions"""
|
|
27
|
+
|
|
28
|
+
m = _model(input=name, **kwargs) # type: ignore
|
|
29
|
+
return m.to_google_adk()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def toolset(
|
|
33
|
+
name: Union[str, ToolSet],
|
|
34
|
+
*,
|
|
35
|
+
prefix: Optional[str] = None,
|
|
36
|
+
modify_tool_name: Optional[Callable[[Tool], Tool]] = None,
|
|
37
|
+
filter_tools_by_name: Optional[Callable[[str], bool]] = None,
|
|
38
|
+
config: Optional[Config] = None,
|
|
39
|
+
) -> List[Any]:
|
|
40
|
+
"""将内置工具集封装为 LangChain ``StructuredTool`` 列表。 / Google ADK Built-in Integration Functions"""
|
|
41
|
+
|
|
42
|
+
ts = _toolset(input=name, config=config)
|
|
43
|
+
return ts.to_google_adk(
|
|
44
|
+
prefix=prefix,
|
|
45
|
+
modify_tool_name=modify_tool_name,
|
|
46
|
+
filter_tools_by_name=filter_tools_by_name,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def sandbox_toolset(
|
|
51
|
+
template_name: str,
|
|
52
|
+
*,
|
|
53
|
+
template_type: TemplateType = TemplateType.CODE_INTERPRETER,
|
|
54
|
+
config: Optional[Config] = None,
|
|
55
|
+
sandbox_idle_timeout_seconds: int = 600,
|
|
56
|
+
prefix: Optional[str] = None,
|
|
57
|
+
) -> List[Any]:
|
|
58
|
+
"""将沙箱模板封装为 LangChain ``StructuredTool`` 列表。 / Google ADK Built-in Integration Functions"""
|
|
59
|
+
|
|
60
|
+
return _sandbox_toolset(
|
|
61
|
+
template_name=template_name,
|
|
62
|
+
template_type=template_type,
|
|
63
|
+
config=config,
|
|
64
|
+
sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
|
|
65
|
+
).to_google_adk(prefix=prefix)
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""Google ADK 消息适配器 / Google ADK Message Adapter
|
|
2
|
+
|
|
3
|
+
将 Google ADK LlmRequest 转换为标准格式,供 ModelAdapter 内部使用。
|
|
4
|
+
Converts Google ADK LlmRequest to canonical format for internal use by ModelAdapter.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
from typing import Any, List
|
|
9
|
+
|
|
10
|
+
from agentrun.integration.utils.adapter import MessageAdapter
|
|
11
|
+
from agentrun.integration.utils.canonical import (
|
|
12
|
+
CanonicalMessage,
|
|
13
|
+
CanonicalToolCall,
|
|
14
|
+
MessageRole,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class GoogleADKMessageAdapter(MessageAdapter):
|
|
19
|
+
"""Google ADK 消息适配器 / Google ADK Message Adapter
|
|
20
|
+
|
|
21
|
+
实现 Google ADK LlmRequest → CanonicalMessage 的转换。
|
|
22
|
+
Implements conversion from Google ADK LlmRequest to CanonicalMessage.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def to_canonical(self, messages: Any) -> List[CanonicalMessage]:
|
|
26
|
+
"""将 Google ADK LlmRequest 转换为标准格式 / Google ADK Message Adapter"""
|
|
27
|
+
canonical = []
|
|
28
|
+
|
|
29
|
+
# Google ADK 使用 LlmRequest,包含 contents 列表
|
|
30
|
+
if hasattr(messages, "contents"):
|
|
31
|
+
contents = messages.contents
|
|
32
|
+
elif isinstance(messages, list):
|
|
33
|
+
contents = messages
|
|
34
|
+
else:
|
|
35
|
+
contents = [messages]
|
|
36
|
+
|
|
37
|
+
# 处理 system_instruction (在 config 中)
|
|
38
|
+
if hasattr(messages, "config") and messages.config:
|
|
39
|
+
if (
|
|
40
|
+
hasattr(messages.config, "system_instruction")
|
|
41
|
+
and messages.config.system_instruction
|
|
42
|
+
):
|
|
43
|
+
canonical.append(
|
|
44
|
+
CanonicalMessage(
|
|
45
|
+
role=MessageRole.SYSTEM,
|
|
46
|
+
content=messages.config.system_instruction,
|
|
47
|
+
)
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
# 处理 contents
|
|
51
|
+
for content in contents:
|
|
52
|
+
# 确定角色
|
|
53
|
+
role = MessageRole.USER
|
|
54
|
+
if hasattr(content, "role"):
|
|
55
|
+
role_str = str(content.role).lower()
|
|
56
|
+
if "model" in role_str or "assistant" in role_str:
|
|
57
|
+
role = MessageRole.ASSISTANT
|
|
58
|
+
elif "system" in role_str:
|
|
59
|
+
role = MessageRole.SYSTEM
|
|
60
|
+
elif "tool" in role_str:
|
|
61
|
+
role = MessageRole.TOOL
|
|
62
|
+
elif "function" in role_str:
|
|
63
|
+
role = MessageRole.TOOL
|
|
64
|
+
else:
|
|
65
|
+
role = MessageRole.USER
|
|
66
|
+
|
|
67
|
+
# 处理 parts
|
|
68
|
+
if hasattr(content, "parts"):
|
|
69
|
+
text_parts = []
|
|
70
|
+
tool_calls = []
|
|
71
|
+
|
|
72
|
+
for part in content.parts:
|
|
73
|
+
# 处理文本
|
|
74
|
+
if hasattr(part, "text") and part.text:
|
|
75
|
+
text_parts.append(part.text)
|
|
76
|
+
|
|
77
|
+
# 处理 function_call
|
|
78
|
+
elif hasattr(part, "function_call") and part.function_call:
|
|
79
|
+
func_call = part.function_call
|
|
80
|
+
args = {}
|
|
81
|
+
if hasattr(func_call, "args"):
|
|
82
|
+
if isinstance(func_call.args, dict):
|
|
83
|
+
args = func_call.args
|
|
84
|
+
else:
|
|
85
|
+
try:
|
|
86
|
+
args = json.loads(str(func_call.args))
|
|
87
|
+
except (json.JSONDecodeError, TypeError):
|
|
88
|
+
args = {}
|
|
89
|
+
|
|
90
|
+
tool_calls.append(
|
|
91
|
+
CanonicalToolCall(
|
|
92
|
+
id=getattr(
|
|
93
|
+
func_call, "id", f"call_{len(tool_calls)}"
|
|
94
|
+
),
|
|
95
|
+
name=getattr(func_call, "name", ""),
|
|
96
|
+
arguments=args,
|
|
97
|
+
)
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
# 处理 function_response
|
|
101
|
+
elif (
|
|
102
|
+
hasattr(part, "function_response")
|
|
103
|
+
and part.function_response
|
|
104
|
+
):
|
|
105
|
+
func_resp = part.function_response
|
|
106
|
+
response_content = ""
|
|
107
|
+
if hasattr(func_resp, "response"):
|
|
108
|
+
if isinstance(func_resp.response, dict):
|
|
109
|
+
response_content = json.dumps(
|
|
110
|
+
func_resp.response
|
|
111
|
+
)
|
|
112
|
+
else:
|
|
113
|
+
response_content = str(func_resp.response)
|
|
114
|
+
else:
|
|
115
|
+
response_content = str(func_resp)
|
|
116
|
+
|
|
117
|
+
# function_response 表示工具返回结果
|
|
118
|
+
canonical.append(
|
|
119
|
+
CanonicalMessage(
|
|
120
|
+
role=MessageRole.TOOL,
|
|
121
|
+
content=response_content,
|
|
122
|
+
tool_call_id=getattr(func_resp, "id", "call_0"),
|
|
123
|
+
)
|
|
124
|
+
)
|
|
125
|
+
continue
|
|
126
|
+
|
|
127
|
+
# 构建消息
|
|
128
|
+
if text_parts or tool_calls:
|
|
129
|
+
content_text = " ".join(text_parts) if text_parts else None
|
|
130
|
+
canonical.append(
|
|
131
|
+
CanonicalMessage(
|
|
132
|
+
role=role,
|
|
133
|
+
content=content_text,
|
|
134
|
+
tool_calls=tool_calls if tool_calls else None,
|
|
135
|
+
)
|
|
136
|
+
)
|
|
137
|
+
else:
|
|
138
|
+
# 没有 parts,直接使用字符串内容
|
|
139
|
+
content_text = str(content) if content else None
|
|
140
|
+
canonical.append(
|
|
141
|
+
CanonicalMessage(role=role, content=content_text)
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
return canonical
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""Google ADK 模型适配器 / Google ADK Model Adapter
|
|
2
|
+
|
|
3
|
+
将 CommonModel 包装为 Google ADK BaseLlm。"""
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from agentrun.integration.google_adk.message_adapter import (
|
|
8
|
+
GoogleADKMessageAdapter,
|
|
9
|
+
)
|
|
10
|
+
from agentrun.integration.utils.adapter import ModelAdapter
|
|
11
|
+
from agentrun.integration.utils.model import CommonModel
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class GoogleADKModelAdapter(ModelAdapter):
|
|
15
|
+
"""Google ADK 模型适配器 / Google ADK Model Adapter
|
|
16
|
+
|
|
17
|
+
将 CommonModel 包装为 Google ADK BaseLlm。"""
|
|
18
|
+
|
|
19
|
+
def __init__(self):
|
|
20
|
+
"""初始化适配器,创建内部的消息适配器 / Google ADK Message Adapter"""
|
|
21
|
+
self._message_adapter = GoogleADKMessageAdapter()
|
|
22
|
+
|
|
23
|
+
def wrap_model(self, common_model: CommonModel) -> Any:
|
|
24
|
+
"""包装 CommonModel 为 Google ADK BaseLlm / Google ADK Model Adapter"""
|
|
25
|
+
|
|
26
|
+
try:
|
|
27
|
+
from google.adk.models.lite_llm import LiteLlm # type: ignore
|
|
28
|
+
except ImportError as e:
|
|
29
|
+
raise ImportError(
|
|
30
|
+
"import google.adk.models.lite_llm failed."
|
|
31
|
+
"Google ADK may not installed, "
|
|
32
|
+
"Install it with: pip install google-adk"
|
|
33
|
+
) from e
|
|
34
|
+
|
|
35
|
+
info = common_model.get_model_info()
|
|
36
|
+
|
|
37
|
+
# 注意:不在此处设置 stream_options,因为:
|
|
38
|
+
# 1. Google ADK 内部决定是否使用流式请求
|
|
39
|
+
# 2. 在非流式请求中传递 stream_options 不符合 OpenAI API 规范
|
|
40
|
+
# 3. Google ADK 会自行处理 usage 信息
|
|
41
|
+
return LiteLlm(
|
|
42
|
+
model=f"{info.provider or 'openai'}/{info.model}",
|
|
43
|
+
api_base=info.base_url,
|
|
44
|
+
api_key=info.api_key,
|
|
45
|
+
extra_headers=info.headers,
|
|
46
|
+
)
|