Skip to content

Commit b6136de

Browse files
authored
feat: Support http_proxy for chatGPT (#87)
Signed-off-by: Lancelot <[email protected]>
1 parent 4cb075c commit b6136de

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
A 🇨🇳 and 🇺🇸🇬🇧 translate tool in your command line, powered by youdao/iciba/ChatGPT.
66

7-
[![NPM version](https://img.shields.io/npm/v/fanyi.svg?style=flat-square)](https://npmjs.org/package/fanyi)
8-
[![NPM downloads](http://img.shields.io/npm/dm/fanyi.svg?style=flat-square)](https://npmjs.org/package/fanyi)
7+
[![NPM version](https://img.shields.io/npm/v/fanyi.svg?style=flat-square)](https://npmjs.org/package/fanyi) [![NPM downloads](http://img.shields.io/npm/dm/fanyi.svg?style=flat-square)](https://npmjs.org/package/fanyi)
98

109
![](https://gw.alipayobjects.com/zos/rmsportal/EAuwmtfxDGueGOdUgVKc.png)
1110

@@ -150,6 +149,18 @@ Turn off ChatGPT translation.
150149
$ fanyi config --openai-api-key false
151150
```
152151
152+
If we get this error:
153+
154+
> Cannot reach OpenAI api, please check network
155+
156+
We need to ensure that the proxy and $http_proxy is correctly set.
157+
158+
For example(for clash X):
159+
160+
```bash
161+
$ export http_proxy=http://127.0.0.1:7890
162+
```
163+
153164
## Error: spawn festival ENOENT
154165
155166
Try this workaround from [say.js](https://github.com/Marak/say.js#linux-notes) in Linux.

lib/print.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const chalk = require('chalk');
22
const ora = require('ora');
33
const gradient = require('gradient-string');
44
const { saveHistory } = require('./searchHistory');
5+
const { HttpsProxyAgent } = require('https-proxy-agent');
56
require('isomorphic-unfetch');
67

78
exports.iciba = function (data, options = {}) {
@@ -165,29 +166,45 @@ exports.chatgpt = async (word, options) => {
165166
chalk = initChalkWithNoColor();
166167
}
167168
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+
171180
const spinner = ora().start();
172-
return api.sendMessage(`
181+
return api
182+
.sendMessage(
183+
`
173184
你是一本中英文字典,请给出这个词的翻译、音标或拼音、同义词和近义词、并提供相关例句:${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();
181207
});
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-
});
191208
};
192209

193210
function log(message, indentNum) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"fs-extra": "^10.1.0",
4545
"gradient-string": "^2.0.2",
4646
"html-entities": "^1.1.2",
47+
"https-proxy-agent": "^5.0.1",
4748
"is-chinese": "^1.2.5",
4849
"isomorphic-unfetch": "^4.0.2",
4950
"needle": "^3.1.0",

0 commit comments

Comments
 (0)