├── .github └── workflows │ └── keynote.yaml ├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── app.py ├── cluster.yaml ├── docker-compose.yaml ├── img └── kccneu24.png ├── kubernetes ├── deployment.yaml └── service.yaml ├── requirements.txt ├── shutdown.sh └── startup.sh /.github/workflows/keynote.yaml: -------------------------------------------------------------------------------- 1 | name: build-keynote 2 | on: 3 | pull_request: 4 | branches: 5 | - main 6 | schedule: 7 | - cron: "0 0 * * TUE" 8 | push: 9 | branches: 10 | - main 11 | paths-ignore: 12 | - "**/README.md" 13 | env: 14 | IMAGE_NAME: keynote 15 | IMAGE_TAGS: latest 16 | IMAGE_REGISTRY: ghcr.io/${{ github.repository_owner }} 17 | 18 | jobs: 19 | push-ghcr: 20 | name: Build and push image 21 | runs-on: ubuntu-latest-4-cores 22 | permissions: 23 | contents: read 24 | packages: write 25 | id-token: write 26 | strategy: 27 | fail-fast: false 28 | steps: 29 | # Checkout push-to-registry action GitHub repository 30 | - name: Checkout Push to Registry action 31 | uses: actions/checkout@v4 32 | 33 | # Build metadata 34 | - name: Image Metadata 35 | uses: docker/metadata-action@v5 36 | id: meta 37 | with: 38 | images: | 39 | ${{ env.IMAGE_NAME }} 40 | labels: | 41 | io.artifacthub.package.readme-url=https://raw.githubusercontent.com/cncf/llm-in-action/main/README.md 42 | 43 | # Build image using Buildah action 44 | - name: Build Image 45 | id: build_image 46 | uses: redhat-actions/buildah-build@v2 47 | with: 48 | containerfiles: | 49 | ./Dockerfile 50 | image: ${{ env.IMAGE_NAME }} 51 | tags: ${{ env.IMAGE_TAGS }} 52 | labels: ${{ steps.meta.outputs.labels }} 53 | oci: false 54 | 55 | # Workaround bug where capital letters in your GitHub username make it impossible to push to GHCR. 56 | # https://github.com/macbre/push-to-ghcr/issues/12 57 | - name: Lowercase Registry 58 | id: registry_case 59 | uses: ASzc/change-string-case-action@v6 60 | with: 61 | string: ${{ env.IMAGE_REGISTRY }} 62 | 63 | # Push the image to GHCR (Image Registry) 64 | - name: Push To GHCR 65 | uses: redhat-actions/push-to-registry@v2 66 | id: push 67 | env: 68 | REGISTRY_USER: ${{ github.actor }} 69 | REGISTRY_PASSWORD: ${{ github.token }} 70 | with: 71 | image: ${{ steps.build_image.outputs.image }} 72 | tags: ${{ steps.build_image.outputs.tags }} 73 | registry: ${{ steps.registry_case.outputs.lowercase }} 74 | username: ${{ env.REGISTRY_USER }} 75 | password: ${{ env.REGISTRY_PASSWORD }} 76 | extra-args: | 77 | --disable-content-trust 78 | 79 | - name: Login to GitHub Container Registry 80 | uses: docker/login-action@v3 81 | with: 82 | registry: ghcr.io 83 | username: ${{ github.actor }} 84 | password: ${{ secrets.GITHUB_TOKEN }} 85 | 86 | - name: Echo outputs 87 | run: | 88 | echo "${{ toJSON(steps.push.outputs) }}" 89 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .env -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3 2 | 3 | WORKDIR /app 4 | 5 | RUN apt-get update && apt-get install -y \ 6 | build-essential \ 7 | curl \ 8 | software-properties-common \ 9 | && rm -rf /var/lib/apt/lists/* 10 | 11 | COPY requirements.txt . 12 | 13 | RUN pip install --upgrade -r requirements.txt 14 | 15 | COPY . . 16 | 17 | EXPOSE 8501 18 | 19 | HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health 20 | 21 | ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LLMs in Action: A Cloud Native Story 2 | 3 | ## Prerequisites 4 | 5 | - [Docker](https://docs.docker.com/install/) 6 | - Docker is a platform for developers and sysadmins to develop, ship, and run applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. 7 | - [Ollama](https://ollama.com/) 8 | - Ollama is a Language Model as a Service (LMaaS) that provides a RESTful API for interacting with large language models. It's a great way to get started with LLMs without having to worry about the infrastructure. 9 | - [kind](https://kind.sigs.k8s.io/) 10 | - kind is "Kubernetes in Docker," used by the Kubernetes project to help test features and run integration tests. It turns out it's a handy way for anyone to spin up a cluster quickly. Big thank you to @bentheelder for developing it 👏🏼 👏🏼 11 | - [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) 12 | - kubectl is how you interact with k8s using the command line. This allows you to become a k8s whisperer :-) 13 | 14 | With Ollama installed on your machine, you will need to pull the LLaVa model by running 15 | 16 | ```sh 17 | ollama pull llava 18 | ``` 19 | 20 | You can verify that the model is installed by running 21 | 22 | ```sh 23 | ollama list 24 | 25 | NAME ID SIZE MODIFIED 26 | llava:latest 8dd30f6b0cb1 4.7 GB 17 seconds ago 27 | ``` 28 | 29 | ## Startup 30 | 31 | We have crafted a few scripts to make this demo run as quickly as possible on your machine once you've installed the prerequisites. 32 | 33 | This script will: 34 | 35 | - Create a kind cluster 36 | - Apply the Kubernetes manifests we need for our demo 37 | - Use port-forwarding to help us access our service in the browser so we can take photos and describe them with LLaVa (Large Language and Vision Assistant) 38 | 39 | ```sh 40 | ./startup.sh 41 | ``` 42 | 43 | To access the service, open your browser and navigate to [http://localhost:8501](http://localhost:8501) 44 | 45 | ## Shutdown 46 | 47 | To shut down the demo, run the following command, which will: 48 | 49 | - Remove the Kubernetes manifests 50 | - Remove the port-forwarding 51 | - Delete the kind cluster 52 | 53 | ```sh 54 | ./shutdown.sh 55 | ``` 56 | 57 | ## Operating System Information 58 | 59 | This demo has been tested on the following operating systems and will work if you have the prerequisites installed. 60 | 61 | - macOS 62 | - Linux 63 | - Windows 64 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import os 2 | import base64 3 | import streamlit as st 4 | from ollama import Client 5 | 6 | ollama_base_url = os.getenv("OLLAMA_BASE_URL") 7 | llm_name = os.getenv("LLM") 8 | 9 | def process_stream(stream): 10 | for chunk in stream: 11 | yield chunk['message']['content'] 12 | 13 | def get_base64_encoded_image(image_path): 14 | with open(image_path, "rb") as img_file: 15 | return base64.b64encode(img_file.read()).decode('utf-8') 16 | 17 | # Streamlit UI 18 | 19 | base64_image = get_base64_encoded_image('img/kccneu24.png') 20 | styl = f""" 21 | 29 | """ 30 | st.markdown(styl, unsafe_allow_html=True) 31 | 32 | st.title(':grey[Describe an Image with LLaVa 📸]') 33 | picture = st.camera_input("") 34 | 35 | st.subheader(':grey[Take a picture. Say ] :blue[_Kubernetes_]:grey[!]') 36 | 37 | if picture: 38 | with open ('snap.jpg','wb') as f: 39 | f.write(picture.getbuffer()) 40 | 41 | # Initialize the Ollama client 42 | client = Client(host=ollama_base_url) 43 | 44 | # Define the path to your image 45 | image_path = 'snap.jpg' 46 | 47 | # Prepare the message to send to the LLaVA model 48 | message = { 49 | 'role': 'user', 50 | 'content': 'Describe this image in a respectful way, without mentioning any text elements.', 51 | 'images': [image_path] 52 | } 53 | 54 | # Use the ollama.chat function to send the image and retrieve the description 55 | stream = client.chat( 56 | model="llava", # Specify the desired LLaVA model size 57 | messages=[message], 58 | stream=True, 59 | ) 60 | 61 | st.write_stream(process_stream(stream)) -------------------------------------------------------------------------------- /cluster.yaml: -------------------------------------------------------------------------------- 1 | kind: Cluster 2 | apiVersion: kind.x-k8s.io/v1alpha4 3 | name: llm 4 | nodes: 5 | - role: control-plane 6 | - role: worker 7 | extraPortMappings: 8 | - containerPort: 11434 9 | hostPort: 11434 10 | listenAddress: "0.0.0.0" 11 | protocol: TCP 12 | -------------------------------------------------------------------------------- /docker-compose.yaml: -------------------------------------------------------------------------------- 1 | services: 2 | bot: 3 | build: 4 | context: . 5 | environment: 6 | - OLLAMA_BASE_URL=${OLLAMA_BASE_URL-http://host.docker.internal:11434} 7 | networks: 8 | - net 9 | develop: 10 | watch: 11 | - action: rebuild 12 | path: . 13 | ports: 14 | - 8501:8501 15 | networks: 16 | net: 17 | -------------------------------------------------------------------------------- /img/kccneu24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cncf/llm-in-action/64ff29db8732253d0178ca14b2d67994d9ab7541/img/kccneu24.png -------------------------------------------------------------------------------- /kubernetes/deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: keynote 5 | labels: 6 | app: keynote 7 | spec: 8 | replicas: 1 # Specifies the number of instances 9 | selector: 10 | matchLabels: 11 | app: keynote 12 | template: 13 | metadata: 14 | labels: 15 | app: keynote 16 | spec: 17 | containers: 18 | - name: keynote 19 | image: ghcr.io/cncf/keynote 20 | imagePullPolicy: IfNotPresent 21 | ports: 22 | - containerPort: 8501 23 | env: 24 | - name: OLLAMA_BASE_URL 25 | value: "http://host.docker.internal:11434" 26 | -------------------------------------------------------------------------------- /kubernetes/service.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: keynote 5 | spec: 6 | type: ClusterIP 7 | selector: 8 | app: keynote 9 | ports: 10 | - protocol: TCP 11 | port: 8501 12 | targetPort: 8501 13 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | python-dotenv 2 | streamlit 3 | ollama -------------------------------------------------------------------------------- /shutdown.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Deleting the Kubernetes manifest for this demo..." 4 | kubectl delete -f kubernetes/ 5 | 6 | # Use pgrep to find the PIDs of the processes with 'kubectl port-forward' command 7 | pids=$(pgrep -f "kubectl port-forward") 8 | 9 | if [ -z "$pids" ]; then 10 | echo "No kubectl port-forward processes found." 11 | else 12 | # Display the PIDs and kill them 13 | for pid in $pids; do 14 | echo "Terminating process with PID: $pid" 15 | kill $pid 16 | if [ $? -eq 0 ]; then 17 | echo "Successfully terminated process with PID: $pid" 18 | else 19 | echo "Failed to terminate process with PID: $pid. You may need to run the script as root or use 'sudo'." 20 | fi 21 | done 22 | fi 23 | 24 | echo "Delete the kind cluster to clean up our machine..." 25 | kind delete cluster -n llm -------------------------------------------------------------------------------- /startup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Create the kind cluster..." 4 | kind create cluster --config cluster.yaml 5 | 6 | kind -n llm load docker-image ghcr.io/cncf/keynote:latest 7 | 8 | echo "Applying the Kubernets manifests..." 9 | kubectl apply -f kubernetes/deployment.yaml 10 | kubectl apply -f kubernetes/service.yaml 11 | 12 | # Wait for the pod starting with "keynote" to be running 13 | while [[ $(kubectl get pods --no-headers | grep 'keynote' | awk '{print $3}') != "Running" ]]; do 14 | echo "Keynote demo is starting, make some noise..." 15 | sleep 10 16 | done 17 | 18 | echo "Forwarding the Keynote pod..." 19 | kubectl port-forward svc/keynote 8501:8501 & 20 | 21 | echo "Keynote demo is running, it's time to present! Open your browser to http://localhost:8501" --------------------------------------------------------------------------------