mirror of
https://github.com/stulzq/azure-openai-proxy.git
synced 2025-12-20 15:54:20 +01:00
feat: initial commit
This commit is contained in:
43
apis/chat.go
Normal file
43
apis/chat.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package apis
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stulzq/azure-openai-proxy/openai"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ChatCompletions xxx
|
||||
// Path: /v1/chat/completions
|
||||
func ChatCompletions(c *gin.Context) {
|
||||
// get auth token from header
|
||||
rawToken := c.GetHeader("Authorization")
|
||||
token := strings.TrimPrefix(rawToken, "Bearer ")
|
||||
|
||||
reqContent, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
SendError(c, errors.Wrap(err, "failed to read request body"))
|
||||
return
|
||||
}
|
||||
|
||||
oaiResp, err := openai.ChatCompletions(token, reqContent)
|
||||
if err != nil {
|
||||
SendError(c, errors.Wrap(err, "failed to call Azure OpenAI"))
|
||||
return
|
||||
}
|
||||
|
||||
// pass-through header
|
||||
extraHeaders := map[string]string{}
|
||||
for k, v := range oaiResp.Header {
|
||||
if _, ok := ignoreHeaders[k]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
extraHeaders[k] = strings.Join(v, ",")
|
||||
}
|
||||
|
||||
c.DataFromReader(oaiResp.StatusCode, oaiResp.ContentLength, oaiResp.Header.Get("Content-Type"), oaiResp.Response.Body, extraHeaders)
|
||||
|
||||
_, _ = c.Writer.Write([]byte{'\n'}) // add a newline to the end of the response https://github.com/Chanzhaoyu/chatgpt-web/issues/831
|
||||
}
|
||||
Reference in New Issue
Block a user