<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Benjamin Breton on Medium]]></title>
        <description><![CDATA[Stories by Benjamin Breton on Medium]]></description>
        <link>https://medium.com/@benjamin.breton45?source=rss-22387610714a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*iymcLJk6szGdz2xk.</url>
            <title>Stories by Benjamin Breton on Medium</title>
            <link>https://medium.com/@benjamin.breton45?source=rss-22387610714a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 05:02:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@benjamin.breton45/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Running a Powerful AI Chat Model without a GPU]]></title>
            <link>https://medium.com/@benjamin.breton45/revolutionizing-chatbot-services-running-a-powerful-ai-model-without-a-gpu-a812a8b6240b?source=rss-22387610714a------2</link>
            <guid isPermaLink="false">https://medium.com/p/a812a8b6240b</guid>
            <dc:creator><![CDATA[Benjamin Breton]]></dc:creator>
            <pubDate>Mon, 11 Dec 2023 00:11:31 GMT</pubDate>
            <atom:updated>2023-12-11T02:59:30.950Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In today’s fast-paced technological landscape, the efficient and private operation of powerful AI models has become crucial. As a fun experiment, I explored running models that work well on my local machine on a serverless setup. While there are specialized services like <a href="https://together.ai/">Together.ai</a> for running LLMs serverlessly, they are external and may not meet all privacy requirements.</p><p>Intrigued by the quality of the recently released StableLM Zephyr 3B LLM from <a href="https://stability.ai/news/stablelm-zephyr-3b-stability-llm">Stability.ai</a>, I investigated if the computational efficiency advances in <a href="https://github.com/ggerganov/llama.cpp">llama-cpp</a> could make it feasible to run this model on a serverless instance like CloudRun, considering aspects like cold starts, cost, and deployment.</p><h3>What is CloudRun ?</h3><p>CloudRun is a serverless, container-based system within the Google Cloud Platform. One of its main advantages is scaling down to zero — <strong>meaning no cost when not in use </strong>— and it scales automatically with increased demand. For the REST API, I used FastAPI, a handy Python library for API development.</p><h3>The Model</h3><p>The model I used is <strong>StableLM Zephyr 3B</strong>, on a version that was quantized to 4 bits by <a href="https://huggingface.co/TheBloke/stablelm-zephyr-3b-GGUF">TheBloke</a>. It is an impresive model for its size released by Stability.ai. Unfortunately it is not available for commercial use, but you can contact Stability.ai for <a href="https://stability.ai/contact">licensing</a>.</p><p>I use <a href="https://github.com/abetlen/llama-cpp-python">llama-cpp-python</a> for interfacing with the model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EvHBzQnm1i2WB97bqekX5g.png" /><figcaption>courtesy of Stability.ai</figcaption></figure><h3>Code</h3><h4>The API Code</h4><p>The api code is straightforward:</p><pre>import fastapi<br>from fastapi.responses import JSONResponse<br>from llama_cpp import Llama<br>from time import time<br>import logging<br><br><br>MODEL_PATH = &quot;./stablelm-zephyr-3b.Q4_K_M.gguf&quot;<br><br># Logger setup<br>logging.basicConfig(level=logging.INFO)<br>logger = logging.getLogger(__name__)<br><br># Initialize Llama model<br>try:<br>    llm = Llama(<br>        model_path=MODEL_PATH,<br>        chat_format=&quot;llama-2&quot;,<br>        n_ctx=4096,<br>        n_threads=8,<br>        n_gpu_layers=0,<br>    )<br>except Exception as e:<br>    logger.error(f&quot;Failed to load model: {e}&quot;)<br>    raise<br><br>app = fastapi.FastAPI()<br><br><br>@app.get(&quot;/&quot;)<br>def index():<br>    return fastapi.responses.RedirectResponse(url=&quot;/docs&quot;)<br><br><br>@app.get(&quot;/health&quot;)<br>def health():<br>    return {&quot;status&quot;: &quot;ok&quot;}<br><br><br># Chat Completion API<br>@app.get(&quot;/complete&quot;)<br>async def complete(<br>    question: str,<br>    system: str = &quot;You are a story writing assistant.&quot;,<br>    temperature: float = 0.7,<br>    seed: int = 42,<br>) -&gt; dict:<br>    try:<br>        st = time()<br>        output = llm.create_chat_completion(<br>            messages=[<br>                {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: system},<br>                {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: question},<br>            ],<br>            temperature=temperature,<br>            seed=seed,<br>        )<br>        et = time()<br>        output[&quot;time&quot;] = et - st<br>        return output<br>    except Exception as e:<br>        logger.error(f&quot;Error in /complete endpoint: {e}&quot;)<br>        return JSONResponse(<br>            status_code=500, content={&quot;message&quot;: &quot;Internal Server Error&quot;}<br>        )<br><br><br>if __name__ == &quot;__main__&quot;:<br>    import uvicorn<br><br>    uvicorn.run(app, host=&quot;0.0.0.0&quot;, port=8000)</pre><p>The main endpoint <em>/complete</em> is where we will call the model.</p><h4>Dockerfile</h4><p>The Dockerfile is pretty standard, with extra dependencies enabling llama-cpp to use <strong>OPENBLAS</strong> for faster model processing.</p><pre>FROM python:3.12-slim<br><br># Update package list, install OpenBLAS, build tools, pkg-config, and other necessary tools<br>RUN apt-get update \<br>    &amp;&amp; apt-get install -y libopenblas-dev build-essential cmake git pkg-config \<br>    &amp;&amp; rm -rf /var/lib/apt/lists/*<br><br># Set working directory<br>WORKDIR /app<br><br># Copy the application files<br>COPY . /app<br><br># Install llama-cpp-python and other dependencies<br>RUN CMAKE_ARGS=&quot;-DLLAMA_BLAS=ON -DLLAMA_BLAS_VENDOR=OpenBLAS&quot; pip install llama-cpp-python==0.2.20<br>RUN pip install --no-cache-dir -r requirements.txt<br><br># Download the model using Hugging Face CLI<br>RUN huggingface-cli download TheBloke/stablelm-zephyr-3b-GGUF stablelm-zephyr-3b.Q4_K_M.gguf --local-dir . --local-dir-use-symlinks False<br><br># Expose the port the app runs on<br>EXPOSE 8000<br><br># Command to run the application<br>CMD [&quot;uvicorn&quot;, &quot;main:app&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;8000&quot;]</pre><p>The model is then deployed using cloudbuild that reads the instructions in a <em>cloudbuild.yam</em>l.</p><p>All deployment details are in the <a href="https://github.com/bbreton3/stablelm-zephyr-serverless">Git repo</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3AJxhRMH4n56s7c-EVUF5g.png" /><figcaption>FastApi Swagger</figcaption></figure><h3>Results</h3><p>I ran a benchmark on the model (the code is in <em>benchmark.py</em>) and the response time is acceptable even tough the model is not running on a GPU.</p><ul><li>Warm-up (cold start) time: <strong>8.44 seconds</strong></li><li>Total time for subsequent requests: 23.63 seconds</li><li>Average request time: 7.88 seconds per request</li><li><strong>Average tokens per second: 11.78 tokens/sec</strong></li></ul><p>This performance is suitable for conversational applications and some backend types of usage.</p><h3>Pricing</h3><p>Let’s estimate the cost of running this setup. According to <a href="https://cloud.google.com/run/pricing">GCP pricing</a>, the cost for a Cloud Run machine in a Tier 1 data center is:</p><ul><li>$0.000212 per second for an 8 CPU, 8GB RAM machine.</li></ul><p>Therefore, the <strong>cost per 1000 tokens is about $0.018</strong>.</p><p>This is comparable to the GPT-4 API but more expensive than the GPT-3.5 Turbo API.</p><p>Using Together.ai would be much cheaper, but running your own service adds control, albeit with higher complexity and cost.</p><h3><strong>Privacy</strong></h3><p>The main advantage of running your own service is full control over data privacy, crucial for sensitive data handling.</p><h3><strong>Conclusion &amp; Future work</strong></h3><p>The advancement of smaller models like StableLM Zephyr 3B represents a significant step in AI deployment. <br>Running a high-capacity model without a GPU simplifies the process while enhancing privacy and accessibility. I look forward to future efficiency improvements and the exciting developments they will bring.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a812a8b6240b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>