Skip to content

Commit d066f5a

Browse files
committed
added lazy imports and minor clean ups
1 parent 2898378 commit d066f5a

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed

src/open_codex/agents/phi_4_mini.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import contextlib
2+
import os
13
import time
24
from typing import List, cast
5+
6+
from huggingface_hub import hf_hub_download # type: ignore
37
from llama_cpp import CreateCompletionResponse, Llama
48
from open_codex.interfaces.llm_agent import LLMAgent
5-
import contextlib
6-
import os
7-
from huggingface_hub import hf_hub_download # type: ignore
89

910
class AgentPhi4Mini(LLMAgent):
11+
1012
def download_model(self, model_filename: str,
1113
repo_id: str,
1214
local_dir: str) -> str:
@@ -47,7 +49,10 @@ def __init__(self, system_prompt: str):
4749
# when loading the model
4850
# this is a temporary solution until the library is fixed
4951
with AgentPhi4Mini.suppress_native_stderr():
50-
self.llm: Llama = Llama(model_path=model_path) # type: ignore
52+
lib_dir = os.path.join(os.path.dirname(__file__), "llama_cpp", "lib")
53+
self.llm: Llama = Llama(
54+
lib_path=os.path.join(lib_dir, "libllama.dylib"),
55+
model_path=model_path)
5156

5257
self.system_prompt = system_prompt
5358

src/open_codex/main.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import sys
22
import argparse
33
import subprocess
4-
import pyperclip
5-
6-
from open_codex.agent_builder import AgentBuilder
7-
from open_codex.interfaces.llm_agent import LLMAgent
8-
94

105
GREEN = "\033[92m"
116
RED = "\033[91m"
@@ -14,18 +9,15 @@
149

1510
# Capture single keypress (terminal) from the user
1611
# and returns it as a string. It works on both Windows and Unix systems.
12+
1713
# Windows
1814
if sys.platform == "win32":
1915
import msvcrt
20-
2116
def get_keypress():
2217
return msvcrt.getch().decode("utf-8")
23-
24-
# Unix (Linux/macOS)
18+
# Unix
2519
else:
26-
import termios
27-
import tty
28-
20+
import termios, tty
2921
def get_keypress():
3022
fd = sys.stdin.fileno()
3123
old_settings = termios.tcgetattr(fd)
@@ -37,61 +29,60 @@ def get_keypress():
3729
return key
3830

3931
def print_response(command: str):
40-
print(f"{BLUE}Command found:")
41-
print(f"{BLUE}=====================")
42-
print(f"{GREEN}{command}{RESET}") # Print the command in green
32+
print(f"{BLUE}Command found:\n=====================")
33+
print(f"{GREEN}{command}{RESET}")
4334
print(f"{BLUE}====================={RESET}")
44-
4535
print(f"{BLUE}What do you want to do with this command?{RESET}")
4636
print(f"{BLUE}[c] Copy [e] Execute [a] Abort{RESET}")
4737
print(f"{BLUE}Press key: ", end="", flush=True)
38+
4839
choice = get_keypress().lower()
49-
print(f"{RESET}") # Clear the line after the prompt
40+
print(f"{RESET}")
5041

5142
if choice == "e":
5243
print(f"{BLUE}Executing command: {command}{RESET}")
5344
result = subprocess.run(command, shell=True, capture_output=True, text=True)
5445
print(f"{GREEN}Command output: {result.stdout}{RESET}")
5546
if result.stderr:
5647
print(f"{RED}Error: {result.stderr}{RESET}")
57-
5848
elif choice == "c":
49+
import pyperclip # ⏱ lazy import
5950
pyperclip.copy(command)
60-
print(f"{GREEN}Command copied to clipboard! Paste it manually in your terminal.{RESET}")
61-
51+
print(f"{GREEN}Command copied to clipboard!{RESET}")
6252
elif choice == "a":
6353
print(f"{BLUE}Aborted.{RESET}")
6454
else:
6555
print(f"{RED}Unknown choice. Nothing happened.{RESET}")
6656

67-
def one_shot_mode(agent: LLMAgent, prompt: str):
57+
def one_shot_mode(prompt: str):
58+
from open_codex.agent_builder import AgentBuilder
59+
print(f"{BLUE}Using model: phi-4-mini-instruct{RESET}")
6860
try:
61+
agent = AgentBuilder.get_agent()
6962
response = agent.one_shot_mode(prompt)
7063
print_response(response)
7164
except Exception as e:
7265
print(f"{RED}Error: {e}{RESET}")
7366

7467
def print_help_message():
75-
"""Print help message with usage examples."""
76-
print(f"{BLUE}Open Codex - Natural Language to CLI commands{RESET}")
7768
print(f"{BLUE}Usage examples:{RESET}")
7869
print(f"{GREEN}open-codex \"list all files in current directory\"")
7970
print(f"{GREEN}open-codex \"find all python files modified in the last week\"")
8071
print(f"{GREEN}open-codex \"create a tarball of the src directory\"")
8172
print()
8273

8374
def main():
84-
parser = argparse.ArgumentParser(description="Open Codex - Natural Language to CLI commands")
85-
parser.add_argument("prompt", nargs="*", help="Optional prompt for one-shot mode")
75+
parser = argparse.ArgumentParser()
76+
parser.add_argument("prompt", nargs="*", help="Natural language prompt")
8677
args = parser.parse_args()
8778
prompt = " ".join(args.prompt).strip()
79+
8880
if not prompt or prompt == "--help":
8981
print_help_message()
90-
sys.exit(1)
91-
92-
agent = AgentBuilder.get_agent()
93-
print(f"{BLUE}Using model: phi-4-mini-instruct{RESET}")
94-
one_shot_mode(agent, prompt)
82+
sys.exit(0)
83+
84+
print(f"{BLUE}Prompt: {prompt}{RESET}", flush=True)
85+
one_shot_mode(prompt)
9586

9687
if __name__ == "__main__":
9788
main()

0 commit comments

Comments
 (0)