Skip to content

Commit

Permalink
fix: migrated setup.py script for ollama to httpx
Browse files Browse the repository at this point in the history
  • Loading branch information
adubovik committed Sep 18, 2024
1 parent 64f4da8 commit f2a8e17
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
9 changes: 6 additions & 3 deletions dial-docker-compose/ollama/ollama_setup/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
FROM python:3.11-alpine
RUN pip install requests
COPY setup.py /setup.py
CMD ["sh", "-c", "python /setup.py && tail -f /dev/null"]

WORKDIR /app
COPY * /app
RUN pip install -r requirements.txt

CMD ["sh", "-c", "python setup.py && tail -f /dev/null"]
1 change: 1 addition & 0 deletions dial-docker-compose/ollama/ollama_setup/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
httpx==0.27.2
36 changes: 20 additions & 16 deletions dial-docker-compose/ollama/ollama_setup/setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import asyncio
from contextlib import asynccontextmanager
import logging
import os
import time
import requests
import httpx

OLLAMA_URL = os.getenv("OLLAMA_URL")
if OLLAMA_URL is None:
Expand All @@ -14,46 +13,51 @@

HEALTH_FILE = "/healthy"

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)

log.info(f"OLLAMA_URL = {OLLAMA_URL}")
log.info(f"OLLAMA_CHAT_MODEL = {OLLAMA_CHAT_MODEL}")
log.info(f"OLLAMA_VISION_MODEL = {OLLAMA_VISION_MODEL}")
def print_info(*args, **kwargs):
print(*args, **kwargs, flush=True)


print_info(f"OLLAMA_URL = {OLLAMA_URL}")
print_info(f"OLLAMA_CHAT_MODEL = {OLLAMA_CHAT_MODEL}")
print_info(f"OLLAMA_VISION_MODEL = {OLLAMA_VISION_MODEL}")


@asynccontextmanager
async def timer(name: str):
log.info(f"[{name}] Starting...")
print_info(f"[{name}] Starting...")
start = time.perf_counter()
yield
elapsed = time.perf_counter() - start
log.info(f"[{name}] Executed in {elapsed:.2f} seconds")
print_info(f"[{name}] Executed in {elapsed:.2f} seconds")


ollama = httpx.AsyncClient(base_url=OLLAMA_URL, timeout=60)


async def wait_for_ollama():
async def wait_for_startup():
while True:
try:
if requests.get(OLLAMA_URL).ok:
if (await ollama.get("/")).is_success:
break
except requests.RequestException:
except Exception:
pass
await asyncio.sleep(1)


async def pull_model(model):
data = {"name": model, "stream": False}
requests.post(f"{OLLAMA_URL}/api/pull", json=data).raise_for_status()
(await ollama.post("/api/pull", json=data)).raise_for_status()


async def create_alias(source, dest):
data = {"source": source, "destination": dest}
requests.post(f"{OLLAMA_URL}/api/copy", json=data).raise_for_status()
(await ollama.post(f"/api/copy", json=data)).raise_for_status()


async def load_model(model):
data = {"model": model}
requests.post(f"{OLLAMA_URL}/api/generate", json=data).raise_for_status()
(await ollama.post(f"/api/generate", json=data)).raise_for_status()


async def mark_as_healthy():
Expand All @@ -62,7 +66,7 @@ async def mark_as_healthy():

async def main():
async with timer("Waiting for Ollama to start"):
await wait_for_ollama()
await wait_for_startup()

for model, alias in [
(OLLAMA_CHAT_MODEL, "chat-model"),
Expand Down

0 comments on commit f2a8e17

Please sign in to comment.