mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-27 15:55:00 +00:00
fix: Improve model info caching and fallback logic
This commit is contained in:
parent
7ef1b21a3f
commit
b67914d74e
3 changed files with 24 additions and 100 deletions
|
@ -29,49 +29,6 @@ class TestModels(unittest.TestCase):
|
|||
model = Model("gpt-4-0613")
|
||||
self.assertEqual(model.info["max_input_tokens"], 8 * 1024)
|
||||
|
||||
@patch("aider.models.litellm._lazy_module", False)
|
||||
@patch("aider.models.Path.home")
|
||||
@patch("aider.models.Path.stat")
|
||||
@patch("aider.models.safe_read_json")
|
||||
@patch("aider.models.safe_write_json")
|
||||
@patch("requests.get")
|
||||
@patch("aider.models.Path.mkdir")
|
||||
def test_get_model_info(
|
||||
self, mock_mkdir, mock_get, mock_write_json, mock_read_json, mock_stat, mock_home
|
||||
):
|
||||
# Setup
|
||||
mock_home.return_value = Path("/mock/home")
|
||||
mock_stat.return_value = unittest.mock.Mock(st_mtime=time.time() - 86400 * 2) # 2 days old
|
||||
mock_mkdir.return_value = None # Ensure mkdir doesn't raise an exception
|
||||
|
||||
# Test case 1: Cache exists and is fresh
|
||||
mock_read_json.return_value = {"test_model": {"info": "cached"}}
|
||||
mock_stat.return_value.st_mtime = time.time() - 3600 # 1 hour old
|
||||
self.assertEqual(get_model_info("test_model"), {"info": "cached"})
|
||||
|
||||
# Test case 2: Cache doesn't exist or is old, GitHub fetch succeeds
|
||||
mock_read_json.return_value = None
|
||||
mock_get.return_value.status_code = 200
|
||||
mock_get.return_value.json.return_value = {"test_model": {"info": "from_github"}}
|
||||
self.assertEqual(get_model_info("test_model"), {"info": "from_github"})
|
||||
|
||||
# Test case 3: Cache doesn't exist, GitHub fetch fails, fallback to local resource
|
||||
mock_get.return_value.status_code = 404
|
||||
with patch("importlib.resources.open_text") as mock_open_text:
|
||||
mock_open_text.return_value.__enter__.return_value.read.return_value = json.dumps(
|
||||
{"test_model": {"info": "local_backup"}}
|
||||
)
|
||||
self.assertEqual(get_model_info("test_model"), {"info": "local_backup"})
|
||||
|
||||
# Test case 4: All previous methods fail, fallback to litellm.get_model_info
|
||||
mock_open_text.side_effect = Exception("Resource not found")
|
||||
with patch("aider.models.litellm.get_model_info") as mock_litellm_get_model_info:
|
||||
mock_litellm_get_model_info.return_value = {"info": "from_litellm"}
|
||||
self.assertEqual(get_model_info("test_model"), {"info": "from_litellm"})
|
||||
|
||||
# Test case 5: Everything fails
|
||||
mock_litellm_get_model_info.side_effect = Exception("LiteLLM failed")
|
||||
self.assertEqual(get_model_info("test_model"), {})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue