-
Notifications
You must be signed in to change notification settings - Fork 540
Expand file tree
/
Copy pathProviderCode.php
More file actions
292 lines (271 loc) · 10.4 KB
/
Copy pathProviderCode.php
File metadata and controls
292 lines (271 loc) · 10.4 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
declare(strict_types=1);
/**
* Copyright (c) The Magic , Distributed under the software license
*/
namespace App\Domain\Provider\Entity\ValueObject;
use App\Domain\Provider\DTO\Item\AbstractProviderConfigItem;
use App\Domain\Provider\DTO\Item\GoogleProviderConfigItem;
use App\Domain\Provider\DTO\Item\ProviderConfigItem;
use App\ErrorCode\ServiceProviderErrorCode;
use App\Infrastructure\Core\Exception\ExceptionBuilder;
use Hyperf\Odin\Model\AnthropicModel;
use Hyperf\Odin\Model\AwsBedrockModel;
use Hyperf\Odin\Model\AzureOpenAIModel;
use Hyperf\Odin\Model\DashScopeModel;
use Hyperf\Odin\Model\DeepSeekModel;
use Hyperf\Odin\Model\DoubaoModel;
use Hyperf\Odin\Model\GeminiModel;
use Hyperf\Odin\Model\KimiModel;
use Hyperf\Odin\Model\OpenAIModel;
enum ProviderCode: string
{
case None = 'None';
case Official = 'Official'; // 官方
case Wuyin = 'Wuyin';
case Cloudsway = 'Cloudsway';
case Volcengine = 'Volcengine'; // 火山
case MagicEngine = 'MagicEngine'; // Built-in self-hosted engine (go-engine)
case OpenAI = 'OpenAI';
case MicrosoftAzure = 'MicrosoftAzure';
case Qwen = 'Qwen';
case DeepSeek = 'DeepSeek';
case Tencent = 'Tencent';
case Baidu = 'Baidu';
case SCNet = 'SCNet';
case Moonshot = 'Moonshot';
case BigModel = 'BigModel';
case MiniMax = 'MiniMax';
case SiliconFlow = 'SiliconFlow';
case TTAPI = 'TTAPI';
case MiracleVision = 'MiracleVision';
case AWSBedrock = 'AWSBedrock';
case Google = 'Google-Image';
case VolcengineArk = 'VolcengineArk';
case Keling = 'Keling';
case Gemini = 'Gemini';
case DashScope = 'DashScope';
case OpenRouter = 'OpenRouter';
case Requesty = 'Requesty';
case SuChuang = 'SuChuang';
case Anthropic = 'Anthropic';
public function getImplementation(): string
{
return $this->getImplementationForModel();
}
public function getImplementationForModel(bool $embedding = false, bool $multiModal = false): string
{
return match ($this) {
self::Volcengine => self::requireImplementationClass(DoubaoModel::class),
self::MicrosoftAzure => self::requireImplementationClass(AzureOpenAIModel::class),
self::AWSBedrock => self::requireImplementationClass(AwsBedrockModel::class),
self::Gemini => self::requireImplementationClass(GeminiModel::class),
self::DeepSeek => self::requireImplementationClass(DeepSeekModel::class),
self::DashScope => self::requireImplementationClass(DashScopeModel::class),
self::Anthropic => self::requireImplementationClass(AnthropicModel::class),
self::Moonshot => self::requireImplementationClass(KimiModel::class),
default => self::requireImplementationClass(OpenAIModel::class),
};
}
public function getImplementationConfig(AbstractProviderConfigItem $config, string $name = ''): array
{
$config->setUrl($this->getModelUrl($config));
switch (get_class($config)) {
case ProviderConfigItem::class:
return match ($this) {
self::MicrosoftAzure => [
'api_key' => $config->getApiKey(),
'api_base' => $config->getUrl(),
'api_version' => $config->getApiVersion(),
'deployment_name' => $name,
],
self::AWSBedrock => [
'access_key' => $config->getAk(),
'secret_key' => $config->getSk(),
'region' => $config->getRegion(),
'auto_cache' => config('llm.aws_bedrock_auto_cache', true),
],
self::Anthropic => [
'api_key' => $config->getApiKey(),
'base_url' => $config->getUrl(),
'anthropic_version' => $config->getApiVersion(),
'auto_cache' => config('llm.anthropic_auto_cache', true),
],
default => [
'api_key' => $config->getApiKey(),
'base_url' => $config->getUrl(),
'auto_cache' => config('llm.openai_auto_cache', true),
'auto_cache_config' => [
'auto_enabled' => config('llm.openai_auto_cache', true),
],
]
};
case GoogleProviderConfigItem::class:
return [
'api_key' => $config->getApiKey(),
'base_url' => $config->getUrl(),
'auto_cache_config' => [
'enable_cache' => config('llm.gemini_auto_cache', true),
],
'service_account' => $config->toOdinServiceAccountConfig(),
];
default:
return [
'api_key' => $config->getApiKey(),
'base_url' => $config->getUrl(),
'auto_cache' => config('llm.openai_auto_cache', true),
'auto_cache_config' => [
'auto_enabled' => config('llm.openai_auto_cache', true),
],
];
}
}
public function isOfficial(): bool
{
return $this === self::Official;
}
/**
* 判断当前服务商是否属于 Provider Control 的 LLM allowlist。
*/
public function isInProviderControlLlmAllowlist(): bool
{
return match ($this) {
self::DashScope,
self::Volcengine,
self::DeepSeek,
self::Tencent,
self::Baidu,
self::SCNet,
self::Moonshot,
self::BigModel,
self::MiniMax,
self::SiliconFlow => true,
default => false,
};
}
/**
* 判断当前服务商是否属于 Provider Control allowlist。
*/
public function isInProviderControlAllowlist(Category $category): bool
{
return match ($category) {
Category::LLM => $this->isInProviderControlLlmAllowlist(),
Category::VLM => match ($this) {
self::Qwen, self::VolcengineArk, self::TTAPI, self::MiracleVision, self::Volcengine => true,
default => false,
},
Category::VGM => match ($this) {
self::Keling, self::DashScope, self::VolcengineArk => true,
default => false,
},
default => false,
};
}
/**
* 获取服务商推荐接入地址。
*/
public function getDefaultUrl(): string
{
return match ($this) {
self::DashScope => 'https://dashscope.aliyuncs.com/compatible-mode/v1',
self::Volcengine,
self::VolcengineArk => 'https://ark.cn-beijing.volces.com/api/v3',
self::DeepSeek => 'https://api.deepseek.com',
self::Tencent => 'https://api.hunyuan.cloud.tencent.com/v1',
self::Baidu => 'https://qianfan.baidubce.com/v2',
self::SCNet => 'https://api.scnet.cn/api/llm/v1',
self::Moonshot => 'https://api.moonshot.cn/v1',
self::BigModel => 'https://open.bigmodel.cn/api/paas/v4',
self::MiniMax => 'https://api.minimaxi.com/v1',
self::SiliconFlow => 'https://api.siliconflow.cn/v1',
default => '',
};
}
/**
* 获取服务商允许的一级域名后缀。
*
* 非官方组织下,用户填写的 URL 只要命中这些一级域名即可通过校验。
*/
public function getAllowedPrimaryDomains(): array
{
return match ($this) {
self::DashScope, self::Qwen => ['aliyuncs.com'],
self::Volcengine, self::VolcengineArk => ['volces.com'],
self::DeepSeek => ['deepseek.com'],
self::Tencent => ['tencent.com'],
self::Baidu => ['baidubce.com'],
self::SCNet => ['scnet.cn'],
self::Moonshot => ['moonshot.cn'],
self::BigModel => ['bigmodel.cn'],
self::MiniMax => ['minimaxi.com'],
self::SiliconFlow => ['siliconflow.cn'],
default => [],
};
}
/**
* 判断服务商配置的 URL 是否命中了允许的一级域名。
*/
public function isAllowedPrimaryDomainUrl(string $url): bool
{
$allowedPrimaryDomains = $this->getAllowedPrimaryDomains();
if ($allowedPrimaryDomains === []) {
return true;
}
$host = strtolower((string) parse_url($url, PHP_URL_HOST));
if ($host === '') {
return false;
}
foreach ($allowedPrimaryDomains as $primaryDomain) {
$primaryDomain = strtolower($primaryDomain);
if ($host === $primaryDomain || str_ends_with($host, '.' . $primaryDomain)) {
return true;
}
}
return false;
}
/**
* 获取服务商的排序顺序(用于非官方服务商列表展示).
* 排序优先级与当前服务商模板展示顺序保持一致。
*
* @return int 排序值,值越小越靠前
*/
public function getSortOrder(): int
{
return match ($this) {
self::MicrosoftAzure => 1,
self::Google, self::Gemini => 2,
self::AWSBedrock => 3,
self::TTAPI => 4,
self::DashScope => 5,
self::OpenRouter => 6,
self::Volcengine, self::VolcengineArk => 7,
self::DeepSeek => 8,
self::Tencent => 9,
self::Baidu => 10,
self::SCNet => 11,
self::Moonshot => 12,
self::BigModel => 13,
self::MiniMax => 14,
self::SiliconFlow => 15,
self::Anthropic => 16,
default => 999, // 其他服务商排在最后
};
}
private static function requireImplementationClass(string $implementationClass): string
{
if (! class_exists($implementationClass)) {
ExceptionBuilder::throw(
ServiceProviderErrorCode::SystemError,
'service_provider.provider_not_implemented'
);
}
return $implementationClass;
}
/**
* 获取模型实际使用的请求地址。
*/
private function getModelUrl(AbstractProviderConfigItem $config): string
{
return $config->getUrl();
}
}