@@ -220,3 +220,130 @@ def validate_context_auth(pat: str, user_id: str, api_base: str = None):
220
220
logger .error (f"❌ Validation failed: \n { error_msg } " )
221
221
logger .error ("Please check your credentials and try again." )
222
222
raise click .Abort () # Exit without saving the configuration
223
+
224
+
225
+ def customize_ollama_model (model_path , model_name , port , context_length ):
226
+ """Customize the Ollama model name in the cloned template files.
227
+ Args:
228
+ model_path: Path to the cloned model directory
229
+ model_name: The model name to set (e.g., 'llama3.1', 'mistral')
230
+
231
+ """
232
+ model_py_path = os .path .join (model_path , "1" , "model.py" )
233
+
234
+ if not os .path .exists (model_py_path ):
235
+ logger .warning (f"Model file { model_py_path } not found, skipping model name customization" )
236
+ return
237
+
238
+ try :
239
+ # Read the model.py file
240
+ with open (model_py_path , 'r' ) as file :
241
+ content = file .read ()
242
+ if model_name :
243
+ # Replace the default model name in the load_model method
244
+ content = content .replace (
245
+ 'self.model = os.environ.get("OLLAMA_MODEL_NAME", \' llama3.2\' )' ,
246
+ f'self.model = os.environ.get("OLLAMA_MODEL_NAME", \' { model_name } \' )' ,
247
+ )
248
+
249
+ if port :
250
+ # Replace the default port variable in the model.py file
251
+ content = content .replace ("PORT = '23333'" , f"PORT = '{ port } '" )
252
+
253
+ if context_length :
254
+ # Replace the default context length variable in the model.py file
255
+ content = content .replace (
256
+ "context_length = '8192'" , f"context_length = '{ context_length } '"
257
+ )
258
+
259
+ # Write the modified content back to model.py
260
+ with open (model_py_path , 'w' ) as file :
261
+ file .write (content )
262
+
263
+ except Exception as e :
264
+ logger .error (f"Failed to customize Ollama model name in { model_py_path } : { e } " )
265
+ raise
266
+
267
+
268
+ def check_ollama_installed ():
269
+ """Check if the Ollama CLI is installed."""
270
+ try :
271
+ import subprocess
272
+
273
+ result = subprocess .run (
274
+ ['ollama' , '--version' ], capture_output = True , text = True , check = False
275
+ )
276
+ if result .returncode == 0 :
277
+ return True
278
+ else :
279
+ return False
280
+ except FileNotFoundError :
281
+ return False
282
+
283
+
284
+ def _is_package_installed (package_name ):
285
+ """Helper function to check if a single package in requirements.txt is installed."""
286
+ import importlib .metadata
287
+
288
+ try :
289
+ importlib .metadata .distribution (package_name )
290
+ logger .debug (f"✅ { package_name } - installed" )
291
+ return True
292
+ except importlib .metadata .PackageNotFoundError :
293
+ logger .debug (f"❌ { package_name } - not installed" )
294
+ return False
295
+ except Exception as e :
296
+ logger .warning (f"Error checking { package_name } : { e } " )
297
+ return False
298
+
299
+
300
+ def check_requirements_installed (model_path ):
301
+ """Check if all dependencies in requirements.txt are installed."""
302
+ import re
303
+ from pathlib import Path
304
+
305
+ requirements_path = Path (model_path ) / "requirements.txt"
306
+
307
+ if not requirements_path .exists ():
308
+ logger .warning (f"requirements.txt not found at { requirements_path } " )
309
+ return True
310
+
311
+ try :
312
+ package_pattern = re .compile (r'^([a-zA-Z0-9_-]+)' )
313
+
314
+ # Getting package name and version (for logging)
315
+ requirements = [
316
+ (match .group (1 ), pack )
317
+ for line in requirements_path .read_text ().splitlines ()
318
+ if (pack := line .strip ())
319
+ and not line .startswith ('#' )
320
+ and (match := package_pattern .match (line ))
321
+ ]
322
+
323
+ if not requirements :
324
+ logger .info ("No dependencies found in requirements.txt" )
325
+ return True
326
+
327
+ logger .info (f"Checking { len (requirements )} dependencies..." )
328
+
329
+ missing = [
330
+ full_req
331
+ for package_name , full_req in requirements
332
+ if not _is_package_installed (package_name )
333
+ ]
334
+
335
+ if not missing :
336
+ logger .info (f"✅ All { len (requirements )} dependencies are installed!" )
337
+ return True
338
+
339
+ # Report missing packages
340
+ logger .error (
341
+ f"❌ { len (missing )} of { len (requirements )} required packages are missing in the current environment"
342
+ )
343
+ logger .error ("\n " .join (f" - { pkg } " for pkg in missing ))
344
+ logger .warning (f"To install: pip install -r { requirements_path } " )
345
+ return False
346
+
347
+ except Exception as e :
348
+ logger .error (f"Failed to check requirements: { e } " )
349
+ return False
0 commit comments