@@ -27,34 +27,89 @@ func init() {
2727}
2828
2929func 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}
0 commit comments