api接口申请 Tips:最近openai的api已经被墙了,所以需要科学上网。 注册openai账号。 生成apikey,初始赠送18美金使用额度,额度用完之后需要自行购买,具体的价格和模型相关。 api接入 Tips:准备好申请的key和梯子后,就可以开始我们的openai之旅了。以下示例以python执行。 安装openai模块 pipinstallopenai 调用openai接口 importopenai importos openai。apikey(申请的key) modelengineidtextdavinci002 prompt帮我生成一段话,因为工作的原因,错误过了和女朋友的约会 completionsopenai。Completion。create( enginemodelengineid, promptprompt, maxtokens60, ) messagecompletions。choices〔0〕。text。strip() print(message) 查看输出结果 Imsorryformissingourdate。Iwasreallylookingforwardtoseeingyou,butsomethingcameupatworkandIhadtostaylate。Ihopeyouunderstand。 自建代理 Tips:没有梯子或者没有海外云服务器的话,可以自建代理。使用cloudFlare作为域名解析服务,原来有一个闲置的域名在阿里云,将ns解析的dns服务器转到cloudFlare 参考域名解析从阿里云转向cloudFlare 1、cloudFlare下准备域名 注册Cloudflare账号,然后将阿里云上的域名添加到CloudFlare 网站添加站点输入要托管的域名添加站点 解析前的dns服务器 将阿里云上的dns服务器 ns1。bdydns。cn ns2。bdydns。cn 改成 konnor。ns。cloudflare。com nadia。ns。cloudflare。com 2、CloudflareWorkers搭建代理 参考worker创建 workers路由管理workers创建服务 将如下代码写入编辑框并保存 Websiteyouintendedtoretrieveforusers。 constupstreamapi。openai。com Custompathnamefortheupstreamwebsite。 constupstreampath Websiteyouintendedtoretrieveforusersusingmobiledevices。 constupstreammobileupstream Countriesandregionswhereyouwishtosuspendyourservice。 constblockedregion〔〕 IPaddresseswhichyouwishtoblockfromusingyourservice。 constblockedipaddress〔0。0。0。0,127。0。0。1〕 WhethertouseHTTPSprotocolforupstreamaddress。 consthttpstrue Whethertodisablecache。 constdisablecachefalse Replacetexts。 constreplacedict{ upstream:customdomain, } addEventListener(fetch,event{ event。respondWith(fetchAndApply(event。request)); }) asyncfunctionfetchAndApply(request){ constregionrequest。headers。get(cfipcountry)。toUpperCase(); constipaddressrequest。headers。get(cfconnectingip); constuseragentrequest。headers。get(useragent); letresponsenull; leturlnewURL(request。url); leturlhostnameurl。hostname; if(httpstrue){ url。protocolhttps:; }else{ url。protocolhttp:; } if(awaitdevicestatus(useragent)){ varupstreamdomainupstream; }else{ varupstreamdomainupstreammobile; } url。hostupstreamdomain; if(url。pathname){ url。pathnameupstreampath; }else{ url。pathnameupstreampathurl。pathname; } if(blockedregion。includes(region)){ responsenewResponse(Accessdenied:WorkersProxyisnotavailableinyourregionyet。,{ status:403 }); }elseif(blockedipaddress。includes(ipaddress)){ responsenewResponse(Accessdenied:YourIPaddressisblockedbyWorkersProxy。,{ status:403 }); }else{ letmethodrequest。method; letrequestheadersrequest。headers; letnewrequestheadersnewHeaders(requestheaders); newrequestheaders。set(Host,upstreamdomain); newrequestheaders。set(Referer,url。protocolurlhostname); letoriginalresponseawaitfetch(url。href,{ method:method, headers:newrequestheaders, body:request。body }) connectionupgradenewrequestheaders。get(Upgrade); if(connectionupgradeconnectionupgrade。toLowerCase()websocket){ returnoriginalresponse; } letoriginalresponsecloneoriginalresponse。clone(); letoriginaltextnull; letresponseheadersoriginalresponse。headers; letnewresponseheadersnewHeaders(responseheaders); letstatusoriginalresponse。status; if(disablecache){ newresponseheaders。set(CacheControl,nostore); } newresponseheaders。set(accesscontrolalloworigin,); newresponseheaders。set(accesscontrolallowcredentials,true); newresponseheaders。delete(contentsecuritypolicy); newresponseheaders。delete(contentsecuritypolicyreportonly); newresponseheaders。delete(clearsitedata); if(newresponseheaders。get(xpjaxurl)){ newresponseheaders。set(xpjaxurl,responseheaders。get(xpjaxurl)。replace(upstreamdomain,urlhostname)); } constcontenttypenewresponseheaders。get(contenttype); if(contenttype!nullcontenttype。includes(texthtml)contenttype。includes(UTF8)){ originaltextawaitreplaceresponsetext(originalresponseclone,upstreamdomain,urlhostname); }else{ originaltextoriginalresponseclone。body } responsenewResponse(originaltext,{ status, headers:newresponseheaders }) } returnresponse; } asyncfunctionreplaceresponsetext(response,upstreamdomain,hostname){ lettextawaitresponse。text() vari,j; for(iinreplacedict){ jreplacedict〔i〕 if(iupstream){ iupstreamdomain }elseif(icustomdomain){ ihostname } if(jupstream){ jupstreamdomain }elseif(jcustomdomain){ jhostname } letrenewRegExp(i,g) texttext。replace(re,j); } returntext; } asyncfunctiondevicestatus(useragentinfo){ varagents〔Android,iPhone,SymbianOS,WindowsPhone,iPad,iPod〕; varflagtrue; for(varv0;vagents。length;v){ if(useragentinfo。indexOf(agents〔v〕)0){ flagfalse; break; } } returnflag; } 选择触发器,将自己的域名执行worker(目前国内国内墙了worker的域名,但是没有墙IP,所以将自己的域名指向worker的ip)。添加自定义域。 配置完后,可以尝试下ping自己的域名,此时的ip应该就是指向了CloudFlare。 2、替换官方域名api。openai。com为自身代理域名 importopenai importos openai。apibasehttps:自己的域名v1 openai。apikey(自己的key) modelengineidtextdavinci002 prompt帮我生成一封邮件,约定下周一的见面时间和地点 completionsopenai。Completion。create( enginemodelengineid, promptprompt, maxtokens300, ) messagecompletions。choices〔0〕。text。strip() print(openai。apibase) print(message) 执行结果: