mirror of
https://github.com/stulzq/azure-openai-proxy.git
synced 2025-12-18 23:04:19 +01:00
feat: add azure proxy
This commit is contained in:
21
README.md
21
README.md
@@ -56,6 +56,27 @@ AZURE_OPENAI_MODEL_MAPPER: gpt-3.5-turbo=gpt-35-turbo
|
||||
|
||||
API Key: This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. You can use either `KEY1` or `KEY2`.
|
||||
|
||||
### Proxy
|
||||
|
||||
**HTTP Proxy**
|
||||
|
||||
Env:
|
||||
|
||||
````shell
|
||||
AZURE_OPENAI_HTTP_PROXY=http://127.0.0.1:1087
|
||||
````
|
||||
|
||||
|
||||
|
||||
**Socks5 Proxy**
|
||||
|
||||
Env:
|
||||
|
||||
````shell
|
||||
AZURE_OPENAI_SOCKS_PROXY=socks5://127.0.0.1:1080
|
||||
````
|
||||
|
||||
|
||||
|
||||
### Use Docker
|
||||
|
||||
|
||||
@@ -85,6 +85,15 @@ func Proxy(c *gin.Context, requestConverter RequestConverter) {
|
||||
}
|
||||
|
||||
proxy := &httputil.ReverseProxy{Director: director}
|
||||
transport, err := util.NewProxyFromEnv()
|
||||
if err != nil {
|
||||
util.SendError(c, errors.Wrap(err, "get proxy error"))
|
||||
return
|
||||
}
|
||||
if transport != nil {
|
||||
proxy.Transport = transport
|
||||
}
|
||||
|
||||
proxy.ServeHTTP(c.Writer, c.Request)
|
||||
|
||||
// issue: https://github.com/Chanzhaoyu/chatgpt-web/issues/831
|
||||
|
||||
@@ -4,4 +4,7 @@ const (
|
||||
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"
|
||||
|
||||
ENV_AZURE_OPENAI_HTTP_PROXY = "AZURE_OPENAI_HTTP_PROXY"
|
||||
ENV_AZURE_OPENAI_SOCKS_PROXY = "AZURE_OPENAI_SOCKS_PROXY"
|
||||
)
|
||||
|
||||
@@ -4,13 +4,29 @@ import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"github.com/stulzq/azure-openai-proxy/constant"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
func NewProxyFromEnv() (*http.Transport, error) {
|
||||
socksProxy := os.Getenv(constant.ENV_AZURE_OPENAI_SOCKS_PROXY)
|
||||
if socksProxy != "" {
|
||||
return NewSocksProxy(socksProxy)
|
||||
}
|
||||
|
||||
httpProxy := os.Getenv(constant.ENV_AZURE_OPENAI_HTTP_PROXY)
|
||||
if httpProxy != "" {
|
||||
return NewHttpProxy(httpProxy)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func NewHttpProxy(proxyAddress string) (*http.Transport, error) {
|
||||
proxyURL, err := url.Parse(proxyAddress)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user