Skip to content

Commit cafd0a0

Browse files
committed
feat: add OpenAI compatible channel (close #2091)
1 parent 8b8cd03 commit cafd0a0

File tree

7 files changed

+94
-57
lines changed

7 files changed

+94
-57
lines changed

relay/adaptor/openai/helper.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ func ResponseText2Usage(responseText string, modelName string, promptTokens int)
1717
}
1818

1919
func GetFullRequestURL(baseURL string, requestURL string, channelType int) string {
20+
if channelType == channeltype.OpenAICompatible {
21+
return fmt.Sprintf("%s%s", strings.TrimSuffix(baseURL, "/"), strings.TrimPrefix(requestURL, "/v1"))
22+
}
2023
fullRequestURL := fmt.Sprintf("%s%s", baseURL, requestURL)
2124

2225
if strings.HasPrefix(baseURL, "https://gateway.ai.cloudflare.com") {

relay/channeltype/define.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ const (
5151
BaiduV2
5252
XunfeiV2
5353
AliBailian
54+
OpenAICompatible
5455
Dummy
5556
)

relay/channeltype/url.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var ChannelBaseURLs = []string{
5151
"https://qianfan.baidubce.com", // 47
5252
"https://spark-api-open.xf-yun.com", // 48
5353
"https://dashscope.aliyuncs.com", // 49
54+
"", // 50
5455
}
5556

5657
func init() {
Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
export const CHANNEL_OPTIONS = [
2-
{key: 1, text: 'OpenAI', value: 1, color: 'green'},
3-
{key: 14, text: 'Anthropic Claude', value: 14, color: 'black'},
4-
{key: 33, text: 'AWS', value: 33, color: 'black'},
5-
{key: 3, text: 'Azure OpenAI', value: 3, color: 'olive'},
6-
{key: 11, text: 'Google PaLM2', value: 11, color: 'orange'},
7-
{key: 24, text: 'Google Gemini', value: 24, color: 'orange'},
8-
{key: 28, text: 'Mistral AI', value: 28, color: 'orange'},
9-
{key: 41, text: 'Novita', value: 41, color: 'purple'},
2+
{key: 1, text: 'OpenAI', value: 1, color: 'green'},
3+
{
4+
key: 50,
5+
text: 'OpenAI 兼容',
6+
value: 50,
7+
color: 'olive',
8+
description: 'OpenAI 兼容渠道,支持设置 Base URL',
9+
},
10+
{key: 14, text: 'Anthropic Claude', value: 14, color: 'black'},
11+
{key: 33, text: 'AWS', value: 33, color: 'black'},
12+
{key: 3, text: 'Azure OpenAI', value: 3, color: 'olive'},
13+
{key: 11, text: 'Google PaLM2', value: 11, color: 'orange'},
14+
{key: 24, text: 'Google Gemini', value: 24, color: 'orange'},
15+
{key: 28, text: 'Mistral AI', value: 28, color: 'orange'},
16+
{key: 41, text: 'Novita', value: 41, color: 'purple'},
1017
{
1118
key: 40,
1219
text: '字节火山引擎',
@@ -28,14 +35,14 @@ export const CHANNEL_OPTIONS = [
2835
color: 'blue',
2936
tip: '请前往<a href="https://console.bce.baidu.com/iam/#/iam/apikey/list" target="_blank">此处</a>获取 API Key,注意本渠道仅支持<a target="_blank" href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/em4tsqo3v">推理服务 V2</a>相关模型',
3037
},
31-
{
32-
key: 17,
33-
text: '阿里通义千问',
34-
value: 17,
35-
color: 'orange',
36-
tip: '如需使用阿里云百炼,请使用<strong>阿里云百炼</strong>渠道',
37-
},
38-
{key: 49, text: '阿里云百炼', value: 49, color: 'orange'},
38+
{
39+
key: 17,
40+
text: '阿里通义千问',
41+
value: 17,
42+
color: 'orange',
43+
tip: '如需使用阿里云百炼,请使用<strong>阿里云百炼</strong>渠道',
44+
},
45+
{key: 49, text: '阿里云百炼', value: 49, color: 'orange'},
3946
{
4047
key: 18,
4148
text: '讯飞星火认知',
@@ -50,38 +57,44 @@ export const CHANNEL_OPTIONS = [
5057
color: 'blue',
5158
tip: 'HTTP 版本的讯飞接口,前往<a href="https://console.xfyun.cn/services/cbm" target="_blank">此处</a>获取 HTTP 服务接口认证密钥',
5259
},
53-
{key: 16, text: '智谱 ChatGLM', value: 16, color: 'violet'},
54-
{key: 19, text: '360 智脑', value: 19, color: 'blue'},
55-
{key: 25, text: 'Moonshot AI', value: 25, color: 'black'},
56-
{key: 23, text: '腾讯混元', value: 23, color: 'teal'},
57-
{key: 26, text: '百川大模型', value: 26, color: 'orange'},
58-
{key: 27, text: 'MiniMax', value: 27, color: 'red'},
59-
{key: 29, text: 'Groq', value: 29, color: 'orange'},
60-
{key: 30, text: 'Ollama', value: 30, color: 'black'},
61-
{key: 31, text: '零一万物', value: 31, color: 'green'},
62-
{key: 32, text: '阶跃星辰', value: 32, color: 'blue'},
63-
{key: 34, text: 'Coze', value: 34, color: 'blue'},
64-
{key: 35, text: 'Cohere', value: 35, color: 'blue'},
65-
{key: 36, text: 'DeepSeek', value: 36, color: 'black'},
66-
{key: 37, text: 'Cloudflare', value: 37, color: 'orange'},
67-
{key: 38, text: 'DeepL', value: 38, color: 'black'},
68-
{key: 39, text: 'together.ai', value: 39, color: 'blue'},
69-
{key: 42, text: 'VertexAI', value: 42, color: 'blue'},
70-
{key: 43, text: 'Proxy', value: 43, color: 'blue'},
71-
{key: 44, text: 'SiliconFlow', value: 44, color: 'blue'},
72-
{key: 45, text: 'xAI', value: 45, color: 'blue'},
73-
{key: 46, text: 'Replicate', value: 46, color: 'blue'},
74-
{key: 8, text: '自定义渠道', value: 8, color: 'pink'},
75-
{key: 22, text: '知识库:FastGPT', value: 22, color: 'blue'},
76-
{key: 21, text: '知识库:AI Proxy', value: 21, color: 'purple'},
77-
{key: 20, text: 'OpenRouter', value: 20, color: 'black'},
78-
{key: 2, text: '代理:API2D', value: 2, color: 'blue'},
79-
{key: 5, text: '代理:OpenAI-SB', value: 5, color: 'brown'},
80-
{key: 7, text: '代理:OhMyGPT', value: 7, color: 'purple'},
81-
{key: 10, text: '代理:AI Proxy', value: 10, color: 'purple'},
82-
{key: 4, text: '代理:CloseAI', value: 4, color: 'teal'},
83-
{key: 6, text: '代理:OpenAI Max', value: 6, color: 'violet'},
84-
{key: 9, text: '代理:AI.LS', value: 9, color: 'yellow'},
85-
{key: 12, text: '代理:API2GPT', value: 12, color: 'blue'},
86-
{key: 13, text: '代理:AIGC2D', value: 13, color: 'purple'},
60+
{key: 16, text: '智谱 ChatGLM', value: 16, color: 'violet'},
61+
{key: 19, text: '360 智脑', value: 19, color: 'blue'},
62+
{key: 25, text: 'Moonshot AI', value: 25, color: 'black'},
63+
{key: 23, text: '腾讯混元', value: 23, color: 'teal'},
64+
{key: 26, text: '百川大模型', value: 26, color: 'orange'},
65+
{key: 27, text: 'MiniMax', value: 27, color: 'red'},
66+
{key: 29, text: 'Groq', value: 29, color: 'orange'},
67+
{key: 30, text: 'Ollama', value: 30, color: 'black'},
68+
{key: 31, text: '零一万物', value: 31, color: 'green'},
69+
{key: 32, text: '阶跃星辰', value: 32, color: 'blue'},
70+
{key: 34, text: 'Coze', value: 34, color: 'blue'},
71+
{key: 35, text: 'Cohere', value: 35, color: 'blue'},
72+
{key: 36, text: 'DeepSeek', value: 36, color: 'black'},
73+
{key: 37, text: 'Cloudflare', value: 37, color: 'orange'},
74+
{key: 38, text: 'DeepL', value: 38, color: 'black'},
75+
{key: 39, text: 'together.ai', value: 39, color: 'blue'},
76+
{key: 42, text: 'VertexAI', value: 42, color: 'blue'},
77+
{key: 43, text: 'Proxy', value: 43, color: 'blue'},
78+
{key: 44, text: 'SiliconFlow', value: 44, color: 'blue'},
79+
{key: 45, text: 'xAI', value: 45, color: 'blue'},
80+
{key: 46, text: 'Replicate', value: 46, color: 'blue'},
81+
{
82+
key: 8,
83+
text: '自定义渠道',
84+
value: 8,
85+
color: 'pink',
86+
tip: '注意,这里所需要填入的代理地址仅会在实际请求时替换域名部分,如果你想填入 OpenAI SDK 中所要求的 Base URL,请使用 OpenAI 兼容渠道类型',
87+
},
88+
{key: 22, text: '知识库:FastGPT', value: 22, color: 'blue'},
89+
{key: 21, text: '知识库:AI Proxy', value: 21, color: 'purple'},
90+
{key: 20, text: 'OpenRouter', value: 20, color: 'black'},
91+
{key: 2, text: '代理:API2D', value: 2, color: 'blue'},
92+
{key: 5, text: '代理:OpenAI-SB', value: 5, color: 'brown'},
93+
{key: 7, text: '代理:OhMyGPT', value: 7, color: 'purple'},
94+
{key: 10, text: '代理:AI Proxy', value: 10, color: 'purple'},
95+
{key: 4, text: '代理:CloseAI', value: 4, color: 'teal'},
96+
{key: 6, text: '代理:OpenAI Max', value: 6, color: 'violet'},
97+
{key: 9, text: '代理:AI.LS', value: 9, color: 'yellow'},
98+
{key: 12, text: '代理:API2GPT', value: 12, color: 'blue'},
99+
{key: 13, text: '代理:AIGC2D', value: 13, color: 'purple'},
87100
];

web/default/src/locales/en/translation.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@
104104
"model_mapping_placeholder": "Optional, used to modify model names in request body. A JSON string where keys are request model names and values are target model names",
105105
"system_prompt": "System Prompt",
106106
"system_prompt_placeholder": "Optional, used to force set system prompt. Use with custom model & model mapping. First create a unique custom model name above, then map it to a natively supported model",
107-
"base_url": "Proxy",
108-
"base_url_placeholder": "Optional, used for API calls through proxy. Enter proxy address in format: https://domain.com",
107+
"proxy_url": "Proxy",
108+
"proxy_url_placeholder": "This is optional and used for API calls via a proxy. Please enter the proxy URL, formatted as: https://domain.com",
109+
"base_url": "Base URL",
110+
"base_url_placeholder": "The Base URL required by the OpenAPI SDK",
109111
"key": "Key",
110112
"key_placeholder": "Please enter key",
111113
"batch": "Batch Create",

web/default/src/locales/zh/translation.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@
104104
"model_mapping_placeholder": "此项可选,用于修改请求体中的模型名称,为一个 JSON 字符串,键为请求中模型名称,值为要替换的模型名称",
105105
"system_prompt": "系统提示词",
106106
"system_prompt_placeholder": "此项可选,用于强制设置给定的系统提示词,请配合自定义模型 & 模型重定向使用,首先创建一个唯一的自定义模型名称并在上面填入,之后将该自定义模型重定向映射到该渠道一个原生支持的模型",
107-
"base_url": "代理",
108-
"base_url_placeholder": "此项可选,用于通过代理站来进行 API 调用,请输入代理站地址,格式为:https://domain.com",
107+
"proxy_url": "代理",
108+
"proxy_url_placeholder": "此项可选,用于通过代理站来进行 API 调用,请输入代理站地址,格式为:https://domain.com。注意,这里所需要填入的代理地址仅会在实际请求时替换域名部分,如果你想填入 OpenAI SDK 中所要求的 Base URL,请使用 OpenAI 兼容渠道类型",
109+
"base_url": "Base URL",
110+
"base_url_placeholder": "OpenAPI SDK 中所要求的 Base URL",
109111
"key": "密钥",
110112
"key_placeholder": "请输入密钥",
111113
"batch": "批量创建",

web/default/src/pages/Channel/EditChannel.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, {useEffect, useState} from 'react';
22
import {useTranslation} from 'react-i18next';
3-
import {Button, Card, Form, Input, Message,} from 'semantic-ui-react';
3+
import {Button, Card, Form, Input, Message} from 'semantic-ui-react';
44
import {useNavigate, useParams} from 'react-router-dom';
55
import {API, copy, getChannelModels, showError, showInfo, showSuccess, verifyJSON,} from '../../helpers';
66
import {CHANNEL_OPTIONS} from '../../constants';
@@ -339,6 +339,20 @@ const EditChannel = () => {
339339
{inputs.type === 8 && (
340340
<Form.Field>
341341
<Form.Input
342+
required
343+
label={t('channel.edit.proxy_url')}
344+
name='base_url'
345+
placeholder={t('channel.edit.proxy_url_placeholder')}
346+
onChange={handleInputChange}
347+
value={inputs.base_url}
348+
autoComplete='new-password'
349+
/>
350+
</Form.Field>
351+
)}
352+
{inputs.type === 50 && (
353+
<Form.Field>
354+
<Form.Input
355+
required
342356
label={t('channel.edit.base_url')}
343357
name='base_url'
344358
placeholder={t('channel.edit.base_url_placeholder')}
@@ -637,12 +651,13 @@ const EditChannel = () => {
637651
{inputs.type !== 3 &&
638652
inputs.type !== 33 &&
639653
inputs.type !== 8 &&
654+
inputs.type !== 50 &&
640655
inputs.type !== 22 && (
641656
<Form.Field>
642657
<Form.Input
643-
label={t('channel.edit.base_url')}
658+
label={t('channel.edit.proxy_url')}
644659
name='base_url'
645-
placeholder={t('channel.edit.base_url_placeholder')}
660+
placeholder={t('channel.edit.proxy_url_placeholder')}
646661
onChange={handleInputChange}
647662
value={inputs.base_url}
648663
autoComplete='new-password'

0 commit comments

Comments
 (0)