Skip to content

Commit 107e3a9

Browse files
guygirvMaroonnirrozenbaum
authored
Chat-Completions Enhancements: Updated Examples + Code Improvements (#92)
* Completed step 2.2. Link python and install requirements in setup (Makefile for local runs, Dockerfile for building) * Removed unneeded files * - example made compact - naming/structuring refactoring for convenience and readability - general refactoring Signed-off-by: Maroon Ayoub <[email protected]> * Update examples/kv_events/README.md Co-authored-by: Nir Rozenbaum <[email protected]> Signed-off-by: Maroon Ayoub <[email protected]> Apply suggestion from @nirrozenbaum Co-authored-by: Nir Rozenbaum <[email protected]> Signed-off-by: Maroon Ayoub <[email protected]> * - upgraded python version - minor fixes - docker fix - addressed review comments Signed-off-by: Maroon Ayoub <[email protected]> --------- Signed-off-by: Guy Girmonsky <[email protected]> Co-authored-by: Maroon Ayoub <[email protected]> Co-authored-by: Nir Rozenbaum <[email protected]>
1 parent bad0612 commit 107e3a9

File tree

17 files changed

+968
-609
lines changed

17 files changed

+968
-609
lines changed

.dockerignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Git
2+
.git
3+
.gitignore
4+
5+
# Build artifacts
6+
bin
7+
build
8+
9+
# IDE and OS files
10+
.idea
11+
.vscode
12+
*.DS_Store
13+
14+
# Local virtual environments
15+
venv
16+
17+
# Python cache files
18+
__pycache__
19+
20+
# Docker files
21+
Dockerfile

.github/workflows/ci-pr-checks.yaml

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@ jobs:
1313
- name: Checkout source
1414
uses: actions/checkout@v4
1515

16-
- name: Install system dependencies (ZeroMQ)
16+
- name: Cache system (apt) dependencies
17+
uses: actions/cache@v4
18+
with:
19+
path: /var/cache/apt/archives
20+
key: ${{ runner.os }}-apt-${{ hashFiles('**/.github/workflows/ci-pr-checks.yaml') }}
21+
restore-keys: |
22+
${{ runner.os }}-apt-
23+
24+
- name: Install system dependencies
1725
run: |
1826
sudo apt-get update
19-
sudo apt-get install -y libzmq3-dev pkg-config
27+
sudo add-apt-repository ppa:deadsnakes/ppa -y
28+
sudo apt-get install -y libzmq3-dev pkg-config python3.12 python3.12-dev python3.12-venv
2029
2130
- name: Sanity check repo contents
2231
run: ls -la
@@ -33,14 +42,33 @@ jobs:
3342
- name: Install dependencies
3443
run: go mod download
3544

36-
- name: Install golangci-lint
45+
# This step is still useful to verify the Python config before subsequent steps
46+
- name: Run detect-python
47+
run: make detect-python
48+
49+
- name: Cache Python (pip) dependencies
50+
uses: actions/cache@v4
51+
with:
52+
path: ~/.cache/pip
53+
# This key is based ONLY on the requirements file
54+
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
55+
56+
- name: Install golangci-lint without running it
3757
uses: golangci/golangci-lint-action@v8
3858
with:
3959
version: v2.1.6
40-
args: ""
60+
args: "--help"
4161

42-
- name: Run precommit checks
43-
run: make precommit
62+
- name: Run precommit checks with CGo environment
63+
run: |
64+
# Export the CGO flags required for the linter to parse CGo files.
65+
# This duplicates some logic from the Makefile but is necessary for the CI step.
66+
export CGO_CFLAGS="$(python3.12-config --cflags) -I./lib"
67+
export CGO_LDFLAGS="$(python3.12-config --ldflags --embed) -L./lib -ltokenizers -ldl -lm"
68+
export CGO_ENABLED=1
69+
70+
# Now run the linting command from the Makefile
71+
make precommit
4472
4573
- name: Run make build
4674
shell: bash

.golangci.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,13 @@ linters:
5050
- reassign
5151
- revive
5252
- staticcheck
53-
- tagliatelle
53+
# - tagliatelle
5454
- testableexamples
5555
- testpackage
5656
- thelper
5757
- tparallel
5858
- unconvert
5959
- unparam
60-
- unused
6160
- usestdlibvars
6261
- varnamelen
6362
- whitespace
@@ -120,4 +119,4 @@ formatters:
120119
paths:
121120
- third_party$
122121
- builtin$
123-
- examples$
122+
- examples$

Dockerfile

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ ARG TARGETARCH
1919

2020
WORKDIR /workspace
2121

22+
# Install system-level dependencies first. This layer is very stable.
2223
USER root
2324
# Install EPEL repository directly and then ZeroMQ, as epel-release is not in default repos.
25+
# Install all necessary dependencies including Python 3.12 for chat-completions templating.
2426
# The builder is based on UBI8, so we need epel-release-8.
2527
RUN dnf install -y 'https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm' && \
26-
dnf install -y gcc-c++ libstdc++ libstdc++-devel clang zeromq-devel pkgconfig && \
28+
dnf install -y gcc-c++ libstdc++ libstdc++-devel clang zeromq-devel pkgconfig python3.12-devel python3.12-pip && \
2729
dnf clean all
2830

2931
# Copy the Go Modules manifests
@@ -33,6 +35,12 @@ COPY go.sum go.sum
3335
# and so that source changes don't invalidate our downloaded layer
3436
RUN go mod download
3537

38+
# Copy only the requirements file.
39+
COPY pkg/preprocessing/chat_completions/requirements.txt ./requirements.txt
40+
# Install Python dependencies. This layer will be cached unless requirements.txt changes.
41+
RUN python3.12 -m pip install --upgrade pip setuptools wheel && \
42+
python3.12 -m pip install -r ./requirements.txt
43+
3644
# Copy the go source
3745
COPY examples/kv_events examples/kv_events
3846
COPY . .
@@ -43,13 +51,17 @@ ARG RELEASE_VERSION=v1.22.1
4351
RUN curl -L https://github.com/daulet/tokenizers/releases/download/${RELEASE_VERSION}/libtokenizers.${TARGETOS}-${TARGETARCH}.tar.gz | tar -xz -C lib
4452
RUN ranlib lib/*.a
4553

46-
# Build
47-
# the GOARCH has not a default value to allow the binary be built according to the host where the command
48-
# was called. For example, if we call make image-build in a local env which has the Apple Silicon M1 SO
49-
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
50-
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
54+
# Set up Python environment variables needed for the build
55+
ENV PYTHONPATH=/workspace/pkg/preprocessing/chat_completions:/usr/lib64/python3.9/site-packages:/usr/lib/python3.9/site-packages
56+
ENV PYTHON=python3.9
5157

52-
RUN CGO_ENABLED=1 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} go build -ldflags="-extldflags '-L$(pwd)/lib'" -a -o bin/kv-cache-manager examples/kv_events/online/main.go
58+
# Build the application with CGO enabled.
59+
# We export CGO_CFLAGS and CGO_LDFLAGS using python3.12-config to ensure the Go compiler
60+
# can find the Python headers and libraries correctly. This mirrors the fix from the Makefile.
61+
RUN export CGO_CFLAGS="$(python3.12-config --cflags) -I/workspace/lib" && \
62+
export CGO_LDFLAGS="$(python3.12-config --ldflags --embed) -L/workspace/lib -ltokenizers -ldl -lm" && \
63+
CGO_ENABLED=1 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} \
64+
go build -a -o bin/kv-cache-manager examples/kv_events/online/main.go
5365

5466
# Use distroless as minimal base image to package the manager binary
5567
# Refer to https://github.com/GoogleContainerTools/distroless for more details
@@ -59,8 +71,25 @@ WORKDIR /
5971
# The final image is UBI9, so we need epel-release-9.
6072
USER root
6173
RUN dnf install -y 'https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm' && \
62-
dnf install -y zeromq
74+
dnf install -y zeromq libxcrypt-compat python3.12 python3.12-pip && \
75+
dnf clean all
76+
77+
78+
79+
# Install Python dependencies in the final image.
80+
COPY --from=builder /workspace/requirements.txt /tmp/requirements.txt
81+
RUN python3.12 -m pip install --upgrade pip setuptools wheel && \
82+
python3.12 -m pip install --no-cache-dir -r /tmp/requirements.txt \
83+
&& rm -rf /tmp/requirements.txt
84+
85+
# Copy this project's own Python source code into the final image
86+
COPY --from=builder /workspace/pkg/preprocessing/chat_completions /app/pkg/preprocessing/chat_completions
87+
88+
# Set the PYTHONPATH. This mirrors the Makefile's export, ensuring both this project's
89+
# Python code and the installed libraries (site-packages) are found at runtime.
90+
ENV PYTHONPATH=/app/pkg/preprocessing/chat_completions:/usr/lib64/python3.12/site-packages
6391

92+
# Copy the compiled Go application
6493
COPY --from=builder /workspace/bin/kv-cache-manager /app/kv-cache-manager
6594
USER 65532:65532
6695

0 commit comments

Comments
 (0)