BuilderIO / ai-shell

A CLI that converts natural language to shell commands.
MIT License
4.04k stars 257 forks source link

feat: support for http_proxy & https_proxy #52

Closed cqlzliang closed 1 year ago

cqlzliang commented 1 year ago

Support handle proxy settings for http requests.

steve8708 commented 1 year ago

thanks @cqlzliang! just so I understand, what use case does this solve for? and should we add a note in the README about when and how to use this?

saltbo commented 1 year ago
✖ Request to OpenAI failed with status 400:

"<html>\r\n<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>\r\n<body>\r\n<center><h1>400 Bad Request</h1></center>\r\n<center>The plain HTTP request was sent to HTTPS port</center>\r\n<hr><center>cloudflare</center>\r\n</body>\r\n</html>\r\n"

An error happens when I use a local proxy with v2ray client.

I try this PR can resolve this error. But, I don't understand why?

Because ai-shell worked fine when I used ClashX as my proxy without this PR.

cqlzliang commented 1 year ago

Due to well-known reasons, accessing api.openai.com from mainland China requires the use of a proxy. The common way to set a proxy in a shell environment is to set the http_proxy and https_proxy environment variables. My modification is to support network requests using these two environment variables as the network proxy. Actually, the latest version of axios already supports this feature, but the lower version of axios that openai package depends on does not.

As I mentioned above, setting a network proxy through http_proxy and https_proxy is a common practice, so I think no additional explanation is needed. However, if there is any, it would be better.

cqlzliang commented 1 year ago

@saltbo Are sure ai-shell will use the system proxy when you used ClashX?I use clashxpro on mac and setting it as system proxy, and ai-shell doesn't use proxy when reqeust api.openai.com

saltbo commented 1 year ago

@saltbo Are sure ai-shell will use the system proxy when you used ClashX?I use clashxpro on mac and setting it as system proxy, and ai-shell doesn't use proxy when reqeust api.openai.com

I'm not using the system proxy, I set the env http_proxy and https_proxy as below:

# proxy
PROXY_ADDR="http://docker.for.mac.localhost:6666"
export http_proxy=$PROXY_ADDR
export https_proxy=$PROXY_ADDR
export all_proxy=$PROXY_ADDR
export no_proxy="localtest.rs,localtest.me,localtest,localhost,127.0.0.1"
cqlzliang commented 1 year ago

@saltbo I tried your setting, and ai-shell used the proxy setting. So the issue is my proxy setting, ai-shell support the proxy setting without this PR. For the error happens when you use a local proxy with v2ray client, you can submmit another issue.

@steve8708 I'm so sorry for my mistake, I wiil close this PR.

saltbo commented 1 year ago

@saltbo I tried your setting, and ai-shell used the proxy setting. So the issue is my proxy setting, ai-shell support the proxy setting without this PR. For the error happens when you use a local proxy with v2ray client, you can submmit another issue.

@steve8708 I'm so sorry for my mistake, I wiil close this PR.

I don't understand what happens with the v2ray client, Please tell me if anyone knows something...

saltbo commented 1 year ago

I found this issue, but I don't know why closed. https://github.com/axios/axios/issues/3384

The errors happen with https_proxy=http://example.com

I debug in the v2ray-client, and I found axios used the plain HTTP request, and the curl used CONNECT tunnel

image

The curl works fine, I prefer to think it's axios's problem.

saltbo commented 1 year ago

As for the fact that Clash can work properly, I guess it may have done some special treatment.

AlaEddineBoughanmmi commented 1 year ago

there is a solution proposed here: https://github.com/openai/openai-node/issues/85#issuecomment-1454359657

cqlzliang commented 1 year ago

I found this issue, but I don't know why closed. axios/axios#3384

The errors happen with https_proxy=http://example.com

I debug in the v2ray-client, and I found axios used the plain HTTP request, and the curl used CONNECT tunnel

image

The curl works fine, I prefer to think it's axios's problem.

I have reproduced this problem, but I have not yet identified the root cause of the problem.

JaysonAlbert commented 1 year ago

@saltbo Are sure ai-shell will use the system proxy when you used ClashX?I use clashxpro on mac and setting it as system proxy, and ai-shell doesn't use proxy when reqeust api.openai.com

I'm not using the system proxy, I set the env http_proxy and https_proxy as below:

# proxy
PROXY_ADDR="http://docker.for.mac.localhost:6666"
export http_proxy=$PROXY_ADDR
export https_proxy=$PROXY_ADDR
export all_proxy=$PROXY_ADDR
export no_proxy="localtest.rs,localtest.me,localtest,localhost,127.0.0.1"

I got this error when i use this setting:


┌  AI Shell
│
◐  Loading.   

✖ Request to OpenAI failed with status 403:

{
  "error": {
    "message": "The OpenAI API can only be accessed over HTTPS. You should access https://api.openai.com rather than the current URL.",
    "type": "invalid_request_error"
  }
}
saltbo commented 1 year ago

@JaysonAlbert same as me. are you using v2ray-client too?