From badceb2798ee0babdf2a0d176303a3f5dbba6d78 Mon Sep 17 00:00:00 2001 From: limccn Date: Wed, 15 Feb 2023 18:52:25 +0800 Subject: [PATCH] feat: use entrypoint.sh override environment variables --- docker/Dockerfile.alpine | 10 +++- docker/Dockerfile.debian | 11 ++-- docker/build.alpine.sh | 2 + docker/build.debian.sh | 2 + docker/docker-compose.yaml | 11 +++- docker/entrypoint.sh | 75 ++++++++++++++++++++++++ docker/sample-chatgpt-on-wechat/.env | 12 ++++ docker/sample-chatgpt-on-wechat/Makefile | 2 +- 8 files changed, 116 insertions(+), 9 deletions(-) create mode 100755 docker/entrypoint.sh diff --git a/docker/Dockerfile.alpine b/docker/Dockerfile.alpine index 5d30df5..741e6fb 100644 --- a/docker/Dockerfile.alpine +++ b/docker/Dockerfile.alpine @@ -9,6 +9,7 @@ ENV BUILD_PREFIX=/app \ BUILD_OPEN_AI_API_KEY='YOUR OPEN AI KEY HERE' RUN apk add --no-cache \ + bash \ curl \ wget \ openssh @@ -23,16 +24,19 @@ WORKDIR ${BUILD_PREFIX} RUN cd ${BUILD_PREFIX} \ && cp config-template.json ${BUILD_PREFIX}/config.json \ - && sed -i "2s/YOUR API KEY/${BUILD_OPEN_AI_API_KEY}/" ${BUILD_PREFIX}/config.json \ - && cat ${BUILD_PREFIX}/config.json + && sed -i "2s/YOUR API KEY/${BUILD_OPEN_AI_API_KEY}/" ${BUILD_PREFIX}/config.json RUN /usr/local/bin/python -m pip install --upgrade pip \ && pip install itchat-uos==1.5.0.dev0 \ && pip install --upgrade openai +ADD ./entrypoint.sh /entrypoint.sh + +RUN chmod +x /entrypoint.sh + RUN adduser -D -h /home/noroot -u 1000 -s /bin/bash noroot \ && chown noroot:noroot ${BUILD_PREFIX} USER noroot -CMD ["python","app.py"] +ENTRYPOINT ["/entrypoint.sh"] diff --git a/docker/Dockerfile.debian b/docker/Dockerfile.debian index b68f5b9..c808b42 100644 --- a/docker/Dockerfile.debian +++ b/docker/Dockerfile.debian @@ -24,17 +24,20 @@ WORKDIR ${BUILD_PREFIX} RUN cd ${BUILD_PREFIX} \ && cp config-template.json ${BUILD_PREFIX}/config.json \ - && sed -i "2s/YOUR API KEY/${BUILD_OPEN_AI_API_KEY}/" ${BUILD_PREFIX}/config.json \ - && cat ${BUILD_PREFIX}/config.json + && sed -i "2s/YOUR API KEY/${BUILD_OPEN_AI_API_KEY}/" ${BUILD_PREFIX}/config.json RUN /usr/local/bin/python -m pip install --upgrade pip \ && pip install itchat-uos==1.5.0.dev0 \ && pip install --upgrade openai +ADD ./entrypoint.sh /entrypoint.sh + +RUN chmod +x /entrypoint.sh + RUN groupadd -r noroot \ - && useradd -r -g noroot noroot \ + && useradd -r -g noroot -s /bin/bash -d /home/noroot noroot \ && chown -R noroot:noroot ${BUILD_PREFIX} USER noroot -CMD ["python","app.py"] +ENTRYPOINT ["/entrypoint.sh"] diff --git a/docker/build.alpine.sh b/docker/build.alpine.sh index d29503b..a5bacab 100644 --- a/docker/build.alpine.sh +++ b/docker/build.alpine.sh @@ -1,3 +1,5 @@ +#!/bin/bash + docker build -f Dockerfile.alpine \ --build-arg CHATGPT_ON_WECHAT_VER=1.0.0\ -t zhayujie/chatgpt-on-wechat:1.0.0-alpine . \ No newline at end of file diff --git a/docker/build.debian.sh b/docker/build.debian.sh index 732aaf7..91a7575 100644 --- a/docker/build.debian.sh +++ b/docker/build.debian.sh @@ -1,3 +1,5 @@ +#!/bin/bash + docker build -f Dockerfile.debian \ --build-arg CHATGPT_ON_WECHAT_VER=1.0.0\ -t zhayujie/chatgpt-on-wechat:1.0.0-debian . \ No newline at end of file diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 7146e1e..cdde5c3 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -5,4 +5,13 @@ services: context: ./ dockerfile: Dockerfile.alpine image: zhayujie/chatgpt-on-wechat:1.0.0-alpine - container_name: sample-chatgpt-on-wechat \ No newline at end of file + container_name: sample-chatgpt-on-wechat + environment: + OPEN_AI_API_KEY: 'YOUR API KEY' + SINGLE_CHAT_PREFIX: '["BOT", "@BOT"]' + SINGLE_CHAT_REPLY_PREFIX: '"[BOT] "' + GROUP_CHAT_PREFIX: '["@BOT"]' + GROUP_NAME_WHITE_LIST: '["CHATGPT测试群", "CHATGPT测试群2"]' + IMAGE_CREATE_PREFIX: '["画", "看", "找"]' + CONVERSATION_MAX_TOKENS: 1000 + CHARACTER_DESC: '你是CHATGPT, 一个由OPENAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。' \ No newline at end of file diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100755 index 0000000..d7923a8 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,75 @@ +#!/bin/bash +set -e + +# build prefix +CHATGPT_ON_WECHAT_PREFIX=${CHATGPT_ON_WECHAT_PREFIX:-""} +# path to config.json +CHATGPT_ON_WECHAT_CONFIG_PATH=${CHATGPT_ON_WECHAT_CONFIG_PATH:-""} +# execution command line +CHATGPT_ON_WECHAT_EXEC=${CHATGPT_ON_WECHAT_EXEC:-""} + +OPEN_AI_API_KEY=${OPEN_AI_API_KEY:-""} +SINGLE_CHAT_PREFIX=${SINGLE_CHAT_PREFIX:-""} +SINGLE_CHAT_REPLY_PREFIX=${SINGLE_CHAT_REPLY_PREFIX:-""} +GROUP_CHAT_PREFIX=${GROUP_CHAT_PREFIX:-""} +GROUP_NAME_WHITE_LIST=${GROUP_NAME_WHITE_LIST:-""} +IMAGE_CREATE_PREFIX=${IMAGE_CREATE_PREFIX:-""} +CONVERSATION_MAX_TOKENS=${CONVERSATION_MAX_TOKENS:-""} +CHARACTER_DESC=${CHARACTER_DESC:-""} + +# CHATGPT_ON_WECHAT_PREFIX is empty, use /app +if [ "$CHATGPT_ON_WECHAT_PREFIX" == "" ] ; then + CHATGPT_ON_WECHAT_PREFIX=/app +fi + +# APP_PREFIX is empty, use '/app/config.json' +if [ "$CHATGPT_ON_WECHAT_CONFIG_PATH" == "" ] ; then + CHATGPT_ON_WECHAT_CONFIG_PATH=$CHATGPT_ON_WECHAT_PREFIX/config.json +fi + +# CHATGPT_ON_WECHAT_EXEC is empty, use ‘python app.py’ +if [ "$CHATGPT_ON_WECHAT_EXEC" == "" ] ; then + CHATGPT_ON_WECHAT_EXEC="python app.py" +fi + +# modify content in config.json +if [ "$OPEN_AI_API_KEY" != "" ] ; then + sed -i "2c \"open_ai_api_key\": \"$OPEN_AI_API_KEY\"," $CHATGPT_ON_WECHAT_CONFIG_PATH +else + echo -e "\033[31m[Warning] You need to set OPEN_AI_API_KEY before running!\033[0m" +fi + +if [ "$SINGLE_CHAT_PREFIX" != "" ] ; then + sed -i "3c \"single_chat_prefix\": $SINGLE_CHAT_PREFIX," $CHATGPT_ON_WECHAT_CONFIG_PATH +fi + +if [ "$SINGLE_CHAT_REPLY_PREFIX" != "" ] ; then + sed -i "4c \"single_chat_reply_prefix\": $SINGLE_CHAT_REPLY_PREFIX," $CHATGPT_ON_WECHAT_CONFIG_PATH +fi + +if [ "$GROUP_CHAT_PREFIX" != "" ] ; then + sed -i "5c \"group_chat_prefix\": $GROUP_CHAT_PREFIX," $CHATGPT_ON_WECHAT_CONFIG_PATH +fi + +if [ "$GROUP_NAME_WHITE_LIST" != "" ] ; then + sed -i "6c \"group_name_white_list\": $GROUP_NAME_WHITE_LIST," $CHATGPT_ON_WECHAT_CONFIG_PATH +fi + +if [ "$IMAGE_CREATE_PREFIX" != "" ] ; then + sed -i "7c \"image_create_prefix\": $IMAGE_CREATE_PREFIX," $CHATGPT_ON_WECHAT_CONFIG_PATH +fi + +if [ "$CONVERSATION_MAX_TOKENS" != "" ] ; then + sed -i "8c \"conversation_max_tokens\": $CONVERSATION_MAX_TOKENS," $CHATGPT_ON_WECHAT_CONFIG_PATH +fi + +if [ "$CHARACTER_DESC" != "" ] ; then + sed -i "9c \"character_desc\": \"$CHARACTER_DESC\"" $CHATGPT_ON_WECHAT_CONFIG_PATH +fi + +# go to prefix dir +cd $CHATGPT_ON_WECHAT_PREFIX +# excute +$CHATGPT_ON_WECHAT_EXEC + + diff --git a/docker/sample-chatgpt-on-wechat/.env b/docker/sample-chatgpt-on-wechat/.env index 8b13789..33395ee 100644 --- a/docker/sample-chatgpt-on-wechat/.env +++ b/docker/sample-chatgpt-on-wechat/.env @@ -1 +1,13 @@ +OPEN_AI_API_KEY=YOUR API KEY +SINGLE_CHAT_PREFIX=["BOT", "@BOT"] +SINGLE_CHAT_REPLY_PREFIX="[BOT] " +GROUP_CHAT_PREFIX=["@BOT"] +GROUP_NAME_WHITE_LIST=["CHATGPT测试群", "CHATGPT测试群2"] +IMAGE_CREATE_PREFIX=["画", "看", "找"] +CONVERSATION_MAX_TOKENS=1000 +CHARACTER_DESC=你是CHATGPT, 一个由OPENAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。 +# Optional +#CHATGPT_ON_WECHAT_PREFIX=/app +#CHATGPT_ON_WECHAT_CONFIG_PATH=/app/config.json +#CHATGPT_ON_WECHAT_EXEC=python app.py \ No newline at end of file diff --git a/docker/sample-chatgpt-on-wechat/Makefile b/docker/sample-chatgpt-on-wechat/Makefile index 6931e0d..31b5f81 100644 --- a/docker/sample-chatgpt-on-wechat/Makefile +++ b/docker/sample-chatgpt-on-wechat/Makefile @@ -1,5 +1,5 @@ IMG:=`cat Name` -MOUNT:=-v `pwd`/config.json:/app/config.json +MOUNT:= PORT_MAP:= DOTENV:=.env CONTAINER_NAME:=sample-chatgpt-on-wechat