Skip to content

Commit bdc1477

Browse files
feat(ollama): Improved error management for Ollama. Also pull model from Ollama libary if needed.
1 parent 1e0666c commit bdc1477

File tree

3 files changed

+84
-15
lines changed

3 files changed

+84
-15
lines changed

describer/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ def log(self, message):
7575
timer = getattr(self, 'timer')
7676
if timer:
7777
timer.step(message)
78+
79+
def log_fatal(self, message):
80+
message = f'ERROR: {message}'
81+
self.log(message)
82+
83+
print(message)
84+
exit(1)
7885

7986
def get_model(self):
8087
if not self.model:

describer/ollama.py

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,27 @@ def _get_ollama_model_code(self):
4545
def answer_question(self, image_path, question):
4646
if not self.model:
4747
self._init_model()
48+
49+
import ollama
4850

49-
response = self.model.chat(
50-
model=self._get_ollama_model_code(),
51-
messages=[{
52-
'role': 'user',
53-
'content': question,
54-
'images': [image_path]
55-
}],
56-
options={
57-
'temperature': 0
58-
}
59-
)
51+
model_code = self._get_ollama_model_code()
52+
53+
try:
54+
response = self.model.chat(
55+
model=model_code,
56+
messages=[{
57+
'role': 'user',
58+
'content': question,
59+
'images': [image_path]
60+
}],
61+
options={
62+
'temperature': 0
63+
}
64+
)
65+
except ollama._types.ResponseError as e:
66+
if 'this model is missing data required for image input' in str(e):
67+
self.log_fatal(f'This ollama model ("{model_code}") does not support image inputs. Please check model doc on ollama.com')
68+
raise(e)
6069

6170
# example of a response
6271
'''
@@ -81,13 +90,59 @@ def answer_question(self, image_path, question):
8190
return response['message']['content']
8291

8392
def _init_model(self):
84-
from ollama import Client
85-
self.model = Client(host=BVQA_OLLAMA_HOST)
93+
from ollama import Client, _types
94+
self.client = Client(host=BVQA_OLLAMA_HOST)
95+
self.model = self.client
96+
97+
self.stop_if_ollama_not_running()
98+
99+
self.pull_ollama_model()
100+
101+
86102
# load the model into memory so get_compute_info get stats.
87103
# and we separate the load from the actual inference in the logs.
88-
res = self.model.generate(model=self._get_ollama_model_code(), prompt='Just say "yes". Nothing else.')
104+
try:
105+
res = self.model.generate(model=self._get_ollama_model_code(), prompt='Just say "yes". Nothing else.')
106+
except _types.ResponseError as e:
107+
print('H0'*40)
108+
raise e
109+
89110
return self.model
90111

112+
def stop_if_ollama_not_running(self):
113+
self.get_ollama_models()
114+
115+
def is_ollama_model_pulled(self):
116+
ret = False
117+
model_code = self._get_ollama_model_code()
118+
models = self.get_ollama_models()
119+
matching_models = [
120+
model
121+
for model
122+
in models
123+
if model.model == model_code
124+
]
125+
return bool(matching_models)
126+
127+
def pull_ollama_model(self):
128+
if not self.is_ollama_model_pulled():
129+
import ollama
130+
model_code = self._get_ollama_model_code()
131+
self.log(f'Pulling model "{model_code}" from Ollama repository...')
132+
try:
133+
res = self.client.pull(model_code)
134+
except ollama._types.ResponseError as e:
135+
self.log_fatal(f'failed to pull model "{model_code}" from Ollama repository. {e}')
136+
137+
def get_ollama_models(self):
138+
ret = []
139+
try:
140+
res = self.client.list()
141+
ret = res.models
142+
except ConnectionError as e:
143+
self.log_fatal(f'{e} ; BVQA_OLLAMA_HOST="{BVQA_OLLAMA_HOST}"')
144+
return ret
145+
91146
def get_compute_info(self):
92147
ret = {
93148
'type': 'ollama',
@@ -96,6 +151,13 @@ def get_compute_info(self):
96151
}
97152
model = self.get_model()
98153

154+
'''
155+
# models=[Model(model='gemma3:4b', name='gemma3:4b', digest='a2af6cc3eb7fa8be8504abaf9b04e88f17a119ec3f04a3addf55f92841195f5a', expires_at=datetime.datetime(2025, 7, 18, 14, 3, 39, 405547, tzinfo=TzInfo(+01:00)), size=6169209844, size_vram=3700725812, details=ModelDetails(parent_model='', format='gguf', family='gemma3', families=['gemma3'], parameter_size='4.3B', quantization_level='Q4_K_M'))]
156+
res = self.client.ps()
157+
print(res)
158+
exit(1)
159+
'''
160+
99161
if model:
100162
res = model.ps()
101163
for m in res.models:

requirements/ollama.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ollama
1+
ollama<0.6

0 commit comments

Comments
 (0)