-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use custom exceptions instead of KeyboardInterrupts and exit() #45
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
) | ||
from .code_change import CodeChange, CodeChangeAction | ||
from .config_manager import ConfigManager | ||
from .errors import UserError | ||
from .git_handler import ( | ||
get_git_diff_for_path, | ||
get_non_gitignored_files, | ||
|
@@ -76,11 +77,7 @@ def _abs_file_paths_from_list(paths: Iterable[str], check_for_text: bool = True) | |
if path.is_file(): | ||
if check_for_text and not _is_file_text_encoded(path): | ||
logging.info(f"File path {path} is not text encoded.") | ||
cprint( | ||
f"Filepath {path} is not text encoded.", | ||
"light_yellow", | ||
) | ||
raise KeyboardInterrupt | ||
raise UserError(f"File path {path} is not text encoded.") | ||
file_paths_direct.add(os.path.realpath(path)) | ||
elif path.is_dir(): | ||
nonignored_files = set( | ||
|
@@ -137,7 +134,7 @@ def _set_file_paths( | |
cprint("The following paths do not exist:") | ||
print("\n".join(invalid_paths)) | ||
print("Exiting...") | ||
exit() | ||
raise UserError() | ||
|
||
excluded_files, excluded_files_from_dir = _abs_file_paths_from_list( | ||
exclude_paths, check_for_text=False | ||
|
@@ -249,7 +246,7 @@ def _get_new_code_lines(self, changes) -> Iterable[str]: | |
min_changed_line = largest_changed_line + 1 | ||
for i, change in enumerate(changes): | ||
if change.last_changed_line >= min_changed_line: | ||
raise ValueError(f"Change line number overlap in file {change.file}") | ||
raise UserError(f"Change line number overlap in file {change.file}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a MentatError; the collision handling is supposed to handle this |
||
min_changed_line = change.first_changed_line | ||
new_code_lines = change.apply(new_code_lines) | ||
return new_code_lines | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,13 @@ | |
from termcolor import cprint | ||
|
||
from .config_manager import mentat_dir_path, user_config_path | ||
from .errors import MentatError, UserError | ||
|
||
package_name = __name__.split(".")[0] | ||
|
||
|
||
# Check for .env file or already exported API key | ||
# If no api key found, exit and warn user | ||
# If no api key found, raise an error | ||
def setup_api_key(): | ||
if not load_dotenv(os.path.join(mentat_dir_path, ".env")): | ||
load_dotenv() | ||
|
@@ -24,12 +25,10 @@ def setup_api_key(): | |
openai.api_key = key | ||
openai.Model.list() # Test the API key | ||
except openai.error.AuthenticationError: | ||
cprint( | ||
raise UserError( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We call this function before the try except in app.py; we should move that call into loop() so this error gets properly caught There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch |
||
"No valid OpenAI api key detected.\nEither place your key into a .env" | ||
" file or export it as an environment variable.", | ||
"red", | ||
" file or export it as an environment variable." | ||
) | ||
sys.exit(0) | ||
|
||
|
||
async def call_llm_api(messages: list[dict[str, str]], model) -> Generator: | ||
|
@@ -38,8 +37,8 @@ async def call_llm_api(messages: list[dict[str, str]], model) -> Generator: | |
and "--benchmark" not in sys.argv | ||
and os.getenv("MENTAT_BENCHMARKS_RUNNING") == "false" | ||
): | ||
logging.critical("OpenAI call made in non benchmark test environment!") | ||
sys.exit(1) | ||
logging.critical("OpenAI call attempted in non benchmark test environment!") | ||
raise MentatError("OpenAI call attempted in non benchmark test environment!") | ||
|
||
response = await openai.ChatCompletion.acreate( | ||
model=model, | ||
|
@@ -73,12 +72,10 @@ def check_model_availability(allow_32k: bool) -> bool: | |
if not allow_32k: | ||
# check if user has access to gpt-4 | ||
if "gpt-4-0314" not in available_models: | ||
cprint( | ||
raise UserError( | ||
"Sorry, but your OpenAI API key doesn't have access to gpt-4-0314," | ||
" which is currently required to run Mentat.", | ||
"red", | ||
" which is currently required to run Mentat." | ||
) | ||
raise KeyboardInterrupt | ||
|
||
return allow_32k | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could change the error to be the following paths don't exist instead of cprinting, and maybe get rid of the 'exiting...'? Since that should be standard (either always have it, or always don't have it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out the warning for bad file paths got broken when we switched to globbing so I moved this chunk of code to expand_paths() in app.py.