From 70bb7740f83a9d2078112adec6d7c465454fcd4f Mon Sep 17 00:00:00 2001 From: GSSwain <12575773+GSSwain@users.noreply.github.com> Date: Sat, 13 Jun 2026 10:01:09 +1000 Subject: [PATCH] Adds support to override Gemini model and model timeout --- README.md | 6 ++++++ src/maca/maca_config.py | 6 ++++++ src/maca/models/gemini.py | 5 +++-- tests/test_behavior.py | 13 +++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 41e2da9..a479656 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,12 @@ MACA currently uses Gemini for medium and above tasks. For secure setup details, ```bash export GEMINI_API_KEY="your-gemini-api-key" ``` + + You can optionally customize the Gemini model and request timeout (in seconds): + ```bash + export GEMINI_MODEL="gemini-3.5-flash" + export GEMINI_TIMEOUT_SECONDS="45" + ``` * **Recommended macOS Keychain + .zshenv Setup**: ```bash security add-generic-password -a "$USER" -s "MACA_GEMINI_API_KEY" -w "your-gemini-key" diff --git a/src/maca/maca_config.py b/src/maca/maca_config.py index 2fdf1fa..a9c3e6e 100644 --- a/src/maca/maca_config.py +++ b/src/maca/maca_config.py @@ -12,6 +12,12 @@ def get_gemini_api_key(): OLLAMA_API_URL = os.environ.get("OLLAMA_API_URL", "http://localhost:11434") OLLAMA_MODEL = os.environ.get("OLLAMA_MODEL", "gemma2:2b") +GEMINI_MODEL = os.environ.get("GEMINI_MODEL", "gemini-3.5-flash") +try: + GEMINI_TIMEOUT_SECONDS = float(os.environ.get("GEMINI_TIMEOUT_SECONDS", "45")) +except ValueError: + GEMINI_TIMEOUT_SECONDS = 45.0 + # If set to True, will mock Gemma/Gemini calls if unavailable/unconfigured MOCK_GEMMA_FALLBACK = os.environ.get("MOCK_GEMMA_FALLBACK", "True").lower() == "true" diff --git a/src/maca/models/gemini.py b/src/maca/models/gemini.py index 0b633fe..59b5f9c 100644 --- a/src/maca/models/gemini.py +++ b/src/maca/models/gemini.py @@ -6,7 +6,8 @@ class GeminiClient: def __init__(self): self.api_key = config.get_gemini_api_key() - self.model = "gemini-3.5-flash" + self.model = config.GEMINI_MODEL + self.timeout = config.GEMINI_TIMEOUT_SECONDS def generate(self, prompt, system_instruction=""): if not self.api_key: @@ -42,7 +43,7 @@ def generate(self, prompt, system_instruction=""): headers={"Content-Type": "application/json"}, method="POST" ) - with urllib.request.urlopen(req, timeout=45, context=ssl_context) as response: + with urllib.request.urlopen(req, timeout=self.timeout, context=ssl_context) as response: res = json.loads(response.read().decode("utf-8")) # Extract response text candidates = res.get("candidates", []) diff --git a/tests/test_behavior.py b/tests/test_behavior.py index f4baf57..2ab70e8 100644 --- a/tests/test_behavior.py +++ b/tests/test_behavior.py @@ -55,6 +55,19 @@ def test_local_gemma_client_falls_back_to_cli(self): self.assertEqual(result, "CLI response") run_mock.assert_called_once() + def test_gemini_config_uses_maca_config(self): + old_model = config.GEMINI_MODEL + old_timeout = config.GEMINI_TIMEOUT_SECONDS + try: + config.GEMINI_MODEL = "gemini-test-model" + config.GEMINI_TIMEOUT_SECONDS = 12.5 + client = GeminiClient() + self.assertEqual(client.model, "gemini-test-model") + self.assertEqual(client.timeout, 12.5) + finally: + config.GEMINI_MODEL = old_model + config.GEMINI_TIMEOUT_SECONDS = old_timeout + if __name__ == "__main__": unittest.main()