Compare commits

..

17 Commits

Author SHA1 Message Date
JustSong
00151a0124 chore: format logs 2023-06-22 10:59:01 +08:00
JustSong
b86de464b5 chore: print more logs 2023-06-22 01:12:28 +08:00
JustSong
567916bd80 fix: only master node can migrate database 2023-06-22 00:52:27 +08:00
quzard
1f3b3ca7ae fix: fix channel table's sorting problem (#188) 2023-06-21 23:42:55 +08:00
JustSong
70cffbc258 docs: update README 2023-06-21 17:51:31 +08:00
JustSong
6d961064d2 feat: do not access database before response return (close #158) 2023-06-21 17:26:26 +08:00
JustSong
ba54c71948 feat: select channel without database (#158) 2023-06-21 17:04:18 +08:00
JustSong
1932c56ea8 ci: ignore alpha version 2023-06-21 16:22:56 +08:00
JustSong
dc7bb78c74 chore: update api message 2023-06-21 15:55:00 +08:00
JustSong
853a288052 chore: update api message 2023-06-21 15:54:06 +08:00
JustSong
6536a7be62 fix: do not show dollar balance if not enabled 2023-06-21 15:45:30 +08:00
JustSong
1b5c628e66 chore: update prompt 2023-06-21 00:20:48 +08:00
JustSong
e398f470a1 feat: support custom base url for channels 2023-06-20 22:32:56 +08:00
JustSong
634099e592 fix: cors allow all headers 2023-06-20 22:04:01 +08:00
JustSong
868f0474a9 docs: update README 2023-06-20 21:14:24 +08:00
JustSong
ced9f060c7 fix: fix used amount not correct 2023-06-20 21:05:07 +08:00
JustSong
14b85318a6 fix: fix crash when Redis is enabled 2023-06-20 20:56:35 +08:00
28 changed files with 202 additions and 80 deletions

View File

@@ -4,6 +4,7 @@ on:
push: push:
tags: tags:
- '*' - '*'
- '!*-alpha*'
workflow_dispatch: workflow_dispatch:
inputs: inputs:
name: name:

View File

@@ -6,6 +6,7 @@ on:
push: push:
tags: tags:
- '*' - '*'
- '!*-alpha*'
jobs: jobs:
release: release:
runs-on: ubuntu-latest runs-on: ubuntu-latest

View File

@@ -6,6 +6,7 @@ on:
push: push:
tags: tags:
- '*' - '*'
- '!*-alpha*'
jobs: jobs:
release: release:
runs-on: macos-latest runs-on: macos-latest

View File

@@ -6,6 +6,7 @@ on:
push: push:
tags: tags:
- '*' - '*'
- '!*-alpha*'
jobs: jobs:
release: release:
runs-on: windows-latest runs-on: windows-latest

View File

@@ -71,17 +71,18 @@ _✨ All in one 的 OpenAI 接口,整合各种 API 访问方式,开箱即用
9. 支持渠道**设置模型列表**。 9. 支持渠道**设置模型列表**。
10. 支持**查看额度明细**。 10. 支持**查看额度明细**。
11. 支持**用户邀请奖励**。 11. 支持**用户邀请奖励**。
12. 支持发布公告,设置充值链接,设置新用户初始额度。 12. 支持以美元为单位显示额度。
13. 支持丰富的**自定义**设置, 13. 支持发布公告,设置充值链接,设置新用户初始额度。
14. 支持丰富的**自定义**设置,
1. 支持自定义系统名称logo 以及页脚。 1. 支持自定义系统名称logo 以及页脚。
2. 支持自定义首页和关于页面,可以选择使用 HTML & Markdown 代码进行自定义,或者使用一个单独的网页通过 iframe 嵌入。 2. 支持自定义首页和关于页面,可以选择使用 HTML & Markdown 代码进行自定义,或者使用一个单独的网页通过 iframe 嵌入。
14. 支持通过系统访问令牌访问管理 API。 15. 支持通过系统访问令牌访问管理 API。
15. 支持 Cloudflare Turnstile 用户校验。 16. 支持 Cloudflare Turnstile 用户校验。
16. 支持用户管理,支持**多种用户登录注册方式** 17. 支持用户管理,支持**多种用户登录注册方式**
+ 邮箱登录注册以及通过邮箱进行密码重置。 + 邮箱登录注册以及通过邮箱进行密码重置。
+ [GitHub 开放授权](https://github.com/settings/applications/new)。 + [GitHub 开放授权](https://github.com/settings/applications/new)。
+ 微信公众号授权(需要额外部署 [WeChat Server](https://github.com/songquanpeng/wechat-server))。 + 微信公众号授权(需要额外部署 [WeChat Server](https://github.com/songquanpeng/wechat-server))。
17. 未来其他大模型开放 API 后,将第一时间支持,并将其封装成同样的 API 访问方式。 18. 未来其他大模型开放 API 后,将第一时间支持,并将其封装成同样的 API 访问方式。
## 部署 ## 部署
### 基于 Docker 进行部署 ### 基于 Docker 进行部署
@@ -150,9 +151,10 @@ sudo service nginx restart
### 多机部署 ### 多机部署
1. 所有服务器 `SESSION_SECRET` 设置一样的值。 1. 所有服务器 `SESSION_SECRET` 设置一样的值。
2. 必须设置 `SQL_DSN`,使用 MySQL 数据库而非 SQLite请自行配置主备数据库同步 2. 必须设置 `SQL_DSN`,使用 MySQL 数据库而非 SQLite所有服务器连接同一个数据库。
3. 所有从服务器必须设置 `SYNC_FREQUENCY`,以定期从数据库同步配置。 3. 所有从服务器必须设置 `SYNC_FREQUENCY`,以定期从数据库同步配置。
4. 从服务器可以选择设置 `FRONTEND_BASE_URL`,以重定向页面请求到主服务器。 4. 从服务器可以选择设置 `FRONTEND_BASE_URL`,以重定向页面请求到主服务器。
5. 推荐每台服务器上都分别装好 Redis设置好 `REDIS_CONN_STRING`,这样可以做到在缓存未过期的情况下数据库零访问,可以减少延迟。
环境变量的具体使用方法详见[此处](#环境变量)。 环境变量的具体使用方法详见[此处](#环境变量)。
@@ -169,7 +171,7 @@ sudo service nginx restart
项目主页https://github.com/Yidadaa/ChatGPT-Next-Web 项目主页https://github.com/Yidadaa/ChatGPT-Next-Web
```bash ```bash
docker run --name chat-next-web -d -p 3001:3000 -e BASE_URL=https://openai.justsong.cn yidadaa/chatgpt-next-web docker run --name chat-next-web -d -p 3001:3000 yidadaa/chatgpt-next-web
``` ```
注意修改端口号和 `BASE_URL`。 注意修改端口号和 `BASE_URL`。
@@ -266,7 +268,7 @@ https://openai.justsong.cn
1. 额度是什么怎么计算的One API 的额度计算有问题? 1. 额度是什么怎么计算的One API 的额度计算有问题?
+ 额度 = token * 倍率 + 额度 = token * 倍率
+ 倍率包括分组的倍率,以及补全的倍率。 + 倍率包括分组的倍率,以及补全的倍率。
+ 如果是非流模式,官方接口会返回消耗的总 token但是你要注意提示和补全的消耗额度不一样。 + 如果是非流模式,官方接口会返回消耗的总 token但是你要注意提示和补全的消耗倍率不一样。
2. 账户额度足够为什么提示额度不足? 2. 账户额度足够为什么提示额度不足?
+ 请检查你的令牌额度是否足够,这个和账户额度是分开的。 + 请检查你的令牌额度是否足够,这个和账户额度是分开的。
+ 令牌额度仅供用户设置最大使用量,用户可自由设置。 + 令牌额度仅供用户设置最大使用量,用户可自由设置。
@@ -276,6 +278,9 @@ https://openai.justsong.cn
4. 渠道测试报错:`invalid character '<' looking for beginning of value` 4. 渠道测试报错:`invalid character '<' looking for beginning of value`
+ 这是因为返回值不是合法的 JSON而是一个 HTML 页面。 + 这是因为返回值不是合法的 JSON而是一个 HTML 页面。
+ 大概率是你的部署站的 IP 或代理的节点被 CloudFlare 封禁了。 + 大概率是你的部署站的 IP 或代理的节点被 CloudFlare 封禁了。
5. ChatGPT Next Web 报错:`Failed to fetch`
+ 部署的时候不要设置 `BASE_URL`。
+ 检查你的接口地址和 API Key 有没有填对。
## 注意 ## 注意
本项目为开源项目,请在遵循 OpenAI 的[使用条款](https://openai.com/policies/terms-of-use)以及**法律法规**的情况下使用,不得用于非法用途。 本项目为开源项目,请在遵循 OpenAI 的[使用条款](https://openai.com/policies/terms-of-use)以及**法律法规**的情况下使用,不得用于非法用途。

View File

@@ -1,6 +1,7 @@
package common package common
import ( import (
"os"
"sync" "sync"
"time" "time"
@@ -67,6 +68,8 @@ var PreConsumedQuota = 500
var RootUserEmail = "" var RootUserEmail = ""
var IsMasterNode = os.Getenv("SYNC_FREQUENCY") == ""
const ( const (
RoleGuestUser = 0 RoleGuestUser = 0
RoleCommonUser = 1 RoleCommonUser = 1

View File

@@ -11,7 +11,7 @@ var GroupRatio = map[string]float64{
func GroupRatio2JSONString() string { func GroupRatio2JSONString() string {
jsonBytes, err := json.Marshal(GroupRatio) jsonBytes, err := json.Marshal(GroupRatio)
if err != nil { if err != nil {
SysError("Error marshalling model ratio: " + err.Error()) SysError("error marshalling model ratio: " + err.Error())
} }
return string(jsonBytes) return string(jsonBytes)
} }
@@ -24,7 +24,7 @@ func UpdateGroupRatioByJSONString(jsonStr string) error {
func GetGroupRatio(name string) float64 { func GetGroupRatio(name string) float64 {
ratio, ok := GroupRatio[name] ratio, ok := GroupRatio[name]
if !ok { if !ok {
SysError("Group ratio not found: " + name) SysError("group ratio not found: " + name)
return 1 return 1
} }
return ratio return ratio

View File

@@ -40,7 +40,7 @@ var ModelRatio = map[string]float64{
func ModelRatio2JSONString() string { func ModelRatio2JSONString() string {
jsonBytes, err := json.Marshal(ModelRatio) jsonBytes, err := json.Marshal(ModelRatio)
if err != nil { if err != nil {
SysError("Error marshalling model ratio: " + err.Error()) SysError("error marshalling model ratio: " + err.Error())
} }
return string(jsonBytes) return string(jsonBytes)
} }
@@ -53,7 +53,7 @@ func UpdateModelRatioByJSONString(jsonStr string) error {
func GetModelRatio(name string) float64 { func GetModelRatio(name string) float64 {
ratio, ok := ModelRatio[name] ratio, ok := ModelRatio[name]
if !ok { if !ok {
SysError("Model ratio not found: " + name) SysError("model ratio not found: " + name)
return 30 return 30
} }
return ratio return ratio

View File

@@ -17,9 +17,10 @@ func InitRedisClient() (err error) {
SysLog("REDIS_CONN_STRING not set, Redis is not enabled") SysLog("REDIS_CONN_STRING not set, Redis is not enabled")
return nil return nil
} }
SysLog("Redis is enabled")
opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING")) opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING"))
if err != nil { if err != nil {
panic(err) FatalLog("failed to parse Redis connection string: " + err.Error())
} }
RDB = redis.NewClient(opt) RDB = redis.NewClient(opt)
@@ -27,13 +28,16 @@ func InitRedisClient() (err error) {
defer cancel() defer cancel()
_, err = RDB.Ping(ctx).Result() _, err = RDB.Ping(ctx).Result()
if err != nil {
FatalLog("Redis ping test failed: " + err.Error())
}
return err return err
} }
func ParseRedisOption() *redis.Options { func ParseRedisOption() *redis.Options {
opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING")) opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING"))
if err != nil { if err != nil {
panic(err) FatalLog("failed to parse Redis connection string: " + err.Error())
} }
return opt return opt
} }

View File

@@ -48,6 +48,9 @@ func GetUsage(c *gin.Context) {
return return
} }
amount := float64(quota) amount := float64(quota)
if common.DisplayInCurrencyEnabled {
amount /= common.QuotaPerUnit
}
usage := OpenAIUsageResponse{ usage := OpenAIUsageResponse{
Object: "list", Object: "list",
TotalUsage: amount, TotalUsage: amount,

View File

@@ -25,9 +25,7 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
if channel.Type == common.ChannelTypeAzure { if channel.Type == common.ChannelTypeAzure {
requestURL = fmt.Sprintf("%s/openai/deployments/%s/chat/completions?api-version=2023-03-15-preview", channel.BaseURL, request.Model) requestURL = fmt.Sprintf("%s/openai/deployments/%s/chat/completions?api-version=2023-03-15-preview", channel.BaseURL, request.Model)
} else { } else {
if channel.Type == common.ChannelTypeCustom { if channel.BaseURL != "" {
requestURL = channel.BaseURL
} else if channel.Type == common.ChannelTypeOpenAI && channel.BaseURL != "" {
requestURL = channel.BaseURL requestURL = channel.BaseURL
} }
requestURL += "/v1/chat/completions" requestURL += "/v1/chat/completions"
@@ -131,7 +129,7 @@ func disableChannel(channelId int, channelName string, reason string) {
content := fmt.Sprintf("通道「%s」#%d已被禁用原因%s", channelName, channelId, reason) content := fmt.Sprintf("通道「%s」#%d已被禁用原因%s", channelName, channelId, reason)
err := common.SendEmail(subject, common.RootUserEmail, content) err := common.SendEmail(subject, common.RootUserEmail, content)
if err != nil { if err != nil {
common.SysError(fmt.Sprintf("发送邮件失败:%s", err.Error())) common.SysError(fmt.Sprintf("failed to send email: %s", err.Error()))
} }
} }
@@ -178,7 +176,7 @@ func testAllChannels(c *gin.Context) error {
} }
err := common.SendEmail("通道测试完成", common.RootUserEmail, "通道测试完成,如果没有收到禁用通知,说明所有通道都正常") err := common.SendEmail("通道测试完成", common.RootUserEmail, "通道测试完成,如果没有收到禁用通知,说明所有通道都正常")
if err != nil { if err != nil {
common.SysError(fmt.Sprintf("发送邮件失败:%s", err.Error())) common.SysError(fmt.Sprintf("failed to send email: %s", err.Error()))
} }
testAllChannelsLock.Lock() testAllChannelsLock.Lock()
testAllChannelsRunning = false testAllChannelsRunning = false

View File

@@ -16,6 +16,7 @@ import (
func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
channelType := c.GetInt("channel") channelType := c.GetInt("channel")
tokenId := c.GetInt("token_id") tokenId := c.GetInt("token_id")
userId := c.GetInt("id")
consumeQuota := c.GetBool("consume_quota") consumeQuota := c.GetBool("consume_quota")
group := c.GetString("group") group := c.GetString("group")
var textRequest GeneralOpenAIRequest var textRequest GeneralOpenAIRequest
@@ -30,12 +31,8 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
} }
baseURL := common.ChannelBaseURLs[channelType] baseURL := common.ChannelBaseURLs[channelType]
requestURL := c.Request.URL.String() requestURL := c.Request.URL.String()
if channelType == common.ChannelTypeCustom { if c.GetString("base_url") != "" {
baseURL = c.GetString("base_url") baseURL = c.GetString("base_url")
} else if channelType == common.ChannelTypeOpenAI {
if c.GetString("base_url") != "" {
baseURL = c.GetString("base_url")
}
} }
fullRequestURL := fmt.Sprintf("%s%s", baseURL, requestURL) fullRequestURL := fmt.Sprintf("%s%s", baseURL, requestURL)
if channelType == common.ChannelTypeAzure { if channelType == common.ChannelTypeAzure {
@@ -77,7 +74,16 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
groupRatio := common.GetGroupRatio(group) groupRatio := common.GetGroupRatio(group)
ratio := modelRatio * groupRatio ratio := modelRatio * groupRatio
preConsumedQuota := int(float64(preConsumedTokens) * ratio) preConsumedQuota := int(float64(preConsumedTokens) * ratio)
if consumeQuota { userQuota, err := model.CacheGetUserQuota(userId)
if err != nil {
return errorWrapper(err, "get_user_quota_failed", http.StatusOK)
}
if userQuota > 10*preConsumedQuota {
// in this case, we do not pre-consume quota
// because the user has enough quota
preConsumedQuota = 0
}
if consumeQuota && preConsumedQuota > 0 {
err := model.PreConsumeTokenQuota(tokenId, preConsumedQuota) err := model.PreConsumeTokenQuota(tokenId, preConsumedQuota)
if err != nil { if err != nil {
return errorWrapper(err, "pre_consume_token_quota_failed", http.StatusOK) return errorWrapper(err, "pre_consume_token_quota_failed", http.StatusOK)
@@ -134,10 +140,9 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
quotaDelta := quota - preConsumedQuota quotaDelta := quota - preConsumedQuota
err := model.PostConsumeTokenQuota(tokenId, quotaDelta) err := model.PostConsumeTokenQuota(tokenId, quotaDelta)
if err != nil { if err != nil {
common.SysError("Error consuming token remain quota: " + err.Error()) common.SysError("error consuming token remain quota: " + err.Error())
} }
tokenName := c.GetString("token_name") tokenName := c.GetString("token_name")
userId := c.GetInt("id")
model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("通过令牌「%s」使用模型 %s 消耗 %s模型倍率 %.2f,分组倍率 %.2f", tokenName, textRequest.Model, common.LogQuota(quota), modelRatio, groupRatio)) model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("通过令牌「%s」使用模型 %s 消耗 %s模型倍率 %.2f,分组倍率 %.2f", tokenName, textRequest.Model, common.LogQuota(quota), modelRatio, groupRatio))
model.UpdateUserUsedQuotaAndRequestCount(userId, quota) model.UpdateUserUsedQuotaAndRequestCount(userId, quota)
channelId := c.GetInt("channel_id") channelId := c.GetInt("channel_id")
@@ -168,7 +173,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
for scanner.Scan() { for scanner.Scan() {
data := scanner.Text() data := scanner.Text()
if len(data) < 6 { // must be something wrong! if len(data) < 6 { // must be something wrong!
common.SysError("Invalid stream response: " + data) common.SysError("invalid stream response: " + data)
continue continue
} }
dataChan <- data dataChan <- data
@@ -179,7 +184,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
var streamResponse ChatCompletionsStreamResponse var streamResponse ChatCompletionsStreamResponse
err = json.Unmarshal([]byte(data), &streamResponse) err = json.Unmarshal([]byte(data), &streamResponse)
if err != nil { if err != nil {
common.SysError("Error unmarshalling stream response: " + err.Error()) common.SysError("error unmarshalling stream response: " + err.Error())
return return
} }
for _, choice := range streamResponse.Choices { for _, choice := range streamResponse.Choices {
@@ -189,7 +194,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
var streamResponse CompletionsStreamResponse var streamResponse CompletionsStreamResponse
err = json.Unmarshal([]byte(data), &streamResponse) err = json.Unmarshal([]byte(data), &streamResponse)
if err != nil { if err != nil {
common.SysError("Error unmarshalling stream response: " + err.Error()) common.SysError("error unmarshalling stream response: " + err.Error())
return return
} }
for _, choice := range streamResponse.Choices { for _, choice := range streamResponse.Choices {

View File

@@ -118,7 +118,7 @@ func Relay(c *gin.Context) {
"error": err.OpenAIError, "error": err.OpenAIError,
}) })
channelId := c.GetInt("channel_id") channelId := c.GetInt("channel_id")
common.SysError(fmt.Sprintf("Relay error (channel #%d): %s", channelId, err.Message)) common.SysError(fmt.Sprintf("relay error (channel #%d): %s", channelId, err.Message))
// https://platform.openai.com/docs/guides/error-codes/api-errors // https://platform.openai.com/docs/guides/error-codes/api-errors
if common.AutomaticDisableChannelEnabled && (err.Type == "insufficient_quota" || err.Code == "invalid_api_key") { if common.AutomaticDisableChannelEnabled && (err.Type == "insufficient_quota" || err.Code == "invalid_api_key") {
channelId := c.GetInt("channel_id") channelId := c.GetInt("channel_id")

11
main.go
View File

@@ -6,7 +6,6 @@ import (
"github.com/gin-contrib/sessions/cookie" "github.com/gin-contrib/sessions/cookie"
"github.com/gin-contrib/sessions/redis" "github.com/gin-contrib/sessions/redis"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"log"
"one-api/common" "one-api/common"
"one-api/middleware" "one-api/middleware"
"one-api/model" "one-api/model"
@@ -30,19 +29,19 @@ func main() {
// Initialize SQL Database // Initialize SQL Database
err := model.InitDB() err := model.InitDB()
if err != nil { if err != nil {
common.FatalLog(err) common.FatalLog("failed to initialize database: " + err.Error())
} }
defer func() { defer func() {
err := model.CloseDB() err := model.CloseDB()
if err != nil { if err != nil {
common.FatalLog(err) common.FatalLog("failed to close database: " + err.Error())
} }
}() }()
// Initialize Redis // Initialize Redis
err = common.InitRedisClient() err = common.InitRedisClient()
if err != nil { if err != nil {
common.FatalLog(err) common.FatalLog("failed to initialize Redis: " + err.Error())
} }
// Initialize options // Initialize options
@@ -53,7 +52,7 @@ func main() {
if os.Getenv("SYNC_FREQUENCY") != "" { if os.Getenv("SYNC_FREQUENCY") != "" {
frequency, err := strconv.Atoi(os.Getenv("SYNC_FREQUENCY")) frequency, err := strconv.Atoi(os.Getenv("SYNC_FREQUENCY"))
if err != nil { if err != nil {
common.FatalLog(err) common.FatalLog("failed to parse SYNC_FREQUENCY: " + err.Error())
} }
go model.SyncOptions(frequency) go model.SyncOptions(frequency)
if common.RedisEnabled { if common.RedisEnabled {
@@ -84,6 +83,6 @@ func main() {
} }
err = server.Run(":" + port) err = server.Run(":" + port)
if err != nil { if err != nil {
log.Println(err) common.FatalLog("failed to start HTTP server: " + err.Error())
} }
} }

View File

@@ -100,7 +100,7 @@ func TokenAuth() func(c *gin.Context) {
c.Abort() c.Abort()
return return
} }
if !model.IsUserEnabled(token.UserId) { if !model.CacheIsUserEnabled(token.UserId) {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"error": gin.H{ "error": gin.H{
"message": "用户已被封禁", "message": "用户已被封禁",

View File

@@ -10,6 +10,6 @@ func CORS() gin.HandlerFunc {
config.AllowAllOrigins = true config.AllowAllOrigins = true
config.AllowCredentials = true config.AllowCredentials = true
config.AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"} config.AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}
config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization", "Accept", "Connection", "x-requested-with"} config.AllowHeaders = []string{"*"}
return cors.New(config) return cors.New(config)
} }

View File

@@ -2,15 +2,21 @@ package model
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"math/rand"
"one-api/common" "one-api/common"
"strconv"
"strings"
"sync" "sync"
"time" "time"
) )
const ( const (
TokenCacheSeconds = 60 * 60 TokenCacheSeconds = 60 * 60
UserId2GroupCacheSeconds = 60 * 60 UserId2GroupCacheSeconds = 60 * 60
UserId2QuotaCacheSeconds = 10 * 60
UserId2StatusCacheSeconds = 60 * 60
) )
func CacheGetTokenByKey(key string) (*Token, error) { func CacheGetTokenByKey(key string) (*Token, error) {
@@ -33,6 +39,7 @@ func CacheGetTokenByKey(key string) (*Token, error) {
if err != nil { if err != nil {
common.SysError("Redis set token error: " + err.Error()) common.SysError("Redis set token error: " + err.Error())
} }
return &token, nil
} }
err = json.Unmarshal([]byte(tokenObjectString), &token) err = json.Unmarshal([]byte(tokenObjectString), &token)
return &token, err return &token, err
@@ -56,18 +63,54 @@ func CacheGetUserGroup(id int) (group string, err error) {
return group, err return group, err
} }
var channelId2channel map[int]*Channel func CacheGetUserQuota(id int) (quota int, err error) {
var channelSyncLock sync.RWMutex if !common.RedisEnabled {
return GetUserQuota(id)
}
quotaString, err := common.RedisGet(fmt.Sprintf("user_quota:%d", id))
if err != nil {
quota, err = GetUserQuota(id)
if err != nil {
return 0, err
}
err = common.RedisSet(fmt.Sprintf("user_quota:%d", id), fmt.Sprintf("%d", quota), UserId2QuotaCacheSeconds*time.Second)
if err != nil {
common.SysError("Redis set user quota error: " + err.Error())
}
return quota, err
}
quota, err = strconv.Atoi(quotaString)
return quota, err
}
func CacheIsUserEnabled(userId int) bool {
if !common.RedisEnabled {
return IsUserEnabled(userId)
}
enabled, err := common.RedisGet(fmt.Sprintf("user_enabled:%d", userId))
if err != nil {
status := common.UserStatusDisabled
if IsUserEnabled(userId) {
status = common.UserStatusEnabled
}
enabled = fmt.Sprintf("%d", status)
err = common.RedisSet(fmt.Sprintf("user_enabled:%d", userId), enabled, UserId2StatusCacheSeconds*time.Second)
if err != nil {
common.SysError("Redis set user enabled error: " + err.Error())
}
}
return enabled == "1"
}
var group2model2channels map[string]map[string][]*Channel var group2model2channels map[string]map[string][]*Channel
var channelSyncLock sync.RWMutex
func InitChannelCache() { func InitChannelCache() {
channelSyncLock.Lock() newChannelId2channel := make(map[int]*Channel)
defer channelSyncLock.Unlock()
channelId2channel = make(map[int]*Channel)
var channels []*Channel var channels []*Channel
DB.Find(&channels) DB.Find(&channels)
for _, channel := range channels { for _, channel := range channels {
channelId2channel[channel.Id] = channel newChannelId2channel[channel.Id] = channel
} }
var abilities []*Ability var abilities []*Ability
DB.Find(&abilities) DB.Find(&abilities)
@@ -75,17 +118,32 @@ func InitChannelCache() {
for _, ability := range abilities { for _, ability := range abilities {
groups[ability.Group] = true groups[ability.Group] = true
} }
group2model2channels = make(map[string]map[string][]*Channel) newGroup2model2channels := make(map[string]map[string][]*Channel)
for group := range groups { for group := range groups {
group2model2channels[group] = make(map[string][]*Channel) newGroup2model2channels[group] = make(map[string][]*Channel)
// TODO: implement this
} }
for _, channel := range channels {
groups := strings.Split(channel.Group, ",")
for _, group := range groups {
models := strings.Split(channel.Models, ",")
for _, model := range models {
if _, ok := newGroup2model2channels[group][model]; !ok {
newGroup2model2channels[group][model] = make([]*Channel, 0)
}
newGroup2model2channels[group][model] = append(newGroup2model2channels[group][model], channel)
}
}
}
channelSyncLock.Lock()
group2model2channels = newGroup2model2channels
channelSyncLock.Unlock()
common.SysLog("channels synced from database")
} }
func SyncChannelCache(frequency int) { func SyncChannelCache(frequency int) {
for { for {
time.Sleep(time.Duration(frequency) * time.Second) time.Sleep(time.Duration(frequency) * time.Second)
common.SysLog("Syncing channels from database") common.SysLog("syncing channels from database")
InitChannelCache() InitChannelCache()
} }
} }
@@ -94,6 +152,12 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error
if !common.RedisEnabled { if !common.RedisEnabled {
return GetRandomSatisfiedChannel(group, model) return GetRandomSatisfiedChannel(group, model)
} }
// TODO: implement this channelSyncLock.RLock()
return nil, nil defer channelSyncLock.RUnlock()
channels := group2model2channels[group][model]
if len(channels) == 0 {
return nil, errors.New("channel not found")
}
idx := rand.Intn(len(channels))
return channels[idx], nil
} }

View File

@@ -42,19 +42,24 @@ func InitDB() (err error) {
var db *gorm.DB var db *gorm.DB
if os.Getenv("SQL_DSN") != "" { if os.Getenv("SQL_DSN") != "" {
// Use MySQL // Use MySQL
common.SysLog("using MySQL as database")
db, err = gorm.Open(mysql.Open(os.Getenv("SQL_DSN")), &gorm.Config{ db, err = gorm.Open(mysql.Open(os.Getenv("SQL_DSN")), &gorm.Config{
PrepareStmt: true, // precompile SQL PrepareStmt: true, // precompile SQL
}) })
} else { } else {
// Use SQLite // Use SQLite
common.SysLog("SQL_DSN not set, using SQLite as database")
common.UsingSQLite = true common.UsingSQLite = true
db, err = gorm.Open(sqlite.Open(common.SQLitePath), &gorm.Config{ db, err = gorm.Open(sqlite.Open(common.SQLitePath), &gorm.Config{
PrepareStmt: true, // precompile SQL PrepareStmt: true, // precompile SQL
}) })
common.SysLog("SQL_DSN not set, using SQLite as database")
} }
common.SysLog("database connected")
if err == nil { if err == nil {
DB = db DB = db
if !common.IsMasterNode {
return nil
}
err := db.AutoMigrate(&Channel{}) err := db.AutoMigrate(&Channel{})
if err != nil { if err != nil {
return err return err
@@ -83,6 +88,7 @@ func InitDB() (err error) {
if err != nil { if err != nil {
return err return err
} }
common.SysLog("database migrated")
err = createRootAccountIfNeed() err = createRootAccountIfNeed()
return err return err
} else { } else {

View File

@@ -75,7 +75,7 @@ func loadOptionsFromDatabase() {
for _, option := range options { for _, option := range options {
err := updateOptionMap(option.Key, option.Value) err := updateOptionMap(option.Key, option.Value)
if err != nil { if err != nil {
common.SysError("Failed to update option map: " + err.Error()) common.SysError("failed to update option map: " + err.Error())
} }
} }
} }
@@ -83,7 +83,7 @@ func loadOptionsFromDatabase() {
func SyncOptions(frequency int) { func SyncOptions(frequency int) {
for { for {
time.Sleep(time.Duration(frequency) * time.Second) time.Sleep(time.Duration(frequency) * time.Second)
common.SysLog("Syncing options from database") common.SysLog("syncing options from database")
loadOptionsFromDatabase() loadOptionsFromDatabase()
} }
} }

View File

@@ -64,7 +64,7 @@ func Redeem(key string, userId int) (quota int, err error) {
redemption.Status = common.RedemptionCodeStatusUsed redemption.Status = common.RedemptionCodeStatusUsed
err := redemption.SelectUpdate() err := redemption.SelectUpdate()
if err != nil { if err != nil {
common.SysError("更新兑换码状态失败:" + err.Error()) common.SysError("failed to update redemption status: " + err.Error())
} }
RecordLog(userId, LogTypeTopup, fmt.Sprintf("通过兑换码充值 %s", common.LogQuota(redemption.Quota))) RecordLog(userId, LogTypeTopup, fmt.Sprintf("通过兑换码充值 %s", common.LogQuota(redemption.Quota)))
}() }()

View File

@@ -34,39 +34,39 @@ func SearchUserTokens(userId int, keyword string) (tokens []*Token, err error) {
func ValidateUserToken(key string) (token *Token, err error) { func ValidateUserToken(key string) (token *Token, err error) {
if key == "" { if key == "" {
return nil, errors.New("未提供 token") return nil, errors.New("未提供令牌")
} }
token, err = CacheGetTokenByKey(key) token, err = CacheGetTokenByKey(key)
if err == nil { if err == nil {
if token.Status != common.TokenStatusEnabled { if token.Status != common.TokenStatusEnabled {
return nil, errors.New("该 token 状态不可用") return nil, errors.New("该令牌状态不可用")
} }
if token.ExpiredTime != -1 && token.ExpiredTime < common.GetTimestamp() { if token.ExpiredTime != -1 && token.ExpiredTime < common.GetTimestamp() {
token.Status = common.TokenStatusExpired token.Status = common.TokenStatusExpired
err := token.SelectUpdate() err := token.SelectUpdate()
if err != nil { if err != nil {
common.SysError("更新 token 状态失败:" + err.Error()) common.SysError("failed to update token status" + err.Error())
} }
return nil, errors.New("该 token 已过期") return nil, errors.New("该令牌已过期")
} }
if !token.UnlimitedQuota && token.RemainQuota <= 0 { if !token.UnlimitedQuota && token.RemainQuota <= 0 {
token.Status = common.TokenStatusExhausted token.Status = common.TokenStatusExhausted
err := token.SelectUpdate() err := token.SelectUpdate()
if err != nil { if err != nil {
common.SysError("更新 token 状态失败:" + err.Error()) common.SysError("failed to update token status" + err.Error())
} }
return nil, errors.New("该 token 额度已用尽") return nil, errors.New("该令牌额度已用尽")
} }
go func() { go func() {
token.AccessedTime = common.GetTimestamp() token.AccessedTime = common.GetTimestamp()
err := token.SelectUpdate() err := token.SelectUpdate()
if err != nil { if err != nil {
common.SysError("更新 token 失败:" + err.Error()) common.SysError("failed to update token" + err.Error())
} }
}() }()
return token, nil return token, nil
} }
return nil, errors.New("无效的 token") return nil, errors.New("无效的令牌")
} }
func GetTokenByIds(id int, userId int) (*Token, error) { func GetTokenByIds(id int, userId int) (*Token, error) {
@@ -166,7 +166,7 @@ func PreConsumeTokenQuota(tokenId int, quota int) (err error) {
go func() { go func() {
email, err := GetUserEmail(token.UserId) email, err := GetUserEmail(token.UserId)
if err != nil { if err != nil {
common.SysError("获取用户邮箱失败:" + err.Error()) common.SysError("failed to fetch user email: " + err.Error())
} }
prompt := "您的额度即将用尽" prompt := "您的额度即将用尽"
if noMoreQuota { if noMoreQuota {
@@ -177,7 +177,7 @@ func PreConsumeTokenQuota(tokenId int, quota int) (err error) {
err = common.SendEmail(prompt, email, err = common.SendEmail(prompt, email,
fmt.Sprintf("%s当前剩余额度为 %d为了不影响您的使用请及时充值。<br/>充值链接:<a href='%s'>%s</a>", prompt, userQuota, topUpLink, topUpLink)) fmt.Sprintf("%s当前剩余额度为 %d为了不影响您的使用请及时充值。<br/>充值链接:<a href='%s'>%s</a>", prompt, userQuota, topUpLink, topUpLink))
if err != nil { if err != nil {
common.SysError("发送邮件失败:" + err.Error()) common.SysError("failed to send email" + err.Error())
} }
} }
}() }()

View File

@@ -220,7 +220,7 @@ func IsAdmin(userId int) bool {
var user User var user User
err := DB.Where("id = ?", userId).Select("role").Find(&user).Error err := DB.Where("id = ?", userId).Select("role").Find(&user).Error
if err != nil { if err != nil {
common.SysError("No such user " + err.Error()) common.SysError("no such user " + err.Error())
return false return false
} }
return user.Role >= common.RoleAdminUser return user.Role >= common.RoleAdminUser
@@ -233,7 +233,7 @@ func IsUserEnabled(userId int) bool {
var user User var user User
err := DB.Where("id = ?", userId).Select("status").Find(&user).Error err := DB.Where("id = ?", userId).Select("status").Find(&user).Error
if err != nil { if err != nil {
common.SysError("No such user " + err.Error()) common.SysError("no such user " + err.Error())
return false return false
} }
return user.Status == common.UserStatusEnabled return user.Status == common.UserStatusEnabled
@@ -300,6 +300,6 @@ func UpdateUserUsedQuotaAndRequestCount(id int, quota int) {
}, },
).Error ).Error
if err != nil { if err != nil {
common.SysError("Failed to update user used quota and request count: " + err.Error()) common.SysError("failed to update user used quota and request count: " + err.Error())
} }
} }

View File

@@ -238,9 +238,17 @@ const ChannelsTable = () => {
if (channels.length === 0) return; if (channels.length === 0) return;
setLoading(true); setLoading(true);
let sortedChannels = [...channels]; let sortedChannels = [...channels];
sortedChannels.sort((a, b) => { if (typeof sortedChannels[0][key] === 'string'){
return ('' + a[key]).localeCompare(b[key]); sortedChannels.sort((a, b) => {
}); return ('' + a[key]).localeCompare(b[key]);
});
} else {
sortedChannels.sort((a, b) => {
if (a[key] === b[key]) return 0;
if (a[key] > b[key]) return -1;
if (a[key] < b[key]) return 1;
});
}
if (sortedChannels[0].id === channels[0].id) { if (sortedChannels[0].id === channels[0].id) {
sortedChannels.reverse(); sortedChannels.reverse();
} }

View File

@@ -154,7 +154,7 @@ const OperationSetting = () => {
placeholder='例如 ChatGPT Next Web 的部署地址' placeholder='例如 ChatGPT Next Web 的部署地址'
/> />
<Form.Input <Form.Input
label='额度汇率' label='单位美元额度'
name='QuotaPerUnit' name='QuotaPerUnit'
onChange={handleInputChange} onChange={handleInputChange}
autoComplete='new-password' autoComplete='new-password'

View File

@@ -46,4 +46,13 @@ export function renderQuota(quota, digits = 2) {
return '$' + (quota / quotaPerUnit).toFixed(digits); return '$' + (quota / quotaPerUnit).toFixed(digits);
} }
return renderNumber(quota); return renderNumber(quota);
}
export function renderQuotaWithPrompt(quota, digits) {
let displayInCurrency = localStorage.getItem('display_in_currency');
displayInCurrency = displayInCurrency === 'true';
if (displayInCurrency) {
return `(等价金额:${renderQuota(quota, digits)}`;
}
return '';
} }

View File

@@ -177,6 +177,20 @@ const EditChannel = () => {
</Form.Field> </Form.Field>
) )
} }
{
inputs.type !== 3 && inputs.type !== 8 && (
<Form.Field>
<Form.Input
label='Base URL'
name='base_url'
placeholder={'请输入自定义 Base URL格式为https://domain.com可不填不填使用渠道默认值'}
onChange={handleInputChange}
value={inputs.base_url}
autoComplete='new-password'
/>
</Form.Field>
)
}
<Form.Field> <Form.Field>
<Form.Input <Form.Input
label='名称' label='名称'

View File

@@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';
import { Button, Form, Header, Segment } from 'semantic-ui-react'; import { Button, Form, Header, Segment } from 'semantic-ui-react';
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';
import { API, downloadTextAsFile, showError, showSuccess } from '../../helpers'; import { API, downloadTextAsFile, showError, showSuccess } from '../../helpers';
import { renderQuota } from '../../helpers/render'; import { renderQuota, renderQuotaWithPrompt } from '../../helpers/render';
const EditRedemption = () => { const EditRedemption = () => {
const params = useParams(); const params = useParams();
@@ -11,7 +11,7 @@ const EditRedemption = () => {
const [loading, setLoading] = useState(isEdit); const [loading, setLoading] = useState(isEdit);
const originInputs = { const originInputs = {
name: '', name: '',
quota: 100, quota: 100000,
count: 1 count: 1
}; };
const [inputs, setInputs] = useState(originInputs); const [inputs, setInputs] = useState(originInputs);
@@ -88,7 +88,7 @@ const EditRedemption = () => {
</Form.Field> </Form.Field>
<Form.Field> <Form.Field>
<Form.Input <Form.Input
label={`额度(等价金额 ${renderQuota(quota)}`} label={`额度${renderQuotaWithPrompt(quota)}`}
name='quota' name='quota'
placeholder={'请输入单个兑换码中包含的额度'} placeholder={'请输入单个兑换码中包含的额度'}
onChange={handleInputChange} onChange={handleInputChange}

View File

@@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';
import { Button, Form, Header, Message, Segment } from 'semantic-ui-react'; import { Button, Form, Header, Message, Segment } from 'semantic-ui-react';
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';
import { API, showError, showSuccess, timestamp2string } from '../../helpers'; import { API, showError, showSuccess, timestamp2string } from '../../helpers';
import { renderQuota } from '../../helpers/render'; import { renderQuota, renderQuotaWithPrompt } from '../../helpers/render';
const EditToken = () => { const EditToken = () => {
const params = useParams(); const params = useParams();
@@ -138,7 +138,7 @@ const EditToken = () => {
<Message>注意令牌的额度仅用于限制令牌本身的最大额度使用量实际的使用受到账户的剩余额度限制</Message> <Message>注意令牌的额度仅用于限制令牌本身的最大额度使用量实际的使用受到账户的剩余额度限制</Message>
<Form.Field> <Form.Field>
<Form.Input <Form.Input
label={`额度(等价金额 ${renderQuota(remain_quota)}`} label={`额度${renderQuotaWithPrompt(remain_quota)}`}
name='remain_quota' name='remain_quota'
placeholder={'请输入额度'} placeholder={'请输入额度'}
onChange={handleInputChange} onChange={handleInputChange}