|
1 | 1 | from aicodebot import version as aicodebot_version |
2 | | -from aicodebot.helpers import exec_and_get_output, get_token_length, git_diff_context, logger |
| 2 | +from aicodebot.helpers import exec_and_get_output, get_llm_model, get_token_length, git_diff_context, logger |
3 | 3 | from aicodebot.prompts import generate_files_context, generate_sidekick_prompt |
4 | 4 | from dotenv import load_dotenv |
5 | 5 | from langchain.callbacks.base import BaseCallbackHandler |
@@ -120,6 +120,10 @@ def commit(verbose, response_token_size, yes, skip_pre_commit): |
120 | 120 | # Check the size of the diff context and adjust accordingly |
121 | 121 | request_token_size = get_token_length(diff_context) + get_token_length(prompt.template) |
122 | 122 | model = get_llm_model(request_token_size) |
| 123 | + if model is None: |
| 124 | + raise click.ClickException( |
| 125 | + f"The diff is too large to generate a commit message ({request_token_size} tokens). 😢" |
| 126 | + ) |
123 | 127 |
|
124 | 128 | # Set up the language model |
125 | 129 | llm = ChatOpenAI(model=model, temperature=PRECISE_TEMPERATURE, max_tokens=DEFAULT_MAX_TOKENS, verbose=verbose) |
@@ -181,7 +185,10 @@ def debug(command, verbose): |
181 | 185 | logger.trace(f"Prompt: {prompt}") |
182 | 186 |
|
183 | 187 | # Set up the language model |
184 | | - model = get_llm_model(get_token_length(error_output) + get_token_length(prompt.template)) |
| 188 | + request_token_size = get_token_length(error_output) + get_token_length(prompt.template) |
| 189 | + model = get_llm_model(request_token_size) |
| 190 | + if model is None: |
| 191 | + raise click.ClickException(f"The output is too large to debug ({request_token_size} tokens). 😢") |
185 | 192 |
|
186 | 193 | with Live(Markdown(""), auto_refresh=True) as live: |
187 | 194 | llm = ChatOpenAI( |
@@ -250,7 +257,8 @@ def review(commit, verbose): |
250 | 257 | response_token_size = DEFAULT_MAX_TOKENS |
251 | 258 | request_token_size = get_token_length(diff_context) + get_token_length(prompt.template) |
252 | 259 | model = get_llm_model(request_token_size) |
253 | | - logger.info(f"Diff context token size: {request_token_size}, using model: {model}") |
| 260 | + if model is None: |
| 261 | + raise click.ClickException(f"The diff is too large to review ({request_token_size} tokens). 😢") |
254 | 262 |
|
255 | 263 | with Live(Markdown(""), auto_refresh=True) as live: |
256 | 264 | llm = ChatOpenAI( |
@@ -291,7 +299,12 @@ def sidekick(request, verbose, files): |
291 | 299 |
|
292 | 300 | # Generate the prompt and set up the model |
293 | 301 | prompt = generate_sidekick_prompt(request, files) |
294 | | - model = get_llm_model(get_token_length(prompt.template) + get_token_length(context)) |
| 302 | + request_token_size = get_token_length(prompt.template) + get_token_length(context) |
| 303 | + model = get_llm_model(request_token_size) |
| 304 | + if model is None: |
| 305 | + raise click.ClickException( |
| 306 | + f"The file context you supplied is too large ({request_token_size} tokens). 😢 Try again with less files." |
| 307 | + ) |
295 | 308 |
|
296 | 309 | llm = ChatOpenAI( |
297 | 310 | model=model, |
@@ -400,41 +413,6 @@ def setup_environment(): |
400 | 413 | ) |
401 | 414 |
|
402 | 415 |
|
403 | | -def get_llm_model(token_size=0): |
404 | | - # https://platform.openai.com/docs/models/gpt-3-5 |
405 | | - # We want to use GPT-4, if it is available for this OPENAI_API_KEY, otherwise GPT-3.5 |
406 | | - # We also want to use the largest model that supports the token size we need |
407 | | - model_options = { |
408 | | - "gpt-4": 8192, |
409 | | - "gpt-4-32k": 32768, |
410 | | - "gpt-3.5-turbo": 4096, |
411 | | - "gpt-3.5-turbo-16k": 16384, |
412 | | - } |
413 | | - gpt_4_supported = os.getenv("GPT_4_SUPPORTED") == "true" |
414 | | - |
415 | | - # For some unknown reason, tiktoken often underestimates the token size by ~10%, so let's buffer |
416 | | - token_size = int(token_size * 1.1) |
417 | | - |
418 | | - if gpt_4_supported: |
419 | | - if token_size <= model_options["gpt-4"]: |
420 | | - logger.info(f"Using GPT-4 for token size {token_size}") |
421 | | - return "gpt-4" |
422 | | - elif token_size <= model_options["gpt-4-32k"]: |
423 | | - logger.info(f"Using GPT-4-32k for token size {token_size}") |
424 | | - return "gpt-4-32k" |
425 | | - else: |
426 | | - raise click.ClickException("🛑 The context is too large to for the Model. 😞") |
427 | | - else: |
428 | | - if token_size <= model_options["gpt-3.5-turbo"]: # noqa: PLR5501 |
429 | | - logger.info(f"Using GPT-3.5-turbo for token size {token_size}") |
430 | | - return "gpt-3.5-turbo" |
431 | | - elif token_size <= model_options["gpt-3.5-turbo-16k"]: |
432 | | - logger.info(f"Using GPT-3.5-turbo-16k for token size {token_size}") |
433 | | - return "gpt-3.5-turbo-16k" |
434 | | - else: |
435 | | - raise click.ClickException("🛑 The context is too large to for the Model. 😞") |
436 | | - |
437 | | - |
438 | 416 | class RichLiveCallbackHandler(BaseCallbackHandler): |
439 | 417 | buffer = [] |
440 | 418 |
|
|
0 commit comments