mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-10-31 13:53:41 +08:00 
			
		
		
		
	Compare commits
	
		
			24 Commits
		
	
	
		
			v0.4.5-alp
			...
			v0.4.7-alp
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 8a4cd403fd | ||
|  | 9ac5410d06 | ||
|  | 7edc2b5376 | ||
|  | d4869dfad2 | ||
|  | 4463224f04 | ||
|  | ad1049b0cf | ||
|  | d0c454c78e | ||
|  | fe135fd508 | ||
|  | b090e50f72 | ||
|  | 7497f24daa | ||
|  | 28fb4d76af | ||
|  | ca779e4ffa | ||
|  | f51c982437 | ||
|  | 36e681e878 | ||
|  | 75cd522c2c | ||
|  | c893d04667 | ||
|  | c6717307d0 | ||
|  | 97cdb616cd | ||
|  | 76a3913115 | ||
|  | 00151a0124 | ||
|  | b86de464b5 | ||
|  | 567916bd80 | ||
|  | 1f3b3ca7ae | ||
|  | 70cffbc258 | 
							
								
								
									
										30
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								README.md
									
									
									
									
									
								
							| @@ -86,7 +86,7 @@ _✨ All in one 的 OpenAI 接口,整合各种 API 访问方式,开箱即用 | ||||
|  | ||||
| ## 部署 | ||||
| ### 基于 Docker 进行部署 | ||||
| 部署命令:`docker run --name one-api -d --restart always -p 3000:3000 -v /home/ubuntu/data/one-api:/data justsong/one-api` | ||||
| 部署命令:`docker run --name one-api -d --restart always -p 3000:3000 -e TZ=Asia/Shanghai -v /home/ubuntu/data/one-api:/data justsong/one-api` | ||||
|  | ||||
| 更新命令:`docker run --rm -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower -cR` | ||||
|  | ||||
| @@ -151,9 +151,12 @@ sudo service nginx restart | ||||
|  | ||||
| ### 多机部署 | ||||
| 1. 所有服务器 `SESSION_SECRET` 设置一样的值。 | ||||
| 2. 必须设置 `SQL_DSN`,使用 MySQL 数据库而非 SQLite,请自行配置主备数据库同步。 | ||||
| 3. 所有从服务器必须设置 `SYNC_FREQUENCY`,以定期从数据库同步配置。 | ||||
| 4. 从服务器可以选择设置 `FRONTEND_BASE_URL`,以重定向页面请求到主服务器。 | ||||
| 2. 必须设置 `SQL_DSN`,使用 MySQL 数据库而非 SQLite,所有服务器连接同一个数据库。 | ||||
| 3. 所有从服务器必须设置 `NODE_TYPE` 为 `slave`。 | ||||
| 4. 设置 `SYNC_FREQUENCY` 后服务器将定期从数据库同步配置。 | ||||
| 5. 从服务器可以选择设置 `FRONTEND_BASE_URL`,以重定向页面请求到主服务器。 | ||||
| 6. 从服务器上**分别**装好 Redis,设置好 `REDIS_CONN_STRING`,这样可以做到在缓存未过期的情况下数据库零访问,可以减少延迟。 | ||||
| 7. 如果主服务器访问数据库延迟也比较高,则也需要启用 Redis,并设置 `SYNC_FREQUENCY`,以定期从数据库同步配置。 | ||||
|  | ||||
| 环境变量的具体使用方法详见[此处](#环境变量)。 | ||||
|  | ||||
| @@ -170,7 +173,7 @@ sudo service nginx restart | ||||
| 项目主页:https://github.com/Yidadaa/ChatGPT-Next-Web | ||||
|  | ||||
| ```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`。 | ||||
| @@ -245,6 +248,14 @@ graph LR | ||||
|    + 例子:`FRONTEND_BASE_URL=https://openai.justsong.cn` | ||||
| 5. `SYNC_FREQUENCY`:设置之后将定期与数据库同步配置,单位为秒,未设置则不进行同步。 | ||||
|    + 例子:`SYNC_FREQUENCY=60` | ||||
| 6. `NODE_TYPE`:设置之后将指定节点类型,可选值为 `master` 和 `slave`,未设置则默认为 `master`。 | ||||
|    + 例子:`NODE_TYPE=slave` | ||||
| 7. `CHANNEL_UPDATE_FREQUENCY`:设置之后将定期更新渠道余额,单位为分钟,未设置则不进行更新。 | ||||
|    + 例子:`CHANNEL_UPDATE_FREQUENCY=1440` | ||||
| 8. `CHANNEL_TEST_FREQUENCY`:设置之后将定期检查渠道,单位为分钟,未设置则不进行检查。 | ||||
|    + 例子:`CHANNEL_TEST_FREQUENCY=1440` | ||||
| 9. `REQUEST_INTERVAL`:批量更新渠道余额以及测试可用性时的请求间隔,单位为秒,默认无间隔。 | ||||
|    + 例子:`POLLING_INTERVAL=5` | ||||
|  | ||||
| ### 命令行参数 | ||||
| 1. `--port <port_number>`: 指定服务器监听的端口号,默认为 `3000`。 | ||||
| @@ -265,9 +276,9 @@ https://openai.justsong.cn | ||||
|  | ||||
| ## 常见问题 | ||||
| 1. 额度是什么?怎么计算的?One API 的额度计算有问题? | ||||
|    + 额度 = token * 倍率 | ||||
|    + 倍率包括分组的倍率,以及补全的倍率。 | ||||
|    + 如果是非流模式,官方接口会返回消耗的总 token,但是你要注意提示和补全的消耗额度不一样。 | ||||
|    + 额度 = 分组倍率 * 模型倍率 * (提示 token 数 + 补全 token 数 * 补全倍率) | ||||
|    + 其中补全倍率对于 GPT3.5 固定为 1.33,GPT4 为 2,与官方保持一致。 | ||||
|    + 如果是非流模式,官方接口会返回消耗的总 token,但是你要注意提示和补全的消耗倍率不一样。 | ||||
| 2. 账户额度足够为什么提示额度不足? | ||||
|    + 请检查你的令牌额度是否足够,这个和账户额度是分开的。 | ||||
|    + 令牌额度仅供用户设置最大使用量,用户可自由设置。 | ||||
| @@ -277,6 +288,9 @@ https://openai.justsong.cn | ||||
| 4. 渠道测试报错:`invalid character '<' looking for beginning of value` | ||||
|    + 这是因为返回值不是合法的 JSON,而是一个 HTML 页面。 | ||||
|    + 大概率是你的部署站的 IP 或代理的节点被 CloudFlare 封禁了。 | ||||
| 5. ChatGPT Next Web 报错:`Failed to fetch` | ||||
|    + 部署的时候不要设置 `BASE_URL`。 | ||||
|    + 检查你的接口地址和 API Key 有没有填对。 | ||||
|  | ||||
| ## 注意 | ||||
| 本项目为开源项目,请在遵循 OpenAI 的[使用条款](https://openai.com/policies/terms-of-use)以及**法律法规**的情况下使用,不得用于非法用途。 | ||||
|   | ||||
							
								
								
									
										36
									
								
								bin/time_test.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								bin/time_test.sh
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| #!/bin/bash | ||||
|  | ||||
| if [ $# -ne 3 ]; then | ||||
|   echo "Usage: time_test.sh <domain> <key> <count>" | ||||
|   exit 1 | ||||
| fi | ||||
|  | ||||
| domain=$1 | ||||
| key=$2 | ||||
| count=$3 | ||||
| total_time=0 | ||||
| times=() | ||||
|  | ||||
| for ((i=1; i<=count; i++)); do | ||||
|   result=$(curl -o /dev/null -s -w %{time_total}\\n \ | ||||
|            https://"$domain"/v1/chat/completions \ | ||||
|            -H "Content-Type: application/json" \ | ||||
|            -H "Authorization: Bearer $key" \ | ||||
|            -d '{"messages": [{"content": "echo hi", "role": "user"}], "model": "gpt-3.5-turbo", "stream": false, "max_tokens": 1}') | ||||
|   echo "$result" | ||||
|   total_time=$(bc <<< "$total_time + $result") | ||||
|   times+=("$result") | ||||
| done | ||||
|  | ||||
| average_time=$(echo "scale=4; $total_time / $count" | bc) | ||||
|  | ||||
| sum_of_squares=0 | ||||
| for time in "${times[@]}"; do | ||||
|   difference=$(echo "scale=4; $time - $average_time" | bc) | ||||
|   square=$(echo "scale=4; $difference * $difference" | bc) | ||||
|   sum_of_squares=$(echo "scale=4; $sum_of_squares + $square" | bc) | ||||
| done | ||||
|  | ||||
| standard_deviation=$(echo "scale=4; sqrt($sum_of_squares / $count)" | bc) | ||||
|  | ||||
| echo "Average time: $average_time±$standard_deviation" | ||||
| @@ -1,6 +1,8 @@ | ||||
| package common | ||||
|  | ||||
| import ( | ||||
| 	"os" | ||||
| 	"strconv" | ||||
| 	"sync" | ||||
| 	"time" | ||||
|  | ||||
| @@ -16,7 +18,8 @@ var Logo = "" | ||||
| var TopUpLink = "" | ||||
| var ChatLink = "" | ||||
| var QuotaPerUnit = 500 * 1000.0 // $0.002 / 1K tokens | ||||
| var DisplayInCurrencyEnabled = false | ||||
| var DisplayInCurrencyEnabled = true | ||||
| var DisplayTokenStatEnabled = true | ||||
|  | ||||
| var UsingSQLite = false | ||||
|  | ||||
| @@ -67,6 +70,11 @@ var PreConsumedQuota = 500 | ||||
|  | ||||
| var RootUserEmail = "" | ||||
|  | ||||
| var IsMasterNode = os.Getenv("NODE_TYPE") != "slave" | ||||
|  | ||||
| var requestInterval, _ = strconv.Atoi(os.Getenv("REQUEST_INTERVAL")) | ||||
| var RequestInterval = time.Duration(requestInterval) * time.Second | ||||
|  | ||||
| const ( | ||||
| 	RoleGuestUser  = 0 | ||||
| 	RoleCommonUser = 1 | ||||
|   | ||||
| @@ -11,7 +11,7 @@ var GroupRatio = map[string]float64{ | ||||
| func GroupRatio2JSONString() string { | ||||
| 	jsonBytes, err := json.Marshal(GroupRatio) | ||||
| 	if err != nil { | ||||
| 		SysError("Error marshalling model ratio: " + err.Error()) | ||||
| 		SysError("error marshalling model ratio: " + err.Error()) | ||||
| 	} | ||||
| 	return string(jsonBytes) | ||||
| } | ||||
| @@ -24,7 +24,7 @@ func UpdateGroupRatioByJSONString(jsonStr string) error { | ||||
| func GetGroupRatio(name string) float64 { | ||||
| 	ratio, ok := GroupRatio[name] | ||||
| 	if !ok { | ||||
| 		SysError("Group ratio not found: " + name) | ||||
| 		SysError("group ratio not found: " + name) | ||||
| 		return 1 | ||||
| 	} | ||||
| 	return ratio | ||||
|   | ||||
| @@ -40,7 +40,7 @@ var ModelRatio = map[string]float64{ | ||||
| func ModelRatio2JSONString() string { | ||||
| 	jsonBytes, err := json.Marshal(ModelRatio) | ||||
| 	if err != nil { | ||||
| 		SysError("Error marshalling model ratio: " + err.Error()) | ||||
| 		SysError("error marshalling model ratio: " + err.Error()) | ||||
| 	} | ||||
| 	return string(jsonBytes) | ||||
| } | ||||
| @@ -53,7 +53,7 @@ func UpdateModelRatioByJSONString(jsonStr string) error { | ||||
| func GetModelRatio(name string) float64 { | ||||
| 	ratio, ok := ModelRatio[name] | ||||
| 	if !ok { | ||||
| 		SysError("Model ratio not found: " + name) | ||||
| 		SysError("model ratio not found: " + name) | ||||
| 		return 30 | ||||
| 	} | ||||
| 	return ratio | ||||
|   | ||||
| @@ -17,9 +17,15 @@ func InitRedisClient() (err error) { | ||||
| 		SysLog("REDIS_CONN_STRING not set, Redis is not enabled") | ||||
| 		return nil | ||||
| 	} | ||||
| 	if os.Getenv("SYNC_FREQUENCY") == "" { | ||||
| 		RedisEnabled = false | ||||
| 		SysLog("SYNC_FREQUENCY not set, Redis is disabled") | ||||
| 		return nil | ||||
| 	} | ||||
| 	SysLog("Redis is enabled") | ||||
| 	opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING")) | ||||
| 	if err != nil { | ||||
| 		panic(err) | ||||
| 		FatalLog("failed to parse Redis connection string: " + err.Error()) | ||||
| 	} | ||||
| 	RDB = redis.NewClient(opt) | ||||
|  | ||||
| @@ -27,13 +33,16 @@ func InitRedisClient() (err error) { | ||||
| 	defer cancel() | ||||
|  | ||||
| 	_, err = RDB.Ping(ctx).Result() | ||||
| 	if err != nil { | ||||
| 		FatalLog("Redis ping test failed: " + err.Error()) | ||||
| 	} | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func ParseRedisOption() *redis.Options { | ||||
| 	opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING")) | ||||
| 	if err != nil { | ||||
| 		panic(err) | ||||
| 		FatalLog("failed to parse Redis connection string: " + err.Error()) | ||||
| 	} | ||||
| 	return opt | ||||
| } | ||||
|   | ||||
| @@ -7,8 +7,17 @@ import ( | ||||
| ) | ||||
|  | ||||
| func GetSubscription(c *gin.Context) { | ||||
| 	userId := c.GetInt("id") | ||||
| 	quota, err := model.GetUserQuota(userId) | ||||
| 	var quota int | ||||
| 	var err error | ||||
| 	var token *model.Token | ||||
| 	if common.DisplayTokenStatEnabled { | ||||
| 		tokenId := c.GetInt("token_id") | ||||
| 		token, err = model.GetTokenById(tokenId) | ||||
| 		quota = token.RemainQuota | ||||
| 	} else { | ||||
| 		userId := c.GetInt("id") | ||||
| 		quota, err = model.GetUserQuota(userId) | ||||
| 	} | ||||
| 	if err != nil { | ||||
| 		openAIError := OpenAIError{ | ||||
| 			Message: err.Error(), | ||||
| @@ -35,8 +44,17 @@ func GetSubscription(c *gin.Context) { | ||||
| } | ||||
|  | ||||
| func GetUsage(c *gin.Context) { | ||||
| 	userId := c.GetInt("id") | ||||
| 	quota, err := model.GetUserUsedQuota(userId) | ||||
| 	var quota int | ||||
| 	var err error | ||||
| 	var token *model.Token | ||||
| 	if common.DisplayTokenStatEnabled { | ||||
| 		tokenId := c.GetInt("token_id") | ||||
| 		token, err = model.GetTokenById(tokenId) | ||||
| 		quota = token.UsedQuota | ||||
| 	} else { | ||||
| 		userId := c.GetInt("id") | ||||
| 		quota, err = model.GetUserUsedQuota(userId) | ||||
| 	} | ||||
| 	if err != nil { | ||||
| 		openAIError := OpenAIError{ | ||||
| 			Message: err.Error(), | ||||
|   | ||||
| @@ -257,6 +257,7 @@ func updateAllChannelsBalance() error { | ||||
| 				disableChannel(channel.Id, channel.Name, "余额不足") | ||||
| 			} | ||||
| 		} | ||||
| 		time.Sleep(common.RequestInterval) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
| @@ -277,3 +278,12 @@ func UpdateAllChannelsBalance(c *gin.Context) { | ||||
| 	}) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func AutomaticallyUpdateChannels(frequency int) { | ||||
| 	for { | ||||
| 		time.Sleep(time.Duration(frequency) * time.Minute) | ||||
| 		common.SysLog("updating all channels") | ||||
| 		_ = updateAllChannelsBalance() | ||||
| 		common.SysLog("channels update done") | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -62,10 +62,9 @@ func testChannel(channel *model.Channel, request ChatRequest) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func buildTestRequest(c *gin.Context) *ChatRequest { | ||||
| 	model_ := c.Query("model") | ||||
| func buildTestRequest() *ChatRequest { | ||||
| 	testRequest := &ChatRequest{ | ||||
| 		Model:     model_, | ||||
| 		Model:     "", // this will be set later | ||||
| 		MaxTokens: 1, | ||||
| 	} | ||||
| 	testMessage := Message{ | ||||
| @@ -93,7 +92,7 @@ func TestChannel(c *gin.Context) { | ||||
| 		}) | ||||
| 		return | ||||
| 	} | ||||
| 	testRequest := buildTestRequest(c) | ||||
| 	testRequest := buildTestRequest() | ||||
| 	tik := time.Now() | ||||
| 	err = testChannel(channel, *testRequest) | ||||
| 	tok := time.Now() | ||||
| @@ -129,11 +128,11 @@ func disableChannel(channelId int, channelName string, reason string) { | ||||
| 	content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason) | ||||
| 	err := common.SendEmail(subject, common.RootUserEmail, content) | ||||
| 	if err != nil { | ||||
| 		common.SysError(fmt.Sprintf("发送邮件失败:%s", err.Error())) | ||||
| 		common.SysError(fmt.Sprintf("failed to send email: %s", err.Error())) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func testAllChannels(c *gin.Context) error { | ||||
| func testAllChannels(notify bool) error { | ||||
| 	if common.RootUserEmail == "" { | ||||
| 		common.RootUserEmail = model.GetRootUserEmail() | ||||
| 	} | ||||
| @@ -146,13 +145,9 @@ func testAllChannels(c *gin.Context) error { | ||||
| 	testAllChannelsLock.Unlock() | ||||
| 	channels, err := model.GetAllChannels(0, 0, true) | ||||
| 	if err != nil { | ||||
| 		c.JSON(http.StatusOK, gin.H{ | ||||
| 			"success": false, | ||||
| 			"message": err.Error(), | ||||
| 		}) | ||||
| 		return err | ||||
| 	} | ||||
| 	testRequest := buildTestRequest(c) | ||||
| 	testRequest := buildTestRequest() | ||||
| 	var disableThreshold = int64(common.ChannelDisableThreshold * 1000) | ||||
| 	if disableThreshold == 0 { | ||||
| 		disableThreshold = 10000000 // a impossible value | ||||
| @@ -173,20 +168,23 @@ func testAllChannels(c *gin.Context) error { | ||||
| 				disableChannel(channel.Id, channel.Name, err.Error()) | ||||
| 			} | ||||
| 			channel.UpdateResponseTime(milliseconds) | ||||
| 		} | ||||
| 		err := common.SendEmail("通道测试完成", common.RootUserEmail, "通道测试完成,如果没有收到禁用通知,说明所有通道都正常") | ||||
| 		if err != nil { | ||||
| 			common.SysError(fmt.Sprintf("发送邮件失败:%s", err.Error())) | ||||
| 			time.Sleep(common.RequestInterval) | ||||
| 		} | ||||
| 		testAllChannelsLock.Lock() | ||||
| 		testAllChannelsRunning = false | ||||
| 		testAllChannelsLock.Unlock() | ||||
| 		if notify { | ||||
| 			err := common.SendEmail("通道测试完成", common.RootUserEmail, "通道测试完成,如果没有收到禁用通知,说明所有通道都正常") | ||||
| 			if err != nil { | ||||
| 				common.SysError(fmt.Sprintf("failed to send email: %s", err.Error())) | ||||
| 			} | ||||
| 		} | ||||
| 	}() | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func TestAllChannels(c *gin.Context) { | ||||
| 	err := testAllChannels(c) | ||||
| 	err := testAllChannels(true) | ||||
| 	if err != nil { | ||||
| 		c.JSON(http.StatusOK, gin.H{ | ||||
| 			"success": false, | ||||
| @@ -200,3 +198,12 @@ func TestAllChannels(c *gin.Context) { | ||||
| 	}) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func AutomaticallyTestChannels(frequency int) { | ||||
| 	for { | ||||
| 		time.Sleep(time.Duration(frequency) * time.Minute) | ||||
| 		common.SysLog("testing all channels") | ||||
| 		_ = testAllChannels(false) | ||||
| 		common.SysLog("channel test finished") | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -13,7 +13,7 @@ func GetOptions(c *gin.Context) { | ||||
| 	var options []*model.Option | ||||
| 	common.OptionMapRWMutex.Lock() | ||||
| 	for k, v := range common.OptionMap { | ||||
| 		if strings.Contains(k, "Token") || strings.Contains(k, "Secret") { | ||||
| 		if strings.HasSuffix(k, "Token") || strings.HasSuffix(k, "Secret") { | ||||
| 			continue | ||||
| 		} | ||||
| 		options = append(options, &model.Option{ | ||||
|   | ||||
| @@ -76,7 +76,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { | ||||
| 	preConsumedQuota := int(float64(preConsumedTokens) * ratio) | ||||
| 	userQuota, err := model.CacheGetUserQuota(userId) | ||||
| 	if err != nil { | ||||
| 		return errorWrapper(err, "get_user_quota_failed", http.StatusOK) | ||||
| 		return errorWrapper(err, "get_user_quota_failed", http.StatusInternalServerError) | ||||
| 	} | ||||
| 	if userQuota > 10*preConsumedQuota { | ||||
| 		// in this case, we do not pre-consume quota | ||||
| @@ -86,12 +86,12 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { | ||||
| 	if consumeQuota && preConsumedQuota > 0 { | ||||
| 		err := model.PreConsumeTokenQuota(tokenId, preConsumedQuota) | ||||
| 		if err != nil { | ||||
| 			return errorWrapper(err, "pre_consume_token_quota_failed", http.StatusOK) | ||||
| 			return errorWrapper(err, "pre_consume_token_quota_failed", http.StatusForbidden) | ||||
| 		} | ||||
| 	} | ||||
| 	req, err := http.NewRequest(c.Request.Method, fullRequestURL, c.Request.Body) | ||||
| 	if err != nil { | ||||
| 		return errorWrapper(err, "new_request_failed", http.StatusOK) | ||||
| 		return errorWrapper(err, "new_request_failed", http.StatusInternalServerError) | ||||
| 	} | ||||
| 	if channelType == common.ChannelTypeAzure { | ||||
| 		key := c.Request.Header.Get("Authorization") | ||||
| @@ -106,15 +106,15 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { | ||||
| 	client := &http.Client{} | ||||
| 	resp, err := client.Do(req) | ||||
| 	if err != nil { | ||||
| 		return errorWrapper(err, "do_request_failed", http.StatusOK) | ||||
| 		return errorWrapper(err, "do_request_failed", http.StatusInternalServerError) | ||||
| 	} | ||||
| 	err = req.Body.Close() | ||||
| 	if err != nil { | ||||
| 		return errorWrapper(err, "close_request_body_failed", http.StatusOK) | ||||
| 		return errorWrapper(err, "close_request_body_failed", http.StatusInternalServerError) | ||||
| 	} | ||||
| 	err = c.Request.Body.Close() | ||||
| 	if err != nil { | ||||
| 		return errorWrapper(err, "close_request_body_failed", http.StatusOK) | ||||
| 		return errorWrapper(err, "close_request_body_failed", http.StatusInternalServerError) | ||||
| 	} | ||||
| 	var textResponse TextResponse | ||||
| 	isStream := strings.HasPrefix(resp.Header.Get("Content-Type"), "text/event-stream") | ||||
| @@ -140,7 +140,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { | ||||
| 			quotaDelta := quota - preConsumedQuota | ||||
| 			err := model.PostConsumeTokenQuota(tokenId, quotaDelta) | ||||
| 			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") | ||||
| 			model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("通过令牌「%s」使用模型 %s 消耗 %s(模型倍率 %.2f,分组倍率 %.2f)", tokenName, textRequest.Model, common.LogQuota(quota), modelRatio, groupRatio)) | ||||
| @@ -173,7 +173,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { | ||||
| 			for scanner.Scan() { | ||||
| 				data := scanner.Text() | ||||
| 				if len(data) < 6 { // must be something wrong! | ||||
| 					common.SysError("Invalid stream response: " + data) | ||||
| 					common.SysError("invalid stream response: " + data) | ||||
| 					continue | ||||
| 				} | ||||
| 				dataChan <- data | ||||
| @@ -184,7 +184,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { | ||||
| 						var streamResponse ChatCompletionsStreamResponse | ||||
| 						err = json.Unmarshal([]byte(data), &streamResponse) | ||||
| 						if err != nil { | ||||
| 							common.SysError("Error unmarshalling stream response: " + err.Error()) | ||||
| 							common.SysError("error unmarshalling stream response: " + err.Error()) | ||||
| 							return | ||||
| 						} | ||||
| 						for _, choice := range streamResponse.Choices { | ||||
| @@ -194,7 +194,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { | ||||
| 						var streamResponse CompletionsStreamResponse | ||||
| 						err = json.Unmarshal([]byte(data), &streamResponse) | ||||
| 						if err != nil { | ||||
| 							common.SysError("Error unmarshalling stream response: " + err.Error()) | ||||
| 							common.SysError("error unmarshalling stream response: " + err.Error()) | ||||
| 							return | ||||
| 						} | ||||
| 						for _, choice := range streamResponse.Choices { | ||||
| @@ -224,22 +224,22 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { | ||||
| 		}) | ||||
| 		err = resp.Body.Close() | ||||
| 		if err != nil { | ||||
| 			return errorWrapper(err, "close_response_body_failed", http.StatusOK) | ||||
| 			return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError) | ||||
| 		} | ||||
| 		return nil | ||||
| 	} else { | ||||
| 		if consumeQuota { | ||||
| 			responseBody, err := io.ReadAll(resp.Body) | ||||
| 			if err != nil { | ||||
| 				return errorWrapper(err, "read_response_body_failed", http.StatusOK) | ||||
| 				return errorWrapper(err, "read_response_body_failed", http.StatusInternalServerError) | ||||
| 			} | ||||
| 			err = resp.Body.Close() | ||||
| 			if err != nil { | ||||
| 				return errorWrapper(err, "close_response_body_failed", http.StatusOK) | ||||
| 				return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError) | ||||
| 			} | ||||
| 			err = json.Unmarshal(responseBody, &textResponse) | ||||
| 			if err != nil { | ||||
| 				return errorWrapper(err, "unmarshal_response_body_failed", http.StatusOK) | ||||
| 				return errorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError) | ||||
| 			} | ||||
| 			if textResponse.Error.Type != "" { | ||||
| 				return &OpenAIErrorWithStatusCode{ | ||||
| @@ -260,11 +260,11 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { | ||||
| 		c.Writer.WriteHeader(resp.StatusCode) | ||||
| 		_, err = io.Copy(c.Writer, resp.Body) | ||||
| 		if err != nil { | ||||
| 			return errorWrapper(err, "copy_response_body_failed", http.StatusOK) | ||||
| 			return errorWrapper(err, "copy_response_body_failed", http.StatusInternalServerError) | ||||
| 		} | ||||
| 		err = resp.Body.Close() | ||||
| 		if err != nil { | ||||
| 			return errorWrapper(err, "close_response_body_failed", http.StatusOK) | ||||
| 			return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError) | ||||
| 		} | ||||
| 		return nil | ||||
| 	} | ||||
|   | ||||
| @@ -118,7 +118,7 @@ func Relay(c *gin.Context) { | ||||
| 			"error": err.OpenAIError, | ||||
| 		}) | ||||
| 		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 | ||||
| 		if common.AutomaticDisableChannelEnabled && (err.Type == "insufficient_quota" || err.Code == "invalid_api_key") { | ||||
| 			channelId := c.GetInt("channel_id") | ||||
| @@ -135,7 +135,7 @@ func RelayNotImplemented(c *gin.Context) { | ||||
| 		Param:   "", | ||||
| 		Code:    "api_not_implemented", | ||||
| 	} | ||||
| 	c.JSON(http.StatusOK, gin.H{ | ||||
| 	c.JSON(http.StatusNotImplemented, gin.H{ | ||||
| 		"error": err, | ||||
| 	}) | ||||
| } | ||||
| @@ -147,7 +147,7 @@ func RelayNotFound(c *gin.Context) { | ||||
| 		Param:   "", | ||||
| 		Code:    "api_not_found", | ||||
| 	} | ||||
| 	c.JSON(http.StatusOK, gin.H{ | ||||
| 	c.JSON(http.StatusNotFound, gin.H{ | ||||
| 		"error": err, | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -2,7 +2,7 @@ version: '3.4' | ||||
|  | ||||
| services: | ||||
|   one-api: | ||||
|     image: ghcr.io/songquanpeng/one-api:latest | ||||
|     image: justsong/one-api:latest | ||||
|     container_name: one-api | ||||
|     restart: always | ||||
|     command: --log-dir /app/logs | ||||
| @@ -11,12 +11,24 @@ services: | ||||
|     volumes: | ||||
|       - ./data:/data | ||||
|       - ./logs:/app/logs | ||||
|     # environment: | ||||
|     #   REDIS_CONN_STRING: redis://default:redispw@localhost:49153 | ||||
|     #   SESSION_SECRET: random_string | ||||
|     #   SQL_DSN: root:123456@tcp(localhost:3306)/one-api | ||||
|     environment: | ||||
|       - SQL_DSN=root:123456@tcp(host.docker.internal:3306)/one-api  # 修改此行,或注释掉以使用 SQLite 作为数据库 | ||||
|       - REDIS_CONN_STRING=redis://redis | ||||
|       - SESSION_SECRET=random_string  # 修改为随机字符串 | ||||
|       - TZ=Asia/Shanghai | ||||
| #      - NODE_TYPE=slave  # 多机部署时从节点取消注释该行 | ||||
| #      - SYNC_FREQUENCY=60  # 需要定期从数据库加载数据时取消注释该行 | ||||
| #      - FRONTEND_BASE_URL=https://openai.justsong.cn  # 多机部署时从节点取消注释该行 | ||||
|  | ||||
|     depends_on: | ||||
|       - redis | ||||
|     healthcheck: | ||||
|       test: ["CMD-SHELL", "curl -s http://localhost:3000/api/status | grep -o '\"success\":\\s*true' | awk '{print $2}' | grep 'true'"] | ||||
|       test: [ "CMD-SHELL", "curl -s http://localhost:3000/api/status | grep -o '\"success\":\\s*true' | awk '{print $2}' | grep 'true'" ] | ||||
|       interval: 30s | ||||
|       timeout: 10s | ||||
|       retries: 3 | ||||
|  | ||||
|   redis: | ||||
|     image: redis:latest | ||||
|     container_name: redis | ||||
|     restart: always | ||||
|   | ||||
							
								
								
									
										26
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								main.go
									
									
									
									
									
								
							| @@ -6,8 +6,8 @@ import ( | ||||
| 	"github.com/gin-contrib/sessions/cookie" | ||||
| 	"github.com/gin-contrib/sessions/redis" | ||||
| 	"github.com/gin-gonic/gin" | ||||
| 	"log" | ||||
| 	"one-api/common" | ||||
| 	"one-api/controller" | ||||
| 	"one-api/middleware" | ||||
| 	"one-api/model" | ||||
| 	"one-api/router" | ||||
| @@ -30,19 +30,19 @@ func main() { | ||||
| 	// Initialize SQL Database | ||||
| 	err := model.InitDB() | ||||
| 	if err != nil { | ||||
| 		common.FatalLog(err) | ||||
| 		common.FatalLog("failed to initialize database: " + err.Error()) | ||||
| 	} | ||||
| 	defer func() { | ||||
| 		err := model.CloseDB() | ||||
| 		if err != nil { | ||||
| 			common.FatalLog(err) | ||||
| 			common.FatalLog("failed to close database: " + err.Error()) | ||||
| 		} | ||||
| 	}() | ||||
|  | ||||
| 	// Initialize Redis | ||||
| 	err = common.InitRedisClient() | ||||
| 	if err != nil { | ||||
| 		common.FatalLog(err) | ||||
| 		common.FatalLog("failed to initialize Redis: " + err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	// Initialize options | ||||
| @@ -53,13 +53,27 @@ func main() { | ||||
| 	if os.Getenv("SYNC_FREQUENCY") != "" { | ||||
| 		frequency, err := strconv.Atoi(os.Getenv("SYNC_FREQUENCY")) | ||||
| 		if err != nil { | ||||
| 			common.FatalLog(err) | ||||
| 			common.FatalLog("failed to parse SYNC_FREQUENCY: " + err.Error()) | ||||
| 		} | ||||
| 		go model.SyncOptions(frequency) | ||||
| 		if common.RedisEnabled { | ||||
| 			go model.SyncChannelCache(frequency) | ||||
| 		} | ||||
| 	} | ||||
| 	if os.Getenv("CHANNEL_UPDATE_FREQUENCY") != "" { | ||||
| 		frequency, err := strconv.Atoi(os.Getenv("CHANNEL_UPDATE_FREQUENCY")) | ||||
| 		if err != nil { | ||||
| 			common.FatalLog("failed to parse CHANNEL_UPDATE_FREQUENCY: " + err.Error()) | ||||
| 		} | ||||
| 		go controller.AutomaticallyUpdateChannels(frequency) | ||||
| 	} | ||||
| 	if os.Getenv("CHANNEL_TEST_FREQUENCY") != "" { | ||||
| 		frequency, err := strconv.Atoi(os.Getenv("CHANNEL_TEST_FREQUENCY")) | ||||
| 		if err != nil { | ||||
| 			common.FatalLog("failed to parse CHANNEL_TEST_FREQUENCY: " + err.Error()) | ||||
| 		} | ||||
| 		go controller.AutomaticallyTestChannels(frequency) | ||||
| 	} | ||||
|  | ||||
| 	// Initialize HTTP server | ||||
| 	server := gin.Default() | ||||
| @@ -84,6 +98,6 @@ func main() { | ||||
| 	} | ||||
| 	err = server.Run(":" + port) | ||||
| 	if err != nil { | ||||
| 		log.Println(err) | ||||
| 		common.FatalLog("failed to start HTTP server: " + err.Error()) | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -91,7 +91,7 @@ func TokenAuth() func(c *gin.Context) { | ||||
| 		key = parts[0] | ||||
| 		token, err := model.ValidateUserToken(key) | ||||
| 		if err != nil { | ||||
| 			c.JSON(http.StatusOK, gin.H{ | ||||
| 			c.JSON(http.StatusUnauthorized, gin.H{ | ||||
| 				"error": gin.H{ | ||||
| 					"message": err.Error(), | ||||
| 					"type":    "one_api_error", | ||||
| @@ -101,7 +101,7 @@ func TokenAuth() func(c *gin.Context) { | ||||
| 			return | ||||
| 		} | ||||
| 		if !model.CacheIsUserEnabled(token.UserId) { | ||||
| 			c.JSON(http.StatusOK, gin.H{ | ||||
| 			c.JSON(http.StatusForbidden, gin.H{ | ||||
| 				"error": gin.H{ | ||||
| 					"message": "用户已被封禁", | ||||
| 					"type":    "one_api_error", | ||||
| @@ -123,7 +123,7 @@ func TokenAuth() func(c *gin.Context) { | ||||
| 			if model.IsAdmin(token.UserId) { | ||||
| 				c.Set("channelId", parts[1]) | ||||
| 			} else { | ||||
| 				c.JSON(http.StatusOK, gin.H{ | ||||
| 				c.JSON(http.StatusForbidden, gin.H{ | ||||
| 					"error": gin.H{ | ||||
| 						"message": "普通用户不支持指定渠道", | ||||
| 						"type":    "one_api_error", | ||||
|   | ||||
| @@ -24,7 +24,7 @@ func Distribute() func(c *gin.Context) { | ||||
| 		if ok { | ||||
| 			id, err := strconv.Atoi(channelId.(string)) | ||||
| 			if err != nil { | ||||
| 				c.JSON(http.StatusOK, gin.H{ | ||||
| 				c.JSON(http.StatusBadRequest, gin.H{ | ||||
| 					"error": gin.H{ | ||||
| 						"message": "无效的渠道 ID", | ||||
| 						"type":    "one_api_error", | ||||
| @@ -35,7 +35,7 @@ func Distribute() func(c *gin.Context) { | ||||
| 			} | ||||
| 			channel, err = model.GetChannelById(id, true) | ||||
| 			if err != nil { | ||||
| 				c.JSON(200, gin.H{ | ||||
| 				c.JSON(http.StatusBadRequest, gin.H{ | ||||
| 					"error": gin.H{ | ||||
| 						"message": "无效的渠道 ID", | ||||
| 						"type":    "one_api_error", | ||||
| @@ -45,7 +45,7 @@ func Distribute() func(c *gin.Context) { | ||||
| 				return | ||||
| 			} | ||||
| 			if channel.Status != common.ChannelStatusEnabled { | ||||
| 				c.JSON(200, gin.H{ | ||||
| 				c.JSON(http.StatusForbidden, gin.H{ | ||||
| 					"error": gin.H{ | ||||
| 						"message": "该渠道已被禁用", | ||||
| 						"type":    "one_api_error", | ||||
| @@ -59,7 +59,7 @@ func Distribute() func(c *gin.Context) { | ||||
| 			var modelRequest ModelRequest | ||||
| 			err := common.UnmarshalBodyReusable(c, &modelRequest) | ||||
| 			if err != nil { | ||||
| 				c.JSON(200, gin.H{ | ||||
| 				c.JSON(http.StatusBadRequest, gin.H{ | ||||
| 					"error": gin.H{ | ||||
| 						"message": "无效的请求", | ||||
| 						"type":    "one_api_error", | ||||
| @@ -75,7 +75,7 @@ func Distribute() func(c *gin.Context) { | ||||
| 			} | ||||
| 			channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model) | ||||
| 			if err != nil { | ||||
| 				c.JSON(200, gin.H{ | ||||
| 				c.JSON(http.StatusServiceUnavailable, gin.H{ | ||||
| 					"error": gin.H{ | ||||
| 						"message": "无可用渠道", | ||||
| 						"type":    "one_api_error", | ||||
|   | ||||
| @@ -137,13 +137,13 @@ func InitChannelCache() { | ||||
| 	channelSyncLock.Lock() | ||||
| 	group2model2channels = newGroup2model2channels | ||||
| 	channelSyncLock.Unlock() | ||||
| 	common.SysLog("Channels synced from database") | ||||
| 	common.SysLog("channels synced from database") | ||||
| } | ||||
|  | ||||
| func SyncChannelCache(frequency int) { | ||||
| 	for { | ||||
| 		time.Sleep(time.Duration(frequency) * time.Second) | ||||
| 		common.SysLog("Syncing channels from database") | ||||
| 		common.SysLog("syncing channels from database") | ||||
| 		InitChannelCache() | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -8,7 +8,7 @@ import ( | ||||
| type Channel struct { | ||||
| 	Id                 int     `json:"id"` | ||||
| 	Type               int     `json:"type" gorm:"default:0"` | ||||
| 	Key                string  `json:"key" gorm:"not null"` | ||||
| 	Key                string  `json:"key" gorm:"not null;index"` | ||||
| 	Status             int     `json:"status" gorm:"default:1"` | ||||
| 	Name               string  `json:"name" gorm:"index"` | ||||
| 	Weight             int     `json:"weight"` | ||||
| @@ -36,7 +36,7 @@ func GetAllChannels(startIdx int, num int, selectAll bool) ([]*Channel, error) { | ||||
| } | ||||
|  | ||||
| func SearchChannels(keyword string) (channels []*Channel, err error) { | ||||
| 	err = DB.Omit("key").Where("id = ? or name LIKE ?", keyword, keyword+"%").Find(&channels).Error | ||||
| 	err = DB.Omit("key").Where("id = ? or name LIKE ? or key = ?", keyword, keyword+"%", keyword).Find(&channels).Error | ||||
| 	return channels, err | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -42,19 +42,24 @@ func InitDB() (err error) { | ||||
| 	var db *gorm.DB | ||||
| 	if os.Getenv("SQL_DSN") != "" { | ||||
| 		// Use MySQL | ||||
| 		common.SysLog("using MySQL as database") | ||||
| 		db, err = gorm.Open(mysql.Open(os.Getenv("SQL_DSN")), &gorm.Config{ | ||||
| 			PrepareStmt: true, // precompile SQL | ||||
| 		}) | ||||
| 	} else { | ||||
| 		// Use SQLite | ||||
| 		common.SysLog("SQL_DSN not set, using SQLite as database") | ||||
| 		common.UsingSQLite = true | ||||
| 		db, err = gorm.Open(sqlite.Open(common.SQLitePath), &gorm.Config{ | ||||
| 			PrepareStmt: true, // precompile SQL | ||||
| 		}) | ||||
| 		common.SysLog("SQL_DSN not set, using SQLite as database") | ||||
| 	} | ||||
| 	common.SysLog("database connected") | ||||
| 	if err == nil { | ||||
| 		DB = db | ||||
| 		if !common.IsMasterNode { | ||||
| 			return nil | ||||
| 		} | ||||
| 		err := db.AutoMigrate(&Channel{}) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| @@ -83,6 +88,7 @@ func InitDB() (err error) { | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		common.SysLog("database migrated") | ||||
| 		err = createRootAccountIfNeed() | ||||
| 		return err | ||||
| 	} else { | ||||
|   | ||||
| @@ -36,6 +36,7 @@ func InitOptionMap() { | ||||
| 	common.OptionMap["AutomaticDisableChannelEnabled"] = strconv.FormatBool(common.AutomaticDisableChannelEnabled) | ||||
| 	common.OptionMap["LogConsumeEnabled"] = strconv.FormatBool(common.LogConsumeEnabled) | ||||
| 	common.OptionMap["DisplayInCurrencyEnabled"] = strconv.FormatBool(common.DisplayInCurrencyEnabled) | ||||
| 	common.OptionMap["DisplayTokenStatEnabled"] = strconv.FormatBool(common.DisplayTokenStatEnabled) | ||||
| 	common.OptionMap["ChannelDisableThreshold"] = strconv.FormatFloat(common.ChannelDisableThreshold, 'f', -1, 64) | ||||
| 	common.OptionMap["SMTPServer"] = "" | ||||
| 	common.OptionMap["SMTPFrom"] = "" | ||||
| @@ -75,7 +76,7 @@ func loadOptionsFromDatabase() { | ||||
| 	for _, option := range options { | ||||
| 		err := updateOptionMap(option.Key, option.Value) | ||||
| 		if err != nil { | ||||
| 			common.SysError("Failed to update option map: " + err.Error()) | ||||
| 			common.SysError("failed to update option map: " + err.Error()) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @@ -83,7 +84,7 @@ func loadOptionsFromDatabase() { | ||||
| func SyncOptions(frequency int) { | ||||
| 	for { | ||||
| 		time.Sleep(time.Duration(frequency) * time.Second) | ||||
| 		common.SysLog("Syncing options from database") | ||||
| 		common.SysLog("syncing options from database") | ||||
| 		loadOptionsFromDatabase() | ||||
| 	} | ||||
| } | ||||
| @@ -144,6 +145,8 @@ func updateOptionMap(key string, value string) (err error) { | ||||
| 			common.LogConsumeEnabled = boolValue | ||||
| 		case "DisplayInCurrencyEnabled": | ||||
| 			common.DisplayInCurrencyEnabled = boolValue | ||||
| 		case "DisplayTokenStatEnabled": | ||||
| 			common.DisplayTokenStatEnabled = boolValue | ||||
| 		} | ||||
| 	} | ||||
| 	switch key { | ||||
|   | ||||
| @@ -64,7 +64,7 @@ func Redeem(key string, userId int) (quota int, err error) { | ||||
| 		redemption.Status = common.RedemptionCodeStatusUsed | ||||
| 		err := redemption.SelectUpdate() | ||||
| 		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))) | ||||
| 	}() | ||||
|   | ||||
| @@ -18,6 +18,7 @@ type Token struct { | ||||
| 	ExpiredTime    int64  `json:"expired_time" gorm:"bigint;default:-1"` // -1 means never expired | ||||
| 	RemainQuota    int    `json:"remain_quota" gorm:"default:0"` | ||||
| 	UnlimitedQuota bool   `json:"unlimited_quota" gorm:"default:false"` | ||||
| 	UsedQuota      int    `json:"used_quota" gorm:"default:0"` // used quota | ||||
| } | ||||
|  | ||||
| func GetAllUserTokens(userId int, startIdx int, num int) ([]*Token, error) { | ||||
| @@ -45,7 +46,7 @@ func ValidateUserToken(key string) (token *Token, err error) { | ||||
| 			token.Status = common.TokenStatusExpired | ||||
| 			err := token.SelectUpdate() | ||||
| 			if err != nil { | ||||
| 				common.SysError("更新令牌状态失败:" + err.Error()) | ||||
| 				common.SysError("failed to update token status" + err.Error()) | ||||
| 			} | ||||
| 			return nil, errors.New("该令牌已过期") | ||||
| 		} | ||||
| @@ -53,7 +54,7 @@ func ValidateUserToken(key string) (token *Token, err error) { | ||||
| 			token.Status = common.TokenStatusExhausted | ||||
| 			err := token.SelectUpdate() | ||||
| 			if err != nil { | ||||
| 				common.SysError("更新令牌状态失败:" + err.Error()) | ||||
| 				common.SysError("failed to update token status" + err.Error()) | ||||
| 			} | ||||
| 			return nil, errors.New("该令牌额度已用尽") | ||||
| 		} | ||||
| @@ -61,7 +62,7 @@ func ValidateUserToken(key string) (token *Token, err error) { | ||||
| 			token.AccessedTime = common.GetTimestamp() | ||||
| 			err := token.SelectUpdate() | ||||
| 			if err != nil { | ||||
| 				common.SysError("更新令牌失败:" + err.Error()) | ||||
| 				common.SysError("failed to update token" + err.Error()) | ||||
| 			} | ||||
| 		}() | ||||
| 		return token, nil | ||||
| @@ -130,7 +131,12 @@ func IncreaseTokenQuota(id int, quota int) (err error) { | ||||
| 	if quota < 0 { | ||||
| 		return errors.New("quota 不能为负数!") | ||||
| 	} | ||||
| 	err = DB.Model(&Token{}).Where("id = ?", id).Update("remain_quota", gorm.Expr("remain_quota + ?", quota)).Error | ||||
| 	err = DB.Model(&Token{}).Where("id = ?", id).Updates( | ||||
| 		map[string]interface{}{ | ||||
| 			"remain_quota": gorm.Expr("remain_quota + ?", quota), | ||||
| 			"used_quota":   gorm.Expr("used_quota - ?", quota), | ||||
| 		}, | ||||
| 	).Error | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| @@ -138,7 +144,12 @@ func DecreaseTokenQuota(id int, quota int) (err error) { | ||||
| 	if quota < 0 { | ||||
| 		return errors.New("quota 不能为负数!") | ||||
| 	} | ||||
| 	err = DB.Model(&Token{}).Where("id = ?", id).Update("remain_quota", gorm.Expr("remain_quota - ?", quota)).Error | ||||
| 	err = DB.Model(&Token{}).Where("id = ?", id).Updates( | ||||
| 		map[string]interface{}{ | ||||
| 			"remain_quota": gorm.Expr("remain_quota - ?", quota), | ||||
| 			"used_quota":   gorm.Expr("used_quota + ?", quota), | ||||
| 		}, | ||||
| 	).Error | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| @@ -166,7 +177,7 @@ func PreConsumeTokenQuota(tokenId int, quota int) (err error) { | ||||
| 		go func() { | ||||
| 			email, err := GetUserEmail(token.UserId) | ||||
| 			if err != nil { | ||||
| 				common.SysError("获取用户邮箱失败:" + err.Error()) | ||||
| 				common.SysError("failed to fetch user email: " + err.Error()) | ||||
| 			} | ||||
| 			prompt := "您的额度即将用尽" | ||||
| 			if noMoreQuota { | ||||
| @@ -177,7 +188,7 @@ func PreConsumeTokenQuota(tokenId int, quota int) (err error) { | ||||
| 				err = common.SendEmail(prompt, email, | ||||
| 					fmt.Sprintf("%s,当前剩余额度为 %d,为了不影响您的使用,请及时充值。<br/>充值链接:<a href='%s'>%s</a>", prompt, userQuota, topUpLink, topUpLink)) | ||||
| 				if err != nil { | ||||
| 					common.SysError("发送邮件失败:" + err.Error()) | ||||
| 					common.SysError("failed to send email" + err.Error()) | ||||
| 				} | ||||
| 			} | ||||
| 		}() | ||||
|   | ||||
| @@ -220,7 +220,7 @@ func IsAdmin(userId int) bool { | ||||
| 	var user User | ||||
| 	err := DB.Where("id = ?", userId).Select("role").Find(&user).Error | ||||
| 	if err != nil { | ||||
| 		common.SysError("No such user " + err.Error()) | ||||
| 		common.SysError("no such user " + err.Error()) | ||||
| 		return false | ||||
| 	} | ||||
| 	return user.Role >= common.RoleAdminUser | ||||
| @@ -233,7 +233,7 @@ func IsUserEnabled(userId int) bool { | ||||
| 	var user User | ||||
| 	err := DB.Where("id = ?", userId).Select("status").Find(&user).Error | ||||
| 	if err != nil { | ||||
| 		common.SysError("No such user " + err.Error()) | ||||
| 		common.SysError("no such user " + err.Error()) | ||||
| 		return false | ||||
| 	} | ||||
| 	return user.Status == common.UserStatusEnabled | ||||
| @@ -300,6 +300,6 @@ func UpdateUserUsedQuotaAndRequestCount(id int, quota int) { | ||||
| 		}, | ||||
| 	).Error | ||||
| 	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()) | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -1,11 +1,16 @@ | ||||
| # File path: /etc/systemd/system/one-api.service | ||||
| # sudo systemctl daemon-reload | ||||
| # sudo systemctl start one-api | ||||
| # sudo systemctl enable one-api | ||||
| # sudo systemctl status one-api | ||||
| [Unit] | ||||
| Description=One API Service | ||||
| After=network.target | ||||
|  | ||||
| [Service] | ||||
| User=yourusername                  # 守护进程用户名 | ||||
| WorkingDirectory=/path/to/One-API  # One API运行路径 | ||||
| ExecStart=/path/to/One-API/one-api --port 3000 --log-dir /path/to/One-API/logs  # 端口 | ||||
| User=ubuntu  # 注意修改用户名 | ||||
| WorkingDirectory=/path/to/one-api  # 注意修改路径 | ||||
| ExecStart=/path/to/one-api/one-api --port 3000 --log-dir /path/to/one-api/logs  # 注意修改路径和端口号 | ||||
| Restart=always | ||||
| RestartSec=5 | ||||
|  | ||||
|   | ||||
| @@ -238,9 +238,17 @@ const ChannelsTable = () => { | ||||
|     if (channels.length === 0) return; | ||||
|     setLoading(true); | ||||
|     let sortedChannels = [...channels]; | ||||
|     sortedChannels.sort((a, b) => { | ||||
|       return ('' + a[key]).localeCompare(b[key]); | ||||
|     }); | ||||
|     if (typeof sortedChannels[0][key] === 'string'){ | ||||
|       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) { | ||||
|       sortedChannels.reverse(); | ||||
|     } | ||||
| @@ -255,7 +263,7 @@ const ChannelsTable = () => { | ||||
|           icon='search' | ||||
|           fluid | ||||
|           iconPosition='left' | ||||
|           placeholder='搜索渠道的 ID 和名称 ...' | ||||
|           placeholder='搜索渠道的 ID,名称和密钥 ...' | ||||
|           value={searchKeyword} | ||||
|           loading={searching} | ||||
|           onChange={handleKeywordChange} | ||||
|   | ||||
| @@ -17,7 +17,8 @@ const OperationSetting = () => { | ||||
|     AutomaticDisableChannelEnabled: '', | ||||
|     ChannelDisableThreshold: 0, | ||||
|     LogConsumeEnabled: '', | ||||
|     DisplayInCurrencyEnabled: '' | ||||
|     DisplayInCurrencyEnabled: '', | ||||
|     DisplayTokenStatEnabled: '' | ||||
|   }); | ||||
|   const [originInputs, setOriginInputs] = useState({}); | ||||
|   let [loading, setLoading] = useState(false); | ||||
| @@ -177,6 +178,12 @@ const OperationSetting = () => { | ||||
|               name='DisplayInCurrencyEnabled' | ||||
|               onChange={handleInputChange} | ||||
|             /> | ||||
|             <Form.Checkbox | ||||
|               checked={inputs.DisplayTokenStatEnabled === 'true'} | ||||
|               label='Billing 相关 API 显示令牌额度而非用户额度' | ||||
|               name='DisplayTokenStatEnabled' | ||||
|               onChange={handleInputChange} | ||||
|             /> | ||||
|           </Form.Group> | ||||
|           <Form.Button onClick={() => { | ||||
|             submitConfig('general').then(); | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| import React, { useEffect, useState } from 'react'; | ||||
| import { Button, Form, Grid, Header, Image, Segment } from 'semantic-ui-react'; | ||||
| import { API, copy, showError, showSuccess } from '../helpers'; | ||||
| import { API, copy, showError, showInfo, showNotice, showSuccess } from '../helpers'; | ||||
| import { useSearchParams } from 'react-router-dom'; | ||||
|  | ||||
| const PasswordResetConfirm = () => { | ||||
| @@ -33,7 +33,7 @@ const PasswordResetConfirm = () => { | ||||
|     if (success) { | ||||
|       let password = res.data.data; | ||||
|       await copy(password); | ||||
|       showSuccess(`密码已重置并已复制到剪贴板:${password}`); | ||||
|       showNotice(`密码已重置并已复制到剪贴板:${password}`); | ||||
|     } else { | ||||
|       showError(message); | ||||
|     } | ||||
|   | ||||
| @@ -181,13 +181,21 @@ const TokensTable = () => { | ||||
|             > | ||||
|               状态 | ||||
|             </Table.HeaderCell> | ||||
|             <Table.HeaderCell | ||||
|               style={{ cursor: 'pointer' }} | ||||
|               onClick={() => { | ||||
|                 sortToken('used_quota'); | ||||
|               }} | ||||
|             > | ||||
|               已用额度 | ||||
|             </Table.HeaderCell> | ||||
|             <Table.HeaderCell | ||||
|               style={{ cursor: 'pointer' }} | ||||
|               onClick={() => { | ||||
|                 sortToken('remain_quota'); | ||||
|               }} | ||||
|             > | ||||
|               额度 | ||||
|               剩余额度 | ||||
|             </Table.HeaderCell> | ||||
|             <Table.HeaderCell | ||||
|               style={{ cursor: 'pointer' }} | ||||
| @@ -221,6 +229,7 @@ const TokensTable = () => { | ||||
|                 <Table.Row key={token.id}> | ||||
|                   <Table.Cell>{token.name ? token.name : '无'}</Table.Cell> | ||||
|                   <Table.Cell>{renderStatus(token.status)}</Table.Cell> | ||||
|                   <Table.Cell>{renderQuota(token.used_quota)}</Table.Cell> | ||||
|                   <Table.Cell>{token.unlimited_quota ? '无限制' : renderQuota(token.remain_quota, 2)}</Table.Cell> | ||||
|                   <Table.Cell>{renderTimestamp(token.created_time)}</Table.Cell> | ||||
|                   <Table.Cell>{token.expired_time === -1 ? '永不过期' : renderTimestamp(token.expired_time)}</Table.Cell> | ||||
|   | ||||
| @@ -32,15 +32,15 @@ const EditChannel = () => { | ||||
|     let res = await API.get(`/api/channel/${channelId}`); | ||||
|     const { success, message, data } = res.data; | ||||
|     if (success) { | ||||
|       if (data.models === "") { | ||||
|         data.models = [] | ||||
|       if (data.models === '') { | ||||
|         data.models = []; | ||||
|       } else { | ||||
|         data.models = data.models.split(",") | ||||
|         data.models = data.models.split(','); | ||||
|       } | ||||
|       if (data.group === "") { | ||||
|         data.groups = [] | ||||
|       if (data.group === '') { | ||||
|         data.groups = []; | ||||
|       } else { | ||||
|         data.groups = data.group.split(",") | ||||
|         data.groups = data.group.split(','); | ||||
|       } | ||||
|       setInputs(data); | ||||
|     } else { | ||||
| @@ -55,10 +55,10 @@ const EditChannel = () => { | ||||
|       setModelOptions(res.data.data.map((model) => ({ | ||||
|         key: model.id, | ||||
|         text: model.id, | ||||
|         value: 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)); | ||||
|       setBasicModels(res.data.data.filter((model) => !model.id.startsWith('gpt-4')).map((model) => model.id)); | ||||
|     } catch (error) { | ||||
|       showError(error.message); | ||||
|     } | ||||
| @@ -70,7 +70,7 @@ const EditChannel = () => { | ||||
|       setGroupOptions(res.data.data.map((group) => ({ | ||||
|         key: group, | ||||
|         text: group, | ||||
|         value: group, | ||||
|         value: group | ||||
|       }))); | ||||
|     } catch (error) { | ||||
|       showError(error.message); | ||||
| @@ -90,6 +90,10 @@ const EditChannel = () => { | ||||
|       showInfo('请填写渠道名称和渠道密钥!'); | ||||
|       return; | ||||
|     } | ||||
|     if (inputs.models.length === 0) { | ||||
|       showInfo('请至少选择一个模型!'); | ||||
|       return; | ||||
|     } | ||||
|     let localInputs = inputs; | ||||
|     if (localInputs.base_url.endsWith('/')) { | ||||
|       localInputs.base_url = localInputs.base_url.slice(0, localInputs.base_url.length - 1); | ||||
| @@ -98,8 +102,8 @@ const EditChannel = () => { | ||||
|       localInputs.other = '2023-03-15-preview'; | ||||
|     } | ||||
|     let res; | ||||
|     localInputs.models = localInputs.models.join(",") | ||||
|     localInputs.group = localInputs.groups.join(",") | ||||
|     localInputs.models = localInputs.models.join(','); | ||||
|     localInputs.group = localInputs.groups.join(','); | ||||
|     if (isEdit) { | ||||
|       res = await API.put(`/api/channel/`, { ...localInputs, id: parseInt(channelId) }); | ||||
|     } else { | ||||
| @@ -181,9 +185,9 @@ const EditChannel = () => { | ||||
|             inputs.type !== 3 && inputs.type !== 8 && ( | ||||
|               <Form.Field> | ||||
|                 <Form.Input | ||||
|                   label='Base URL' | ||||
|                   label='镜像' | ||||
|                   name='base_url' | ||||
|                   placeholder={'请输入自定义 Base URL,格式为:https://domain.com,可不填,不填使用渠道默认值'} | ||||
|                   placeholder={'请输入镜像站地址,格式为:https://domain.com,可不填,不填则使用渠道默认值'} | ||||
|                   onChange={handleInputChange} | ||||
|                   value={inputs.base_url} | ||||
|                   autoComplete='new-password' | ||||
| @@ -231,28 +235,17 @@ const EditChannel = () => { | ||||
|               options={modelOptions} | ||||
|             /> | ||||
|           </Form.Field> | ||||
|           <div style={{ lineHeight: '40px', marginBottom: '12px'}}> | ||||
|           <div style={{ lineHeight: '40px', marginBottom: '12px' }}> | ||||
|             <Button type={'button'} onClick={() => { | ||||
|               handleInputChange(null, { name: 'models', value: basicModels }); | ||||
|             }}>填入基础模型</Button> | ||||
|             <Button type={'button'} onClick={() => { | ||||
|               handleInputChange(null, { name: 'models', value: fullModels }); | ||||
|             }}>填入所有模型</Button> | ||||
|             <Button type={'button'} onClick={() => { | ||||
|               handleInputChange(null, { name: 'models', value: [] }); | ||||
|             }}>清除所有模型</Button> | ||||
|           </div> | ||||
|           { | ||||
|             inputs.type === 1 && ( | ||||
|               <Form.Field> | ||||
|                 <Form.Input | ||||
|                   label='代理' | ||||
|                   name='base_url' | ||||
|                   placeholder={'请输入 OpenAI API 代理地址,如果不需要请留空,格式为:https://api.openai.com'} | ||||
|                   onChange={handleInputChange} | ||||
|                   value={inputs.base_url} | ||||
|                   autoComplete='new-password' | ||||
|                 /> | ||||
|               </Form.Field> | ||||
|             ) | ||||
|           } | ||||
|           { | ||||
|             batch ? <Form.Field> | ||||
|               <Form.TextArea | ||||
|   | ||||
		Reference in New Issue
	
	Block a user