feat: support dall-e3 api mirrors, add name field for ApiKey

This commit is contained in:
RockYang
2024-01-04 16:29:57 +08:00
parent a06a81a415
commit bcc622a24d
13 changed files with 51 additions and 75 deletions

View File

@@ -27,6 +27,7 @@ func (h *ApiKeyHandler) Save(c *gin.Context) {
var data struct {
Id uint `json:"id"`
Platform string `json:"platform"`
Name string `json:"name"`
Type string `json:"type"`
Value string `json:"value"`
ApiURL string `json:"api_url"`
@@ -48,6 +49,7 @@ func (h *ApiKeyHandler) Save(c *gin.Context) {
apiKey.ApiURL = data.ApiURL
apiKey.Enabled = data.Enabled
apiKey.UseProxy = data.UseProxy
apiKey.Name = data.Name
res := h.db.Save(&apiKey)
if res.Error != nil {
resp.ERROR(c, "更新数据库失败!")

View File

@@ -448,8 +448,9 @@ func (h *ChatHandler) doRequest(ctx context.Context, req types.ApiRequest, platf
request = request.WithContext(ctx)
request.Header.Set("Content-Type", "application/json")
proxyURL := h.App.Config.ProxyURL
if proxyURL != "" && platform == types.OpenAI { // 使用代理
var proxyURL string
if h.App.Config.ProxyURL != "" && apiKey.UseProxy { // 使用代理
proxyURL = h.App.Config.ProxyURL
proxy, _ := url.Parse(proxyURL)
client = &http.Client{
Transport: &http.Transport{

View File

@@ -231,15 +231,10 @@ func (h *FunctionHandler) Dall3(c *gin.Context) {
// translate prompt
const translatePromptTemplate = "Translate the following painting prompt words into English keyword phrases. Without any explanation, directly output the keyword phrases separated by commas. The content to be translated is: [%s]"
pt, err := utils.OpenAIRequest(fmt.Sprintf(translatePromptTemplate, params["prompt"]), apiKey, h.App.Config.ProxyURL)
pt, err := utils.OpenAIRequest(h.db, fmt.Sprintf(translatePromptTemplate, params["prompt"]), h.App.Config.ProxyURL)
if err == nil {
prompt = pt
}
apiURL := chatConfig.DallApiURL
if utils.IsEmptyValue(apiURL) {
apiURL = "https://api.openai.com/v1/images/generations"
}
imgNum := chatConfig.DallImgNum
if imgNum <= 0 {
imgNum = 1
@@ -247,11 +242,12 @@ func (h *FunctionHandler) Dall3(c *gin.Context) {
var res imgRes
var errRes ErrRes
var request *req.Request
if strings.Contains(apiURL, "api.openai.com") {
if apiKey.UseProxy && h.proxyURL != "" {
request = req.C().SetProxyURL(h.proxyURL).R()
} else {
request = req.C().R()
}
logger.Debugf("Sending %s request, ApiURL:%s, ApiKey:%s, PROXY: %s", apiKey.Platform, apiKey.ApiURL, apiKey.Value, h.proxyURL)
r, err := request.SetHeader("Content-Type", "application/json").
SetHeader("Authorization", "Bearer "+apiKey.Value).
SetBody(imgReq{
@@ -261,7 +257,7 @@ func (h *FunctionHandler) Dall3(c *gin.Context) {
Size: "1024x1024",
}).
SetErrorResult(&errRes).
SetSuccessResult(&res).Post(apiURL)
SetSuccessResult(&res).Post(apiKey.ApiURL)
if r.IsErrorState() {
resp.ERROR(c, "请求 OpenAI API 失败: "+errRes.Error.Message)
return

View File

@@ -3,11 +3,8 @@ package handler
import (
"chatplus/core"
"chatplus/core/types"
"chatplus/store/model"
"chatplus/utils"
"chatplus/utils/resp"
"fmt"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
@@ -36,7 +33,7 @@ func (h *PromptHandler) Rewrite(c *gin.Context) {
return
}
content, err := h.request(data.Prompt, rewritePromptTemplate)
content, err := utils.OpenAIRequest(h.db, data.Prompt, rewritePromptTemplate)
if err != nil {
resp.ERROR(c, err.Error())
return
@@ -54,7 +51,7 @@ func (h *PromptHandler) Translate(c *gin.Context) {
return
}
content, err := h.request(data.Prompt, translatePromptTemplate)
content, err := utils.OpenAIRequest(h.db, data.Prompt, translatePromptTemplate)
if err != nil {
resp.ERROR(c, err.Error())
return
@@ -62,14 +59,3 @@ func (h *PromptHandler) Translate(c *gin.Context) {
resp.SUCCESS(c, content)
}
func (h *PromptHandler) request(prompt string, promptTemplate string) (string, error) {
// 获取 OpenAI 的 API KEY
var apiKey model.ApiKey
res := h.db.Where("platform = ?", types.OpenAI).Where("type = ?", "chat").Where("enabled = ?", true).First(&apiKey)
if res.Error != nil {
return "", fmt.Errorf("error with fetch OpenAI API KEY%v", res.Error)
}
return utils.OpenAIRequest(fmt.Sprintf(promptTemplate, prompt), apiKey, h.App.Config.ProxyURL)
}

View File

@@ -303,7 +303,7 @@ func (h *UserHandler) ProfileUpdate(c *gin.Context) {
}
h.db.First(&user, user.Id)
user.Avatar = data.Avatar
user.ChatConfig = utils.JsonEncode(data.ChatConfig)
user.Nickname = data.Nickname
res := h.db.Updates(&user)
if res.Error != nil {
resp.ERROR(c, "更新用户信息失败")