Skip to content

Commit 3859718

Browse files
committed
fix /v1/models bug
1 parent 8e2c57a commit 3859718

File tree

2 files changed

+114
-28
lines changed

2 files changed

+114
-28
lines changed

main.go

Lines changed: 83 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,89 @@ func init() {
2727
}
2828

2929
func main() {
30-
r := gin.Default()
31-
r.Any("*path", func(c *gin.Context) {
32-
if ProxyMode == "azure" {
33-
// BUGFIX: fix options request, see https://github.com/diemus/azure-openai-proxy/issues/1
34-
if c.Request.Method == http.MethodOptions {
35-
c.Header("Access-Control-Allow-Origin", "*")
36-
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
37-
c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")
38-
c.Status(200)
39-
return
40-
}
41-
42-
server := azure.NewOpenAIReverseProxy()
43-
server.ServeHTTP(c.Writer, c.Request)
44-
//BUGFIX: try to fix the difference between azure and openai
45-
//Azure's response is missing a \n at the end of the stream
46-
//see https://github.com/Chanzhaoyu/chatgpt-web/issues/831
47-
if c.Writer.Header().Get("Content-Type") == "text/event-stream" {
48-
if _, err := c.Writer.Write([]byte("\n")); err != nil {
49-
log.Printf("rewrite azure response error: %v", err)
50-
}
51-
}
52-
} else {
53-
server := openai.NewOpenAIReverseProxy()
54-
server.ServeHTTP(c.Writer, c.Request)
55-
}
56-
})
30+
router := gin.Default()
31+
if ProxyMode == "azure" {
32+
router.GET("/v1/models", handleGetModels)
33+
router.OPTIONS("/v1/*path", handleOptions)
34+
35+
router.POST("/v1/chat/completions", handleAzureProxy)
36+
router.POST("/v1/completions", handleAzureProxy)
37+
router.POST("/v1/embeddings", handleAzureProxy)
38+
} else {
39+
router.Any("*path", handleOpenAIProxy)
40+
}
5741

58-
r.Run(Address)
42+
router.Run(Address)
43+
44+
}
45+
46+
func handleGetModels(c *gin.Context) {
47+
// BUGFIX: fix options request, see https://github.com/diemus/azure-openai-proxy/issues/3
48+
models := []string{"gpt-4", "gpt-4-0314", "gpt-4-32k", "gpt-4-32k-0314", "gpt-3.5-turbo", "gpt-3.5-turbo-0301", "text-davinci-003", "text-embedding-ada-002"}
49+
result := azure.ListModelResponse{
50+
Object: "list",
51+
}
52+
for _, model := range models {
53+
result.Data = append(result.Data, azure.Model{
54+
ID: model,
55+
Object: "model",
56+
Created: 1677649963,
57+
OwnedBy: "openai",
58+
Permission: []azure.ModelPermission{
59+
{
60+
ID: "",
61+
Object: "model",
62+
Created: 1679602087,
63+
AllowCreateEngine: true,
64+
AllowSampling: true,
65+
AllowLogprobs: true,
66+
AllowSearchIndices: true,
67+
AllowView: true,
68+
AllowFineTuning: true,
69+
Organization: "*",
70+
Group: nil,
71+
IsBlocking: false,
72+
},
73+
},
74+
Root: model,
75+
Parent: nil,
76+
})
77+
}
78+
c.JSON(200, result)
79+
}
80+
81+
func handleOptions(c *gin.Context) {
82+
// BUGFIX: fix options request, see https://github.com/diemus/azure-openai-proxy/issues/1
83+
c.Header("Access-Control-Allow-Origin", "*")
84+
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
85+
c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")
86+
c.Status(200)
87+
return
88+
}
89+
90+
func handleAzureProxy(c *gin.Context) {
91+
// BUGFIX: fix options request, see https://github.com/diemus/azure-openai-proxy/issues/1
92+
if c.Request.Method == http.MethodOptions {
93+
c.Header("Access-Control-Allow-Origin", "*")
94+
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
95+
c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")
96+
c.Status(200)
97+
return
98+
}
99+
100+
server := azure.NewOpenAIReverseProxy()
101+
server.ServeHTTP(c.Writer, c.Request)
102+
//BUGFIX: try to fix the difference between azure and openai
103+
//Azure's response is missing a \n at the end of the stream
104+
//see https://github.com/Chanzhaoyu/chatgpt-web/issues/831
105+
if c.Writer.Header().Get("Content-Type") == "text/event-stream" {
106+
if _, err := c.Writer.Write([]byte("\n")); err != nil {
107+
log.Printf("rewrite azure response error: %v", err)
108+
}
109+
}
110+
}
59111

112+
func handleOpenAIProxy(c *gin.Context) {
113+
server := openai.NewOpenAIReverseProxy()
114+
server.ServeHTTP(c.Writer, c.Request)
60115
}

pkg/azure/types.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package azure
2+
3+
type ListModelResponse struct {
4+
Object string `json:"object"`
5+
Data []Model `json:"data"`
6+
}
7+
8+
type Model struct {
9+
ID string `json:"id"`
10+
Object string `json:"object"`
11+
Created int `json:"created"`
12+
OwnedBy string `json:"owned_by"`
13+
Permission []ModelPermission `json:"permission"`
14+
Root string `json:"root"`
15+
Parent any `json:"parent"`
16+
}
17+
18+
type ModelPermission struct {
19+
ID string `json:"id"`
20+
Object string `json:"object"`
21+
Created int `json:"created"`
22+
AllowCreateEngine bool `json:"allow_create_engine"`
23+
AllowSampling bool `json:"allow_sampling"`
24+
AllowLogprobs bool `json:"allow_logprobs"`
25+
AllowSearchIndices bool `json:"allow_search_indices"`
26+
AllowView bool `json:"allow_view"`
27+
AllowFineTuning bool `json:"allow_fine_tuning"`
28+
Organization string `json:"organization"`
29+
Group any `json:"group"`
30+
IsBlocking bool `json:"is_blocking"`
31+
}

0 commit comments

Comments
 (0)