ไปอ่านเอกสารของเลิฟเลซ
Lovelace เป็นเว็บแอปพลิเคชั่นที่อนุญาตให้โต้ตอบกับ ChatGPT ได้ฟรีโดยใช้ไลบรารี่ GPT4FREE ของ Python ซอฟต์แวร์นี้เขียนด้วย JavaScript โดยใช้ NodeJS + Express + SocketIO บนฝั่งเซิร์ฟเวอร์และ Vite + ReactJS บนส่วนหน้า
แบ็กเอนด์ช่วยให้ไคลเอ็นต์ต่างๆ สื่อสารกับ ChatGPT ได้ หากจุดประสงค์ของคุณกับ Lovelace คือการใช้มันเพื่อจุดประสงค์ของคุณเอง คุณสามารถติดตั้งเซิร์ฟเวอร์แบ็กเอนด์ในเครือข่ายของคุณได้เท่านั้น และไม่ต้องสนใจอีกด้านหนึ่งของแอปพลิเคชัน นั่นก็คือ ไคลเอนต์ แบ็กเอนด์อนุญาตให้โต้ตอบผ่าน API หรือคุณสามารถใช้การเชื่อมต่อโดย WebSocket โดยใช้ไคลเอ็นต์ SocketIO บางตัว
สารบัญ:
การติดตั้ง Lovelace บนคอมพิวเตอร์หรือเซิร์ฟเวอร์ของคุณนั้นค่อนข้างง่าย คุณไม่ควรมีปัญหายุ่งยากใด ๆ ในกระบวนการนี้ อย่างไรก็ตาม ก่อนที่คุณจะเริ่มโคลนพื้นที่เก็บข้อมูล ตรวจสอบให้แน่ใจว่าคุณมี NodeJS v18.0.0
และ Python v3.10
เป็นอย่างน้อย
โปรดทราบว่า ในกรณีที่คุณไม่ได้ติดตั้งเวอร์ชัน NodeJS ที่จำเป็นบนระบบของคุณ คุณสามารถใช้ตัวจัดการเวอร์ชัน NVM (Node Version Manager)
ได้
# Installing NVM on your system...
export NVM_DIR= " $HOME /.nvm " && (
git clone https://github.com/nvm-sh/nvm.git " $NVM_DIR "
cd " $NVM_DIR "
git checkout ` git describe --abbrev=0 --tags --match " v[0-9]* " $( git rev-list --tags --max-count=1 ) `
) && . " $NVM_DIR /nvm.sh "
# Once NVM has been installed, we proceed to install the specified NodeJS version (> 18.0.0)
nvm install 18.0.0
หากคุณไม่มี Python v3.10
บนระบบของคุณ คุณอาจพิจารณาสิ่งต่อไปนี้:
# (DEBIAN)
sudo add-apt-repository ppa:deadsnakes/ppa && sudo apt update && sudo apt install python3.10
# (MacOS)
brew install [email protected]
ในทำนองเดียวกัน ให้พิจารณาติดตั้ง pip บนระบบของคุณ เนื่องจากจะใช้เมื่อติดตั้งโมดูลที่จำเป็นเพื่อให้สามารถติดตั้งเซิร์ฟเวอร์แบ็กเอนด์ภายในเครือข่ายได้
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
ตอนนี้ สมมติว่าคุณได้ติดตั้งการพึ่งพาดังกล่าวข้างต้นไว้ในระบบของคุณแล้ว เราสามารถดำเนินการขั้นตอนต่อไปได้...
ก่อนที่เราจะเริ่มการติดตั้งและกำหนดค่าเซิร์ฟเวอร์แบ็กเอนด์เป็นส่วนหน้าของ Lovelace เราจำเป็นต้องโคลนพื้นที่เก็บข้อมูล Github ซึ่งมีซอร์สโค้ดของแอปพลิเคชันอยู่
พิจารณาว่า ณ จุดนี้ของการอ่าน ฉันคิดว่าคุณมี Python เวอร์ชัน 3.10 หรือสูงกว่าและ NodeJS เวอร์ชัน 18.0.0 อยู่แล้ว ในการอ่านครั้งก่อน ก่อนที่จะดำเนินการติดตั้งต่อไป มีการอธิบายขั้นตอนในการติดตั้งแต่ละรายการ
# Cloning the Github repository that contains the source code (I stole it from a cyber).
git clone https://github.com/CodeWithRodi/Lovelace/ && cd Lovelace
# Accessing the "Client" folder, which stores the source code of the
# Vite + ReactjS (Frontend) application, and then installing its required modules from NPM.
cd Server && npm install --force && pip install -r Requirements.txt
# Like the previous line, we access the "Server" folder that houses the source code
# for the Lovelace Backend, then we install the NPM packages required to mount on the network.
cd ../Client && npm install --force
คุณอาจต้องการรันทุกอย่างในบรรทัดเดียว...
git clone https://github.com/CodeWithRodi/Lovelace/ && cd Lovelace && cd Server && npm install --force && pip install -r Requirements.txt && cd ../Client && npm install --force && cd ..
โปรดทราบว่าเมื่อคุณติดตั้งโมดูลที่จำเป็นเพื่อรันเซิร์ฟเวอร์ คำสั่ง pip install -r Requirements.txt
จะถูกดำเนินการเพื่อติดตั้งแพ็คเกจที่จำเป็นเพื่อให้สามารถใช้ไลบรารี GPT4FREE
ได้ จากหลาม หากคุณไม่ได้ติดตั้ง pip
หรือไม่ได้ติดตั้งแพ็คเกจ Python แม้ว่าคุณจะมีเซิร์ฟเวอร์แบ็กเอนด์และไคลเอนต์ติดตั้งอยู่บนเครือข่าย คุณจะไม่สามารถทำอะไรได้เลย เพราะเมื่อมีการร้องขอ ทั้งผ่าน WebSocket หรือผ่าน API โดยใช้ไลบรารี่ NodeJS python-shell
จากแบ็กเอนด์ การสื่อสารจะถูกส่งไปยังไฟล์ Python ที่เกี่ยวข้องซึ่งรับผิดชอบในการส่งคืนการตอบกลับ และหากไม่มีข้อกำหนดที่จำเป็น ก็จะเกิดข้อผิดพลาด
เมื่อคุณโคลนพื้นที่เก็บข้อมูล Github แล้ว เราก็สามารถไปยังการกำหนดค่าและการติดตั้งเครือข่ายทั้งแอปพลิเคชันฟรอนต์เอนด์และแบ็กเอนด์ได้ อย่างไรก็ตาม เราจะมาเจาะลึกลงไปอีกหน่อยว่าอะไรจะจัดเก็บโฟลเดอร์ย่อยแต่ละโฟลเดอร์ที่มีโฟลเดอร์ที่สร้างขึ้นโดยการโคลนพื้นที่เก็บข้อมูล
โฟลเดอร์ | คำอธิบาย |
---|---|
ลูกค้า | โฟลเดอร์ "ไคลเอนต์" เก็บซอร์สโค้ดของแอปพลิเคชัน Vite + React นั่นคือฟรอนต์เอนด์ Lovelace ซึ่งคุณสามารถติดตั้งเว็บไซต์บนเครือข่ายของคุณเพื่อให้สามารถสื่อสารกับแบ็กเอนด์และสร้างการสนทนาที่มีคุณภาพด้วย AI! |
เอกสารประกอบ | โฟลเดอร์ "Documentation" จะเป็นที่เก็บซอร์สโค้ดของเอกสารซอฟต์แวร์: https://lovelace-docs.codewithrodi.com/ |
เซิร์ฟเวอร์ | โฟลเดอร์ "เซิร์ฟเวอร์" เป็นที่เก็บซอร์สโค้ดสำหรับแบ็กเอนด์ Lovelace ซึ่งสร้างขึ้นภายใต้ NodeJS โดยใช้ Express เพื่อจัดเตรียม API และ SocketIO สำหรับการส่งการตอบสนองภายใต้ WebSocket |
นอกจากนี้ นอกเหนือจากโฟลเดอร์ต่างๆ แล้ว คุณจะพบไฟล์บางไฟล์ซึ่งจะแสดงพร้อมกับคำอธิบายด้านล่างในลักษณะเดียวกัน
ไฟล์ | คำอธิบาย |
---|---|
.clocignore | มันถูกใช้โดยซอฟต์แวร์ "cloc" ซึ่งช่วยให้สามารถนับบรรทัดของโค้ดซอฟต์แวร์ โดยแยกตามเทคโนโลยีและความคิดเห็นที่ภาษาการเขียนโปรแกรมที่ใช้อาจมี ภายในไฟล์ "clocignore" มีเส้นทางของไฟล์และไดเร็กทอรีเหล่านั้นที่ซอฟต์แวร์ต้องละเว้นเมื่อทำการนับ |
ใบอนุญาต | ประกอบด้วยใบอนุญาต Lovelace ซึ่งอยู่ภายใต้ซอร์สโค้ดของลูกค้าและเซิร์ฟเวอร์ ซอฟต์แวร์นี้ได้รับอนุญาตภายใต้ใบอนุญาต MIT |
เมื่อพื้นที่เก็บข้อมูลถูกโคลนและคุณได้ติดตั้งโมดูลเซิร์ฟเวอร์และไคลเอ็นต์ NPM ที่จำเป็นในเวลาต่อมา ก็ถึงเวลาตั้งค่าแบ็กเอนด์เพื่อเริ่มใช้งานซอฟต์แวร์
เริ่มจากเซิร์ฟเวอร์กันก่อน นี่คือจุดที่ความมหัศจรรย์จะเกิดขึ้น คุณสามารถสื่อสารกับ AI ผ่านการร้องขอ API หรือใช้ WebSocket ถัดไป คุณจะเห็นชุดคำสั่งเพื่อให้สามารถเมานต์เซิร์ฟเวอร์ภายในเครือข่ายได้
# Accessing the <Server> folder that houses the repository you cloned earlier
cd Server/
# Running the server...
npm run start
หากคุณทำทุกอย่างถูกต้องแล้ว เซิร์ฟเวอร์ควรจะทำงานบนระบบของคุณอยู่แล้ว คุณสามารถตรวจสอบได้หากคุณเข้าถึง http://0.0.0.0:8000/api/v1/
!
สคริปต์ ( npm run <script_name> ) | คำอธิบาย |
---|---|
เริ่ม | การดำเนินการตามปกติของเซิร์ฟเวอร์เริ่มต้นขึ้น คุณสามารถพิจารณาตัวเลือกนี้ได้ในกรณีที่คุณต้องการติดตั้งในการใช้งานจริง |
ผู้พัฒนา | เริ่มการทำงานของเซิร์ฟเวอร์ในโหมดการพัฒนาด้วยความช่วยเหลือของแพ็คเกจ "nodemon" |
คุณควรทราบว่าตัวแปรสภาพแวดล้อมเป็นค่าอักขระแบบไดนามิก ซึ่งช่วยให้คุณสามารถจัดเก็บข้อมูลที่เกี่ยวข้องกับข้อมูลประจำตัว การกำหนดค่า ฯลฯ... จากนั้นคุณจะเห็นไฟล์ ".env" ที่อยู่ในซอร์สโค้ดของเซิร์ฟเวอร์ ซึ่งในทางกลับกัน คุณจะมีคำอธิบายเกี่ยวกับการทำงานของตัวแปรที่มีอยู่
# Specifies the execution mode of the server, considers the value of <NODE_ENV>
# can be <development> and <production>.
NODE_ENV = production
# Address of the server where the client application is running.
CLIENT_HOST = https://lovelace.codewithrodi.com/
# Port where the server will
# start executing over the network.
SERVER_PORT = 8000
# Hostname where the server will be launched in
# complement with the previously established
# port on the network.
SERVER_HOST = 0.0.0.0
# If you have an SSL certificate, you must
# specify the certificate and then the key.
SSL_CERT =
SSL_KEY =
# Others...
CORS_ORIGIN = *
BODY_MAX_SIZE = 100kb
สมมติว่า ณ จุดนี้การอ่านคุณได้ตั้งค่าเซิร์ฟเวอร์แบ็คเอนด์บนเครือข่ายแล้ว เราสามารถตั้งค่าเซิร์ฟเวอร์ของลูกค้าต่อไปได้ โดยด้วยวิธีนี้ คุณสามารถเริ่มโต้ตอบกับ AI ผ่านเว็บไซต์ที่คุณ จะกำหนดค่าต่อไป...
แอปพลิเคชันไคลเอ็นต์สร้างขึ้นด้วย ReactJS โดยใช้ Vite เป็นเครื่องมือในการพัฒนา ด้วยคำสั่งเทอร์มินัลเพียงไม่กี่คำสั่ง คุณสามารถตั้งค่าและปรับใช้แอปพลิเคชันบนเครือข่ายของคุณได้อย่างรวดเร็วในเวลาอันรวดเร็ว! ด้วยการทำตามคำแนะนำของเราและใช้พลังของ ReactJS และ Vite คุณจะได้สัมผัสกับกระบวนการตั้งค่าที่ราบรื่นและมีประสิทธิภาพ
ตรวจสอบให้แน่ใจว่า เพื่อที่จะใช้งานเว็บแอปพลิเคชั่นได้อย่างถูกต้อง จำเป็นที่เซิร์ฟเวอร์กำลังทำงานบนเครือข่ายอยู่แล้ว
# Accessing the existing <Client> folder within the cloned repository
cd Client/
# Assuming you have already installed the necessary npm packages <npm install --force>
# we will proceed to start the server in development mode
npm run dev
ขอให้มีความสุขในการแฮ็ก!... เซิร์ฟเวอร์ของคุณควรทำงานที่ http://0.0.0.0:5173/
ในทำนองเดียวกันกับที่เคยทำในการอ่านครั้งก่อน รายการตัวแปรสภาพแวดล้อมที่แอปพลิเคชันไคลเอนต์มีในไฟล์ ".env" จะถูกนำเสนอต่อไป พร้อมด้วยคำอธิบาย
# Address where the backend server was mounted, you must
# be sure to specify in the address if you have ridden
# the server under HTTPS, changing <http> to <https> ;)
VITE_SERVER = http://0.0.0.0:8000
# The server has a suffix to be able to access its respective API
# in this case we use v1
VITE_API_SUFFIX = /api/v1
# Others...
VITE_DONATE_LINK = https://ko-fi.com/codewithrodi
VITE_GPT4FREE_LINK = https://github.com/xtekky/gpt4free
VITE_SOFTWARE_REPOSITORY_LINK = https://github.com/codewithrodi/Lovelace
ในกรณีที่คุณต้องการแก้ไขที่อยู่เครือข่ายหรือพอร์ตที่ใช้เมื่อเปิดใช้งานเซิร์ฟเวอร์ Vite บนเครือข่าย คุณสามารถพิจารณาแก้ไขไฟล์ vite.config.js
ได้ ไฟล์นี้มีการตั้งค่าสำหรับเซิร์ฟเวอร์ Vite ด้านล่างนี้คือเนื้อหาของไฟล์ vite.config.js
:
export default defineConfig ( {
plugins : [ react ( ) ] ,
server : {
// If you want to change the network address where the server will be mounted
// you must change <0.0.0.0> to the desired one.
host : '0.0.0.0' ,
// Following the same line above, you must modify the port <5173>
// for which you want to ride on the network.
port : 5173
} ,
define : {
global : { }
}
} ) ;
โปรดทราบว่าการแก้ไขการตั้งค่าเหล่านี้ควรทำด้วยความระมัดระวัง เนื่องจากอาจส่งผลต่อการเข้าถึงเซิร์ฟเวอร์ ตรวจสอบให้แน่ใจว่าได้เลือกที่อยู่เครือข่ายที่เหมาะสมและพอร์ตที่ยังไม่ได้ใช้งาน
Vite เป็นตัวเลือกยอดนิยมสำหรับการพัฒนาเว็บแอปพลิเคชันที่เขียนด้วย JavaScript เนื่องจากมีสภาพแวดล้อมที่มีประสิทธิภาพสูง มีข้อได้เปรียบที่สำคัญ เช่น ลดเวลาเริ่มต้นลงอย่างมากเมื่อโหลดโมดูลใหม่หรือรวบรวมซอร์สโค้ดในระหว่างกระบวนการพัฒนา ด้วยการใช้ประโยชน์จาก Vite นักพัฒนาจะได้สัมผัสกับประสิทธิภาพการผลิตที่ดีขึ้นและวงจรการพัฒนาที่เร็วขึ้น การเพิ่มประสิทธิภาพความเร็วและประสิทธิภาพทำให้เป็นเครื่องมือที่มีค่าสำหรับโครงการพัฒนาเว็บไซต์
เว็บแอปพลิเคชันมีความสามารถในการตรวจจับภาษาของเว็บเบราว์เซอร์ที่เข้าถึงแพลตฟอร์ม เพื่อให้สามารถตรวจจับได้ในภายหลังว่ามีการแปลเนื้อหาในภาษาที่ร้องขอหรือไม่ หากไม่มีอยู่ การแปลจะถูกส่งกลับ โดยค่าเริ่มต้นซึ่งสอดคล้องกับภาษาอังกฤษ
โปรดทราบว่าหากต้องการเพิ่มการแปลใหม่ คุณสามารถเข้าถึง Client/src/Locale/
โดยที่ Locale/
โฟลเดอร์สุดท้ายนี้เก็บชุดของ JSON ซึ่งอยู่ในรูปแบบต่อไปนี้ {LANGUAGE_IN_ISO_369}.json
; ในกรณีที่คุณต้องการเพิ่มการแปลใหม่ คุณเพียงแค่ต้องปฏิบัติตามรูปแบบและคัดลอกคีย์ที่เกี่ยวข้องซึ่งมีการอัพเดตค่าเป็นภาษาที่คุณต้องการที่คุณกำลังสร้าง
ปัจจุบันมีคำแปลต่อไปนี้ในเว็บแอปพลิเคชัน: French - Arabic - Chinese - German - English - Spanish - Italian - Portuguese - Russian - Turkish
หากความตั้งใจของคุณคือการใช้ Lovelace ตามความต้องการและวัตถุประสงค์ส่วนบุคคลของคุณ คุณอาจเพิกเฉยต่อแอปพลิเคชันไคลเอนต์ที่ใช้งานใน ReactJS ให้หันเหความสนใจของคุณไปที่เซิร์ฟเวอร์แทน เนื่องจากเป็นที่ที่การร่ายมนตร์เกิดขึ้นอย่างแท้จริง
โปรดทราบว่าเมื่อสื่อสารกับแบ็กเอนด์โดยใช้ API หรือของ WebSocket ข้อมูลที่ส่งเป็น Model or Role
จะไม่คำนึงถึงขนาดตัวพิมพ์ นั่นคือ หากค่าของ Model
เป็น gPT-3.5-TUrbO
จะไม่มีความสำคัญ เนื่องจากจะเป็น ที่จัดรูปแบบจากแบ็กเอนด์ Prompt
ก็เห็นได้ชัดว่าไม่สำคัญเช่นกัน แต่ค่าที่กำหนดให้กับ Provider
คือ ในการอ่านในภายหลัง คุณจะได้เรียนรู้วิธีรับผู้ให้บริการที่มีอยู่เพื่อให้สามารถใช้งานได้เมื่อสร้างการโต้ตอบกับ AI ในลักษณะเดียวกับที่คุณจะสามารถรู้ว่าโมเดลที่เกี่ยวข้องคืออะไร หรือคุณสามารถเข้าถึงเส้นทางเดียวกัน /api/v1/chat/providers/
ของอินสแตนซ์สาธารณะของแบ็กเอนด์ lovelace และดูข้อมูลได้
นี่คือตัวอย่างการใช้ API ผ่านฟังก์ชัน Fetch ดั้งเดิม:
const Data = {
// Select the model you want to use for the request.
// <GPT-3.5-Turbo> | <GPT-4>
Model : 'GPT-3.5-Turbo' , // Recommended Model
// Use a provider according to the model you used, consider
// that you can see the list of providers next to the models
// that have available in:
// [GET REQUEST]: http://lovelace-backend.codewithrodi.com/api/v1/chat/providers/
Provider : 'GetGpt' , // Recommended Provider, you can also use 'DeepAi'
// GPT Role
Role : 'User' ,
// Prompt that you will send to the model
Prompt : 'Hi Ada, Who are you?'
} ;
// Note that if you want to use your own instance replace
// <https://lovelace-backend.codewithrodi.com> for the address
// from your server, or <http://0.0.0.0:8000> in case it is
// is running locally.
const Endpoint = 'https://lovelace-backend.codewithrodi.com/api/v1/chat/completions' ;
// We will make the request with the Fetch API provided in a way
// native by JavaScript, specified in the first instance
// the endpoint where our request will be made, while as a second
// parameter we specify by means of an object the method, the header and the
// body that will have the request.
fetch ( Endpoint , {
// /api/v1/chat/completions/
method : 'POST' ,
// We are sending a JSON, we specify the format
// in the request header
headers : { 'Content-Type' : 'application/json' } ,
body : JSON . stringify ( Data )
} )
// We transform the response into JSON
. then ( ( Response ) => Response . json ( ) )
// Once the response has been transformed to the desired format, we proceed
// to display the response from the AI in the console.
. then ( ( Response ) => console . log ( Response . Data . Answer ) )
// Consider that <Response> has the following structure
// Response -> { Data: { Answer: String }, Status: String(Success | ClientError) }
. catch ( ( RequestError ) => console . error ( RequestError ) ) ;
ในกรณีที่คุณต้องการใช้ Axios ในการสื่อสาร คุณสามารถพิจารณา:
const Axios = require ( 'axios' ) ;
const Data = {
Model : 'GPT-3.5-Turbo' , // Recommended Model
Provider : 'GetGpt' , // Recommended Provider, you can also use 'DeepAi'
// GPT Role
Role : 'User' ,
Prompt : 'Hi Ada, Who are you?'
} ;
const Endpoint = 'https://lovelace-backend.codewithrodi.com/api/v1/chat/completions' ;
( async function ( ) {
const Response = ( await Axios . post ( Endpoint , Data , { headers : { 'Content-Type' : 'application/json' } } ) ) . data ;
console . log ( Response . Data . Answer ) ;
} ) ( ) ;
คุณสามารถดูวิธีที่ไคลเอ็นต์สื่อสารกับแบ็คเอนด์ผ่าน API ได้โดยดูที่ไฟล์ Client/src/Services/Chat/Context.jsx
และ Client/src/Services/Chat/Service.js
ซึ่งเกิดความมหัศจรรย์ขึ้น
จากเซิร์ฟเวอร์แบ็กเอนด์ เซิร์ฟเวอร์ของ WebSocket ได้รับความช่วยเหลือจาก SocketIO ดังนั้นจึงขอแนะนำให้ใช้ไคลเอ็นต์ที่มาจากไลบรารีเดียวกัน เช่น npm i socket.io-client
ในกรณีของ NodeJS ขอแนะนำให้ใช้การสื่อสารประเภทนี้หากคุณต้องการการตอบสนองแบบ "ทันที" เนื่องจากการตอบสนองจาก AI ต่างจากการใช้การสื่อสารผ่าน API คุณไม่ควรรอให้ AI ประมวลผลการตอบสนองเสร็จสิ้นเพื่อที่จะแสดงผล . - การใช้ WebSocket การตอบสนองจาก AI จะถูกส่งเป็นบางส่วน ทำให้เกิดการโต้ตอบกับลูกค้าได้ทันที
const { io } = require ( 'socket.io-client' ) ;
// Using the NodeJS 'readline' module, like this
// allow <Prompts> to be created by the user
// to our console application.
const ReadLine = require ( 'readline' ) . createInterface ( {
input : process . stdin ,
output : process . stdout
} ) ;
// We store the address where the Lovelace backend is mounted.
// In case your instance is running locally
// you can change the value of <Endpoint> to something like <http://0.0.0.0:8000>.
const Endpoint = 'http://lovelace-backend.codewithrodi.com/' ;
( async function ( ) {
const Socket = io ( Endpoint ) . connect ( ) ;
console . log ( `Connecting to the server... [ ${ Endpoint } ]` ) ;
Socket . on ( 'connect' , ( ) => {
console . log ( 'Connected, happy hacking!' ) ;
RunApplicationLoop ( ) ;
} ) ;
Socket . on ( 'disconnect' , ( ) => {
console . log ( 'nDisconnected, bye bye...!' ) ;
process . exit ( 0 ) ;
} ) ;
// We use <process.stdout.write(...)> instead of <console.log(...)> because
// in this way we print directly to the console without each time
// that a part of the response is received, a new line (n) is executed.
Socket . on ( 'Response' , ( StreamedAnswer ) => process . stdout . write ( StreamedAnswer ) ) ;
const BaseQuery = {
// We indicate the model that we want to use to communicate with the AI
// 'GPT-3.5-Turbo' - 'GPT-4'
Model : 'GPT-3.5-Turbo' ,
// Provider to use in the communication, keep in mind that not all
// providers offer ChatGPT 3.5 or ChatGPT 4. You can make a request
// [GET] to <https://lovelace-backend.codewithrodi.com/api/v1/chat/providers/>
Provider : 'GetGpt' ,
Role : 'User' ,
} ;
const HandleClientPrompt = ( ) => new Promise ( ( Resolve , Reject ) => {
const HandleStreamedResponseEnd = ( MaybeError ) => {
if ( MaybeError ) {
return Reject ( MaybeError ) ;
}
Resolve ( ) ;
} ;
ReadLine . question ( 'Prompt > ' , ( Prompt ) => {
// We issue <Prompt> to the server, where as the second parameter
// send the Query to it, specifying the Model, Provider, Role and Prompt.
// The last parameter corresponds to the Callback that will be called
// once the transmission of the response is finished, consider that this
// callback receives a parameter, which corresponds to whether there is an error
// or not during transmission, its content is therefore the error.
Socket . emit ( 'Prompt' , { Prompt , ... BaseQuery } , HandleStreamedResponseEnd ) ;
} ) ;
} ) ;
const RunApplicationLoop = async ( ) => {
while ( true ) {
await HandleClientPrompt ( ) ;
console . log ( 'n' ) ;
}
} ;
} ) ( ) ;
หากคุณต้องการสร้างการสื่อสารกับ Lovelace Backend ผ่าน WebSocket ในภาษาอื่นนอกเหนือจากที่นำเสนอ คุณสามารถพิจารณา:
ลองพิจารณาว่า แม้ว่าไลบรารี Python GPT4FREE
จะถูกนำมาใช้ภายในแบ็กเอนด์ แต่ผู้ให้บริการรายหลังก็แตกต่างจากที่ Lovelace นำเสนอ คุณสามารถรับรายชื่อผู้ให้บริการที่มีอยู่ได้โดยใช้ API ซึ่งคุณจะได้รับข้อมูล เช่น โมเดลที่อนุญาตให้ใช้ ที่อยู่เว็บที่โฮสต์บริการ และชื่อที่ต้องระบุเมื่อโต้ตอบกับ AI เช่น คุณได้เห็นในตัวอย่าง ก่อนหน้า (API, WS)
คำตอบที่คุณควรได้รับจาก https://lovelace-backend.codewithrodi.com/api/v1/chat/providers/
ควรเป็น:
{
"Status" : " Success " ,
"Data" :{
"Providers" :{
// List of providers available to use on WebSocket's
"WS" :[
{
// Name to specify when making the query
"Name" : " DeepAi " ,
// Web address where the service is hosted
"Website" : " https://deepai.org " ,
// Available models
"Models" :[ " gpt-3.5-turbo " ]
},
// ! Others WebSocket's providers...
{ "Name" : " Theb " , "Website" : " https://theb.ai " , "Models" :[ " gpt-3.5-turbo " ] },
{ "Name" : " Yqcloud " , "Website" : " https://chat9.yqcloud.top/ " , "Models" :[ " gpt-3.5-turbo " ] },
{ "Name" : " You " , "Website" : " https://you.com " , "Models" :[ " gpt-3.5-turbo " ] },
{ "Name" : " GetGpt " , "Website" : " https://chat.getgpt.world/ " , "Models" :[ " gpt-3.5-turbo " ] }
],
// List of Providers available to be able to use through the API
"API" :[
{
// Name to specify when making the query
"Name" : " Aichat " ,
// Web address where the service is hosted
"Website" : " https://chat-gpt.org/chat " ,
// Available models
"Models" :[ " gpt-3.5-turbo " ]
},
// ! Others API providers...
{ "Name" : " ChatgptLogin " , "Website" : " https://chatgptlogin.ac " , "Models" :[ " gpt-3.5-turbo " ] },
{ "Name" : " DeepAi " , "Website" : " https://deepai.org " , "Models" :[ " gpt-3.5-turbo " ] },
{ "Name" : " Yqcloud " , "Website" : " https://chat9.yqcloud.top/ " , "Models" :[ " gpt-3.5-turbo " ] },
{ "Name" : " You " , "Website" : " https://you.com " , "Models" :[ " gpt-3.5-turbo " ] },
{ "Name" : " GetGpt " , "Website" : " https://chat.getgpt.world/ " , "Models" :[ " gpt-3.5-turbo " ] }
]
}
}
}
ดังที่คุณเห็นแล้ว รายชื่อผู้ให้บริการแบ่งออกเป็น 2 ส่วน ส่วนแรกสำหรับการสืบค้นที่ทำผ่าน API ในขณะที่ส่วนที่ใช้ WebSocket
ต่างจากตัวอย่างอื่นๆ ที่มีการร้องขอไปยังเซิร์ฟเวอร์แบ็กเอนด์ของ Lovelace การได้รับรายชื่อผู้ให้บริการและรุ่นที่เกี่ยวข้องนั้นค่อนข้างเป็นงานที่ค่อนข้างง่าย เนื่องจากเราต้องส่งคำขอ [GET] ไปที่ /api/v1 /chat/providers/
เท่านั้น โดยที่การตอบกลับจะเป็น JSON ที่แสดงให้คุณเห็นก่อนหน้านี้
สำหรับตัวอย่างต่อไปนี้ เราจะใช้ Axios ภายใน NodeJS ซึ่งคุณสามารถติดตั้งได้โดยใช้ตัวจัดการแพ็กเกจ NPM โดยใช้คำสั่ง npm i axios
const Axios = require ( 'axios' ) ;
( async function ( ) {
// Consider that, you can replace <https://lovelace-backend.codewithrodi.com> with
// the address where your backend server is mounted. If t