文章閱讀時(shí)長:大約5分鐘
需求分析:
OpenAI屏蔽了國內(nèi)IP,國內(nèi)網(wǎng)絡(luò)直連ChatGPT會(huì)被block,如何國內(nèi)自己使用呢?
解決方案:
國內(nèi)連接海外服務(wù)器,海外服務(wù)器轉(zhuǎn)接請(qǐng)求到ChatGPT
需要資源:
海外服務(wù)器 + 基礎(chǔ)編程能力 + OpenAI的Key
準(zhǔn)備一臺(tái)海外服務(wù)器(這篇文章介紹如何白嫖一年Amazon服務(wù)器),先安裝NodeJS環(huán)境。
服務(wù)器驗(yàn)證
ssh進(jìn)入你的服務(wù)器,假如你的服務(wù)器ip為123.456.78.9
% ssh root@123.456.78.9
在你的服務(wù)器里,先試下使用cURL可不可以訪問OpenAi。
(記得替換你的key: sk-xxxx,然后幾行一起復(fù)制,在終端里輸入以下代碼)
curl "https://api.openai.com/v1/chat/completions" \
-X POST \
-H "Authorization: Bearer sk-xxxx" \
-H "Content-Type: application/json" \
-d '{ "model": "gpt-3.5-turbo", "messages": [ {"role": "user", "content": "軟件工程師,如何在職場中增加影響力"} ] }'
這個(gè)時(shí)候返回ChatGPT的回答,就證明你的服務(wù)器可以用。如果有其他錯(cuò)誤,要查查你的服務(wù)器,是不是海外ip了。
下面是服務(wù)器配置
安裝NodeJS (LTS版本的)
% curl -fsSL https://raw.githubusercontent.com/tj/n/master/bin/n | bash -s lts
裝完查看node版本號(hào)
% node -v
安裝Nginx (NodeJS本身能運(yùn)行web服務(wù),為什么還要Nginx、Apache這些?可以看看這個(gè)文章,主要是因?yàn)镹ginx有負(fù)載均衡、靜態(tài)緩存這些優(yōu)化。)
% yum install nginx
裝完查看nginx版本號(hào)
% nginx -v
查看nginx配置
% nginx -t
啟動(dòng)nginx
% nginx
啟動(dòng)完nginx后,在瀏覽器輸入你的服務(wù)器ip,訪問到一個(gè)“Welcome to nginx!”的頁面,證明服務(wù)器已經(jīng)可以使用。
編寫代碼:
// 新建 server.js 文件,直接copy代碼的話記得替換sk-xxxxx為你自己的key。
// server.js 文件
var express = require('express'); // 沒有裝過的話可以先安裝: npm install express
var app = express();
var path = require('path');
const https = require('https');
// 要解析application/json
app.use(express.json());
app.post('/chat', (req, res) => {
var auth = req.body.auth;
// 這里是你自己的簡單驗(yàn)證key,可以不要
if (auth === 'aaa') {
var msg = req.body.msg;
try {
PostTextToChat(msg, function(callbackString) {
// 把openAi返回的字段全部透傳給你
res.send(callbackString);
});
} catch (error) {
console.log('error: ' + error)
}
} else {
res.send('Invalid Auth');
}
});
// 向chatGPT發(fā)出Post請(qǐng)求
function PostTextToChat(msg, callback) {
const apiKey = 'sk-xxxxx'; // 這里填寫你的key
const prompt = msg;
const messages = [];
messages.push({
"role": "user",
"content": prompt
});
const postData = JSON.stringify({
"model": "gpt-3.5-turbo",
"messages": messages,
// "max_tokens": 200,
});
const options = {
hostname: 'api.openai.com',
port: 443,
path: '/v1/chat/completions',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apiKey
}
};
const req = https.request(options, (res) => {
let response = '';
res.on('data', (chunk) => {
response += chunk;
});
res.on('end', () => {
response = JSON.parse(response);
callback(response);
});
});
req.on('error', (error) => {
console.error('error~~ ' + error);
callback(error);
});
req.write(postData);
req.end();
}
// 生成服務(wù)器并監(jiān)聽3000端口
var server = app.listen(3000, 'localhost', function () {
var host = server.address().address
var port = server.address().port
console.log('應(yīng)用實(shí)例,訪問地址為 http://%s:%s', host, port)
});
使用工具,把server.js文件復(fù)制到你的服務(wù)器任意路徑下。Mac端可以使用Transmit等工具
配置nginx
剛剛使用的命令 % nginx -t
可以獲取配置路徑,如 /etc/nginx/nginx.conf
修改nginx.conf文件
% vim /etc/nginx/nginx.conf
按i
鍵開始編輯;
編輯好后,按esc鍵,先輸入:
再輸入 wq
這樣就會(huì)保存退出。
配置里主要是proxy_pass http://Localhost:3000;
這行,表示服務(wù)器在監(jiān)聽80端口(你平時(shí)在瀏覽器輸入域名或者ip后,不另外加端口號(hào)的話,默認(rèn)就是80端口),監(jiān)聽到80端口的請(qǐng)求后,會(huì)映射到3000端口。然后你的server.js在監(jiān)聽3000端口,這樣請(qǐng)求就到你的server.js去了。
配置好后重啟下nginx
% nginx -s reload
啟動(dòng)你的server.js
% cd [server.js目錄]
% node server.js
(開啟后當(dāng)前終端就一直掛在這里了。如果你關(guān)掉終端,server.js也會(huì)失效。這個(gè)時(shí)候可以用一個(gè)叫pm2的工具,它可以讓js服務(wù)在后臺(tái)開啟。這個(gè)是后話,有空再寫pm2的東西,現(xiàn)在先測試整個(gè)流程。)
這個(gè)時(shí)候,你已經(jīng)配置好nginx:監(jiān)聽80端口,然后反向路由到3000端口;且開啟了server.js;
當(dāng)你向服務(wù)器發(fā)出請(qǐng)求的時(shí)候,就能收到響應(yīng)了。
如何向服務(wù)器發(fā)出請(qǐng)求
可以在你電腦另外開一個(gè)終端,輸入以下cURL代碼 / 使用Postman
curl "http://123.456.78.9/chat" \
-X POST \
-H "Content-Type: application/json" \
-H 'Accept: application/json' \
-d '{ "auth": "aaa", "msg": "軟件工程師,如何在職場中增加影響力"}'
如果收到大洋彼岸的服務(wù)器的響應(yīng),恭喜你,成功解決了被block的問題了~??
(利用這個(gè)項(xiàng)目,你還能寫App或小程序,給你的小伙伴無障礙使用chatGPT,去炫耀你的技術(shù)吧??)