From 79a7840f24e1a18cbeba9ab379b4557b0cd1fbf3 Mon Sep 17 00:00:00 2001 From: jeffser Date: Tue, 3 Sep 2024 21:56:37 -0600 Subject: [PATCH] Better checker for Ollama instance missing --- src/connection_handler.py | 46 ++++++++++++++++++++++----------------- src/window.py | 6 ----- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/connection_handler.py b/src/connection_handler.py index a23b0d6..f711c19 100644 --- a/src/connection_handler.py +++ b/src/connection_handler.py @@ -2,7 +2,7 @@ """ Handles requests to remote and integrated instances of Ollama """ -import json, os, requests, subprocess, threading +import json, os, requests, subprocess, threading, shutil from .internal import data_dir, cache_dir from logging import getLogger from time import sleep @@ -92,25 +92,31 @@ class instance(): self.idle_timer.start() def start(self): - if not os.path.isdir(os.path.join(cache_dir, 'tmp/ollama')): - os.mkdir(os.path.join(cache_dir, 'tmp/ollama')) - self.instance = None - params = self.overrides.copy() - params["OLLAMA_DEBUG"] = "1" - params["OLLAMA_HOST"] = f"127.0.0.1:{self.local_port}" # You can't change this directly sorry :3 - params["HOME"] = data_dir - params["TMPDIR"] = os.path.join(cache_dir, 'tmp/ollama') - instance = subprocess.Popen(["ollama", "serve"], env={**os.environ, **params}, stderr=subprocess.PIPE, stdout=subprocess.PIPE, text=True) - threading.Thread(target=log_output, args=(instance.stdout,)).start() - threading.Thread(target=log_output, args=(instance.stderr,)).start() - logger.info("Starting Alpaca's Ollama instance...") - logger.debug(params) - logger.info("Started Alpaca's Ollama instance") - v_str = subprocess.check_output("ollama -v", shell=True).decode('utf-8') - logger.info('Ollama version: {}'.format(v_str.split('client version is ')[1].strip())) - self.instance = instance - if not self.idle_timer: - self.start_timer() + if shutil.which('ollama'): + if not os.path.isdir(os.path.join(cache_dir, 'tmp/ollama')): + os.mkdir(os.path.join(cache_dir, 'tmp/ollama')) + self.instance = None + params = self.overrides.copy() + params["OLLAMA_DEBUG"] = "1" + params["OLLAMA_HOST"] = f"127.0.0.1:{self.local_port}" # You can't change this directly sorry :3 + params["HOME"] = data_dir + params["TMPDIR"] = os.path.join(cache_dir, 'tmp/ollama') + instance = subprocess.Popen(["ollama", "serve"], env={**os.environ, **params}, stderr=subprocess.PIPE, stdout=subprocess.PIPE, text=True) + threading.Thread(target=log_output, args=(instance.stdout,)).start() + threading.Thread(target=log_output, args=(instance.stderr,)).start() + logger.info("Starting Alpaca's Ollama instance...") + logger.debug(params) + logger.info("Started Alpaca's Ollama instance") + v_str = subprocess.check_output("ollama -v", shell=True).decode('utf-8') + logger.info('Ollama version: {}'.format(v_str.split('client version is ')[1].strip())) + self.instance = instance + if not self.idle_timer: + self.start_timer() + else: + self.remote = True + if not self.remote_url: + self.remote_url = 'http://0.0.0.0:11434' + window.connection_error() def stop(self): if self.idle_timer: diff --git a/src/window.py b/src/window.py index 07d1c9a..a7b3e63 100644 --- a/src/window.py +++ b/src/window.py @@ -777,12 +777,6 @@ Generate a title following these rules: if show_launch_dialog: GLib.idle_add(self.launch_dialog.present, self) - #Check if Ollama instance doesn't exists - if not shutil.which('ollama'): - remote = True - if not remote_url: - remote_url = 'http://0.0.0.0:11434' - #Instance self.launch_level_bar.set_value(0) self.launch_status.set_description(_('Loading instance'))