-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodel_adapter.py
More file actions
46 lines (35 loc) · 1.65 KB
/
model_adapter.py
File metadata and controls
46 lines (35 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Google ADK 模型适配器 / Google ADK Model Adapter
将 CommonModel 包装为 Google ADK BaseLlm。"""
from typing import Any
from agentrun.integration.google_adk.message_adapter import (
GoogleADKMessageAdapter,
)
from agentrun.integration.utils.adapter import ModelAdapter
from agentrun.integration.utils.model import CommonModel
class GoogleADKModelAdapter(ModelAdapter):
"""Google ADK 模型适配器 / Google ADK Model Adapter
将 CommonModel 包装为 Google ADK BaseLlm。"""
def __init__(self):
"""初始化适配器,创建内部的消息适配器 / Google ADK Message Adapter"""
self._message_adapter = GoogleADKMessageAdapter()
def wrap_model(self, common_model: CommonModel) -> Any:
"""包装 CommonModel 为 Google ADK BaseLlm / Google ADK Model Adapter"""
try:
from google.adk.models.lite_llm import LiteLlm # type: ignore
except ImportError as e:
raise ImportError(
"import google.adk.models.lite_llm failed."
"Google ADK may not installed, "
"Install it with: pip install google-adk"
) from e
info = common_model.get_model_info()
# 注意:不在此处设置 stream_options,因为:
# 1. Google ADK 内部决定是否使用流式请求
# 2. 在非流式请求中传递 stream_options 不符合 OpenAI API 规范
# 3. Google ADK 会自行处理 usage 信息
return LiteLlm(
model=f"{info.provider or 'openai'}/{info.model}",
api_base=info.base_url,
api_key=info.api_key,
extra_headers=info.headers,
)