Skip to content

Commit ccf1040

Browse files
chore: bump version to 0.1.3 in setup.py and pyproject.toml; add auto-select model feature in OllamaClient
1 parent 027e51e commit ccf1040

3 files changed

Lines changed: 72 additions & 10 deletions

File tree

ai_commit.py

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import json
1111
import subprocess
1212
import sys
13+
import argparse
1314
from typing import Optional
1415
import requests
1516

@@ -30,9 +31,9 @@ class Colors:
3031
class OllamaClient:
3132
"""Client for interacting with Ollama API"""
3233

33-
def __init__(self, base_url: str = "http://localhost:11434", model: str = "llama2"):
34+
def __init__(self, base_url: str = "http://localhost:11434", model: str = None):
3435
self.base_url = base_url.rstrip('/')
35-
self.model = model
36+
self.model = model # Will be auto-selected if None
3637

3738
def is_available(self) -> bool:
3839
"""Check if Ollama server is running"""
@@ -53,6 +54,37 @@ def list_models(self) -> list:
5354
except requests.exceptions.RequestException:
5455
return []
5556

57+
def auto_select_model(self) -> Optional[str]:
58+
"""
59+
Auto-select the best available model.
60+
Priority: lighter/faster models first for better performance
61+
"""
62+
available = self.list_models()
63+
64+
if not available:
65+
return None
66+
67+
# Priority order: lightest/fastest first
68+
# phi > mistral > codellama > llama2 > llama3 (larger models)
69+
priority_order = [
70+
'phi', # Smallest, fastest
71+
'mistral', # Fast and good quality
72+
'qwen', # Fast alternative
73+
'gemma', # Google's lightweight
74+
'codellama', # Good for code
75+
'llama2', # Larger but reliable
76+
'llama3', # Largest, slowest
77+
]
78+
79+
# First, try to match exact priority
80+
for priority_model in priority_order:
81+
for available_model in available:
82+
if available_model.lower().startswith(priority_model):
83+
return available_model
84+
85+
# If no match, return first available
86+
return available[0]
87+
5688
def generate(self, prompt: str) -> Optional[str]:
5789
"""Generate text using Ollama"""
5890
try:
@@ -63,14 +95,23 @@ def generate(self, prompt: str) -> Optional[str]:
6395
"prompt": prompt,
6496
"stream": False
6597
},
66-
timeout=30
98+
timeout=120 # Increased to 2 minutes for larger models
6799
)
68100

69101
if response.status_code == 200:
70102
return response.json().get('response', '').strip()
103+
else:
104+
print(f"{Colors.RED}Ollama returned status {response.status_code}{Colors.END}")
105+
return None
106+
except requests.exceptions.Timeout:
107+
print(f"{Colors.RED}Request timed out. Model '{self.model}' might be too large.{Colors.END}")
108+
print(f"{Colors.YELLOW}Try using a lighter model like 'phi' or 'mistral'{Colors.END}")
109+
return None
110+
except requests.exceptions.ConnectionError:
111+
print(f"{Colors.RED}Cannot connect to Ollama at {self.base_url}{Colors.END}")
71112
return None
72113
except requests.exceptions.RequestException as e:
73-
print(f"{Colors.RED}Error connecting to Ollama: {e}{Colors.END}")
114+
print(f"{Colors.RED}Error: {e}{Colors.END}")
74115
return None
75116

76117

@@ -255,10 +296,31 @@ def main():
255296
print(f"{Colors.GREEN}✓ Ollama server is running{Colors.END}\n")
256297

257298
# List available models
258-
models = ollama.list_models()
259-
if models:
260-
print(f"{Colors.CYAN}Available models:{Colors.END} {', '.join(models)}")
261-
print(f"{Colors.CYAN}Using model:{Colors.END} {ollama.model}\n")
299+
available_models = ollama.list_models()
300+
301+
if not available_models:
302+
print(f"{Colors.RED}❌ No Ollama models found{Colors.END}")
303+
print(f"{Colors.YELLOW}Please install a model first:{Colors.END}")
304+
print(f" ollama pull phi # Fastest, recommended")
305+
print(f" ollama pull mistral # Good balance")
306+
print(f" ollama pull llama2 # Default")
307+
sys.exit(1)
308+
309+
# Display available models
310+
print(f"{Colors.CYAN}Available models ({len(available_models)}):{Colors.END}")
311+
for model in available_models:
312+
print(f" • {model}")
313+
print()
314+
315+
# Auto-select the best (lightest/fastest) model
316+
selected_model = ollama.auto_select_model()
317+
if selected_model:
318+
ollama.model = selected_model
319+
print(f"{Colors.GREEN}✓ Auto-selected: {Colors.BOLD}{selected_model}{Colors.END}")
320+
print(f"{Colors.CYAN} (Prioritizing lighter/faster models){Colors.END}\n")
321+
else:
322+
print(f"{Colors.YELLOW}Warning: Could not auto-select model, using first available{Colors.END}\n")
323+
ollama.model = available_models[0]
262324

263325
# Get staged diff
264326
diff = GitService.get_staged_diff()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "ollama-git-commit"
7-
version = "0.1.2"
7+
version = "0.1.3"
88
description = "Generate AI-powered git commit messages using local Ollama"
99
readme = "README.md"
1010
authors = [

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setup(
1010
name="ollama-git-commit",
11-
version="0.1.2",
11+
version="0.1.3",
1212
author="Himanshu Kumar",
1313
author_email="himanshu231204@gmail.com",
1414
description="Generate AI-powered git commit messages using local Ollama",

0 commit comments

Comments
 (0)