diff --git a/Dockerfile b/Dockerfile index 686d856..4f2962c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ -# Use Ubuntu 20.04 as the base image -FROM ubuntu:20.04 +# Stage 1: Build stage +FROM ubuntu:20.04 as build # Set environment variables ENV DEBIAN_FRONTEND=noninteractive @@ -30,8 +30,7 @@ RUN wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/nul echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ focal main' | \ tee /etc/apt/sources.list.d/kitware.list >/dev/null && \ apt-get update && rm /usr/share/keyrings/kitware-archive-keyring.gpg && \ - apt-get -y install kitware-archive-keyring \ - cmake && \ + apt-get -y install kitware-archive-keyring cmake && \ rm -rf /var/lib/apt/lists/* && apt-get clean # Clone vcpkg and install packages @@ -41,15 +40,30 @@ RUN git clone https://github.com/Microsoft/vcpkg.git \ RUN vcpkg install cpr nlohmann-json -# Set the working directory WORKDIR /app - -# Copy the current directory contents into the container at /app COPY . . # Build the project using CMake RUN cmake -B build/ -S . -DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake RUN cmake --build build/ -# Define the command to run on container start -CMD ["./build/ChatBot", "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"] +# Stage 2: Runtime stage +FROM ubuntu:20.04 as runtime + +# Set necessary environment variables for runtime +ENV DEBIAN_FRONTEND=noninteractive + +# Install runtime dependencies +RUN apt-get update && apt-get install -y \ + libssl-dev \ + ca-certificates \ + && rm -rf /var/lib/apt/lists/* && apt-get clean + +# Copy the built application from the build stage +COPY --from=build /app/build/ /app/ + +# Set the working directory +WORKDIR /app + +# Command to run the application +CMD ["./ChatBot"] \ No newline at end of file diff --git a/README.md b/README.md index 3f5b1a2..84f6d94 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A header-only version of ChatGPT Official API written in C++ ## Run with Docker -You can run this project with Docker. This is the easiest way to get started. You need to get OpenAI API token first. You can get it from [here](https://platform.openai.com/). +You can run this project with Docker. This is the easiest way to get started. ### Steps @@ -17,40 +17,51 @@ git clone https://github.com/jschang19/ntu-im-final-project.git cd ntu-im-final-project ``` -2. Install [Docker](https://www.docker.com/products/docker-desktop/), then run the following commands: +2. Get your OpenAI token from [here](https://platform.openai.com/) and set it as the `ENV` in the `Dockerfile` file. + +3. Install [Docker](https://www.docker.com/products/docker-desktop/), then run the following commands: ```bash -docker build -t chatcpp . // this will take a while -docker run -it chatcpp +docker build -t chatcpp . # this will take a while +docker run -it --env OPENAI_API_KEY= chatcpp ``` ## Development To develop this project, you need to install [CMake](https://cmake.org/download/) and [Vcpkg](https://github.com/microsoft/vcpkg). -1. Install dependencies with VCPKG +1. Export your OpenAI token to environment variable +```bash +export OPENAI_API_KEY= +``` + +2. Install dependencies with VCPKG ```bash git clone https://github.com/Microsoft/vcpkg.git cd vcpkg -// Windows +# Windows ./bootstrap-vcpkg.bat -// Mac/Linux +# Mac/Linux ./bootstrap-vcpkg.sh ./vcpkg install nlohmann-json ./vcpkg install cpr ./vcpkg integrate install ``` -* With CMake: +3. Build the project with CMake ```bash rm -rf build mkdir build cmake -B build/ -S . -DCMAKE_TOOLCHAIN_FILE= cd build cmake --build . -./ChatBot ``` +4. Run the project +```bash +./ChatGPT +``` + Every time you want to rebuild the project, you need to run `cmake --build .` in the build directory. ## Update Vcpkg @@ -66,3 +77,7 @@ rm -rf installed ``` Then run the cmake command again. You need to `rm -rf build/` and build again. + +## References +- This repository is based on [deni2312/ChatGPT-Cpp](https://github.com/deni2312/ChatGPT-Cpp) +- Dockerize this C++ project with this [article](https://medium.com/codex/a-practical-guide-to-containerize-your-c-application-with-docker-50abb197f6d4) \ No newline at end of file diff --git a/main.cpp b/main.cpp index 9878168..c392238 100644 --- a/main.cpp +++ b/main.cpp @@ -4,28 +4,31 @@ #include #include #include +#include // For getenv #include "ChatGPT/include/Error.h" #include "ChatGPT/include/Game.h" // this is the main function const int STORY_NUM=5; -int main(int args,char** argv){ +int main(){ std::cout << "\033[2J\033[H"; System::Game game; - if(args<2){ + const char* openaiKey = std::getenv("OPENAI_API_KEY"); + if(openaiKey == nullptr){ game.print("沒有 OpenAI API Key,請在參數後方輸入你的 API key","r", true); game.print("範例:./ChatGPT "); game.print("請至 https://platform.openai.com/api-keys 取得 API key"); return 0; } + std::string key = openaiKey; game.print("\U0001F389 歡迎來到 NTU 模擬器!","w", true); - game.checkStatus(argv[1]); + game.checkStatus(key); std::cout< story_ids = game.getRandStoryIds(STORY_NUM);