fix: custom_endpoint

This commit is contained in:
Sakurasan
2024-12-18 01:00:45 +08:00
parent 000fc2f616
commit f8b364a052
2 changed files with 10 additions and 21 deletions

View File

@@ -25,13 +25,13 @@ const (
) )
var ( var (
BaseURL string // "https://api.openai.com" Custom_Endpoint string
AIGateWay_Endpoint = "https://gateway.ai.cloudflare.com/v1/431ba10f11200d544922fbca177aaa7f/openai/openai/chat/completions" AIGateWay_Endpoint string // "https://gateway.ai.cloudflare.com/v1/431ba10f11200d544922fbca177aaa7f/openai/openai/chat/completions"
) )
func init() { func init() {
if os.Getenv("OpenAI_Endpoint") != "" { if os.Getenv("OpenAI_Endpoint") != "" {
BaseURL = os.Getenv("OpenAI_Endpoint") Custom_Endpoint = os.Getenv("OpenAI_Endpoint")
} }
if os.Getenv("AIGateWay_Endpoint") != "" { if os.Getenv("AIGateWay_Endpoint") != "" {
AIGateWay_Endpoint = os.Getenv("AIGateWay_Endpoint") AIGateWay_Endpoint = os.Getenv("AIGateWay_Endpoint")
@@ -247,16 +247,16 @@ func ChatProxy(c *gin.Context, chatReq *ChatCompletionRequest) {
req.Header = c.Request.Header req.Header = c.Request.Header
req.Header.Set("api-key", onekey.Key) req.Header.Set("api-key", onekey.Key)
default: default:
req, err = http.NewRequest(c.Request.Method, OpenAI_Endpoint, bytes.NewReader(chatReq.ToByteJson())) req, err = http.NewRequest(c.Request.Method, OpenAI_Endpoint, bytes.NewReader(chatReq.ToByteJson())) // default endpoint
if onekey.EndPoint != "" { // 优先key的endpoint
req, err = http.NewRequest(c.Request.Method, onekey.EndPoint+c.Request.RequestURI, bytes.NewReader(chatReq.ToByteJson()))
}
if AIGateWay_Endpoint != "" { // cloudflare gateway的endpoint if AIGateWay_Endpoint != "" { // cloudflare gateway的endpoint
req, err = http.NewRequest(c.Request.Method, AIGateWay_Endpoint, bytes.NewReader(chatReq.ToByteJson())) req, err = http.NewRequest(c.Request.Method, AIGateWay_Endpoint, bytes.NewReader(chatReq.ToByteJson()))
} }
customEndpoint := os.Getenv("CUSTOM_ENDPOINT") // 最后是用户自定义的endpoint CUSTOM_ENDPOINT=true OpenAI_Endpoint if Custom_Endpoint != "" { // 自定义endpoint
if customEndpoint == "true" && OpenAI_Endpoint != "" { req, err = http.NewRequest(c.Request.Method, Custom_Endpoint, bytes.NewReader(chatReq.ToByteJson()))
req, err = http.NewRequest(c.Request.Method, BaseURL, bytes.NewReader(chatReq.ToByteJson())) }
if onekey.EndPoint != "" { // 优先key的endpoint
req, err = http.NewRequest(c.Request.Method, onekey.EndPoint+c.Request.RequestURI, bytes.NewReader(chatReq.ToByteJson()))
} }
req.Header = c.Request.Header req.Header = c.Request.Header

View File

@@ -2,22 +2,18 @@ package router
import ( import (
"crypto/tls" "crypto/tls"
"fmt"
"log"
"net" "net"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"opencatd-open/pkg/claude" "opencatd-open/pkg/claude"
oai "opencatd-open/pkg/openai" oai "opencatd-open/pkg/openai"
"opencatd-open/store" "opencatd-open/store"
"os"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
var ( var (
baseUrl = "https://api.openai.com"
GPT3Dot5Turbo = "gpt-3.5-turbo" GPT3Dot5Turbo = "gpt-3.5-turbo"
GPT4 = "gpt-4" GPT4 = "gpt-4"
) )
@@ -62,13 +58,6 @@ var (
// } `json:"usage"` // } `json:"usage"`
// } // }
func init() {
if openai_endpoint := os.Getenv("openai_endpoint"); openai_endpoint != "" {
log.Println(fmt.Sprintf("replace %s to %s", baseUrl, openai_endpoint))
baseUrl = openai_endpoint
}
}
func HandleProxy(c *gin.Context) { func HandleProxy(c *gin.Context) {
var ( var (
localuser bool localuser bool