Skip to content

Commit 4465984

Browse files
committed
Update model selection logic in helpers.py
This commit updates the `get_llm_model` function in `helpers.py` to dynamically select the best available model based on the OpenAI API key's supported engines and the token size. The previous hard-coded model selection logic has been replaced with a more flexible approach that queries the available engines from the OpenAI API. This ensures that the most suitable model is selected for the given token size, improving the efficiency and adaptability of the code. In addition, the commit also includes minor changes to the import statements and the logging messages to accommodate the updated model selection logic. Remember, the future is not set in stone. It's a wildly malleable thing that's still very much in the works. And you, dear developer, are one of the many hands that get to shape it. Let's keep coding a better future together!
1 parent a66057f commit 4465984

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

aicodebot/helpers.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from loguru import logger
2+
from openai.api_resources import engine
23
from pathlib import Path
3-
import fnmatch, os, subprocess, sys, tiktoken, yaml
4+
import fnmatch, openai, os, subprocess, sys, tiktoken, yaml
45

56
# ---------------------------------------------------------------------------- #
67
# Global logging configuration for loguru #
@@ -46,41 +47,40 @@ def generate_directory_structure(path, ignore_patterns=None, use_gitignore=True,
4647

4748

4849
def get_llm_model(token_size=0):
49-
# https://platform.openai.com/docs/models/gpt-3-5
50-
# We want to use GPT-4, if it is available for this OPENAI_API_KEY, otherwise GPT-3.5
51-
# We also want to use the largest model that supports the token size we need
5250
model_options = {
5351
"gpt-4": 8192,
5452
"gpt-4-32k": 32768,
5553
"gpt-3.5-turbo": 4096,
5654
"gpt-3.5-turbo-16k": 16384,
5755
}
56+
5857
config = read_config()
59-
gpt_4_supported = config["gpt_4_supported"]
58+
openai.api_key = config["openai_api_key"]
59+
engines = engine.Engine.list()
60+
logger.trace(f"Engines: {engines}")
6061

6162
# For some unknown reason, tiktoken often underestimates the token size by ~10%, so let's buffer
6263
token_size = int(token_size * 1.1)
6364

64-
if gpt_4_supported:
65-
if token_size <= model_options["gpt-4"]:
66-
logger.info(f"Using GPT-4 for token size {token_size}")
67-
return "gpt-4"
68-
elif token_size <= model_options["gpt-4-32k"]:
69-
logger.info(f"Using GPT-4-32k for token size {token_size}")
70-
return "gpt-4-32k"
71-
else:
72-
logger.critical("🛑 The context is too large to for the Model. 😞")
73-
return None
65+
# Try to use GPT-4 if it is supported and the token size is small enough
66+
if "gpt-4" in [engine.id for engine in engines.data] and token_size <= model_options["gpt-4"]:
67+
logger.info(f"Using GPT-4 for token size {token_size}")
68+
return "gpt-4"
69+
elif "gpt-4-32k" in [engine.id for engine in engines.data] and token_size <= model_options["gpt-4-32k"]:
70+
logger.info(f"Using GPT-4-32k for token size {token_size}")
71+
return "gpt-4-32k"
72+
elif token_size <= model_options["gpt-3.5-turbo"]:
73+
logger.info(f"Using GPT-3.5-turbo for token size {token_size}")
74+
return "gpt-3.5-turbo"
75+
elif token_size <= model_options["gpt-3.5-turbo-16k"]:
76+
logger.info(f"Using GPT-3.5-turbo-16k for token size {token_size}")
77+
return "gpt-3.5-turbo-16k"
7478
else:
75-
if token_size <= model_options["gpt-3.5-turbo"]: # noqa: PLR5501
76-
logger.info(f"Using GPT-3.5-turbo for token size {token_size}")
77-
return "gpt-3.5-turbo"
78-
elif token_size <= model_options["gpt-3.5-turbo-16k"]:
79-
logger.info(f"Using GPT-3.5-turbo-16k for token size {token_size}")
80-
return "gpt-3.5-turbo-16k"
81-
else:
82-
logger.critical("🛑 The context is too large to for the Model. 😞")
83-
return None
79+
logger.critical(
80+
f"🛑 The context is too large ({token_size})"
81+
"for the any of the models supported by your Open AI API key. 😞"
82+
)
83+
return None
8484

8585

8686
def get_token_length(text, model="gpt-3.5-turbo"):

0 commit comments

Comments
 (0)