mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 15:53:42 +08:00 
			
		
		
		
	Compare commits
	
		
			8 Commits
		
	
	
		
			v0.4.7-alp
			...
			v0.4.8-alp
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					6f05128368 | ||
| 
						 | 
					9b178a28a3 | ||
| 
						 | 
					4a6a7f4635 | ||
| 
						 | 
					6b1a24d650 | ||
| 
						 | 
					94ba3dd024 | ||
| 
						 | 
					f6eb4e5628 | ||
| 
						 | 
					57bd907f83 | ||
| 
						 | 
					dd8e8d5ee8 | 
@@ -32,6 +32,9 @@ func GetSubscription(c *gin.Context) {
 | 
			
		||||
	if common.DisplayInCurrencyEnabled {
 | 
			
		||||
		amount /= common.QuotaPerUnit
 | 
			
		||||
	}
 | 
			
		||||
	if token != nil && token.UnlimitedQuota {
 | 
			
		||||
		amount = 100000000
 | 
			
		||||
	}
 | 
			
		||||
	subscription := OpenAISubscriptionResponse{
 | 
			
		||||
		Object:             "billing_subscription",
 | 
			
		||||
		HasPaymentMethod:   true,
 | 
			
		||||
 
 | 
			
		||||
@@ -224,6 +224,24 @@ func init() {
 | 
			
		||||
			Root:       "text-moderation-stable",
 | 
			
		||||
			Parent:     nil,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			Id:         "text-davinci-edit-001",
 | 
			
		||||
			Object:     "model",
 | 
			
		||||
			Created:    1677649963,
 | 
			
		||||
			OwnedBy:    "openai",
 | 
			
		||||
			Permission: permission,
 | 
			
		||||
			Root:       "text-davinci-edit-001",
 | 
			
		||||
			Parent:     nil,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			Id:         "code-davinci-edit-001",
 | 
			
		||||
			Object:     "model",
 | 
			
		||||
			Created:    1677649963,
 | 
			
		||||
			OwnedBy:    "openai",
 | 
			
		||||
			Permission: permission,
 | 
			
		||||
			Root:       "code-davinci-edit-001",
 | 
			
		||||
			Parent:     nil,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	openAIModelsMap = make(map[string]OpenAIModels)
 | 
			
		||||
	for _, model := range openAIModels {
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ import (
 | 
			
		||||
	"bufio"
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"io"
 | 
			
		||||
@@ -26,9 +27,32 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
 | 
			
		||||
			return errorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if relayMode == RelayModeModeration && textRequest.Model == "" {
 | 
			
		||||
	if relayMode == RelayModeModerations && textRequest.Model == "" {
 | 
			
		||||
		textRequest.Model = "text-moderation-latest"
 | 
			
		||||
	}
 | 
			
		||||
	// request validation
 | 
			
		||||
	if textRequest.Model == "" {
 | 
			
		||||
		return errorWrapper(errors.New("model is required"), "required_field_missing", http.StatusBadRequest)
 | 
			
		||||
	}
 | 
			
		||||
	switch relayMode {
 | 
			
		||||
	case RelayModeCompletions:
 | 
			
		||||
		if textRequest.Prompt == "" {
 | 
			
		||||
			return errorWrapper(errors.New("field prompt is required"), "required_field_missing", http.StatusBadRequest)
 | 
			
		||||
		}
 | 
			
		||||
	case RelayModeChatCompletions:
 | 
			
		||||
		if textRequest.Messages == nil || len(textRequest.Messages) == 0 {
 | 
			
		||||
			return errorWrapper(errors.New("field messages is required"), "required_field_missing", http.StatusBadRequest)
 | 
			
		||||
		}
 | 
			
		||||
	case RelayModeEmbeddings:
 | 
			
		||||
	case RelayModeModerations:
 | 
			
		||||
		if textRequest.Input == "" {
 | 
			
		||||
			return errorWrapper(errors.New("field input is required"), "required_field_missing", http.StatusBadRequest)
 | 
			
		||||
		}
 | 
			
		||||
	case RelayModeEdits:
 | 
			
		||||
		if textRequest.Instruction == "" {
 | 
			
		||||
			return errorWrapper(errors.New("field instruction is required"), "required_field_missing", http.StatusBadRequest)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	baseURL := common.ChannelBaseURLs[channelType]
 | 
			
		||||
	requestURL := c.Request.URL.String()
 | 
			
		||||
	if c.GetString("base_url") != "" {
 | 
			
		||||
@@ -64,7 +88,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
 | 
			
		||||
		promptTokens = countTokenMessages(textRequest.Messages, textRequest.Model)
 | 
			
		||||
	case RelayModeCompletions:
 | 
			
		||||
		promptTokens = countTokenInput(textRequest.Prompt, textRequest.Model)
 | 
			
		||||
	case RelayModeModeration:
 | 
			
		||||
	case RelayModeModerations:
 | 
			
		||||
		promptTokens = countTokenInput(textRequest.Input, textRequest.Model)
 | 
			
		||||
	}
 | 
			
		||||
	preConsumedTokens := common.PreConsumedQuota
 | 
			
		||||
@@ -124,7 +148,10 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if consumeQuota {
 | 
			
		||||
			quota := 0
 | 
			
		||||
			completionRatio := 1.333333 // default for gpt-3
 | 
			
		||||
			completionRatio := 1.0
 | 
			
		||||
			if strings.HasPrefix(textRequest.Model, "gpt-3.5") {
 | 
			
		||||
				completionRatio = 1.333333
 | 
			
		||||
			}
 | 
			
		||||
			if strings.HasPrefix(textRequest.Model, "gpt-4") {
 | 
			
		||||
				completionRatio = 2
 | 
			
		||||
			}
 | 
			
		||||
@@ -139,17 +166,25 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
 | 
			
		||||
			if ratio != 0 && quota <= 0 {
 | 
			
		||||
				quota = 1
 | 
			
		||||
			}
 | 
			
		||||
			totalTokens := promptTokens + completionTokens
 | 
			
		||||
			if totalTokens == 0 {
 | 
			
		||||
				// in this case, must be some error happened
 | 
			
		||||
				// we cannot just return, because we may have to return the pre-consumed quota
 | 
			
		||||
				quota = 0
 | 
			
		||||
			}
 | 
			
		||||
			quotaDelta := quota - preConsumedQuota
 | 
			
		||||
			err := model.PostConsumeTokenQuota(tokenId, quotaDelta)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				common.SysError("error consuming token remain quota: " + err.Error())
 | 
			
		||||
			}
 | 
			
		||||
			tokenName := c.GetString("token_name")
 | 
			
		||||
			logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f", modelRatio, groupRatio)
 | 
			
		||||
			model.RecordConsumeLog(userId, promptTokens, completionTokens, textRequest.Model, tokenName, quota, logContent)
 | 
			
		||||
			model.UpdateUserUsedQuotaAndRequestCount(userId, quota)
 | 
			
		||||
			channelId := c.GetInt("channel_id")
 | 
			
		||||
			model.UpdateChannelUsedQuota(channelId, quota)
 | 
			
		||||
			if quota != 0 {
 | 
			
		||||
				tokenName := c.GetString("token_name")
 | 
			
		||||
				logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f,补全倍率 %.2f", modelRatio, groupRatio, completionRatio)
 | 
			
		||||
				model.RecordConsumeLog(userId, promptTokens, completionTokens, textRequest.Model, tokenName, quota, logContent)
 | 
			
		||||
				model.UpdateUserUsedQuotaAndRequestCount(userId, quota)
 | 
			
		||||
				channelId := c.GetInt("channel_id")
 | 
			
		||||
				model.UpdateChannelUsedQuota(channelId, quota)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -19,8 +19,9 @@ const (
 | 
			
		||||
	RelayModeChatCompletions
 | 
			
		||||
	RelayModeCompletions
 | 
			
		||||
	RelayModeEmbeddings
 | 
			
		||||
	RelayModeModeration
 | 
			
		||||
	RelayModeModerations
 | 
			
		||||
	RelayModeImagesGenerations
 | 
			
		||||
	RelayModeEdits
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// https://platform.openai.com/docs/api-reference/chat
 | 
			
		||||
@@ -35,6 +36,7 @@ type GeneralOpenAIRequest struct {
 | 
			
		||||
	TopP        float64   `json:"top_p"`
 | 
			
		||||
	N           int       `json:"n"`
 | 
			
		||||
	Input       any       `json:"input"`
 | 
			
		||||
	Instruction string    `json:"instruction"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ChatRequest struct {
 | 
			
		||||
@@ -99,9 +101,11 @@ func Relay(c *gin.Context) {
 | 
			
		||||
	} else if strings.HasPrefix(c.Request.URL.Path, "/v1/embeddings") {
 | 
			
		||||
		relayMode = RelayModeEmbeddings
 | 
			
		||||
	} else if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
 | 
			
		||||
		relayMode = RelayModeModeration
 | 
			
		||||
		relayMode = RelayModeModerations
 | 
			
		||||
	} else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
 | 
			
		||||
		relayMode = RelayModeImagesGenerations
 | 
			
		||||
	} else if strings.HasPrefix(c.Request.URL.Path, "/v1/edits") {
 | 
			
		||||
		relayMode = RelayModeEdits
 | 
			
		||||
	}
 | 
			
		||||
	var err *OpenAIErrorWithStatusCode
 | 
			
		||||
	switch relayMode {
 | 
			
		||||
 
 | 
			
		||||
@@ -19,7 +19,7 @@ func SetRelayRouter(router *gin.Engine) {
 | 
			
		||||
	{
 | 
			
		||||
		relayV1Router.POST("/completions", controller.Relay)
 | 
			
		||||
		relayV1Router.POST("/chat/completions", controller.Relay)
 | 
			
		||||
		relayV1Router.POST("/edits", controller.RelayNotImplemented)
 | 
			
		||||
		relayV1Router.POST("/edits", controller.Relay)
 | 
			
		||||
		relayV1Router.POST("/images/generations", controller.RelayNotImplemented)
 | 
			
		||||
		relayV1Router.POST("/images/edits", controller.RelayNotImplemented)
 | 
			
		||||
		relayV1Router.POST("/images/variations", controller.RelayNotImplemented)
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react';
 | 
			
		||||
import { Button, Form, Header, Segment } from 'semantic-ui-react';
 | 
			
		||||
import { useParams } from 'react-router-dom';
 | 
			
		||||
import { API, showError, showSuccess } from '../../helpers';
 | 
			
		||||
import { renderQuota, renderQuotaWithPrompt } from '../../helpers/render';
 | 
			
		||||
 | 
			
		||||
const EditUser = () => {
 | 
			
		||||
  const params = useParams();
 | 
			
		||||
@@ -134,7 +135,7 @@ const EditUser = () => {
 | 
			
		||||
              </Form.Field>
 | 
			
		||||
              <Form.Field>
 | 
			
		||||
                <Form.Input
 | 
			
		||||
                  label='剩余额度'
 | 
			
		||||
                  label={`剩余额度${renderQuotaWithPrompt(quota)}`}
 | 
			
		||||
                  name='quota'
 | 
			
		||||
                  placeholder={'请输入新的剩余额度'}
 | 
			
		||||
                  onChange={handleInputChange}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user