-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
162 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
FROM alpine:3 | ||
|
||
EXPOSE 8080 | ||
COPY ./bin/azure-openai-proxy /usr/bin | ||
|
||
ENTRYPOINT ["/usr/bin/azure-openai-proxy"] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package azure | ||
|
||
import ( | ||
"log" | ||
"net/url" | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/stulzq/azure-openai-proxy/constant" | ||
) | ||
|
||
const ( | ||
AuthHeaderKey = "api-key" | ||
) | ||
|
||
var ( | ||
AzureOpenAIEndpoint = "" | ||
AzureOpenAIEndpointParse *url.URL | ||
|
||
AzureOpenAIAPIVer = "" | ||
|
||
AzureOpenAIModelMapper = map[string]string{ | ||
"gpt-3.5-turbo": "gpt-35-turbo", | ||
"gpt-3.5-turbo-0301": "gpt-35-turbo-0301", | ||
} | ||
fallbackModelMapper = regexp.MustCompile(`[.:]`) | ||
) | ||
|
||
func init() { | ||
AzureOpenAIAPIVer = os.Getenv(constant.ENV_AZURE_OPENAI_API_VER) | ||
AzureOpenAIEndpoint = os.Getenv(constant.ENV_AZURE_OPENAI_ENDPOINT) | ||
|
||
if AzureOpenAIAPIVer == "" { | ||
AzureOpenAIAPIVer = "2023-03-15-preview" | ||
} | ||
|
||
var err error | ||
AzureOpenAIEndpointParse, err = url.Parse(AzureOpenAIEndpoint) | ||
if err != nil { | ||
log.Fatal("parse AzureOpenAIEndpoint error: ", err) | ||
} | ||
|
||
if v := os.Getenv(constant.ENV_AZURE_OPENAI_MODEL_MAPPER); v != "" { | ||
for _, pair := range strings.Split(v, ",") { | ||
info := strings.Split(pair, "=") | ||
if len(info) != 2 { | ||
log.Fatalf("error parsing %s, invalid value %s", constant.ENV_AZURE_OPENAI_MODEL_MAPPER, pair) | ||
} | ||
|
||
AzureOpenAIModelMapper[info[0]] = info[1] | ||
} | ||
} | ||
|
||
log.Println("AzureOpenAIAPIVer: ", AzureOpenAIAPIVer) | ||
log.Println("AzureOpenAIEndpoint: ", AzureOpenAIEndpoint) | ||
log.Println("AzureOpenAIModelMapper: ", AzureOpenAIModelMapper) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package azure | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/bytedance/sonic" | ||
"github.com/pkg/errors" | ||
"github.com/stulzq/azure-openai-proxy/util" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/http/httputil" | ||
"path" | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// Proxy Azure OpenAI | ||
func Proxy(c *gin.Context) { | ||
// improve performance some code from https://github.com/diemus/azure-openai-proxy/blob/main/pkg/azure/proxy.go | ||
director := func(req *http.Request) { | ||
if req.Body == nil { | ||
util.SendError(c, errors.New("request body is empty")) | ||
return | ||
} | ||
body, _ := io.ReadAll(req.Body) | ||
req.Body = io.NopCloser(bytes.NewBuffer(body)) | ||
|
||
// get model from body | ||
model, err := sonic.Get(body, "model") | ||
if err != nil { | ||
util.SendError(c, errors.Wrap(err, "get model error")) | ||
return | ||
} | ||
|
||
deployment, err := model.String() | ||
if err != nil { | ||
util.SendError(c, errors.Wrap(err, "get deployment error")) | ||
return | ||
} | ||
deployment = GetDeploymentByModel(deployment) | ||
|
||
// get auth token from header | ||
rawToken := req.Header.Get("Authorization") | ||
token := strings.TrimPrefix(rawToken, "Bearer ") | ||
req.Header.Set(AuthHeaderKey, token) | ||
req.Header.Del("Authorization") | ||
|
||
originURL := req.URL.String() | ||
req.Host = AzureOpenAIEndpointParse.Host | ||
req.URL.Scheme = AzureOpenAIEndpointParse.Scheme | ||
req.URL.Host = AzureOpenAIEndpointParse.Host | ||
req.URL.Path = path.Join(fmt.Sprintf("/openai/deployments/%s", deployment), strings.Replace(req.URL.Path, "/v1/", "/", 1)) | ||
req.URL.RawPath = req.URL.EscapedPath() | ||
|
||
query := req.URL.Query() | ||
query.Add("api-version", AzureOpenAIAPIVer) | ||
req.URL.RawQuery = query.Encode() | ||
|
||
log.Printf("proxying request [%s] %s -> %s", model, originURL, req.URL.String()) | ||
} | ||
|
||
proxy := &httputil.ReverseProxy{Director: director} | ||
proxy.ServeHTTP(c.Writer, c.Request) | ||
|
||
// https://github.com/Chanzhaoyu/chatgpt-web/issues/831 | ||
if c.Writer.Header().Get("Content-Type") == "text/event-stream" { | ||
if _, err := c.Writer.Write([]byte{'\n'}); err != nil { | ||
log.Printf("rewrite response error: %v", err) | ||
} | ||
} | ||
} | ||
|
||
func GetDeploymentByModel(model string) string { | ||
if v, ok := AzureOpenAIModelMapper[model]; ok { | ||
return v | ||
} | ||
|
||
return fallbackModelMapper.ReplaceAllString(model, "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
set -e | ||
|
||
VERSION=v1.0.0 | ||
VERSION=v1.1.0 | ||
|
||
rm -rf bin | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package constant | ||
|
||
const ( | ||
ENV_AZURE_OPENAI_ENDPOINT = "AZURE_OPENAI_ENDPOINT" | ||
ENV_AZURE_OPENAI_API_VER = "AZURE_OPENAI_API_VER" | ||
ENV_AZURE_OPENAI_DEPLOY = "AZURE_OPENAI_DEPLOY" | ||
ENV_AZURE_OPENAI_ENDPOINT = "AZURE_OPENAI_ENDPOINT" | ||
ENV_AZURE_OPENAI_API_VER = "AZURE_OPENAI_API_VER" | ||
ENV_AZURE_OPENAI_MODEL_MAPPER = "AZURE_OPENAI_MODEL_MAPPER" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package apis | ||
package util | ||
|
||
type ApiResponse struct { | ||
Error ErrorDescription `json:"error"` | ||
|