mirror of
https://github.com/stulzq/azure-openai-proxy.git
synced 2025-12-19 15:24:24 +01:00
feat: code clean
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Azure OpenAI Service Proxy. Convert OpenAI official API request to Azure OpenAI API request.
|
Azure OpenAI Service Proxy. Convert OpenAI official API request to Azure OpenAI API request.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Get Start
|
## Get Start
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ Azure Deployment Names: **Resource Management** > **Deployments**
|
|||||||
AZURE_OPENAI_MODEL_MAPPER: gpt-3.5-turbo=azure-gpt-35
|
AZURE_OPENAI_MODEL_MAPPER: gpt-3.5-turbo=azure-gpt-35
|
||||||
````
|
````
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
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`.
|
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`.
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ curl --location --request POST 'localhost:8080/v1/chat/completions' \
|
|||||||
|
|
||||||
ChatGPT Web: https://github.com/Chanzhaoyu/chatgpt-web
|
ChatGPT Web: https://github.com/Chanzhaoyu/chatgpt-web
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Envs:
|
Envs:
|
||||||
|
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 156 KiB After Width: | Height: | Size: 156 KiB |
|
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 55 KiB |
@@ -2,13 +2,14 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/stulzq/azure-openai-proxy/azure"
|
"github.com/stulzq/azure-openai-proxy/pkg/azure"
|
||||||
)
|
)
|
||||||
|
|
||||||
// registerRoute registers all routes
|
// registerRoute registers all routes
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
package azure
|
package azure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/stulzq/azure-openai-proxy/pkg/constant"
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/stulzq/azure-openai-proxy/constant"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -3,9 +3,7 @@ package azure
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/bytedance/sonic"
|
"github.com/stulzq/azure-openai-proxy/pkg/util"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/stulzq/azure-openai-proxy/util"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -13,12 +11,14 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bytedance/sonic"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Proxy Azure OpenAI
|
// Proxy Azure OpenAI
|
||||||
func Proxy(c *gin.Context) {
|
func Proxy(c *gin.Context) {
|
||||||
// improve performance some code from https://github.com/diemus/azure-openai-proxy/blob/main/pkg/azure/proxy.go
|
// some code from https://github.com/diemus/azure-openai-proxy/blob/main/pkg/azure/proxy.go
|
||||||
director := func(req *http.Request) {
|
director := func(req *http.Request) {
|
||||||
if req.Body == nil {
|
if req.Body == nil {
|
||||||
util.SendError(c, errors.New("request body is empty"))
|
util.SendError(c, errors.New("request body is empty"))
|
||||||
@@ -34,6 +34,7 @@ func Proxy(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get deployment from request
|
||||||
deployment, err := model.String()
|
deployment, err := model.String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.SendError(c, errors.Wrap(err, "get deployment error"))
|
util.SendError(c, errors.Wrap(err, "get deployment error"))
|
||||||
@@ -64,7 +65,7 @@ func Proxy(c *gin.Context) {
|
|||||||
proxy := &httputil.ReverseProxy{Director: director}
|
proxy := &httputil.ReverseProxy{Director: director}
|
||||||
proxy.ServeHTTP(c.Writer, c.Request)
|
proxy.ServeHTTP(c.Writer, c.Request)
|
||||||
|
|
||||||
// https://github.com/Chanzhaoyu/chatgpt-web/issues/831
|
// issue: https://github.com/Chanzhaoyu/chatgpt-web/issues/831
|
||||||
if c.Writer.Header().Get("Content-Type") == "text/event-stream" {
|
if c.Writer.Header().Get("Content-Type") == "text/event-stream" {
|
||||||
if _, err := c.Writer.Write([]byte{'\n'}); err != nil {
|
if _, err := c.Writer.Write([]byte{'\n'}); err != nil {
|
||||||
log.Printf("rewrite response error: %v", err)
|
log.Printf("rewrite response error: %v", err)
|
||||||
Reference in New Issue
Block a user