Skip to content

Commit

Permalink
Improve performance, short-circuit earlier when disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
nicovank committed Jan 25, 2024
1 parent d940576 commit aebbe49
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
13 changes: 11 additions & 2 deletions src/cwhy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ def wrapper(args):
textwrap.dedent(
f"""
#! {sys.executable}
from cwhy import cwhy
cwhy.wrapper({vars(args)})
import os
import sys
if "CWHY_DISABLE" in os.environ:
import subprocess
sys.exit(subprocess.run([*{args.command}, *sys.argv[1:]]).returncode)
else:
import argparse
from cwhy import cwhy
args = argparse.Namespace(**{vars(args)})
args.command.extend(sys.argv[1:])
cwhy.main(args)
"""
).strip()
+ "\n"
Expand Down
31 changes: 12 additions & 19 deletions src/cwhy/cwhy.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,18 @@ def main(args):

print(process.stdout, end="")
print(process.stderr, file=sys.stderr, end="")
if "CWHY_DISABLE" not in os.environ:
print("==================================================")
print("CWhy")
print("==================================================")
try:
client = openai.OpenAI(timeout=args.timeout)
except openai.OpenAIError:
print("You need an OpenAI key to use this tool.")
print("You can get a key here: https://platform.openai.com/api-keys")
print("Set the environment variable OPENAI_API_KEY to your key value.")
sys.exit(1)
print(evaluate(client, args, process.stderr))
print("==================================================")
print("==================================================")
print("CWhy")
print("==================================================")
try:
client = openai.OpenAI(timeout=args.timeout)
except openai.OpenAIError:
print("You need an OpenAI key to use this tool.")
print("You can get a key here: https://platform.openai.com/api-keys")
print("Set the environment variable OPENAI_API_KEY to your key value.")
sys.exit(1)
print(evaluate(client, args, process.stderr))
print("==================================================")

sys.exit(process.returncode)

Expand All @@ -165,9 +164,3 @@ def evaluate_text_prompt(client, args, prompt, wrap=True, **kwargs):
text += f"(Total cost: approximately ${cost:.2f} USD.)"

return text


def wrapper(args):
args = argparse.Namespace(**args)
args.command.extend(sys.argv[1:])
main(args)

0 comments on commit aebbe49

Please sign in to comment.