refact: azure
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
AzureApiVersion = "2024-02-01"
|
||||||
OpenAI_Endpoint = "https://api.openai.com/v1/chat/completions"
|
OpenAI_Endpoint = "https://api.openai.com/v1/chat/completions"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -202,6 +203,18 @@ func ChatProxy(c *gin.Context, chatReq *ChatCompletionRequest) {
|
|||||||
|
|
||||||
var req *http.Request
|
var req *http.Request
|
||||||
|
|
||||||
|
switch onekey.ApiType {
|
||||||
|
case "azure":
|
||||||
|
var buildurl string
|
||||||
|
if onekey.EndPoint != "" {
|
||||||
|
buildurl = fmt.Sprintf("%s/openai/deployments/%s/chat/completions?api-version=%s", onekey.EndPoint, modelmap(chatReq.Model), AzureApiVersion)
|
||||||
|
} else {
|
||||||
|
buildurl = fmt.Sprintf("https://%s.openai.azure.com/openai/deployments/%s/chat/completions?api-version=%s", onekey.ResourceNmae, modelmap(chatReq.Model), AzureApiVersion)
|
||||||
|
}
|
||||||
|
req, err = http.NewRequest(c.Request.Method, buildurl, bytes.NewReader(chatReq.ToByteJson()))
|
||||||
|
req.Header = c.Request.Header
|
||||||
|
req.Header.Set("api-key", onekey.Key)
|
||||||
|
default:
|
||||||
if onekey.EndPoint != "" { // 优先key的endpoint
|
if onekey.EndPoint != "" { // 优先key的endpoint
|
||||||
req, err = http.NewRequest(c.Request.Method, onekey.EndPoint+c.Request.RequestURI, bytes.NewReader(chatReq.ToByteJson()))
|
req, err = http.NewRequest(c.Request.Method, onekey.EndPoint+c.Request.RequestURI, bytes.NewReader(chatReq.ToByteJson()))
|
||||||
} else {
|
} else {
|
||||||
@@ -211,14 +224,14 @@ func ChatProxy(c *gin.Context, chatReq *ChatCompletionRequest) {
|
|||||||
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()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
req.Header = c.Request.Header
|
||||||
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", onekey.Key))
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header = c.Request.Header
|
|
||||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", onekey.Key))
|
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
@@ -301,6 +314,14 @@ func ChatProxy(c *gin.Context, chatReq *ChatCompletionRequest) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func modelmap(in string) string {
|
||||||
|
// gpt-3.5-turbo -> gpt-35-turbo
|
||||||
|
if strings.Contains(in, ".") {
|
||||||
|
return strings.ReplaceAll(in, ".", "")
|
||||||
|
}
|
||||||
|
return in
|
||||||
|
}
|
||||||
|
|
||||||
type ErrResponse struct {
|
type ErrResponse struct {
|
||||||
Error struct {
|
Error struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
|
|||||||
@@ -49,6 +49,11 @@ func SelectKeyCache(apitype string) (Key, error) {
|
|||||||
if item.Object.(Key).ApiType == apitype {
|
if item.Object.(Key).ApiType == apitype {
|
||||||
keys = append(keys, item.Object.(Key))
|
keys = append(keys, item.Object.(Key))
|
||||||
}
|
}
|
||||||
|
if apitype == "openai" {
|
||||||
|
if item.Object.(Key).ApiType == "azure" {
|
||||||
|
keys = append(keys, item.Object.(Key))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if len(keys) == 0 {
|
if len(keys) == 0 {
|
||||||
return Key{}, errors.New("No key found")
|
return Key{}, errors.New("No key found")
|
||||||
|
|||||||
Reference in New Issue
Block a user