77 "fmt"
88 "io/ioutil"
99 "net/http"
10+ "net/url"
11+ "start-feishubot/initialization"
1012 "start-feishubot/services/loadbalancer"
1113 "strings"
1214 "time"
@@ -49,9 +51,10 @@ type ChatGPTRequestBody struct {
4951 PresencePenalty int `json:"presence_penalty"`
5052}
5153type ChatGPT struct {
52- Lb * loadbalancer.LoadBalancer
53- ApiKey []string
54- ApiUrl string
54+ Lb * loadbalancer.LoadBalancer
55+ ApiKey []string
56+ ApiUrl string
57+ HttpProxy string
5558}
5659
5760type ImageGenerationRequestBody struct {
@@ -68,8 +71,9 @@ type ImageGenerationResponseBody struct {
6871 } `json:"data"`
6972}
7073
71- func (gpt ChatGPT ) sendRequest (url , method string ,
72- requestBody interface {}, responseBody interface {}) error {
74+ func (gpt ChatGPT ) doRequest (url , method string ,
75+ requestBody interface {}, responseBody interface {},
76+ client * http.Client ) error {
7377 api := gpt .Lb .GetAPI ()
7478 if api == nil {
7579 return errors .New ("no available API" )
@@ -87,7 +91,7 @@ func (gpt ChatGPT) sendRequest(url, method string,
8791
8892 req .Header .Set ("Content-Type" , "application/json" )
8993 req .Header .Set ("Authorization" , "Bearer " + api .Key )
90- client := & http. Client { Timeout : 110 * time . Second }
94+
9195 response , err := client .Do (req )
9296 if err != nil {
9397 gpt .Lb .SetAvailability (api .Key , false )
@@ -114,6 +118,33 @@ func (gpt ChatGPT) sendRequest(url, method string,
114118 return nil
115119}
116120
121+ func (gpt ChatGPT ) sendRequest (link , method string ,
122+ requestBody interface {}, responseBody interface {}) error {
123+ var err error
124+ client := & http.Client {Timeout : 110 * time .Second }
125+ if gpt .HttpProxy == "" {
126+ err = gpt .doRequest (link , method , requestBody , responseBody , client )
127+ } else {
128+ //fmt.Println("using proxy: " + gpt.HttpProxy)
129+ proxyUrl , err := url .Parse (gpt .HttpProxy )
130+ if err != nil {
131+ return err
132+ }
133+
134+ transport := & http.Transport {
135+ Proxy : http .ProxyURL (proxyUrl ),
136+ }
137+ proxyClient := & http.Client {
138+ Transport : transport ,
139+ Timeout : 110 * time .Second ,
140+ }
141+
142+ err = gpt .doRequest (link , method , requestBody , responseBody , proxyClient )
143+ }
144+
145+ return err
146+ }
147+
117148func (gpt ChatGPT ) Completions (msg []Messages ) (resp Messages , err error ) {
118149 requestBody := ChatGPTRequestBody {
119150 Model : engine ,
@@ -128,8 +159,11 @@ func (gpt ChatGPT) Completions(msg []Messages) (resp Messages, err error) {
128159 err = gpt .sendRequest (gpt .ApiUrl + "/v1/chat/completions" , "POST" ,
129160 requestBody , gptResponseBody )
130161
131- if err == nil {
162+ if err == nil && len ( gptResponseBody . Choices ) > 0 {
132163 resp = gptResponseBody .Choices [0 ].Message
164+ } else {
165+ resp = Messages {}
166+ err = errors .New ("openai 请求失败" )
133167 }
134168 return resp , err
135169}
@@ -165,11 +199,15 @@ func (gpt ChatGPT) GenerateOneImage(prompt string, size string) (string, error)
165199 return b64s [0 ], nil
166200}
167201
168- func NewChatGPT (apiKeys []string , apiUrl string ) * ChatGPT {
202+ func NewChatGPT (config initialization.Config ) * ChatGPT {
203+ apiKeys := config .OpenaiApiKeys
204+ apiUrl := config .OpenaiApiUrl
205+ httpProxy := config .HttpProxy
169206 lb := loadbalancer .NewLoadBalancer (apiKeys )
170207 return & ChatGPT {
171- Lb : lb ,
172- ApiKey : apiKeys ,
173- ApiUrl : apiUrl ,
208+ Lb : lb ,
209+ ApiKey : apiKeys ,
210+ ApiUrl : apiUrl ,
211+ HttpProxy : httpProxy ,
174212 }
175213}
0 commit comments