-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodel_adapter.py
More file actions
107 lines (88 loc) · 3.28 KB
/
model_adapter.py
File metadata and controls
107 lines (88 loc) · 3.28 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""LangChain 模型适配器 / LangChain Model Adapter
将 CommonModel 包装为 LangChain BaseChatModel。"""
from typing import Any, Set
from agentrun.integration.langchain.message_adapter import (
LangChainMessageAdapter,
)
from agentrun.integration.utils.adapter import ModelAdapter
OPENAI_COMPATIBLE_PROVIDERS: Set[str] = {
"openai",
"tongyi",
"deepseek",
"moonshot",
"baichuan",
"hunyuan",
"minimax",
"spark",
"stepfun",
"wenxin",
"yi",
"zhipuai",
"custom",
}
class LangChainModelAdapter(ModelAdapter):
"""LangChain 模型适配器 / LangChain Model Adapter
将 CommonModel 包装为 LangChain BaseChatModel。
根据 provider 自动选择对应的 LangChain Chat Model 类。"""
def __init__(self):
"""初始化适配器,创建内部的消息适配器 / LangChain Message Adapter"""
self._message_adapter = LangChainMessageAdapter()
def wrap_model(self, common_model: Any) -> Any:
"""包装 CommonModel 为 LangChain BaseChatModel / LangChain Model Adapter
根据 BaseInfo.provider 分发到对应的 LangChain Chat Model 类:
- anthropic -> ChatAnthropic
- gemini / vertex_ai -> ChatGoogleGenerativeAI
- 其他(openai 兼容) -> ChatOpenAI
"""
info = common_model.get_model_info()
provider = (info.provider or "").lower()
if provider == "anthropic":
return self._create_anthropic(info)
elif provider in ("gemini", "vertex_ai"):
return self._create_google(info)
else:
return self._create_openai(info)
def _create_openai(self, info: Any) -> Any:
from langchain_openai import ChatOpenAI
return ChatOpenAI(
name=info.model,
api_key=info.api_key,
model=info.model,
base_url=info.base_url,
default_headers=info.headers,
stream_usage=True,
streaming=True,
)
def _create_anthropic(self, info: Any) -> Any:
try:
from langchain_anthropic import ChatAnthropic
except ImportError as e:
raise ImportError(
"langchain-anthropic is required for Anthropic models. "
"Install it with: "
'pip install "agentrun-sdk[langchain-anthropic]"'
) from e
kwargs: dict[str, Any] = {
"model": info.model or "",
"anthropic_api_key": info.api_key,
"default_headers": info.headers or {},
"streaming": True,
}
if info.base_url:
kwargs["anthropic_api_url"] = info.base_url
return ChatAnthropic(**kwargs)
def _create_google(self, info: Any) -> Any:
try:
from langchain_google_genai import ChatGoogleGenerativeAI
except ImportError as e:
raise ImportError(
"langchain-google-genai is required for Google / "
"Vertex AI models. Install it with: "
'pip install "agentrun-sdk[langchain-google]"'
) from e
kwargs: dict[str, Any] = {
"model": info.model or "",
"google_api_key": info.api_key,
"default_headers": info.headers or {},
}
return ChatGoogleGenerativeAI(**kwargs)