feat: code clean

This commit is contained in:
Zhiqiang Li
2023-03-30 16:55:15 +08:00
parent 26d230f587
commit 656a7d1f1c
11 changed files with 14 additions and 13 deletions

View File

@@ -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.
![aoai-proxy.jpg](docs%2Fassets%2Fimages%2Faoai-proxy.jpg) ![aoai-proxy.jpg](assets%2Fimages%2Faoai-proxy.jpg)
## 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
```` ````
![Screenshot of the overview UI for an OpenAI Resource in the Azure portal with the endpoint & access keys location circled in red.](docs/assets/images/endpoint.png) ![Screenshot of the overview UI for an OpenAI Resource in the Azure portal with the endpoint & access keys location circled in red.](assets/images/endpoint.png)
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
![image-20230324163357406](docs/assets/images/image-20230324163357406.png) ![chatgpt-web](assets/images/chatgpt-web.png)
Envs: Envs:

View File

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 38 KiB

View File

Before

Width:  |  Height:  |  Size: 156 KiB

After

Width:  |  Height:  |  Size: 156 KiB

View File

Before

Width:  |  Height:  |  Size: 55 KiB

After

Width:  |  Height:  |  Size: 55 KiB

View File

@@ -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() {

View File

@@ -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

View File

@@ -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 (

View File

@@ -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)