From 56d98310229e13ccc8c4a212507465f3d64a92da Mon Sep 17 00:00:00 2001 From: mepha89 Date: Mon, 24 Feb 2025 20:08:16 -0330 Subject: [PATCH 1/4] add-ollama-helper-funcs --- .pre-commit-config.yaml | 2 +- app/utils/ollama.py | 58 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 app/utils/ollama.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9800571..50115df 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,7 @@ # See https://pre-commit.com for more information # See https://pre-commit.com/hooks.html for more hooks default_language_version: - python: python3.10 + python: python3.12.5 repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v5.0.0 diff --git a/app/utils/ollama.py b/app/utils/ollama.py new file mode 100644 index 0000000..0844269 --- /dev/null +++ b/app/utils/ollama.py @@ -0,0 +1,58 @@ +""" +This module defines helper functions for interacting with the OLLAMA API. +""" + +import requests +from requests.exceptions import RequestException + + +def ollama_pull_model(machine_public_ip, model_name): + url = f"http://{machine_public_ip}:11434/api/pull" + model_request = { + "model": model_name, + "stream": False, + } + + try: + print("Before pull") # TODO: remove print + response = requests.post( + url=url, + json=model_request, + ) + response.raise_for_status() + return {"status": response.status_code, "response": response.json()} + except RequestException as e: + error_message = f"An error occurred during pulling Ollama Model request: {e}" + return { + "status": "failed", + "message": error_message, + } + + +def check_model_on_machine(machine_public_ip, model_name): + print("Before check") # TODO: remove print + check_url = f"http://{machine_public_ip}:11434/api/tags" + try: + check_response = requests.get(url=check_url) + check_response.raise_for_status() + + if check_response.status_code == 200: + check_response_data = check_response.json() + print(check_response_data) # TODO: remove print + for model in check_response_data.get("models", []): + print(model) # TODO: remove print + print(model["name"]) # TODO: remove print + if model_name in model["name"]: + return { + "status": "success", + "message": f"Model {model_name} pulled to instance", + } + return { + "status": "failed", + "error": f"Model {model_name} not found on instance", + } + except RequestException as e: + return { + "status": "failed", + "message": f"An error occurred during checking Ollama Model on machine: {e}", + } From f1131c61851f75838a5551bd4130c55f70cdf06f Mon Sep 17 00:00:00 2001 From: mepha89 Date: Mon, 24 Feb 2025 20:10:50 -0330 Subject: [PATCH 2/4] update-cpu-pull-model --- app/api/v1/endpoints/machine_endpoints.py | 38 +++++++---------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/app/api/v1/endpoints/machine_endpoints.py b/app/api/v1/endpoints/machine_endpoints.py index 0bd8401..91aeedc 100644 --- a/app/api/v1/endpoints/machine_endpoints.py +++ b/app/api/v1/endpoints/machine_endpoints.py @@ -7,7 +7,7 @@ import requests from typing import Annotated, List -from fastapi import APIRouter, Depends, status +from fastapi import APIRouter, Depends, status, HTTPException from fastapi.security import OAuth2PasswordBearer from core.config import settings @@ -29,6 +29,7 @@ MachineNotFoundError, MachineOperationFailedError, ) +from utils.ollama import ollama_pull_model, check_model_on_machine logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) @@ -437,34 +438,19 @@ async def pull_cpu_model( model_name = selection_request.model_name machine_public_ip = ec2_service.get_instance_public_ip(machine_id) - url = f"http://{machine_public_ip}:11434/api/pull" - model_request = { - "model": model_name, - "stream": False, - } + pull_response = ollama_pull_model(machine_public_ip, model_name) + if pull_response["status"] == "failed": + raise HTTPException( + status_code=500, + detail=f"Failed to pull model {model_name} to instance.", + ) - response = requests.post( - url=url, - json=model_request, - ) + check_response = check_model_on_machine(machine_public_ip, model_name) + if check_response["status"] == "failed": + raise HTTPException(status_code=404, detail=check_response["error"]) - if response.status_code == 200: - response_data = response.json() - response_data["message"] = f"Model {model_name} pulled to instance" - return response_data - elif response.status_code == 500: - error_message = response.json().get("error", "Unknown error") - if error_message == "pull model manifest: file does not exist": - logger.error(f"Error: {error_message}") - return {"status": "failed", "error": error_message} - else: - response.raise_for_status() - else: - response.raise_for_status() - except httpx.HTTPStatusError as e: - logger.error(f"HTTP error occurred: {e}") - return {"error": str(e)} + return {"message": check_response["message"]} except Exception as e: logger.error(f"An error occurred: {e}") return {"error": str(e)} From 2867800ba3948251f6dcf2fd4b24b66dc9ed1257 Mon Sep 17 00:00:00 2001 From: mepha89 Date: Mon, 24 Feb 2025 20:13:52 -0330 Subject: [PATCH 3/4] update-gpu-ollama-pull --- app/api/v1/endpoints/machine_endpoints.py | 35 +++++++---------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/app/api/v1/endpoints/machine_endpoints.py b/app/api/v1/endpoints/machine_endpoints.py index 91aeedc..6ebdbd5 100644 --- a/app/api/v1/endpoints/machine_endpoints.py +++ b/app/api/v1/endpoints/machine_endpoints.py @@ -248,34 +248,19 @@ async def pull_gpu_model( model_name = selection_request.model_name machine_public_ip = ec2_service.get_instance_public_ip(machine_id) - url = f"http://{machine_public_ip}:11434/api/pull" - model_request = { - "model": model_name, - "stream": False, - } + pull_response = ollama_pull_model(machine_public_ip, model_name) + if pull_response["status"] == "failed": + raise HTTPException( + status_code=500, + detail=f"Failed to pull model {model_name} to instance.", + ) - response = requests.post( - url=url, - json=model_request, - ) + check_response = check_model_on_machine(machine_public_ip, model_name) + if check_response["status"] == "failed": + raise HTTPException(status_code=404, detail=check_response["error"]) - if response.status_code == 200: - response_data = response.json() - response_data["message"] = f"Model {model_name} pulled to instance" - return response_data - elif response.status_code == 500: - error_message = response.json().get("error", "Unknown error") - if error_message == "pull model manifest: file does not exist": - logger.error(f"Error: {error_message}") - return {"status": "failed", "error": error_message} - else: - response.raise_for_status() - else: - response.raise_for_status() - except httpx.HTTPStatusError as e: - logger.error(f"HTTP error occurred: {e}") - return {"error": str(e)} + return {"message": check_response["message"]} except Exception as e: logger.error(f"An error occurred: {e}") return {"error": str(e)} From c2724e3fcad9bc9afc45002b84ef8b27e562018e Mon Sep 17 00:00:00 2001 From: mepha89 Date: Mon, 24 Feb 2025 20:14:51 -0330 Subject: [PATCH 4/4] clean-ollama-helper --- app/utils/ollama.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/utils/ollama.py b/app/utils/ollama.py index 0844269..39989cb 100644 --- a/app/utils/ollama.py +++ b/app/utils/ollama.py @@ -12,9 +12,7 @@ def ollama_pull_model(machine_public_ip, model_name): "model": model_name, "stream": False, } - try: - print("Before pull") # TODO: remove print response = requests.post( url=url, json=model_request, @@ -30,7 +28,6 @@ def ollama_pull_model(machine_public_ip, model_name): def check_model_on_machine(machine_public_ip, model_name): - print("Before check") # TODO: remove print check_url = f"http://{machine_public_ip}:11434/api/tags" try: check_response = requests.get(url=check_url) @@ -38,10 +35,7 @@ def check_model_on_machine(machine_public_ip, model_name): if check_response.status_code == 200: check_response_data = check_response.json() - print(check_response_data) # TODO: remove print for model in check_response_data.get("models", []): - print(model) # TODO: remove print - print(model["name"]) # TODO: remove print if model_name in model["name"]: return { "status": "success",