Compare commits

..

26 Commits
0.5.1 ... 0.0.1

Author SHA1 Message Date
ckt
b59757434e fix: react-script issue 2023-07-21 10:47:25 +00:00
ckt1031
f126d783c9 fix: use legacy react-scripts 2023-07-20 19:55:22 +08:00
ckt1031
66e02a4bcf feat: support custom http header 2023-07-19 21:25:34 +08:00
ckt1031
cf564f36fa fix: log model also 2023-07-19 13:54:53 +08:00
ckt1031
43f8d5fd92 fix: discord server joining issue 2023-07-19 13:53:49 +08:00
ckt1031
15d9a0c177 feat: remove terser 2023-07-19 13:49:03 +08:00
ckt1031
17125448cb fix: discord personal settings bind 2023-07-19 13:29:39 +08:00
ckt1031
a891a3e64f feat: bump deps 2023-07-18 22:29:47 +08:00
ckt1031
fd72565011 feat: support Discord Guild Join 2023-07-18 22:24:38 +08:00
ckt1031
4b9756b257 feat: support chatbot ui 2023-07-17 15:35:02 +08:00
ckt1031
a6ae20ed54 fix: chatgptweb 2023-07-16 21:48:54 +08:00
ckt1031
617149d731 fix: custom models 2023-07-16 21:23:56 +08:00
ckt1031
edd2c4f6e9 fix: testing issue 2023-07-16 16:01:52 +08:00
ckt1031
481c4ebf49 fix: chatgptweb issue 2023-07-16 15:35:32 +08:00
ckt1031
203471d7a9 Merge remote-tracking branch 'upstream/main' 2023-07-16 13:12:45 +08:00
JustSong
4139a7036f chore: make subscription api compatible with official api 2023-07-15 23:01:54 +08:00
JustSong
02da0b51f8 docs: update README 2023-07-15 19:07:38 +08:00
JustSong
35cfebee12 feat: retry on failed (close #112) 2023-07-15 19:06:51 +08:00
JustSong
0e088f7c3e feat: support ChatGLM2 (close #274) 2023-07-15 17:07:05 +08:00
JustSong
f61d326721 revert: do not enable turnstile check on login 2023-07-15 16:06:01 +08:00
JustSong
74b06b643a Merge branch 'main' of github.com:songquanpeng/one-api 2023-07-15 13:52:26 +08:00
JustSong
ccf7709e23 feat: support custom model now (close #276) 2023-07-15 13:51:46 +08:00
ckt
d592e2c8b8 feat: add turnstile for login form (#263) 2023-07-15 12:41:21 +08:00
ckt
b520b54625 feat: initial support of Dall-E (#148, #266)
* feat: initial support of Dall-E

* fix: fix N not timed

---------

Co-authored-by: JustSong <songquanpeng@foxmail.com>
Co-authored-by: JustSong <39998050+songquanpeng@users.noreply.github.com>
2023-07-15 12:30:06 +08:00
玩牛牛
81c5901123 feat: add support for /v1/engines/text-embedding-ada-002/embeddings (#224, close #222) 2023-07-15 12:03:23 +08:00
JustSong
abc53cb208 feat: disable channel when account_deactivated received (close #271) 2023-07-15 11:49:58 +08:00
74 changed files with 16345 additions and 681 deletions

View File

@@ -24,7 +24,7 @@ jobs:
run: |
cd web
npm install
VITE_REACT_APP_VERSION=$(git describe --tags) npm run build
REACT_APP_VERSION=$(git describe --tags) npm run build
cd ..
- name: Set up Go
uses: actions/setup-go@v3

View File

@@ -24,7 +24,7 @@ jobs:
run: |
cd web
npm install
VITE_REACT_APP_VERSION=$(git describe --tags) npm run build
REACT_APP_VERSION=$(git describe --tags) npm run build
cd ..
- name: Set up Go
uses: actions/setup-go@v3

View File

@@ -27,7 +27,7 @@ jobs:
run: |
cd web
npm install
VITE_REACT_APP_VERSION=$(git describe --tags) npm run build
REACT_APP_VERSION=$(git describe --tags) npm run build
cd ..
- name: Set up Go
uses: actions/setup-go@v3

View File

@@ -5,7 +5,7 @@ COPY ./web/package*.json ./
RUN npm ci
COPY ./web .
COPY ./VERSION .
RUN VITE_REACT_APP_VERSION=$(cat VERSION) npm run build
RUN REACT_APP_VERSION=$(cat VERSION) npm run build
# Go build stage
FROM golang AS builder2

View File

@@ -56,6 +56,9 @@ var GitHubClientSecret = ""
var DiscordClientId = ""
var DiscordClientSecret = ""
var DiscordGuildId = ""
var DiscordAllowJoiningGuild = "false"
var DiscordBotToken = ""
var WeChatServerAddress = ""
var WeChatServerToken = ""
@@ -158,6 +161,7 @@ const (
// Reserve engineering for public projects
ChannelTypeChatGPTWeb = 14 // Chanzhaoyu/chatgpt-web
ChannelTypeChatbotUI = 15 // mckaywrigley/chatbot-ui
)
var ChannelBaseURLs = []string{
@@ -178,4 +182,5 @@ var ChannelBaseURLs = []string{
// Reserve engineering for public projects
"", // 14 // Chanzhaoyu/chatgpt-web
"", // 15 // mckaywrigley/chatbot-ui
}

View File

@@ -8,7 +8,8 @@ import (
)
func GetSubscription(c *gin.Context) {
var quota int
var remainQuota int
var usedQuota int
var err error
var expirationDate int64
@@ -18,10 +19,14 @@ func GetSubscription(c *gin.Context) {
expirationDate = token.ExpiredTime
if common.DisplayTokenStatEnabled {
quota = token.RemainQuota
tokenId := c.GetInt("token_id")
token, err = model.GetTokenById(tokenId)
remainQuota = token.RemainQuota
usedQuota = token.UsedQuota
} else {
userId := c.GetInt("id")
quota, err = model.GetUserQuota(userId)
remainQuota, err = model.GetUserQuota(userId)
usedQuota, err = model.GetUserUsedQuota(userId)
}
if err != nil {
openAIError := OpenAIError{
@@ -33,6 +38,7 @@ func GetSubscription(c *gin.Context) {
})
return
}
quota := remainQuota + usedQuota
amount := float64(quota)
if common.DisplayInCurrencyEnabled {
amount /= common.QuotaPerUnit

View File

@@ -18,6 +18,13 @@ import (
"github.com/gin-gonic/gin"
)
func formatFloat(input float64) float64 {
if input == float64(int64(input)) {
return input
}
return float64(int64(input*10)) / 10
}
func testChannel(channel *model.Channel, request ChatRequest) error {
switch channel.Type {
case common.ChannelTypeAzure:
@@ -32,6 +39,10 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
if channel.BaseURL != "" {
requestURL = channel.BaseURL
}
} else if channel.Type == common.ChannelTypeChatbotUI {
if channel.BaseURL != "" {
requestURL = channel.BaseURL
}
} else {
if channel.BaseURL != "" {
requestURL = channel.BaseURL
@@ -65,16 +76,49 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
}
// Construct json data without adding escape character
map1 := map[string]string{
"prompt": prompt,
"systemMessage": systemMessage.Content,
"temperature": strconv.FormatFloat(request.Temperature, 'f', 2, 64),
"top_p": strconv.FormatFloat(request.TopP, 'f', 2, 64),
map1 := make(map[string]interface{})
map1["prompt"] = prompt
map1["systemMessage"] = systemMessage.Content
if request.Temperature != 0 {
map1["temperature"] = formatFloat(request.Temperature)
}
if request.TopP != 0 {
map1["top_p"] = formatFloat(request.TopP)
}
// Convert map to json string
jsonData, err = json.Marshal(map1)
} else if channel.Type == common.ChannelTypeChatbotUI {
// Get system message from Message json, Role == "system"
var systemMessage string
for _, message := range request.Messages {
if message.Role == "system" {
systemMessage = message.Content
break
}
}
// Construct json data without adding escape character
map1 := make(map[string]interface{})
map1["prompt"] = systemMessage
map1["temperature"] = formatFloat(request.Temperature)
map1["key"] = ""
map1["messages"] = request.Messages
map1["model"] = map[string]interface{}{
"id": request.Model,
}
// Convert map to json string
jsonData, err = json.Marshal(map1)
//Print jsoinData to console
log.Println(string(jsonData))
}
if err != nil {
return err
}
@@ -102,6 +146,20 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
req.Header.Set("X-Remote-Addr", ip)
}
custom_http_headers := channel.CustomHttpHeaders
if custom_http_headers != "" {
var custom_http_headers_map map[string]string
err := json.Unmarshal([]byte(custom_http_headers), &custom_http_headers_map)
if err != nil {
return err
}
for key, value := range custom_http_headers_map {
req.Header.Set(key, value)
}
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
@@ -119,11 +177,11 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
return errors.New("error response: " + strconv.Itoa(resp.StatusCode))
}
var done = false
var streamResponseText = ""
if channel.Type != common.ChannelTypeChatGPTWeb {
scanner := bufio.NewScanner(resp.Body)
scanner := bufio.NewScanner(resp.Body)
if channel.Type != common.ChannelTypeChatGPTWeb && channel.Type != common.ChannelTypeChatbotUI {
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
@@ -139,12 +197,35 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
return 0, nil, nil
})
for scanner.Scan() {
data := scanner.Text()
if len(data) < 6 { // must be something wrong!
continue
}
for scanner.Scan() {
data := scanner.Text()
if len(data) < 6 { // must be something wrong!
continue
}
if channel.Type == common.ChannelTypeChatGPTWeb {
var chatResponse ChatGptWebChatResponse
err = json.Unmarshal([]byte(data), &chatResponse)
if err != nil {
// Print the body in string
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
common.SysError("error unmarshalling chat response: " + err.Error() + " " + buf.String())
return err
}
// if response role is assistant and contains delta, append the content to streamResponseText
if chatResponse.Role == "assistant" && chatResponse.Detail != nil {
for _, choice := range chatResponse.Detail.Choices {
streamResponseText += choice.Delta.Content
}
}
} else if channel.Type == common.ChannelTypeChatbotUI {
streamResponseText += data
} else if channel.Type != common.ChannelTypeChatGPTWeb {
// If data has event: event content inside, remove it, it can be prefix or inside the data
if strings.HasPrefix(data, "event:") || strings.Contains(data, "event:") {
// Remove event: event in the front or back
@@ -181,38 +262,15 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
for _, choice := range streamResponse.Choices {
streamResponseText += choice.Delta.Content
}
} else {
done = true
break
}
}
} else if channel.Type == common.ChannelTypeChatGPTWeb {
scanner := bufio.NewScanner(resp.Body)
go func() {
for scanner.Scan() {
var chatResponse ChatGptWebChatResponse
err = json.Unmarshal(scanner.Bytes(), &chatResponse)
if err != nil {
log.Println("error unmarshal chat response: " + err.Error())
continue
}
// if response role is assistant and contains delta, append the content to streamResponseText
if chatResponse.Role == "assistant" && chatResponse.Detail != nil {
for _, choice := range chatResponse.Detail.Choices {
streamResponseText += choice.Delta.Content
}
}
}
}()
}
defer resp.Body.Close()
// Check if streaming is complete and streamResponseText is populated
if streamResponseText == "" || !done && channel.Type != common.ChannelTypeChatGPTWeb {
if streamResponseText == "" {
return errors.New("Streaming not complete")
}
@@ -226,7 +284,7 @@ func buildTestRequest() *ChatRequest {
}
testMessage := Message{
Role: "user",
Content: "say hi word only",
Content: "Hello ChatGPT!",
}
testRequest.Messages = append(testRequest.Messages, testMessage)
return testRequest

View File

@@ -1,9 +1,11 @@
package controller
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"one-api/common"
"one-api/model"
@@ -36,7 +38,7 @@ func getDiscordUserInfoByCode(codeFromURLParamaters string, host string) (*Disco
ClientID: common.DiscordClientId,
ClientSecret: common.DiscordClientSecret,
RedirectURI: fmt.Sprintf("https://%s/oauth/discord", host),
Scopes: []string{disgoauth.ScopeIdentify, disgoauth.ScopeEmail},
Scopes: []string{disgoauth.ScopeIdentify, disgoauth.ScopeEmail, disgoauth.ScopeGuilds, disgoauth.ScopeGuildsJoin},
})
accessToken, _ := dc.GetOnlyAccessToken(codeFromURLParamaters)
@@ -58,6 +60,46 @@ func getDiscordUserInfoByCode(codeFromURLParamaters string, host string) (*Disco
return nil, err
}
// Add guild member.
if common.DiscordGuildId != "" && discordUser.Id != "" && common.DiscordBotToken != "" && common.DiscordAllowJoiningGuild == "true" {
url := fmt.Sprintf("https://discord.com/api/guilds/%s/members/%s", common.DiscordGuildId, discordUser.Id)
// Set JSON
map1 := map[string]interface{}{
// accessToken remove "Bearer "
"access_token": string(accessToken[7:]),
}
// Convert map to JSON
jsonData, _ := json.Marshal(map1)
req, _ := http.NewRequest("PUT", url, bytes.NewBuffer(jsonData))
// Set Header
req.Header.Set("Authorization", fmt.Sprintf("Bot %s", common.DiscordBotToken))
req.Header.Set("Content-Type", "application/json")
// Create a new HTTP Client
client := &http.Client{}
resp, err := client.Do(req)
log.Print(resp.StatusCode)
if err != nil || (resp.StatusCode != 200 && resp.StatusCode != 201 && resp.StatusCode != 204) {
// Print content
stringBuff := new(bytes.Buffer)
stringBuff.ReadFrom(resp.Body)
// Print error
fmt.Println("Error: ", stringBuff.String())
return nil, errors.New("You must join the discord server first or be verified member to be able to login!")
}
// Close the response body
defer resp.Body.Close()
}
if discordUser.Username == "" {
return nil, errors.New("Invalid return value, user field is empty, please try again later!")
}

View File

@@ -15,25 +15,27 @@ func GetStatus(c *gin.Context) {
"success": true,
"message": "",
"data": gin.H{
"version": common.Version,
"start_time": common.StartTime,
"email_verification": common.EmailVerificationEnabled,
"github_oauth": common.GitHubOAuthEnabled,
"github_client_id": common.GitHubClientId,
"discord_oauth": common.DiscordOAuthEnabled,
"discord_client_id": common.DiscordClientId,
"system_name": common.SystemName,
"logo": common.Logo,
"footer_html": common.Footer,
"wechat_qrcode": common.WeChatAccountQRCodeImageURL,
"wechat_login": common.WeChatAuthEnabled,
"server_address": common.ServerAddress,
"turnstile_check": common.TurnstileCheckEnabled,
"turnstile_site_key": common.TurnstileSiteKey,
"top_up_link": common.TopUpLink,
"chat_link": common.ChatLink,
"quota_per_unit": common.QuotaPerUnit,
"display_in_currency": common.DisplayInCurrencyEnabled,
"version": common.Version,
"start_time": common.StartTime,
"email_verification": common.EmailVerificationEnabled,
"github_oauth": common.GitHubOAuthEnabled,
"github_client_id": common.GitHubClientId,
"discord_oauth": common.DiscordOAuthEnabled,
"discord_client_id": common.DiscordClientId,
"discord_guild_id": common.DiscordGuildId,
"discord_allow_joining_guild": common.DiscordAllowJoiningGuild,
"system_name": common.SystemName,
"logo": common.Logo,
"footer_html": common.Footer,
"wechat_qrcode": common.WeChatAccountQRCodeImageURL,
"wechat_login": common.WeChatAuthEnabled,
"server_address": common.ServerAddress,
"turnstile_check": common.TurnstileCheckEnabled,
"turnstile_site_key": common.TurnstileSiteKey,
"top_up_link": common.TopUpLink,
"chat_link": common.ChatLink,
"quota_per_unit": common.QuotaPerUnit,
"display_in_currency": common.DisplayInCurrencyEnabled,
},
})
return

View File

@@ -109,6 +109,20 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode
req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type"))
req.Header.Set("Accept", c.Request.Header.Get("Accept"))
custom_http_headers := c.GetString("custom_http_headers")
if custom_http_headers != "" {
var custom_http_headers_map map[string]string
err := json.Unmarshal([]byte(custom_http_headers), &custom_http_headers_map)
if err != nil {
return errorWrapper(err, "unmarshal_custom_http_headers_failed", http.StatusInternalServerError)
}
for key, value := range custom_http_headers_map {
req.Header.Set(key, value)
}
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {

View File

@@ -13,6 +13,7 @@ import (
"one-api/model"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
)
@@ -122,6 +123,10 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
// remove /v1/chat/completions from request url
requestURL := strings.Split(requestURL, "/v1/chat/completions")[0]
fullRequestURL = fmt.Sprintf("%s%s", baseURL, requestURL)
} else if channelType == common.ChannelTypeChatbotUI {
// remove /v1/chat/completions from request url
requestURL := strings.Split(requestURL, "/v1/chat/completions")[0]
fullRequestURL = fmt.Sprintf("%s%s", baseURL, requestURL)
} else if channelType == common.ChannelTypePaLM {
err := relayPaLM(textRequest, c)
return err
@@ -223,11 +228,57 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
}
// Construct json data without adding escape character
map1 := map[string]string{
"prompt": prompt,
"systemMessage": systemMessage.Content,
"temperature": strconv.FormatFloat(reqBody.Temperature, 'f', 2, 64),
"top_p": strconv.FormatFloat(reqBody.TopP, 'f', 2, 64),
map1 := make(map[string]interface{})
map1["prompt"] = prompt + "\nResponse as assistant, but do not include the role in response."
map1["systemMessage"] = systemMessage.Content
if reqBody.Temperature != 0 {
map1["temperature"] = formatFloat(reqBody.Temperature)
}
if reqBody.TopP != 0 {
map1["top_p"] = formatFloat(reqBody.TopP)
}
// Convert map to json string
jsonData, err := json.Marshal(map1)
if err != nil {
return errorWrapper(err, "marshal_json_failed", http.StatusInternalServerError)
}
// Convert json string to io.Reader
requestBody = bytes.NewReader(jsonData)
} else if channelType == common.ChannelTypeChatbotUI {
// Get system message from Message json, Role == "system"
var reqBody ChatRequest
// Parse requestBody into systemMessage
err := json.NewDecoder(requestBody).Decode(&reqBody)
if err != nil {
return errorWrapper(err, "decode_request_body_failed", http.StatusInternalServerError)
}
// Get system message from Message json, Role == "system"
var systemMessage string
for _, message := range reqBody.Messages {
if message.Role == "system" {
systemMessage = message.Content
break
}
}
// Construct json data without adding escape character
map1 := make(map[string]interface{})
map1["prompt"] = systemMessage
map1["temperature"] = formatFloat(reqBody.Temperature)
map1["key"] = ""
map1["messages"] = reqBody.Messages
map1["model"] = map[string]interface{}{
"id": reqBody.Model,
}
// Convert map to json string
@@ -269,6 +320,20 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
req.Header.Set("X-Remote-Addr", ip)
}
custom_http_headers := c.GetString("custom_http_headers")
if custom_http_headers != "" {
var custom_http_headers_map map[string]string
err := json.Unmarshal([]byte(custom_http_headers), &custom_http_headers_map)
if err != nil {
return errorWrapper(err, "unmarshal_custom_http_headers_failed", http.StatusInternalServerError)
}
for key, value := range custom_http_headers_map {
req.Header.Set(key, value)
}
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
@@ -279,7 +344,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
if resp.Body != nil {
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
log.Printf("Error Channel (%s): %s", baseURL, buf.String())
log.Printf("Error Channel (%s) (%s): %s", baseURL, textRequest.Model, buf.String())
return errorWrapper(err, "request_failed", resp.StatusCode)
}
@@ -344,20 +409,46 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
}
}()
if isStream {
if isStream || channelType == common.ChannelTypeChatGPTWeb || channelType == common.ChannelTypeChatbotUI {
dataChan := make(chan string)
stopChan := make(chan bool)
if channelType == common.ChannelTypeChatGPTWeb {
scanner := bufio.NewScanner(resp.Body)
go func() {
for scanner.Scan() {
var chatResponse ChatGptWebChatResponse
err = json.Unmarshal(scanner.Bytes(), &chatResponse)
scanner := bufio.NewScanner(resp.Body)
if channelType != common.ChannelTypeChatGPTWeb && channelType != common.ChannelTypeChatbotUI {
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := strings.Index(string(data), "\n"); i >= 0 {
return i + 2, data[0:i], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
})
}
go func() {
for scanner.Scan() {
data := scanner.Text()
if len(data) < 6 { // must be something wrong!
continue
}
if channelType == common.ChannelTypeChatGPTWeb {
var chatResponse ChatGptWebChatResponse
err = json.Unmarshal([]byte(data), &chatResponse)
if err != nil {
log.Println("error unmarshal chat response: " + err.Error())
continue
// Print the body in string
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
common.SysError("error unmarshalling chat response: " + err.Error() + " " + buf.String())
return
}
// if response role is assistant and contains delta, append the content to streamResponseText
@@ -387,33 +478,28 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
dataChan <- "data: " + string(jsonData)
}
}
}
stopChan <- true
}()
} else {
scanner := bufio.NewScanner(resp.Body)
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := strings.Index(string(data), "\n"); i >= 0 {
return i + 1, data[0:i], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
})
go func() {
for scanner.Scan() {
data := scanner.Text()
if len(data) < 6 { // must be something wrong!
// common.SysError("invalid stream response: " + data)
continue
} else if channelType == common.ChannelTypeChatbotUI {
returnObj := map[string]interface{}{
"id": "chatcmpl-" + strconv.Itoa(int(time.Now().UnixNano())),
"object": "text_completion",
"created": time.Now().Unix(),
"model": textRequest.Model,
"choices": []map[string]interface{}{
// set finish_reason to null in json
{
"finish_reason": nil,
"index": 0,
"delta": map[string]interface{}{
"content": data,
},
},
},
}
jsonData, _ := json.Marshal(returnObj)
dataChan <- "data: " + string(jsonData)
} else {
// If data has event: event content inside, remove it, it can be prefix or inside the data
if strings.HasPrefix(data, "event:") || strings.Contains(data, "event:") {
// Remove event: event in the front or back
@@ -463,10 +549,11 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
}
}
}
stopChan <- true
}()
}
}
stopChan <- true
}()
c.Writer.Header().Set("Content-Type", "text/event-stream")
c.Writer.Header().Set("Cache-Control", "no-cache")

View File

@@ -10,7 +10,7 @@ WORKDIR /build
COPY ./web/package*.json ./
RUN npm ci
COPY --from=translator /app .
RUN cd web && VITE_REACT_APP_VERSION=$(cat VERSION) npm run build
RUN cd web && REACT_APP_VERSION=$(cat VERSION) npm run build
# Go build stage
FROM golang:1.20.5 AS goBuilder

9
go.mod
View File

@@ -19,11 +19,16 @@ require (
gorm.io/gorm v1.25.2
)
require (
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/knz/go-libedit v1.10.1 // indirect
)
require (
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect
github.com/bytedance/sonic v1.9.2 // indirect
github.com/bytedance/sonic v1.10.0-rc2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect

11
go.sum
View File

@@ -5,6 +5,10 @@ github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/bytedance/sonic v1.9.2 h1:GDaNjuWSGu09guE9Oql0MSTNhNCLlWwO8y/xM5BzcbM=
github.com/bytedance/sonic v1.9.2/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/bytedance/sonic v1.10.0-rc h1:3S5HeWxjX08CUqNrXtEittExpJsEKBNzrV5UnrzHxVQ=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.0-rc2 h1:oDfRZ+4m6AYCOC0GFeOCeYqvBmucy1isvouS2K0cPzo=
github.com/bytedance/sonic v1.10.0-rc2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
@@ -12,6 +16,10 @@ github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA=
github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo=
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -99,6 +107,8 @@ github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZX
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1 h1:0pHpWtx9vcvC0xGZqEQlQdfSQs7WRlAjuPvk3fOZDCo=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
@@ -251,4 +261,5 @@ gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.2 h1:gs1o6Vsa+oVKG/a9ElL3XgyGfghFfkKA2SInQaCyMho=
gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

View File

@@ -105,6 +105,7 @@ func Distribute() func(c *gin.Context) {
c.Set("channel_id", channel.Id)
c.Set("channel_name", channel.Name)
c.Set("model_mapping", channel.ModelMapping)
c.Set("custom_http_headers", channel.CustomHttpHeaders)
c.Set("enable_ip_randomization", channel.EnableIpRandomization)
c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
c.Set("base_url", channel.BaseURL)

View File

@@ -26,7 +26,8 @@ type Channel struct {
ModelMapping string `json:"model_mapping" gorm:"type:varchar(1024);default:''"`
// Additional fields, default value is false
EnableIpRandomization bool `json:"enable_ip_randomization"`
EnableIpRandomization bool `json:"enable_ip_randomization"`
CustomHttpHeaders string `json:"custom_http_headers"`
}
func GetAllChannels(startIdx int, num int, selectAll bool) ([]*Channel, error) {

View File

@@ -56,6 +56,9 @@ func InitOptionMap() {
common.OptionMap["GitHubClientSecret"] = ""
common.OptionMap["DiscordClientId"] = ""
common.OptionMap["DiscordClientSecret"] = ""
common.OptionMap["DiscordGuildId"] = ""
common.OptionMap["DiscordBotToken"] = ""
common.OptionMap["DiscordAllowJoiningGuild"] = ""
common.OptionMap["WeChatServerAddress"] = ""
common.OptionMap["WeChatServerToken"] = ""
common.OptionMap["WeChatAccountQRCodeImageURL"] = ""
@@ -178,6 +181,12 @@ func updateOptionMap(key string, value string) (err error) {
common.GitHubClientSecret = value
case "DiscordClientId":
common.DiscordClientId = value
case "DiscordGuildId":
common.DiscordGuildId = value
case "DiscordBotToken":
common.DiscordBotToken = value
case "DiscordAllowJoiningGuild":
common.DiscordAllowJoiningGuild = value
case "DiscordClientSecret":
common.DiscordClientSecret = value
case "Footer":

View File

@@ -10,8 +10,8 @@ npm start
npm run build
```
If you want to change the default server, please set `VITE_REACT_APP_SERVER` environment variables before build,
for example: `VITE_REACT_APP_SERVER=http://your.domain.com`.
If you want to change the default server, please set `REACT_APP_SERVER` environment variables before build,
for example: `REACT_APP_SERVER=http://your.domain.com`.
Before you start editing, make sure your `Actions on Save` options have `Optimize imports` & `Run Prettier` enabled.

16379
web/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -3,21 +3,25 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"axios": "^1.4.0",
"history": "^5.3.0",
"marked": "^5.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-dropzone": "^14.2.3",
"react-router-dom": "^6.14.1",
"react-router-dom": "^6.14.2",
"react-scripts": "5.0.1",
"react-toastify": "^9.1.3",
"react-turnstile": "^1.1.1",
"semantic-ui-css": "^2.5.0",
"semantic-ui-react": "^2.1.4"
},
"scripts": {
"start": "vite preview",
"build": "vite build"
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
@@ -38,10 +42,7 @@
]
},
"devDependencies": {
"@vitejs/plugin-react": "^4.0.3",
"prettier": "3.0.0",
"terser": "^5.19.0",
"vite": "^4.4.4"
"prettier": "^3.0.0"
},
"prettier": {
"singleQuote": true,

View File

@@ -14,6 +14,5 @@
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="module" src="./src/index.jsx"></script>
</body>
</html>

View File

@@ -38,7 +38,7 @@ const Footer = () => {
) : (
<div className='custom-footer'>
<a href='https://github.com/songquanpeng/one-api' target='_blank'>
{systemName} {import.meta.env.VITE_REACT_APP_VERSION}{' '}
{systemName} {process.env.REACT_APP_VERSION}{' '}
</a>
{' '}
<a href='https://github.com/songquanpeng' target='_blank'>

View File

@@ -59,7 +59,7 @@ const LoginForm = () => {
const onDiscordOAuthClicked = () => {
window.open(
`https://discord.com/oauth2/authorize?response_type=code&client_id=${status.discord_client_id}&redirect_uri=${window.location.origin}/oauth/discord&scope=identify`,
`https://discord.com/oauth2/authorize?response_type=code&client_id=${status.discord_client_id}&redirect_uri=${window.location.origin}/oauth/discord&scope=identify%20guilds%20email%20guilds.join`,
);
};

View File

@@ -99,7 +99,7 @@ const OtherSetting = () => {
'https://api.github.com/repos/songquanpeng/one-api/releases/latest',
);
const { tag_name, body } = res.data;
if (tag_name === import.meta.env.VITE_REACT_APP_VERSION) {
if (tag_name === process.env.REACT_APP_VERSION) {
showSuccess(`已是最新版本:${tag_name}`);
} else {
setUpdateData({

View File

@@ -121,7 +121,7 @@ const PersonalSetting = () => {
const openDiscordOAuth = () => {
window.open(
`https://discord.com/api/oauth2/authorize?client_id=${status.discord_client_id}&scope=identify%20email&response_type=code&redirect_uri=${window.location.origin}/oauth/discord`,
`https://discord.com/api/oauth2/authorize?client_id=${status.discord_client_id}&response_type=code&redirect_uri=${window.location.origin}/oauth/discord&scope=identify%20guilds%20email%20guilds.join`,
);
};

View File

@@ -12,6 +12,9 @@ const SystemSetting = () => {
GitHubClientId: '',
GitHubClientSecret: '',
DiscordClientId: '',
DiscordAllowJoiningGuild: 'false',
DiscordGuildId: '',
DiscordBotToken: '',
DiscordClientSecret: '',
Notice: '',
SMTPServer: '',
@@ -87,6 +90,9 @@ const SystemSetting = () => {
name.startsWith('SMTP') ||
name === 'ServerAddress' ||
name === 'DiscordClientId' ||
name === 'DiscordGuildId' ||
name === 'DiscordAllowJoiningGuild' ||
name === 'DiscordBotToken' ||
name === 'DiscordClientSecret' ||
name === 'GitHubClientId' ||
name === 'GitHubClientSecret' ||
@@ -177,6 +183,24 @@ const SystemSetting = () => {
) {
await updateOption('DiscordClientSecret', inputs.DiscordClientSecret);
}
if (originInputs['DiscordGuildId'] !== inputs.DiscordGuildId) {
await updateOption('DiscordGuildId', inputs.DiscordGuildId);
}
if (
originInputs['DiscordBotToken'] !== inputs.DiscordBotToken &&
inputs.DiscordBotToken !== ''
) {
await updateOption('DiscordBotToken', inputs.DiscordBotToken);
}
if (
originInputs['DiscordAllowJoiningGuild'] !==
inputs.DiscordAllowJoiningGuild
) {
await updateOption(
'DiscordAllowJoiningGuild',
inputs.DiscordAllowJoiningGuild,
);
}
};
const submitTurnstile = async () => {
@@ -352,6 +376,32 @@ const SystemSetting = () => {
value={inputs.DiscordClientSecret}
placeholder='Sensitive information will not be displayed in the frontend'
/>
<Form.Checkbox
label='Allow Joining Guild'
name='DiscordAllowJoiningGuild'
autoComplete='new-password'
checked={inputs.DiscordAllowJoiningGuild === 'true'}
onChange={(e, { name, checked }) =>
handleInputChange(e, { name, value: checked ? 'true' : 'false' })
}
/>
<Form.Input
label='Discord Guild ID'
name='DiscordGuildId'
onChange={handleInputChange}
autoComplete='new-password'
value={inputs.DiscordGuildId}
placeholder='Enter the ID of your Discord server'
/>
<Form.Input
label='Discord Bot Token'
name='DiscordBotToken'
onChange={handleInputChange}
type='password'
autoComplete='new-password'
value={inputs.DiscordBotToken}
placeholder='Sensitive information will not be displayed in the frontend'
/>
</Form.Group>
<Form.Button onClick={submitDiscordOAuth}>
Save Discord OAuth Settings

View File

@@ -14,4 +14,5 @@ export const CHANNEL_OPTIONS = [
//
{ key: 14, text: 'Chanzhaoyu/chatgpt-web', value: 14, color: 'purple' },
{ key: 14, text: 'mckaywrigley/chatbot-ui', value: 15, color: 'orange' },
];

View File

@@ -2,7 +2,7 @@ import { showError } from './utils';
import axios from 'axios';
export const API = axios.create({
baseURL: import.meta.env.VITE_REACT_APP_SERVER ? import.meta.env.VITE_REACT_APP_SERVER : '',
baseURL: process.env.REACT_APP_SERVER ? process.env.REACT_APP_SERVER : '',
});
API.interceptors.response.use(

View File

@@ -23,6 +23,10 @@ const MODEL_MAPPING_EXAMPLE = {
'gpt-4-32k-0314': 'gpt-4-32k',
};
const CUSTOM_HTTP_HEADERS_EXAMPLE = {
'X-OpenAI-Organization': 'OpenAI',
};
const EditChannel = () => {
const params = useParams();
const channelId = params.id;
@@ -35,6 +39,7 @@ const EditChannel = () => {
base_url: '',
other: '',
model_mapping: '',
custom_http_headers: '',
models: [],
groups: ['default'],
enable_ip_randomization: false,
@@ -51,7 +56,7 @@ const EditChannel = () => {
setInputs((inputs) => ({ ...inputs, [name]: value }));
};
const loadChannel = async () => {
const loadChannel = async (modelOptions) => {
let res = await API.get(`/api/channel/${channelId}`);
const { success, message, data } = res.data;
if (success) {
@@ -59,19 +64,19 @@ const EditChannel = () => {
data.models = [];
} else {
data.models = data.models.split(',');
// setTimeout(() => {
// let localModelOptions = [...modelOptions];
// data.models.forEach((model) => {
// if (!localModelOptions.find((option) => option.key === model)) {
// localModelOptions.push({
// key: model,
// text: model,
// value: model,
// });
// }
// });
// setModelOptions(localModelOptions);
// }, 1000);
setTimeout(() => {
let localModelOptions = [...modelOptions];
data.models.forEach((model) => {
if (!localModelOptions.find((option) => option.key === model)) {
localModelOptions.push({
key: model,
text: model,
value: model,
});
}
});
setModelOptions(localModelOptions);
}, 1000);
}
if (data.group === '') {
data.groups = [];
@@ -85,6 +90,13 @@ const EditChannel = () => {
2,
);
}
if (data.custom_http_headers !== '') {
data.custom_http_headers = JSON.stringify(
JSON.parse(data.custom_http_headers),
null,
2,
);
}
setInputs(data);
} else {
showError(message);
@@ -95,19 +107,23 @@ const EditChannel = () => {
const fetchModels = async () => {
try {
let res = await API.get(`/api/channel/models`);
setModelOptions(
res.data.data.map((model) => ({
key: model.id,
text: model.id,
value: model.id,
})),
);
setFullModels(res.data.data.map((model) => model.id));
setBasicModels(
res.data.data
.filter((model) => !model.id.startsWith('gpt-4'))
.map((model) => model.id),
);
const allModels = res.data.data.map((model) => ({
key: model.id,
text: model.id,
value: model.id,
}));
setModelOptions(allModels);
return allModels;
} catch (error) {
showError(error.message);
}
@@ -128,12 +144,12 @@ const EditChannel = () => {
}
};
useEffect(() => {
useEffect(async () => {
const models = await fetchModels();
await fetchGroups();
if (isEdit) {
loadChannel().then();
await loadChannel(models);
}
fetchModels().then();
fetchGroups().then();
}, []);
const submit = async () => {
@@ -149,6 +165,13 @@ const EditChannel = () => {
showInfo('模型映射必须是合法的 JSON 格式!');
return;
}
if (
inputs.custom_http_headers !== '' &&
!verifyJSON(inputs.custom_http_headers)
) {
showInfo('自定义 HTTP 头必须是合法的 JSON 格式!');
return;
}
let localInputs = inputs;
if (localInputs.base_url.endsWith('/')) {
localInputs.base_url = localInputs.base_url.slice(
@@ -390,6 +413,21 @@ const EditChannel = () => {
autoComplete='new-password'
/>
</Form.Field>
<Form.Field>
<Form.TextArea
label='自定义 HTTP 头'
placeholder={`此项可选,为一个 JSON 文本,键为 HTTP 头名称,值为 HTTP 头内容,例如:\n${JSON.stringify(
CUSTOM_HTTP_HEADERS_EXAMPLE,
null,
2,
)}`}
name='custom_http_headers'
onChange={handleInputChange}
value={inputs.custom_http_headers}
style={{ minHeight: 150, fontFamily: 'JetBrains Mono, Consolas' }}
autoComplete='new-password'
/>
</Form.Field>
{batch ? (
<Form.Field>
<Form.TextArea

View File

@@ -1,11 +0,0 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
build: {
outDir: 'build',
minify: 'terser',
},
})