Skip to content

Commit 23992cc

Browse files
committed
[chatqna-core] Rename config variable name and create validator class
Signed-off-by: Yeoh, Hoong Tee <[email protected]>
1 parent 13c6016 commit 23992cc

19 files changed

+119
-102
lines changed

sample-applications/chat-question-and-answer-core/app/chain.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
# If RUN_TEST is set to "True", the model download and conversion steps are skipped.
1818
# This flag is set in the conftest.py file before running the tests.
1919
if os.getenv("RUN_TEST", "").lower() != "true":
20-
if config.MODEL_BACKEND == "openvino":
21-
backend_module = importlib.import_module("app.openvino_backend")
22-
backend_instance = backend_module.OpenVINOBackend()
20+
if config.MODEL_RUNTIME == "openvino":
21+
runtime_module = importlib.import_module("app.openvino_backend")
22+
runtime_instance = runtime_module.OpenVINOBackend()
2323

24-
elif config.MODEL_BACKEND == "ollama":
25-
backend_module = importlib.import_module("app.ollama_backend")
26-
backend_instance = backend_module.OllamaBackend()
24+
elif config.MODEL_RUNTIME == "ollama":
25+
runtime_module = importlib.import_module("app.ollama_backend")
26+
runtime_instance = runtime_module.OllamaBackend()
2727

2828
else:
29-
raise ValueError(f"Unsupported model backend: {config.MODEL_BACKEND}")
29+
raise ValueError(f"Unsupported model runtime: {config.MODEL_RUNTIME}")
3030

31-
embedding, llm, reranker = backend_instance.init_models()
31+
embedding, llm, reranker = runtime_instance.init_models()
3232

3333
template = config.PROMPT_TEMPLATE
3434

@@ -62,6 +62,7 @@ def get_retriever():
6262
"""
6363

6464
enable_rerank = config._ENABLE_RERANK
65+
logger.info(f"Reranker enabled: {enable_rerank}")
6566
search_method = config._SEARCH_METHOD
6667
fetch_k = config._FETCH_K
6768

sample-applications/chat-question-and-answer-core/app/config.py

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
from typing import Union
44
from os.path import dirname, abspath
55
from .prompt import get_prompt_template
6+
from .runtime_validators import OpenVINOValidator, OllamaValidator
67
import os
78
import yaml
89

910
class Settings(BaseSettings):
1011
"""
1112
Settings class for configuring the Chatqna-Core application.
12-
This class manages application settings, including model backend selection,
13+
This class manages application settings, including model backend runtime selection,
1314
model IDs, device configurations, prompt templates, and various internal paths.
1415
It loads configuration from a YAML file, validates backend-specific requirements,
1516
and ensures prompt templates contain required placeholders.
@@ -20,7 +21,7 @@ class Settings(BaseSettings):
2021
SUPPORTED_FORMATS (set): Supported file formats for input documents.
2122
DEBUG (bool): Debug mode flag.
2223
HF_ACCESS_TOKEN (str): Hugging Face access token.
23-
MODEL_BACKEND (str): Backend to use for models ('openvino' or 'ollama').
24+
MODEL_RUNTIME (str): Backend runtime to use for models ('openvino' or 'ollama').
2425
EMBEDDING_MODEL_ID (str): Identifier for the embedding model.
2526
RERANKER_MODEL_ID (str): Identifier for the reranker model.
2627
LLM_MODEL_ID (str): Identifier for the large language model.
@@ -56,7 +57,7 @@ class Settings(BaseSettings):
5657
DEBUG: bool = False
5758

5859
HF_ACCESS_TOKEN: str = ""
59-
MODEL_BACKEND: str = ""
60+
MODEL_RUNTIME: str = ""
6061
EMBEDDING_MODEL_ID: str = ""
6162
RERANKER_MODEL_ID: str = ""
6263
LLM_MODEL_ID: str = ""
@@ -98,58 +99,23 @@ def __init__(self, **kwargs):
9899
if hasattr(self, key):
99100
setattr(self, key, value)
100101

101-
self._validate_backend_settings()
102+
self._validate_runtime_settings()
102103
self._check_and_validate_prompt_template()
103104

105+
def _validate_runtime_settings(self):
106+
validators = {
107+
"openvino": OpenVINOValidator,
108+
"ollama": OllamaValidator,
109+
}
104110

105-
def _validate_backend_settings(self):
106-
if self.MODEL_BACKEND:
107-
self.MODEL_BACKEND = self.MODEL_BACKEND.lower()
108-
else:
109-
raise ValueError("MODEL_BACKEND must not be an empty string.")
110-
111-
if self.MODEL_BACKEND == "openvino":
112-
self._ENABLE_RERANK = True
113-
114-
# Validate Huggingface token
115-
if not self.HF_ACCESS_TOKEN:
116-
raise ValueError("HF_ACCESS_TOKEN must not be an empty string for 'openvino' backend.")
117-
118-
# Validate required model IDs
119-
for model_name in ["EMBEDDING_MODEL_ID", "RERANKER_MODEL_ID", "LLM_MODEL_ID"]:
120-
model_id = getattr(self, model_name)
121-
if not model_id:
122-
raise ValueError(f"{model_name} must not be an empty string for 'openvino' backend.")
123-
124-
elif self.MODEL_BACKEND == "ollama":
125-
self._ENABLE_RERANK = False
126-
127-
# Validate that all devices are set to "CPU" as ollama currently only enabled for CPU
128-
invalid_devices = [
129-
attr for attr in ["EMBEDDING_DEVICE", "RERANKER_DEVICE", "LLM_DEVICE"]
130-
if getattr(self, attr, "") != "CPU"
131-
]
132-
133-
if invalid_devices:
134-
raise ValueError(
135-
f"When MODEL_BACKEND is 'ollama', the following devices must be set to 'CPU': {', '.join(invalid_devices)}"
136-
)
137-
138-
# Handle RERANKER_MODEL_ID
139-
if self.RERANKER_MODEL_ID:
140-
print("WARNING - RERANKER_MODEL_ID is ignored when MODEL_BACKEND is 'ollama'. Setting it to empty.")
141-
self.RERANKER_MODEL_ID = ""
142-
else:
143-
print("INFO - MODEL_BACKEND is 'ollama'. Reranker model is not supported.")
144-
145-
# Validate required model IDs (excluding reranker)
146-
for model_name in ["EMBEDDING_MODEL_ID", "LLM_MODEL_ID"]:
147-
model_id = getattr(self, model_name)
148-
if not model_id:
149-
raise ValueError(f"{model_name} must not be an empty string for 'ollama' backend.")
111+
runtime = self.MODEL_RUNTIME.lower()
112+
validator_cls = validators.get(runtime)
150113

151-
else:
152-
raise ValueError(f"Unsupported MODEL_BACKEND '{self.MODEL_BACKEND}'. Only 'openvino' and 'ollama' are supported.")
114+
if not validator_cls:
115+
raise ValueError(f"Unsupported model runtime: {self.MODEL_RUNTIME}. Supported runtimes are: {', '.join(validators.keys())}")
116+
117+
validator = validator_cls(self)
118+
validator.validate()
153119

154120
def _check_and_validate_prompt_template(self):
155121
if not self.PROMPT_TEMPLATE:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from typing import TYPE_CHECKING
2+
3+
if TYPE_CHECKING:
4+
# To avoid circular imports
5+
from config import Settings
6+
7+
class BackendValidator:
8+
def __init__(self, settings: "Settings"):
9+
self.settings = settings
10+
11+
def validate(self):
12+
raise NotImplementedError("Subclasses must implement this method.")
13+
14+
15+
class OpenVINOValidator(BackendValidator):
16+
def validate(self):
17+
self.settings.MODEL_RUNTIME = "openvino"
18+
if not self.settings.HF_ACCESS_TOKEN:
19+
raise ValueError("HF_ACCESS_TOKEN must not be empty for 'openvino' backend.")
20+
21+
for model_name in ["EMBEDDING_MODEL_ID", "RERANKER_MODEL_ID", "LLM_MODEL_ID"]:
22+
if not getattr(self.settings, model_name):
23+
raise ValueError(f"{model_name} must not be empty for 'openvino' backend.")
24+
25+
self.settings._ENABLE_RERANK = True
26+
27+
28+
class OllamaValidator(BackendValidator):
29+
def validate(self):
30+
self.settings.MODEL_RUNTIME = "ollama"
31+
invalid_devices = [
32+
attr for attr in ["EMBEDDING_DEVICE", "RERANKER_DEVICE", "LLM_DEVICE"]
33+
if getattr(self.settings, attr) != "CPU"
34+
]
35+
if invalid_devices:
36+
raise ValueError(
37+
f"When MODEL_RUNTIME is 'ollama', the following devices must be set to 'CPU': {', '.join(invalid_devices)}"
38+
)
39+
40+
if self.settings.RERANKER_MODEL_ID:
41+
print("WARNING - RERANKER_MODEL_ID is ignored for 'ollama'. Setting it to empty.")
42+
self.settings.RERANKER_MODEL_ID = ""
43+
else:
44+
print("INFO - Reranker model not supported for 'ollama'.")
45+
46+
for model_name in ["EMBEDDING_MODEL_ID", "LLM_MODEL_ID"]:
47+
if not getattr(self.settings, model_name):
48+
raise ValueError(f"{model_name} must not be empty for 'ollama' backend.")
49+
50+
self.settings._ENABLE_RERANK = False

sample-applications/chat-question-and-answer-core/app/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ class ChatRequest(BaseModel):
4747

4848

4949
# Conditionally include OpenVINO routes
50-
if config.MODEL_BACKEND == "openvino":
50+
if config.MODEL_RUNTIME == "openvino":
5151
from .openvino_routes import router as openvino_router
5252
app.include_router(openvino_router)
5353

54-
elif config.MODEL_BACKEND == "ollama":
54+
elif config.MODEL_RUNTIME == "ollama":
5555
from .ollama_routes import router as ollama_router
5656
app.include_router(ollama_router)
5757

sample-applications/chat-question-and-answer-core/chart/templates/_helpers.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ Define the name for nginx Chart.
2727
{{- end }}
2828

2929
{{- define "chatqna-core.validateGpuSettings" -}}
30-
{{- $backend := .Values.global.MODEL_BACKEND | lower }}
30+
{{- $backend := .Values.global.MODEL_RUNTIME | lower }}
3131
{{- if eq $backend "ollama" }}
3232
{{- if or (eq .Values.global.EMBEDDING_DEVICE "GPU") (eq .Values.global.LLM_DEVICE "GPU") }}
33-
{{- fail "MODEL_BACKEND is set to 'ollama', but EMBEDDING_DEVICE or LLM_DEVICE is set to 'GPU'. Ollama backend only supports CPU devices." }}
33+
{{- fail "MODEL_RUNTIME is set to 'ollama', but EMBEDDING_DEVICE or LLM_DEVICE is set to 'GPU'. Ollama backend only supports CPU devices." }}
3434
{{- end }}
3535

3636
{{- if and (not .Values.gpu.enabled) (or (eq .Values.global.EMBEDDING_DEVICE "GPU") (eq .Values.global.RERANKER_DEVICE "GPU") (eq .Values.global.LLM_DEVICE "GPU")) }}

sample-applications/chat-question-and-answer-core/chart/templates/configmap.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ metadata:
77
data:
88
config.yaml: |
99
model_settings:
10-
MODEL_BACKEND: "{{ .Values.global.MODEL_BACKEND }}"
10+
MODEL_RUNTIME: "{{ .Values.global.MODEL_RUNTIME }}"
1111
EMBEDDING_MODEL_ID: "{{ .Values.global.EMBEDDING_MODEL }}"
1212
LLM_MODEL_ID: "{{ .Values.global.LLM_MODEL }}"
1313
MAX_TOKENS: {{ .Values.global.MAX_TOKENS }}
@@ -16,15 +16,15 @@ data:
1616
{{ .Values.global.PROMPT_TEMPLATE | nindent 8 }}
1717
{{- end }}
1818
19-
{{- if and (ne (.Values.global.MODEL_BACKEND | lower) "ollama") (.Values.global.RERANKER_MODEL) }}
19+
{{- if and (ne (.Values.global.MODEL_RUNTIME | lower) "ollama") (.Values.global.RERANKER_MODEL) }}
2020
RERANKER_MODEL_ID: "{{ .Values.global.RERANKER_MODEL }}"
2121
{{- end }}
2222
23-
{{- if eq (.Values.global.MODEL_BACKEND | lower) "ollama" }}
23+
{{- if eq (.Values.global.MODEL_RUNTIME | lower) "ollama" }}
2424
KEEP_ALIVE: {{ .Values.global.KEEP_ALIVE }}
2525
{{- end }}
2626
27-
{{- if ne (.Values.global.MODEL_BACKEND | lower) "ollama" }}
27+
{{- if ne (.Values.global.MODEL_RUNTIME | lower) "ollama" }}
2828
device_settings:
2929
EMBEDDING_DEVICE: "{{ .Values.global.EMBEDDING_DEVICE }}"
3030
LLM_DEVICE: "{{ .Values.global.LLM_DEVICE }}"

sample-applications/chat-question-and-answer-core/chart/templates/deployment.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ spec:
2020
fsGroup: 1000
2121
containers:
2222
- name: chatqna-core
23-
{{- if eq (.Values.global.MODEL_BACKEND | lower) "ollama" }}
23+
{{- if eq (.Values.global.MODEL_RUNTIME | lower) "ollama" }}
2424
image: "{{ .Values.image.registry }}chatqna:{{ .Values.image.tags.ollama }}"
2525
{{- else }}
2626
image: "{{ .Values.image.registry }}chatqna:{{ if .Values.gpu.enabled }}{{ .Values.image.tags.openvinoGPU }}{{ else }}{{ .Values.image.tags.openvinoCPU }}{{ end }}"
@@ -41,7 +41,7 @@ spec:
4141
value: "{{ .Values.global.https_proxy }}"
4242
- name: no_proxy
4343
value: "{{ .Values.global.no_proxy }},127.0.0.1"
44-
{{- if eq (.Values.global.MODEL_BACKEND | lower) "openvino" }}
44+
{{- if eq (.Values.global.MODEL_RUNTIME | lower) "openvino" }}
4545
- name: HF_ACCESS_TOKEN
4646
value: "{{ .Values.global.huggingface.apiToken }}"
4747
{{- end }}

sample-applications/chat-question-and-answer-core/chart/values-ollama.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
global:
2-
MODEL_BACKEND: "ollama"
2+
MODEL_RUNTIME: "ollama"
33
EMBEDDING_MODEL:
44
LLM_MODEL:
55
KEEP_ALIVE: -1

sample-applications/chat-question-and-answer-core/chart/values-openvino.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
global:
22
huggingface:
33
apiToken:
4-
MODEL_BACKEND: "openvino"
4+
MODEL_RUNTIME: "openvino"
55
EMBEDDING_MODEL:
66
LLM_MODEL:
77
RERANKER_MODEL:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
model_settings:
2-
MODEL_BACKEND: "ollama"
2+
MODEL_RUNTIME: "ollama"
33
EMBEDDING_MODEL_ID: "mxbai-embed-large"
44
LLM_MODEL_ID: "llama3.1"
55
KEEP_ALIVE: -1

0 commit comments

Comments
 (0)