| 8 | from open_codex.interfaces.llm_agent import LLMAgent |
| 9 | |
| 10 | class AgentPhi4Mini(LLMAgent): |
| 11 | |
| 12 | def download_model(self, model_filename: str, |
| 13 | repo_id: str, |
| 14 | local_dir: str) -> str: |
| 15 | print( |
| 16 | "\n🤖 Thank you for using Open Codex!\n" |
| 17 | "📦 For the first run, we need to download the model from Hugging Face.\n" |
| 18 | "⏬ This only happens once – it’ll be cached locally for future use.\n" |
| 19 | "🔄 Sit tight, the download will begin now...\n" |
| 20 | ) |
| 21 | print("\n⏬ Downloading model phi4-mini ...") |
| 22 | |
| 23 | start = time.time() |
| 24 | model_path:str = hf_hub_download( |
| 25 | repo_id=repo_id, |
| 26 | filename=model_filename, |
| 27 | local_dir=local_dir, |
| 28 | ) |
| 29 | end = time.time() |
| 30 | print(f"✅ Model downloaded in {end - start:.2f}s\n") |
| 31 | return model_path |
| 32 | |
| 33 | def __init__(self, system_prompt: str): |
| 34 | model_filename = "Phi-4-mini-instruct-Q3_K_L.gguf" |
| 35 | repo_id = "lmstudio-community/Phi-4-mini-instruct-GGUF" |
| 36 | local_dir = os.path.expanduser("~/.cache/open-codex") |
| 37 | model_path = os.path.join(local_dir, model_filename) |
| 38 | |
| 39 | # check if the model is already downloaded |
| 40 | if not os.path.exists(model_path): |
| 41 | # download the model |
| 42 | model_path = self.download_model(model_filename, repo_id, local_dir) |
| 43 | else: |
| 44 | print(f"We are locking and loading the model for you...\n") |
| 45 | |
| 46 | # suppress the stderr output from llama_cpp |
| 47 | # this is a workaround for the llama_cpp library |
| 48 | # which prints a lot of warnings and errors to stderr |
| 49 | # when loading the model |
| 50 | # this is a temporary solution until the library is fixed |
| 51 | with AgentPhi4Mini.suppress_native_stderr(): |
| 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) |
| 56 | |
| 57 | self.system_prompt = system_prompt |
| 58 | |
| 59 | |
| 60 | def one_shot_mode(self, user_input: str) -> str: |
| 61 | chat_history = [{"role": "system", "content": self.system_prompt}] |
| 62 | chat_history.append({"role": "user", "content": user_input}) |
| 63 | full_prompt = self.format_chat(chat_history) |
| 64 | with AgentPhi4Mini.suppress_native_stderr(): |
| 65 | output_raw = self.llm(prompt=full_prompt, max_tokens=100, temperature=0.2, stream=False) |
| 66 | |
| 67 | # unfortuntely llama_cpp has a union type for the output |