在家chatgpt!基本上是更好的Google Nest Hub或Amazon Alexa家庭助理。使用OpenAI API建立在Raspberry Pi上。
本指南将解释如何构建自己的。这很简单。您也可以将其用作在Raspberry Pi上构建其他项目的参考。
|
|
echo " export OPENAI_API_KEY='your_api_key_here' " >> ~ /.bashrc && source ~ /.bashrc
LITELLM_API_KEY
的环境变量。有关所有支持提供者的列表,请参见Litellm文档。 echo " export LITELLM_API_KEY='your_api_key_here' " >> ~ /.bashrc && source ~ /.bashrc
--no-build
Flag运行设置脚本,以从Dockerhub汲取最新图像: curl -s https://raw.githubusercontent.com/judahpaul16/gpt-home/main/contrib/setup.sh |
bash -s -- --no-build
重要的是:左侧的图像是出于插图目的。请勿将电池直接连接到Raspberry Pi。使用这样的电池使用UPS或电源。将电池直接连接到Raspberry Pi可能会因电压波动而损坏板。
在连接电池之前,请确保极性是正确的,以避免对覆盆子PI或其他组件损坏。进行更改之前,请断开电源源。
![]() | ![]() |
这是我用来建造我的第一个GPT房屋的零件列表。您可以将其用作构建自己的参考。我还提供了可选的零件,您可以添加这些零件以增强您的设置。要明确,您可以使用任何运行Linux的系统。
核心组件
可选组件
要在Raspberry Pi上配置Wi-Fi,您需要编辑wpa_supplicant.conf
文件并确保在启动时启用无线接口。此方法支持配置多个Wi-Fi网络,适用于无头设置。您也可以使用raspi-config
或nmcli
实用程序来配置Wi-Fi;或者,如果您愿意,请简单地使用以太网连接。
步骤1:创建bash脚本
sudo nano /usr/local/bin/start_wifi.sh
将以下内容添加到脚本:
#! /bin/bash
# Set the interface and SSID details
INTERFACE= " wlan0 "
SSID= " your_wifi_ssid "
PASSWORD= " your_wifi_password "
# Make sure no previous configuration interferes
sudo killall wpa_supplicant
sudo dhcpcd -x $INTERFACE
# Ensure the wireless interface is up
sudo ip link set $INTERFACE up
# Create a wpa_supplicant configuration file
WPA_CONF= " /etc/wpa_supplicant/wpa_supplicant.conf "
wpa_passphrase " $SSID " " $PASSWORD " | sudo tee $WPA_CONF > /dev/null
# Start wpa_supplicant
sudo wpa_supplicant -B -i $INTERFACE -c $WPA_CONF
# Obtain an IP address
sudo dhcpcd $INTERFACE
确保用您的实际WiFi网络的SSID和密码替换your_wifi_ssid
和your_wifi_password
。
步骤2:使脚本可执行
sudo chmod +x /usr/local/bin/start_wifi.sh
步骤3:创建一个SystemD服务文件
sudo nano /etc/systemd/system/start_wifi.service
将以下内容添加到服务文件:
[Unit]
Description =Start WiFi at boot
After =network.target
[Service]
ExecStart =/usr/local/bin/start_wifi.sh
RemainAfterExit =true
[Install]
WantedBy =multi-user.target
步骤4:重新加载SystemD并启用服务
sudo systemctl daemon-reload
sudo systemctl enable start_wifi.service
sudo systemctl start start_wifi.service
现在,您的Raspberry Pi应该在启动时自动连接到Wi-Fi网络。
如果要连接到隐藏的网络或多个网络,请编辑位于/etc/wpa_supplicant/wpa_supplicant.conf
上的wpa_supplicant.conf
文件,并添加以下配置:
network={
priority=1 # Higher priority networks are attempted first
ssid= " Your_Wi-Fi_Name "
psk= " Your_Wi-Fi_Password "
key_mgmt=WPA-PSK
scan_ssid=1 # Hidden network
priority=2
ssid= " Enterprise_Wi-Fi_Name "
key_mgmt=WPA-EAP
eap=PEAP # or TTLS, TLS, FAST, LEAP
identity= " Your_Username "
password= " Your_Password "
phase1= " peaplabel=0 " # or "peapver=0" for PEAPv0
phase2= " auth=MSCHAPV2 " # or "auth=MSCHAP" for MSCHAPv1
}
重新启动wpa_supplicant
服务以应用更改:
sudo systemctl restart wpa_supplicant
有关配置选项的更多信息,请参见WPA_SUPPLICANT示例文件。
在系统上运行此项目之前,请确保您的系统时钟同步,更新软件包列表,并安装Nginx和Docker。设置脚本将为您照顾,但您也可以手动执行此操作。
同步您的系统时钟:
安装chrony
以进行时间同步:
sudo apt install -y chrony # For Debian/Ubuntu
sudo yum install -y chrony # For RHEL/CentOS/Alma
sudo dnf install -y chrony # # For RHEL/CentOS/Alma 9^
sudo zypper install -y chrony # For openSUSE
sudo pacman -S chrony # For Arch Linux
激活和同步时间立即与chrony
:
sudo chronyc makestep
更新您的软件包列表:
定期更新包装列表确保访问最新的软件和安全补丁。
sudo apt update # For Debian/Ubuntu
sudo yum makecache # For RHEL/CentOS/Alma
sudo dnf makecache # For RHEL/CentOS/Alma 9^
sudo zypper refresh # For openSUSE
sudo pacman -Sy # For Arch Linux
启用其他存储库:
对于使用EPEL和其他特殊存储库的系统,您可能需要使它们能够访问更广泛的可用软件包。
对于Debian/Ubuntu:
sudo add-apt-repository universe
sudo apt update
对于Rhel/Centos/Alma和Fedora:
sudo yum install -y epel-release # For RHEL/CentOS/Alma
sudo dnf install -y epel-release # For RHEL/CentOS/Alma 9^
sudo yum makecache --timer # For RHEL/CentOS/Alma
sudo dnf makecache --timer # For RHEL/CentOS/Alma 9^
安装开发工具:
开发工具对于构建软件包和编译软件至关重要。确保您安装了必要的工具。
对于Debian/Ubuntu:
sudo apt install -y build-essential
对于Rhel/Centos/Alma和Fedora:
sudo yum groupinstall -y " Development Tools " # For RHEL/CentOS/Alma
sudo dnf groupinstall -y " Development Tools " # For RHEL/CentOS/Alma 9^
安装系统依赖项
Docker :容器化所必需的。
sudo apt-get install -y docker.io # For Debian/Ubuntu
sudo yum install -y docker # For RHEL/CentOS/Alma
sudo dnf install -y docker # For RHEL/CentOS/Alma 9^
sudo zypper install -y docker # For openSUSE
sudo pacman -S docker # For Arch Linux
然后sudo systemctl enable --now docker
NGINX :Web接口的反向代理所需。
sudo apt-get install -y nginx # For Debian/Ubuntu
sudo yum install -y nginx # For RHEL/CentOS/Alma
sudo dnf install -y nginx # For RHEL/CentOS/Alma 9^
sudo zypper install -y nginx # For openSUSE
sudo pacman -S nginx # For Arch Linux
在运行设置脚本以构建容器之前,应首先确保将OpenAI API密钥导出到环境变量。
LITELLM_API_KEY
的环境变量。有关所有支持提供者的列表,请参见Litellm文档。设置脚本将使用此变量来初始化容器。注意:重新启动后直接执行export
不会持续。
export OPENAI_API_KEY= " your_api_key_here "
另外,您可以将其放在~/.bashrc
文件的末尾。 (受到推崇的)
# export your API Key in here to initialize it at boot
export OPENAI_API_KEY= " your_api_key_here "
# Optional: Anthropic, Mistral, Cohere, HuggingFace, etc.
export LITELLM_API_KEY= " your_api_key_here "
# Optional: Add these aliases to your .bashrc file for easier management
alias gpt-start= " docker exec -it gpt-home supervisorctl start app "
alias gpt-restart= " docker exec -it gpt-home supervisorctl restart app "
alias gpt-stop= " docker exec -it gpt-home supervisorctl stop app "
alias gpt-status= " docker exec -it gpt-home supervisorctl status app "
alias gpt-log= " docker exec -it gpt-home tail -n 100 -f /app/src/events.log "
alias wi-start= " docker exec -it gpt-home supervisorctl start web-interface "
alias wi-restart= " docker exec -it gpt-home supervisorctl restart web-interface && sudo systemctl restart nginx "
alias wi-stop= " docker exec -it gpt-home supervisorctl stop web-interface "
alias wi-status= " docker exec -it gpt-home supervisorctl status web-interface "
alias wi-build= " docker exec -it gpt-home bash -c 'cd /app/src/frontend && npm run build' "
alias wi-log= " tail -n 100 -f /var/log/nginx/access.log "
alias wi-error= " tail -n 100 -f /var/log/nginx/error.log "
alias spotifyd-start= " docker exec -it gpt-home supervisorctl start spotifyd "
alias spotifyd-restart= " docker exec -it gpt-home supervisorctl restart spotifyd "
alias spotifyd-stop= " docker exec -it gpt-home supervisorctl stop spotifyd "
alias spotifyd-status= " docker exec -it gpt-home supervisorctl status spotifyd "
alias spotifyd-log= " docker exec -it gpt-home tail -n 100 -f /var/log/spotifyd.log "
运行source ~/.bashrc
以将更改应用于当前终端会话。
设置脚本将需要一段时间才能运行(900.0S+才能在我的四核Raspberry Pi 4b w/ 1g RAM上构建和设置依赖关系) 。它将安装所有依赖项并构建Docker容器。但是,您可以通过将--no-build
Flag传递给脚本来跳过构建过程;它将安装依赖项,设置防火墙和NGINX,然后从Docker Hub中拉出容器并运行它。
curl -s https://raw.githubusercontent.com/judahpaul16/gpt-home/main/contrib/setup.sh |
bash -s -- --no-build
另外,出于开发目的,在没有--no-build
Flag -v ~/gpt-home:/app
情况docker run
运行setup.sh
。这使您可以更改Raspberry Pi上的项目文件,并查看在容器中反映的更改而无需重建图像。这对于测试代码库的更改很有用。直接运行:
curl -s https://raw.githubusercontent.com/judahpaul16/gpt-home/main/contrib/setup.sh |
bash -s
如果需要使用-it
(交互式终端), --entrypoint /bin/bash
和--rm
(删除流程出口)标志对代码库进行调试或测试更改,也可以交互式运行容器。这将使您进入容器内的外壳会话。另外,如果Conatiner已经运行:
docker exec -it gpt-home bash
这将启动容器,然后将您放入容器内的外壳会话中。
Docker Run Flags的说明
- -tmpfs /run :
Mounts a tmpfs at /run for transient runtime data.
- -tmpfs /run/lock :
Mounts a tmpfs at /run/lock for lock files.
- -privileged :
Grants extended privileges to the container
Necessary for accessing host audio devices.
- -net=host :
Uses the host network stack directly.
May be necessary for avahi-daemon services.
- v /dev/snd:/dev/snd :
Provides access to the host's sound devices.
- v /dev/shm:/dev/shm :
Provides access to shared memory.
- v /usr/share/alsa:/usr/share/alsa:ro :
Maps the ALSA shared data as read-only.
- v /var/run/dbus:/var/run/dbus :
Provides access to the D-Bus system for inter-process communication.
- -mount type=bind,source=/etc/asound.conf,target=/etc/asound.conf :
Binds the host's ALSA configuration to the container.
如果您希望手动运行设置脚本,则可以这样做。使用vim ~/setup.sh
或nano ~/setup.sh
并在以下内容中创建一个脚本:
#! /bin/bash
latest_release= $( curl -s https://api.github.com/repos/judahpaul16/gpt-home/releases/latest | grep ' tag_name ' | cut -d " -f4 )
# Colors
RED= ' 33[0;31m '
GREEN= ' 33[0;32m '
YELLOW= ' 33[0;33m '
BLUE= ' 33[0;34m '
MAGENTA= ' 33[0;35m '
CYAN= ' 33[0;36m '
WHITE= ' 33[0;37m '
NC= ' 33[0m ' # No Color
echo " "
echo -e " ${MAGENTA} "
echo " GPT Home $latest_release "
echo " Created by Judah Paul "
echo " More info @ https://github.com/judahpaul16/gpt-home/ "
echo -e " ${NC} "
echo -e " ${GREEN} "
echo " ____ ____ _____ _ _ "
echo " / ___| _ \ _ _| | | | | ___ _ __ ___ ___ "
echo " | | _| |_) || | | |_| |/ _ \ | '_ ` _ \ / _ \ "
echo " | |_| | __/ | | | _ | (_) | | | | | | __/ "
echo " \ ____|_| |_| |_| |_| \ ___/|_| |_| |_| \ ___| "
echo -e " ${NC} "
echo -e " ${CYAN} "
echo " ______________ "
echo " | how may I | "
echo " | assist you | "
echo " | today? | "
echo " |______________| "
echo " \ | "
echo " \ | "
echo " \ | "
echo " _______ ________ | "
echo " |ooooooo| ____ | __ __ | | "
echo " |[]+++[]| [____] |/ \ / \ | | "
echo " |+ ___ +| ]()()[ | \ __/ \ __/| | "
echo " |:| |:| ___ \ __/___ |[][][][]| | "
echo " |:|___|:| |__| |__| |++++++++| | "
echo " |[]===[]| |_|/ \ |_| | ______ | | "
echo " _ ||||||||| _ | | __ | | __ ||______|| __| "
echo " |_______| |_|[::]|_| |________| \ "
echo " \ _|_||_|_/ \ "
echo " |_||_| \ "
echo " _|_||_|_ \ "
echo " ____ |___||___| \ "
echo -e " ${NC} "
# Mask systemd-networkd-wait-online.service to prevent boot delays
sudo systemctl mask systemd-networkd-wait-online.service
# Set Permissions
sudo chown -R $( whoami ) : $( whoami ) .
sudo chmod -R 755 .
# Function to install system dependencies
function install() {
local package= $1
echo " Ensuring package ' $package ' is installed... "
# Detect the package management system
if command -v apt-get > /dev/null ; then
if ! dpkg -s " $package " > /dev/null 2>&1 ; then
sudo yes | add-apt-repository universe > /dev/null 2>&1 || true
sudo apt update || true
if [ " $package " == " docker " ] ; then
sudo apt-get install -y docker.io
else
sudo apt-get install -y " $package "
fi
fi
elif command -v yum > /dev/null ; then
if ! rpm -q " $package " > /dev/null 2>&1 ; then
sudo yum install -y epel-release > /dev/null 2>&1 || true
sudo yum makecache --timer || true
sudo yum install -y " $package "
fi
elif command -v dnf > /dev/null ; then
if ! dnf list installed " $package " > /dev/null 2>&1 ; then
sudo dnf install -y epel-release > /dev/null 2>&1 || true
sudo dnf makecache --timer || true
sudo dnf install -y " $package "
fi
elif command -v zypper > /dev/null ; then
if ! zypper se -i " $package " > /dev/null 2>&1 ; then
sudo zypper refresh || true
sudo zypper install -y " $package "
fi
elif command -v pacman > /dev/null ; then
if ! pacman -Q " $package " > /dev/null 2>&1 ; then
sudo pacman -Sy
sudo pacman -S --noconfirm " $package "
fi
else
echo " Package manager not supported. "
return 1
fi
if [ " $package " == " docker " ] ; then
if ! docker ps > /dev/null 2>&1 ; then
echo " Docker installed. Adding $( whoami ) to the 'docker' group... "
sudo usermod -aG docker $( whoami )
echo -e " ${RED} User added to ` docker ` group but the session must be reloaded to access the Docker daemon. Please log out, log back in, and rerun the script. Exiting... ${NC} "
exit 0
fi
fi
}
install chrony
install nginx
install containerd
install docker
install docker-buildx-plugin
install alsa-utils
sudo systemctl enable docker
sudo systemctl start docker
# Create ALSA config (asound.conf, adjust as needed)
sudo tee /etc/asound.conf > /dev/null << EOF
pcm.!default { type hw card Headphones device 0 }
ctl.!default { type hw card Headphones }
EOF
# Install Docker Buildx plugin
mkdir -p $HOME /.docker/cli-plugins
curl -Lo $HOME /.docker/cli-plugins/docker-buildx https://github.com/docker/buildx/releases/download/v0.14.0/buildx-v0.14.0.linux-arm64
sudo chmod +x $HOME /.docker/cli-plugins/docker-buildx
docker buildx version
# Setup UFW Firewall
echo " Setting up UFW Firewall... "
if which firewalld > /dev/null ; then
sudo systemctl stop firewalld
sudo systemctl disable firewalld
sudo yum remove firewalld -y 2> /dev/null || sudo apt-get remove firewalld -y 2> /dev/null || sudo zypper remove firewalld -y 2> /dev/null
fi
if ! which ufw > /dev/null ; then
sudo yum install ufw -y 2> /dev/null || sudo apt-get install ufw -y 2> /dev/null || sudo zypper install ufw -y 2> /dev/null
fi
sudo ufw allow ssh
sudo ufw allow 80,443/tcp
sudo ufw allow 5353/udp
echo " y " | sudo ufw enable
# Setup NGINX for reverse proxy
echo " Setting up NGINX... "
sudo mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled
sudo tee /etc/nginx/sites-available/gpt-home << EOF
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $ host;
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
}
}
EOF
# Remove gpt-home site symlink if it exists
[ -L " /etc/nginx/sites-enabled/gpt-home " ] && sudo unlink /etc/nginx/sites-enabled/gpt-home
# Remove the default site if it exists
[ -L " /etc/nginx/sites-enabled/default " ] && sudo unlink /etc/nginx/sites-enabled/default
# Create a symlink to the gpt-home site and reload NGINX
sudo ln -s /etc/nginx/sites-available/gpt-home /etc/nginx/sites-enabled
sudo systemctl enable nginx
sudo nginx -t && sudo systemctl restart nginx
sudo systemctl status --no-pager nginx
if [[ " $1 " != " --no-build " ]] ; then
[ -d ~ /gpt-home ] && rm -rf ~ /gpt-home
git clone https://github.com/judahpaul16/gpt-home ~ /gpt-home
cd ~ /gpt-home
echo " Checking if the container 'gpt-home' is already running... "
if [ $( docker ps -q -f name=gpt-home ) ] ; then
echo " Stopping running container 'gpt-home'... "
docker stop gpt-home
fi
echo " Checking for existing container 'gpt-home'... "
if [ $( docker ps -aq -f status=exited -f name=gpt-home ) ] ; then
echo " Removing existing container 'gpt-home'... "
docker rm -f gpt-home
fi
echo " Pruning Docker system... "
docker system prune -f
# Check if the buildx builder exists, if not create and use it
if ! docker buildx ls | grep -q mybuilder ; then
docker buildx create --name mybuilder --use
docker buildx inspect --bootstrap
fi
# Building Docker image 'gpt-home' for ARMhf architecture
echo " Building Docker image 'gpt-home' for ARMhf... "
timeout 3600 docker buildx build --platform linux/arm64 -t gpt-home --load .
if [ $? -ne 0 ] ; then
echo " Docker build failed. Exiting... "
exit 1
fi
echo " Container 'gpt-home' is now ready to run. "
echo " Running container 'gpt-home' from image 'gpt-home'... "
docker run --restart unless-stopped -d --name gpt-home
--mount type=bind,source=/etc/asound.conf,target=/etc/asound.conf
--privileged
--net=host
--tmpfs /run
--tmpfs /run/lock
-v ~ /gpt-home:/app
-v /dev/snd:/dev/snd
-v /dev/shm:/dev/shm
-v /usr/share/alsa:/usr/share/alsa
-v /var/run/dbus:/var/run/dbus
-e OPENAI_API_KEY= $OPENAI_API_KEY
-e LITELLM_API_KEY= $LITELLM_API_KEY
gpt-home
echo " Container 'gpt-home' is now running. "
# Show status of the container
docker ps -a | grep gpt-home
sleep 10
# Show status of all programs managed by Supervisor
docker exec -i gpt-home supervisorctl status
fi
if [[ " $1 " == " --no-build " ]] ; then
docker ps -aq -f name=gpt-home | xargs -r docker rm -f
docker pull judahpaul/gpt-home
docker run --restart unless-stopped -d --name gpt-home
--mount type=bind,source=/etc/asound.conf,target=/etc/asound.conf
--privileged
--net=host
--tmpfs /run
--tmpfs /run/lock
-v /dev/snd:/dev/snd
-v /dev/shm:/dev/shm
-v /usr/share/alsa:/usr/share/alsa
-v /var/run/dbus:/var/run/dbus
-e OPENAI_API_KEY= $OPENAI_API_KEY
-e LITELLM_API_KEY= $LITELLM_API_KEY
judahpaul/gpt-home
docker ps -a | grep gpt-home
sleep 10
docker exec -i gpt-home supervisorctl status
fi
确保使脚本可执行以运行它
chmod +x setup.sh
./setup.sh
|
|
|
|
|
|
|
当然,欢迎贡献!请阅读有关如何贡献的更多信息的contributing guidelines
。
该项目已根据GNU GPL V3.0许可证获得许可 - 有关详细信息,请参见LICENSE
文件。