Skip to content

Commit

Permalink
Remove dprint
Browse files Browse the repository at this point in the history
  • Loading branch information
nicovank committed Oct 3, 2024
1 parent 23acaea commit ff01baf
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 46 deletions.
5 changes: 0 additions & 5 deletions src/cwhy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ def main() -> None:
action="store_true",
help="when enabled, only print prompt and exit (for debugging purposes)",
)
parser.add_argument(
"--debug",
action="store_true",
help=argparse.SUPPRESS,
)
parser.add_argument(
"--wrapper",
action="store_true",
Expand Down
5 changes: 2 additions & 3 deletions src/cwhy/conversation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import json
import textwrap

import openai # type: ignore
import openai

from . import utils
from .diff_functions import DiffFunctions
from ..print_debug import dprint


def diff_converse(client: openai.OpenAI, args, diagnostic):
Expand Down Expand Up @@ -87,4 +86,4 @@ def diff_converse(client: openai.OpenAI, args, diagnostic):
}
)

dprint()
print()
11 changes: 5 additions & 6 deletions src/cwhy/conversation/diff_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from . import utils
from .explain_functions import ExplainFunctions
from ..print_debug import dprint


class DiffFunctions:
Expand All @@ -29,15 +28,15 @@ def dispatch(self, function_call) -> Optional[str]:
arguments = json.loads(function_call.arguments)
try:
if function_call.name == "apply_modification":
dprint("Calling: apply_modification(...)")
print("Calling: apply_modification(...)")
return self.apply_modification(
arguments["filename"],
arguments["start-line-number"],
arguments["number-lines-remove"],
arguments["replacement"],
)
elif function_call.name == "try_compiling":
dprint("Calling: try_compiling()")
print("Calling: try_compiling()")
return self.try_compiling()
else:
return self.explain_functions.dispatch(function_call)
Expand Down Expand Up @@ -107,9 +106,9 @@ def apply_modification(
whitespace = replaced_line[:n]
replacement_lines[0] = whitespace + replacement_lines[0]

dprint("CWhy wants to do the following modification:")
print("CWhy wants to do the following modification:")
for line in difflib.unified_diff(replaced_lines, replacement_lines):
dprint(line)
print(line)
if not input("Is this modification okay? (y/n) ") == "y":
return "The user declined this modification, it is probably wrong."

Expand All @@ -133,7 +132,7 @@ def try_compiling(self) -> Optional[str]:
)

if process.returncode == 0:
dprint("Compilation successful!")
print("Compilation successful!")
sys.exit(0)

return utils.get_truncated_error_message(self.args, process.stderr)
10 changes: 4 additions & 6 deletions src/cwhy/conversation/explain_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import llm_utils

from ..print_debug import dprint


class ExplainFunctions:
def __init__(self, args: argparse.Namespace):
Expand All @@ -24,7 +22,7 @@ def as_tools(self):

def dispatch(self, function_call) -> Optional[str]:
arguments = json.loads(function_call.arguments)
dprint(
print(
f"Calling: {function_call.name}({', '.join([f'{k}={v}' for k, v in arguments.items()])})"
)
try:
Expand All @@ -37,7 +35,7 @@ def dispatch(self, function_call) -> Optional[str]:
elif function_call.name == "list_directory":
return self.list_directory(arguments["path"])
except Exception as e:
dprint(e)
print(e)
return None

def get_compile_or_run_command(self) -> str:
Expand All @@ -48,7 +46,7 @@ def get_compile_or_run_command(self) -> str:
}
"""
result = " ".join(self.args.command)
dprint(result)
print(result)
return result

def get_code_surrounding(self, filename: str, lineno: int) -> str:
Expand Down Expand Up @@ -77,7 +75,7 @@ def get_code_surrounding(self, filename: str, lineno: int) -> str:
"""
(lines, first) = llm_utils.read_lines(filename, lineno - 7, lineno + 3)
result = llm_utils.number_group_of_lines(lines, first)
dprint(result)
print(result)
return result

def list_directory(self, path: str) -> str:
Expand Down
42 changes: 19 additions & 23 deletions src/cwhy/cwhy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import openai

from . import conversation, prompts
from .print_debug import dprint, enable_debug_printing


def complete(
Expand All @@ -22,17 +21,17 @@ def complete(
)
return completion
except openai.NotFoundError as e:
dprint(f"'{args.llm}' either does not exist or you do not have access to it.")
print(f"'{args.llm}' either does not exist or you do not have access to it.")
raise e
except openai.BadRequestError as e:
dprint("Something is wrong with your prompt.")
print("Something is wrong with your prompt.")
raise e
except openai.RateLimitError as e:
dprint("You have exceeded a rate limit or have no remaining funds.")
print("You have exceeded a rate limit or have no remaining funds.")
raise e
except openai.APITimeoutError as e:
dprint("The API timed out.")
dprint("You can increase the timeout with the --timeout option.")
print("The API timed out.")
print("You can increase the timeout with the --timeout option.")
raise e


Expand Down Expand Up @@ -109,32 +108,29 @@ def main(args: argparse.Namespace) -> None:
if process.returncode == 0:
return

if args.debug:
enable_debug_printing()

if args.show_prompt:
dprint("===================== Prompt =====================")
print("===================== Prompt =====================")
if args.subcommand == "explain":
dprint(prompts.explain_prompt(args, process.stderr))
print(prompts.explain_prompt(args, process.stderr))
elif args.subcommand == "diff":
dprint(prompts.diff_prompt(args, process.stderr))
dprint("==================================================")
print(prompts.diff_prompt(args, process.stderr))
print("==================================================")
sys.exit(0)

dprint(process.stdout)
dprint(process.stderr, file=sys.stderr)
dprint("==================================================")
dprint("CWhy")
dprint("==================================================")
print(process.stdout)
print(process.stderr, file=sys.stderr)
print("==================================================")
print("CWhy")
print("==================================================")
try:
client = openai.OpenAI()
result = evaluate(
client, args, process.stderr if process.stderr else process.stdout
)
dprint(result)
print(result)
except openai.OpenAIError as e:
dprint(str(e).strip())
dprint("==================================================")
print(str(e).strip())
print("==================================================")

sys.exit(process.returncode)

Expand All @@ -149,8 +145,8 @@ def evaluate_text_prompt(
completion = complete(client, args, prompt, **kwargs)

msg = f"Analysis from {args.llm}:"
dprint(msg)
dprint("-" * len(msg))
print(msg)
print("-" * len(msg))
text: str = completion.choices[0].message.content

if wrap:
Expand Down
4 changes: 1 addition & 3 deletions src/cwhy/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import llm_utils

from .print_debug import dprint


# Define error patterns with associated information. The numbers
# correspond to the groups matching file name and line number.
Expand Down Expand Up @@ -74,7 +72,7 @@ def __init__(self, args: argparse.Namespace, diagnostic: str):
file_name, line_number - 7, line_number + 3
)
except FileNotFoundError:
dprint(
print(
f"Cwhy warning: file not found: {file_name.lstrip()}",
file=sys.stderr,
)
Expand Down

0 comments on commit ff01baf

Please sign in to comment.