@@ -2,6 +2,7 @@ const chalk = require('chalk');
2
2
const ora = require ( 'ora' ) ;
3
3
const gradient = require ( 'gradient-string' ) ;
4
4
const { saveHistory } = require ( './searchHistory' ) ;
5
+ const { HttpsProxyAgent } = require ( 'https-proxy-agent' ) ;
5
6
require ( 'isomorphic-unfetch' ) ;
6
7
7
8
exports . iciba = function ( data , options = { } ) {
@@ -165,29 +166,45 @@ exports.chatgpt = async (word, options) => {
165
166
chalk = initChalkWithNoColor ( ) ;
166
167
}
167
168
const { ChatGPTAPI } = await import ( 'chatgpt' ) ;
168
- const api = new ChatGPTAPI ( {
169
- apiKey : options . openaiApiKey ,
170
- } ) ;
169
+
170
+ const api = process . env . http_proxy
171
+ ? new ChatGPTAPI ( {
172
+ apiKey : options . openaiApiKey ,
173
+ fetch : ( url , options ) => {
174
+ options . agent = new HttpsProxyAgent ( process . env . http_proxy ) ;
175
+ return fetch ( url , options ) ;
176
+ } ,
177
+ } )
178
+ : new ChatGPTAPI ( { apiKey : options . openaiApiKey } ) ;
179
+
171
180
const spinner = ora ( ) . start ( ) ;
172
- return api . sendMessage ( `
181
+ return api
182
+ . sendMessage (
183
+ `
173
184
你是一本中英文字典,请给出这个词的翻译、音标或拼音、同义词和近义词、并提供相关例句:${ word }
174
- ` ) . then ( res => {
175
- spinner . stop ( ) ;
176
- spinner . clear ( ) ;
177
- log ( word + chalk . gray ( ' ~ ChatGPT' ) ) ;
178
- log ( ) ;
179
- res . text . trim ( ) . split ( '\n' ) . forEach ( line => {
180
- log ( options . color === false ? line : gradient . atlas ( line ) ) ;
185
+ ` ,
186
+ )
187
+ . then ( ( res ) => {
188
+ spinner . stop ( ) ;
189
+ spinner . clear ( ) ;
190
+ log ( word + chalk . gray ( ' ~ ChatGPT' ) ) ;
191
+ log ( ) ;
192
+ res . text
193
+ . trim ( )
194
+ . split ( '\n' )
195
+ . forEach ( ( line ) => {
196
+ log ( options . color === false ? line : gradient . atlas ( line ) ) ;
197
+ } ) ;
198
+ log ( ) ;
199
+ log ( chalk . gray ( '-----' ) ) ;
200
+ log ( ) ;
201
+ } )
202
+ . catch ( ( e ) => {
203
+ spinner . stop ( ) ;
204
+ spinner . clear ( ) ;
205
+ log ( chalk . yellow ( 'Cannot reach OpenAI api, please check network.' ) ) ;
206
+ log ( ) ;
181
207
} ) ;
182
- log ( ) ;
183
- log ( chalk . gray ( '-----' ) ) ;
184
- log ( ) ;
185
- } ) . catch ( e => {
186
- spinner . stop ( ) ;
187
- spinner . clear ( ) ;
188
- log ( chalk . yellow ( 'Cannot reach OpenAI api, please check network.' ) ) ;
189
- log ( ) ;
190
- } ) ;
191
208
} ;
192
209
193
210
function log ( message , indentNum ) {
0 commit comments