Skip to content

Commit

Permalink
refactor: use env & minify docker build size
Browse files Browse the repository at this point in the history
  • Loading branch information
jschang19 committed Dec 17, 2023
1 parent b285a0c commit 3dab78a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
32 changes: 23 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"]
33 changes: 24 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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=<YOUR_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=<YOUR_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=<YOUR_VCPKG_PATH/scripts/buildsystems/vcpkg.cmake>
cd build
cmake --build .
./ChatBot <OPENA_AI_TOKEN>
```

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
Expand All @@ -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)
11 changes: 7 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,31 @@
#include <stdlib.h>
#include <thread>
#include <chrono>
#include <cstdlib> // 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 <YOUR_API_KEY>");
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<<std::endl;

game.count=STORY_NUM;
OpenAI::ChatGPT chatGpt{argv[1]};
OpenAI::ChatGPT chatGpt{key};
game.printWelcome();
// get random story ids
std::vector<int> story_ids = game.getRandStoryIds(STORY_NUM);
Expand Down

0 comments on commit 3dab78a

Please sign in to comment.