mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-10-31 13:53:41 +08:00 
			
		
		
		
	Compare commits
	
		
			4 Commits
		
	
	
		
			v0.3.0
			...
			v0.3.1-alp
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | cc1ef2ffd5 | ||
|  | 7201bd1c97 | ||
|  | 73d5e0f283 | ||
|  | efc744ca35 | 
| @@ -132,7 +132,7 @@ const ( | ||||
| var ChannelBaseURLs = []string{ | ||||
| 	"",                            // 0 | ||||
| 	"https://api.openai.com",      // 1 | ||||
| 	"https://openai.api2d.net",    // 2 | ||||
| 	"https://oa.api2d.net",        // 2 | ||||
| 	"",                            // 3 | ||||
| 	"https://api.openai-asia.com", // 4 | ||||
| 	"https://api.openai-sb.com",   // 5 | ||||
|   | ||||
							
								
								
									
										153
									
								
								controller/model.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										153
									
								
								controller/model.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,153 @@ | ||||
| package controller | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"github.com/gin-gonic/gin" | ||||
| ) | ||||
|  | ||||
| // https://platform.openai.com/docs/api-reference/models/list | ||||
|  | ||||
| type OpenAIModelPermission struct { | ||||
| 	Id                 string  `json:"id"` | ||||
| 	Object             string  `json:"object"` | ||||
| 	Created            int     `json:"created"` | ||||
| 	AllowCreateEngine  bool    `json:"allow_create_engine"` | ||||
| 	AllowSampling      bool    `json:"allow_sampling"` | ||||
| 	AllowLogprobs      bool    `json:"allow_logprobs"` | ||||
| 	AllowSearchIndices bool    `json:"allow_search_indices"` | ||||
| 	AllowView          bool    `json:"allow_view"` | ||||
| 	AllowFineTuning    bool    `json:"allow_fine_tuning"` | ||||
| 	Organization       string  `json:"organization"` | ||||
| 	Group              *string `json:"group"` | ||||
| 	IsBlocking         bool    `json:"is_blocking"` | ||||
| } | ||||
|  | ||||
| type OpenAIModels struct { | ||||
| 	Id         string                `json:"id"` | ||||
| 	Object     string                `json:"object"` | ||||
| 	Created    int                   `json:"created"` | ||||
| 	OwnedBy    string                `json:"owned_by"` | ||||
| 	Permission OpenAIModelPermission `json:"permission"` | ||||
| 	Root       string                `json:"root"` | ||||
| 	Parent     *string               `json:"parent"` | ||||
| } | ||||
|  | ||||
| var openAIModels []OpenAIModels | ||||
| var openAIModelsMap map[string]OpenAIModels | ||||
|  | ||||
| func init() { | ||||
| 	permission := OpenAIModelPermission{ | ||||
| 		Id:                 "modelperm-LwHkVFn8AcMItP432fKKDIKJ", | ||||
| 		Object:             "model_permission", | ||||
| 		Created:            1626777600, | ||||
| 		AllowCreateEngine:  true, | ||||
| 		AllowSampling:      true, | ||||
| 		AllowLogprobs:      true, | ||||
| 		AllowSearchIndices: false, | ||||
| 		AllowView:          true, | ||||
| 		AllowFineTuning:    false, | ||||
| 		Organization:       "*", | ||||
| 		Group:              nil, | ||||
| 		IsBlocking:         false, | ||||
| 	} | ||||
| 	// https://platform.openai.com/docs/models/model-endpoint-compatibility | ||||
| 	openAIModels = []OpenAIModels{ | ||||
| 		{ | ||||
| 			Id:         "gpt-3.5-turbo", | ||||
| 			Object:     "model", | ||||
| 			Created:    1677649963, | ||||
| 			OwnedBy:    "openai", | ||||
| 			Permission: permission, | ||||
| 			Root:       "gpt-3.5-turbo", | ||||
| 			Parent:     nil, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Id:         "gpt-3.5-turbo-0301", | ||||
| 			Object:     "model", | ||||
| 			Created:    1677649963, | ||||
| 			OwnedBy:    "openai", | ||||
| 			Permission: permission, | ||||
| 			Root:       "gpt-3.5-turbo-0301", | ||||
| 			Parent:     nil, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Id:         "gpt-4", | ||||
| 			Object:     "model", | ||||
| 			Created:    1677649963, | ||||
| 			OwnedBy:    "openai", | ||||
| 			Permission: permission, | ||||
| 			Root:       "gpt-4", | ||||
| 			Parent:     nil, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Id:         "gpt-4-0314", | ||||
| 			Object:     "model", | ||||
| 			Created:    1677649963, | ||||
| 			OwnedBy:    "openai", | ||||
| 			Permission: permission, | ||||
| 			Root:       "gpt-4-0314", | ||||
| 			Parent:     nil, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Id:         "gpt-4-32k", | ||||
| 			Object:     "model", | ||||
| 			Created:    1677649963, | ||||
| 			OwnedBy:    "openai", | ||||
| 			Permission: permission, | ||||
| 			Root:       "gpt-4-32k", | ||||
| 			Parent:     nil, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Id:         "gpt-4-32k-0314", | ||||
| 			Object:     "model", | ||||
| 			Created:    1677649963, | ||||
| 			OwnedBy:    "openai", | ||||
| 			Permission: permission, | ||||
| 			Root:       "gpt-4-32k-0314", | ||||
| 			Parent:     nil, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Id:         "gpt-3.5-turbo", | ||||
| 			Object:     "model", | ||||
| 			Created:    1677649963, | ||||
| 			OwnedBy:    "openai", | ||||
| 			Permission: permission, | ||||
| 			Root:       "gpt-3.5-turbo", | ||||
| 			Parent:     nil, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Id:         "text-embedding-ada-002", | ||||
| 			Object:     "model", | ||||
| 			Created:    1677649963, | ||||
| 			OwnedBy:    "openai", | ||||
| 			Permission: permission, | ||||
| 			Root:       "text-embedding-ada-002", | ||||
| 			Parent:     nil, | ||||
| 		}, | ||||
| 	} | ||||
| 	openAIModelsMap = make(map[string]OpenAIModels) | ||||
| 	for _, model := range openAIModels { | ||||
| 		openAIModelsMap[model.Id] = model | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func ListModels(c *gin.Context) { | ||||
| 	c.JSON(200, openAIModels) | ||||
| } | ||||
|  | ||||
| func RetrieveModel(c *gin.Context) { | ||||
| 	modelId := c.Param("model") | ||||
| 	if model, ok := openAIModelsMap[modelId]; ok { | ||||
| 		c.JSON(200, model) | ||||
| 	} else { | ||||
| 		openAIError := OpenAIError{ | ||||
| 			Message: fmt.Sprintf("The model '%s' does not exist", modelId), | ||||
| 			Type:    "invalid_request_error", | ||||
| 			Param:   "model", | ||||
| 			Code:    "model_not_found", | ||||
| 		} | ||||
| 		c.JSON(200, gin.H{ | ||||
| 			"error": openAIError, | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
| @@ -175,7 +175,7 @@ func relayHelper(c *gin.Context) error { | ||||
| 		return err | ||||
| 	} | ||||
| 	var textResponse TextResponse | ||||
| 	isStream := resp.Header.Get("Content-Type") == "text/event-stream" | ||||
| 	isStream := strings.HasPrefix(resp.Header.Get("Content-Type"), "text/event-stream") | ||||
| 	var streamResponseText string | ||||
|  | ||||
| 	defer func() { | ||||
|   | ||||
| @@ -85,6 +85,8 @@ func RootAuth() func(c *gin.Context) { | ||||
| func TokenAuth() func(c *gin.Context) { | ||||
| 	return func(c *gin.Context) { | ||||
| 		key := c.Request.Header.Get("Authorization") | ||||
| 		key = strings.TrimPrefix(key, "Bearer ") | ||||
| 		key = strings.TrimPrefix(key, "sk-") | ||||
| 		parts := strings.Split(key, "-") | ||||
| 		key = parts[0] | ||||
| 		token, err := model.ValidateUserToken(key) | ||||
|   | ||||
| @@ -6,7 +6,6 @@ import ( | ||||
| 	_ "gorm.io/driver/sqlite" | ||||
| 	"gorm.io/gorm" | ||||
| 	"one-api/common" | ||||
| 	"strings" | ||||
| ) | ||||
|  | ||||
| type Token struct { | ||||
| @@ -38,7 +37,6 @@ func ValidateUserToken(key string) (token *Token, err error) { | ||||
| 	if key == "" { | ||||
| 		return nil, errors.New("未提供 token") | ||||
| 	} | ||||
| 	key = strings.Replace(key, "Bearer ", "", 1) | ||||
| 	token = &Token{} | ||||
| 	err = DB.Where("`key` = ?", key).First(token).Error | ||||
| 	if err == nil { | ||||
|   | ||||
| @@ -11,8 +11,8 @@ func SetRelayRouter(router *gin.Engine) { | ||||
| 	relayV1Router := router.Group("/v1") | ||||
| 	relayV1Router.Use(middleware.TokenAuth(), middleware.Distribute()) | ||||
| 	{ | ||||
| 		relayV1Router.GET("/models", controller.Relay) | ||||
| 		relayV1Router.GET("/models/:model", controller.Relay) | ||||
| 		relayV1Router.GET("/models", controller.ListModels) | ||||
| 		relayV1Router.GET("/models/:model", controller.RetrieveModel) | ||||
| 		relayV1Router.POST("/completions", controller.RelayNotImplemented) | ||||
| 		relayV1Router.POST("/chat/completions", controller.Relay) | ||||
| 		relayV1Router.POST("/edits", controller.RelayNotImplemented) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user