2023-04-23 10:24:11 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"net/http"
|
|
|
|
"one-api/common"
|
2023-04-25 12:27:53 +00:00
|
|
|
"strings"
|
2023-04-23 10:24:11 +00:00
|
|
|
)
|
|
|
|
|
2023-04-28 08:58:55 +00:00
|
|
|
type Message struct {
|
2023-05-19 03:07:17 +00:00
|
|
|
Role string `json:"role"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
Name *string `json:"name,omitempty"`
|
2023-04-28 08:58:55 +00:00
|
|
|
}
|
|
|
|
|
2023-06-08 06:54:02 +00:00
|
|
|
const (
|
|
|
|
RelayModeUnknown = iota
|
|
|
|
RelayModeChatCompletions
|
|
|
|
RelayModeCompletions
|
|
|
|
RelayModeEmbeddings
|
2023-06-25 03:46:23 +00:00
|
|
|
RelayModeModerations
|
2023-06-19 02:28:55 +00:00
|
|
|
RelayModeImagesGenerations
|
2023-06-25 03:46:23 +00:00
|
|
|
RelayModeEdits
|
2023-06-08 06:54:02 +00:00
|
|
|
)
|
|
|
|
|
2023-05-21 06:26:59 +00:00
|
|
|
// https://platform.openai.com/docs/api-reference/chat
|
|
|
|
|
|
|
|
type GeneralOpenAIRequest struct {
|
2023-06-27 05:42:45 +00:00
|
|
|
Model string `json:"model,omitempty"`
|
|
|
|
Messages []Message `json:"messages,omitempty"`
|
|
|
|
Prompt any `json:"prompt,omitempty"`
|
|
|
|
Stream bool `json:"stream,omitempty"`
|
|
|
|
MaxTokens int `json:"max_tokens,omitempty"`
|
|
|
|
Temperature float64 `json:"temperature,omitempty"`
|
|
|
|
TopP float64 `json:"top_p,omitempty"`
|
|
|
|
N int `json:"n,omitempty"`
|
|
|
|
Input any `json:"input,omitempty"`
|
|
|
|
Instruction string `json:"instruction,omitempty"`
|
2023-05-21 06:26:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 02:48:52 +00:00
|
|
|
type ChatRequest struct {
|
2023-05-16 08:22:25 +00:00
|
|
|
Model string `json:"model"`
|
|
|
|
Messages []Message `json:"messages"`
|
|
|
|
MaxTokens int `json:"max_tokens"`
|
2023-05-15 02:48:52 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 08:58:55 +00:00
|
|
|
type TextRequest struct {
|
2023-05-16 08:18:35 +00:00
|
|
|
Model string `json:"model"`
|
|
|
|
Messages []Message `json:"messages"`
|
|
|
|
Prompt string `json:"prompt"`
|
|
|
|
MaxTokens int `json:"max_tokens"`
|
2023-04-28 08:58:55 +00:00
|
|
|
//Stream bool `json:"stream"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Usage struct {
|
|
|
|
PromptTokens int `json:"prompt_tokens"`
|
|
|
|
CompletionTokens int `json:"completion_tokens"`
|
|
|
|
TotalTokens int `json:"total_tokens"`
|
|
|
|
}
|
|
|
|
|
2023-05-15 02:48:52 +00:00
|
|
|
type OpenAIError struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Param string `json:"param"`
|
2023-06-11 01:49:57 +00:00
|
|
|
Code any `json:"code"`
|
2023-05-15 02:48:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 03:11:15 +00:00
|
|
|
type OpenAIErrorWithStatusCode struct {
|
|
|
|
OpenAIError
|
|
|
|
StatusCode int `json:"status_code"`
|
|
|
|
}
|
|
|
|
|
2023-04-28 08:58:55 +00:00
|
|
|
type TextResponse struct {
|
|
|
|
Usage `json:"usage"`
|
2023-05-15 02:48:52 +00:00
|
|
|
Error OpenAIError `json:"error"`
|
2023-04-28 08:58:55 +00:00
|
|
|
}
|
|
|
|
|
2023-06-08 06:54:02 +00:00
|
|
|
type ChatCompletionsStreamResponse struct {
|
2023-04-28 08:58:55 +00:00
|
|
|
Choices []struct {
|
|
|
|
Delta struct {
|
|
|
|
Content string `json:"content"`
|
|
|
|
} `json:"delta"`
|
|
|
|
FinishReason string `json:"finish_reason"`
|
|
|
|
} `json:"choices"`
|
|
|
|
}
|
|
|
|
|
2023-06-08 06:54:02 +00:00
|
|
|
type CompletionsStreamResponse struct {
|
|
|
|
Choices []struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
FinishReason string `json:"finish_reason"`
|
|
|
|
} `json:"choices"`
|
|
|
|
}
|
|
|
|
|
2023-04-23 10:24:11 +00:00
|
|
|
func Relay(c *gin.Context) {
|
2023-06-08 06:54:02 +00:00
|
|
|
relayMode := RelayModeUnknown
|
|
|
|
if strings.HasPrefix(c.Request.URL.Path, "/v1/chat/completions") {
|
|
|
|
relayMode = RelayModeChatCompletions
|
|
|
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/completions") {
|
|
|
|
relayMode = RelayModeCompletions
|
|
|
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/embeddings") {
|
|
|
|
relayMode = RelayModeEmbeddings
|
2023-06-11 01:37:36 +00:00
|
|
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
|
2023-06-25 03:46:23 +00:00
|
|
|
relayMode = RelayModeModerations
|
2023-06-19 02:28:55 +00:00
|
|
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
|
|
|
|
relayMode = RelayModeImagesGenerations
|
2023-06-25 03:46:23 +00:00
|
|
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/edits") {
|
|
|
|
relayMode = RelayModeEdits
|
2023-06-19 02:28:55 +00:00
|
|
|
}
|
|
|
|
var err *OpenAIErrorWithStatusCode
|
|
|
|
switch relayMode {
|
|
|
|
case RelayModeImagesGenerations:
|
|
|
|
err = relayImageHelper(c, relayMode)
|
|
|
|
default:
|
2023-06-19 07:00:22 +00:00
|
|
|
err = relayTextHelper(c, relayMode)
|
2023-06-08 06:54:02 +00:00
|
|
|
}
|
2023-04-28 09:11:57 +00:00
|
|
|
if err != nil {
|
2023-05-18 07:27:15 +00:00
|
|
|
if err.StatusCode == http.StatusTooManyRequests {
|
2023-06-11 01:55:50 +00:00
|
|
|
err.OpenAIError.Message = "当前分组负载已饱和,请稍后再试,或升级账户以提升服务质量。"
|
2023-05-18 07:27:15 +00:00
|
|
|
}
|
2023-05-18 03:11:15 +00:00
|
|
|
c.JSON(err.StatusCode, gin.H{
|
|
|
|
"error": err.OpenAIError,
|
2023-04-28 09:11:57 +00:00
|
|
|
})
|
2023-05-17 12:20:48 +00:00
|
|
|
channelId := c.GetInt("channel_id")
|
2023-06-22 02:59:01 +00:00
|
|
|
common.SysError(fmt.Sprintf("relay error (channel #%d): %s", channelId, err.Message))
|
2023-05-21 12:58:00 +00:00
|
|
|
// https://platform.openai.com/docs/guides/error-codes/api-errors
|
|
|
|
if common.AutomaticDisableChannelEnabled && (err.Type == "insufficient_quota" || err.Code == "invalid_api_key") {
|
2023-05-15 09:34:09 +00:00
|
|
|
channelId := c.GetInt("channel_id")
|
|
|
|
channelName := c.GetString("channel_name")
|
2023-05-18 03:11:15 +00:00
|
|
|
disableChannel(channelId, channelName, err.Message)
|
2023-05-15 09:34:09 +00:00
|
|
|
}
|
2023-04-28 09:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 08:58:55 +00:00
|
|
|
func RelayNotImplemented(c *gin.Context) {
|
2023-05-18 03:11:15 +00:00
|
|
|
err := OpenAIError{
|
|
|
|
Message: "API not implemented",
|
|
|
|
Type: "one_api_error",
|
|
|
|
Param: "",
|
|
|
|
Code: "api_not_implemented",
|
|
|
|
}
|
2023-06-23 14:59:44 +00:00
|
|
|
c.JSON(http.StatusNotImplemented, gin.H{
|
2023-05-18 03:11:15 +00:00
|
|
|
"error": err,
|
2023-04-28 08:58:55 +00:00
|
|
|
})
|
|
|
|
}
|
2023-06-17 01:46:07 +00:00
|
|
|
|
|
|
|
func RelayNotFound(c *gin.Context) {
|
|
|
|
err := OpenAIError{
|
|
|
|
Message: fmt.Sprintf("API not found: %s:%s", c.Request.Method, c.Request.URL.Path),
|
|
|
|
Type: "one_api_error",
|
|
|
|
Param: "",
|
|
|
|
Code: "api_not_found",
|
|
|
|
}
|
2023-06-23 14:59:44 +00:00
|
|
|
c.JSON(http.StatusNotFound, gin.H{
|
2023-06-17 01:46:07 +00:00
|
|
|
"error": err,
|
|
|
|
})
|
|
|
|
}
|