Skip to content

Commit

Permalink
feat: Azure API supported without verification (#48, #57)
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed May 13, 2023
1 parent fd19d7d commit 7a3378b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 120 deletions.
28 changes: 21 additions & 7 deletions controller/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ func relayHelper(c *gin.Context) error {
channelType := c.GetInt("channel")
tokenId := c.GetInt("token_id")
consumeQuota := c.GetBool("consume_quota")
baseURL := common.ChannelBaseURLs[channelType]
if channelType == common.ChannelTypeCustom {
baseURL = c.GetString("base_url")
}
var textRequest TextRequest
if consumeQuota {
if consumeQuota || channelType == common.ChannelTypeAzure {
requestBody, err := io.ReadAll(c.Request.Body)
if err != nil {
return err
Expand All @@ -89,12 +85,30 @@ func relayHelper(c *gin.Context) error {
// Reset request body
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
}
baseURL := common.ChannelBaseURLs[channelType]
requestURL := c.Request.URL.String()
req, err := http.NewRequest(c.Request.Method, fmt.Sprintf("%s%s", baseURL, requestURL), c.Request.Body)
if channelType == common.ChannelTypeCustom {
baseURL = c.GetString("base_url")
}
fullRequestURL := fmt.Sprintf("%s%s", baseURL, requestURL)
if channelType == common.ChannelTypeAzure {
// https://learn.microsoft.com/en-us/azure/cognitive-services/openai/chatgpt-quickstart?pivots=rest-api&tabs=command-line#rest-api
baseURL = c.GetString("base_url")
task := strings.TrimPrefix(requestURL, "/v1/")
model_ := textRequest.Model
fullRequestURL = fmt.Sprintf("%s/openai/deployments/%s/%s", baseURL, model_, task)
}
req, err := http.NewRequest(c.Request.Method, fullRequestURL, c.Request.Body)
if err != nil {
return err
}
req.Header.Set("Authorization", c.Request.Header.Get("Authorization"))
if channelType == common.ChannelTypeAzure {
key := c.Request.Header.Get("Authorization")
key = strings.TrimPrefix(key, "Bearer ")
req.Header.Set("api-key", key)
} else {
req.Header.Set("Authorization", c.Request.Header.Get("Authorization"))
}
req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type"))
req.Header.Set("Accept", c.Request.Header.Get("Accept"))
req.Header.Set("Connection", c.Request.Header.Get("Connection"))
Expand Down
2 changes: 1 addition & 1 deletion middleware/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Distribute() func(c *gin.Context) {
}
c.Set("channel", channel.Type)
c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
if channel.Type == common.ChannelTypeCustom {
if channel.Type == common.ChannelTypeCustom || channel.Type == common.ChannelTypeAzure {
c.Set("base_url", channel.BaseURL)
}
c.Next()
Expand Down
3 changes: 1 addition & 2 deletions web/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import Channel from './pages/Channel';
import Token from './pages/Token';
import EditToken from './pages/Token/EditToken';
import EditChannel from './pages/Channel/EditChannel';
import AddChannel from './pages/Channel/AddChannel';
import Redemption from './pages/Redemption';
import EditRedemption from './pages/Redemption/EditRedemption';

Expand Down Expand Up @@ -93,7 +92,7 @@ function App() {
path='/channel/add'
element={
<Suspense fallback={<Loading></Loading>}>
<AddChannel />
<EditChannel />
</Suspense>
}
/>
Expand Down
95 changes: 0 additions & 95 deletions web/src/pages/Channel/AddChannel.js

This file was deleted.

65 changes: 50 additions & 15 deletions web/src/pages/Channel/EditChannel.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import React, { useEffect, useState } from 'react';
import { Button, Form, Header, Segment } from 'semantic-ui-react';
import { Button, Form, Header, Message, Segment } from 'semantic-ui-react';
import { useParams } from 'react-router-dom';
import { API, showError, showSuccess } from '../../helpers';
import { CHANNEL_OPTIONS } from '../../constants';

const EditChannel = () => {
const params = useParams();
const channelId = params.id;
const [loading, setLoading] = useState(true);
const [inputs, setInputs] = useState({
const isEdit = channelId !== undefined;
const [loading, setLoading] = useState(isEdit);
const originInputs = {
name: '',
key: '',
type: 1,
base_url: '',
});
key: '',
base_url: ''
};
const [inputs, setInputs] = useState(originInputs);
const handleInputChange = (e, { name, value }) => {
setInputs((inputs) => ({ ...inputs, [name]: value }));
};
Expand All @@ -30,17 +32,31 @@ const EditChannel = () => {
setLoading(false);
};
useEffect(() => {
loadChannel().then();
if (isEdit) {
loadChannel().then();
}
}, []);

const submit = async () => {
if (inputs.base_url.endsWith('/')) {
inputs.base_url = inputs.base_url.slice(0, inputs.base_url.length - 1);
if (!isEdit && (inputs.name === '' || inputs.key === '')) return;
let localInputs = inputs;
if (localInputs.base_url.endsWith('/')) {
localInputs.base_url = localInputs.base_url.slice(0, localInputs.base_url.length - 1);
}
let res;
if (isEdit) {
res = await API.put(`/api/channel/`, { ...localInputs, id: parseInt(channelId) });
} else {
res = await API.post(`/api/channel/`, localInputs);
}
let res = await API.put(`/api/channel/`, { ...inputs, id: parseInt(channelId) });
const { success, message } = res.data;
if (success) {
showSuccess('渠道更新成功!');
if (isEdit) {
showSuccess('渠道更新成功!');
} else {
showSuccess('渠道创建成功!');
setInputs(originInputs);
}
} else {
showError(message);
}
Expand All @@ -49,7 +65,7 @@ const EditChannel = () => {
return (
<>
<Segment loading={loading}>
<Header as='h3'>更新渠道信息</Header>
<Header as='h3'>{isEdit ? '更新渠道信息' : '创建新的渠道'}</Header>
<Form autoComplete='new-password'>
<Form.Field>
<Form.Select
Expand All @@ -60,13 +76,32 @@ const EditChannel = () => {
onChange={handleInputChange}
/>
</Form.Field>
{
inputs.type === 3 && (
<>
<Message>
注意,创建资源时,部署名称必须和 OpenAI 官方的模型名称保持一致,因为 One API 会把请求体中的 model 参数替换为你的部署名称。
</Message>
<Form.Field>
<Form.Input
label='AZURE_OPENAI_ENDPOINT'
name='base_url'
placeholder={'请输入 AZURE_OPENAI_ENDPOINT,例如:https://docs-test-001.openai.azure.com'}
onChange={handleInputChange}
value={inputs.base_url}
autoComplete='new-password'
/>
</Form.Field>
</>
)
}
{
inputs.type === 8 && (
<Form.Field>
<Form.Input
label='Base URL'
name='base_url'
placeholder={'请输入新的自定义渠道的 Base URL,例如:https://openai.justsong.cn'}
placeholder={'请输入自定义渠道的 Base URL,例如:https://openai.justsong.cn'}
onChange={handleInputChange}
value={inputs.base_url}
autoComplete='new-password'
Expand All @@ -78,7 +113,7 @@ const EditChannel = () => {
<Form.Input
label='名称'
name='name'
placeholder={'请输入新的名称'}
placeholder={'请输入名称'}
onChange={handleInputChange}
value={inputs.name}
autoComplete='new-password'
Expand All @@ -88,7 +123,7 @@ const EditChannel = () => {
<Form.Input
label='密钥'
name='key'
placeholder={'请输入新的密钥'}
placeholder={'请输入密钥'}
onChange={handleInputChange}
value={inputs.key}
// type='password'
Expand Down

0 comments on commit 7a3378b

Please sign in to comment.