-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathllm_utils.ts
More file actions
141 lines (133 loc) · 4.16 KB
/
llm_utils.ts
File metadata and controls
141 lines (133 loc) · 4.16 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
import { ModelProviderEnum } from '../types';
export function normalizeOpenAIApiHostAndPath(
options: { apiHost?: string; apiPath?: string },
defaults?: { apiHost?: string; apiPath?: string }
) {
let { apiHost, apiPath } = options
if (apiHost) {
apiHost = apiHost.trim()
}
if (apiPath) {
apiPath = apiPath.trim()
}
const DEFAULT_HOST = defaults?.apiHost ?? 'https://api.openai.com/v1'
const DEFAULT_PATH = defaults?.apiPath ?? '/chat/completions'
// 如果 apiHost 为空,直接返回默认的 apiHost 和 apiPath
if (!apiHost) {
apiHost = DEFAULT_HOST
apiPath = DEFAULT_PATH
return { apiHost, apiPath }
}
// 处理前后 '/' 的干扰
if (apiHost.endsWith('/')) {
apiHost = apiHost.slice(0, -1)
}
if (apiPath && !apiPath.startsWith('/')) {
apiPath = '/' + apiPath
}
// https 协议
if (apiHost && !apiHost.startsWith('http://') && !apiHost.startsWith('https://')) {
apiHost = 'https://' + apiHost
}
// 如果用户在 host 配置了完整的 host+path 接口地址
// 可以兼容的输入情况有:
// apiHost=https://my.proxy.com/v1/chat/completions
if (apiHost.endsWith(DEFAULT_PATH)) {
apiHost = apiHost.replace(DEFAULT_PATH, '')
apiPath = DEFAULT_PATH
}
// 如果当前配置的是 OpenAI 的 API,统一为默认的 apiHost 和 apiPath
if (apiHost.endsWith('://api.openai.com') || apiHost.endsWith('://api.openai.com/v1')) {
apiHost = DEFAULT_HOST
apiPath = DEFAULT_PATH
return { apiHost, apiPath }
}
// 如果当前配置的是 OpenRouter 的 API,统一 apiHost 和 apiPath
if (apiHost.endsWith('://openrouter.ai') || apiHost.endsWith('://openrouter.ai/api')) {
apiHost = 'https://openrouter.ai/api/v1'
apiPath = DEFAULT_PATH
return { apiHost, apiPath }
}
// 如果当前配置的是 x 的 API,统一 apiHost 和 apiPath
if (apiHost.endsWith('://api.x.com') || apiHost.endsWith('://api.x.com/v1')) {
apiHost = 'https://api.x.com/v1'
apiPath = DEFAULT_PATH
return { apiHost, apiPath }
}
// 如果只配置 apiHost,且 apiHost 不以 /v1 结尾
if (!apiHost.endsWith('/v1') && !apiPath) {
apiHost = apiHost + '/v1'
apiPath = DEFAULT_PATH
}
if (!apiPath) {
apiPath = DEFAULT_PATH
}
return { apiHost, apiPath }
}
export function normalizeOpenAIResponsesHostAndPath(options: { apiHost?: string; apiPath?: string }) {
const trimmedApiPath = options.apiPath?.trim()
const hasCustomApiPath = !!trimmedApiPath && trimmedApiPath !== '/responses'
const normalized = normalizeOpenAIApiHostAndPath(
hasCustomApiPath ? { ...options, apiPath: trimmedApiPath } : { ...options, apiPath: undefined },
{ apiPath: '/responses' }
)
if (!hasCustomApiPath) {
normalized.apiPath = '/responses'
}
return normalized
}
export function normalizeClaudeHost(apiHost: string) {
apiHost = apiHost.trim()
if (apiHost === 'https://api.anthropic.com') {
apiHost = `${apiHost}/v1`
}
if (apiHost.endsWith('/')) {
apiHost = apiHost.slice(0, apiHost.length - 1)
}
return {
apiHost,
apiPath: '/messages',
}
}
export function normalizeGeminiHost(apiHost: string) {
apiHost = apiHost.trim()
if (apiHost.endsWith('/')) {
apiHost = apiHost.slice(0, apiHost.length - 1)
}
apiHost = `${apiHost}/v1beta`
return {
apiHost,
apiPath: '/models/[model]',
}
}
export function normalizeAzureEndpoint(endpoint: string) {
let origin = endpoint
try {
origin = new URL(endpoint.trim()).origin
} catch (_error) {
origin = `https://${origin}.openai.azure.com`
}
return {
endpoint: `${origin}/openai`,
apiPath: '/chat/completions',
}
}
export function isOpenAICompatible(providerId: string, _modelId: string) {
if (providerId === 'chatbox-ai') {
return false
}
return (
[
ModelProviderEnum.OpenAI,
ModelProviderEnum.SiliconFlow,
ModelProviderEnum.OpenRouter,
ModelProviderEnum.Ollama,
ModelProviderEnum.ChatGLM6B,
ModelProviderEnum.XAI,
ModelProviderEnum.Groq,
ModelProviderEnum.DeepSeek,
ModelProviderEnum.LMStudio,
ModelProviderEnum.ModelScope,
].includes(providerId as ModelProviderEnum) || providerId.startsWith('custom-provider-')
)
}