Add MODEL_X reference support to app.py

Critical fix for activity model configuration:
- Handle MODEL_1, MODEL_2, MODEL_3 references in get_openai_client_and_model()
- Look up MODEL_ENDPOINT_{n}, MODEL_API_KEY_{n}, MODEL_NAME_{n} from environment
- Fall back gracefully to default model if MODEL_{n} not configured
- Matches implementation in research/guarded_ai.py

Fixes error: 'NoneType' object has no attribute 'chat'
This error occurred when activities tried to use classifier_model="MODEL_1"
but the app didn't know how to resolve the MODEL_X reference.

Now activity37 (programming languages) will work correctly with:
- classifier_model: "MODEL_1" (Hermes for classification)
- feedback_model: "MODEL_3" (Qwen3-Coder for code generation)
This commit is contained in:
Claude 2025-11-08 19:47:32 +00:00
parent 0f06772afb
commit 8cebcbf118
No known key found for this signature in database

29
app.py
View file

@ -132,6 +132,35 @@ def get_client_for_model(model_name: str):
def get_openai_client_and_model(
model_name="adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic",
):
"""Get OpenAI client and model name.
Supports MODEL_X references (e.g., MODEL_1, MODEL_2, MODEL_3) that map to
environment variables MODEL_ENDPOINT_X and MODEL_API_KEY_X.
"""
# Handle MODEL_X references
if model_name and model_name.startswith("MODEL_"):
try:
model_num = model_name.split("_")[1]
endpoint_key = f"MODEL_ENDPOINT_{model_num}"
api_key_key = f"MODEL_API_KEY_{model_num}"
model_name_key = f"MODEL_NAME_{model_num}"
endpoint = os.environ.get(endpoint_key)
api_key = os.environ.get(api_key_key)
if endpoint and api_key:
client = get_client_for_endpoint(endpoint, api_key)
# Get the actual model name from environment, or use sensible default
actual_model = os.environ.get(model_name_key, "model")
return client, actual_model
else:
print(f"Warning: MODEL_{model_num} not configured ({endpoint_key} or {api_key_key} missing)")
# Fall back to default model
model_name = "adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic"
except Exception as e:
print(f"Warning: Failed to load {model_name}: {e}, falling back to default")
model_name = "adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic"
return get_client_for_model(model_name), model_name