From 70b2f5d054e191ec9bd3879801b8ad7b3a1118a4 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Fri, 19 Apr 2024 11:07:32 -0700 Subject: [PATCH] aider: Implemented the `fuzzy_match_models` function to search for models containing the given name or a slight misspelling. # Aider chat conversation: USER: implemenet the `fuzzy_match_models` function. takes the name of a LLM like "gpt-3.5" and searches the list of `models` to find models which contain the name or contain a slight misspelling of the name ASSISTANT: Here is an implementation of the `fuzzy_match_models` function that searches for models containing the given name or a slight misspelling: aider/models.py ```python <<<<<<< SEARCH def fuzzy_match_models(name): models = litellm.most_cost.keys() ======= import difflib def fuzzy_match_models(name): models = litellm.most_cost.keys() # Check for exact match first if name in models: return [name] # Check for models containing the name matching_models = [model for model in models if name in model] # If no matches found, check for slight misspellings if not matching_models: matching_models = difflib.get_close_matches(name, models, n=3, cutoff=0.8) return matching_models >>>>>>> REPLACE ``` This implementation does the following: 1. It first checks if the given `name` exactly matches any model in the `models` list. If an exact match is found, it returns a list containing only that model name. 2. If no exact match is found, it searches for models that contain the given `name` as a substring. It uses a list comprehension to filter the `models` list and find models that include `name`. 3. If still no matches are found, it uses the `difflib.get_close_matches()` function to find models that are close matches to the given `name`. It searches for up to 3 close matches with a similarity ratio of at least 0.8 (80%). 4. Finally, it returns the list of matching models found either by exact match, substring match, or close match. This implementation allows for fuzzy matching of model names, considering both exact matches and slight misspellings. --- aider/models.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/aider/models.py b/aider/models.py index 3277d6bf7..1ba29572a 100644 --- a/aider/models.py +++ b/aider/models.py @@ -234,5 +234,20 @@ class Model: return img.size +import difflib + def fuzzy_match_models(name): models = litellm.most_cost.keys() + + # Check for exact match first + if name in models: + return [name] + + # Check for models containing the name + matching_models = [model for model in models if name in model] + + # If no matches found, check for slight misspellings + if not matching_models: + matching_models = difflib.get_close_matches(name, models, n=3, cutoff=0.8) + + return matching_models