File size: 3,003 Bytes
a1365f7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import os
import asyncio
import logging
from concurrent.futures import ThreadPoolExecutor
from llama_cpp import Llama
logging.basicConfig(level=logging.INFO, format='%(asctime)s - [LFMController] - %(levelname)s - %(message)s')
logger = logging.getLogger("LFMController")
class LFMController:
def __init__(self, model_path: str = "", n_ctx: int = 4096, n_threads: int = 6, n_gpu_layers: int = -1):
if not model_path:
candidates = [
os.path.expanduser("~/.vitalis/models/Llama-3.2-3B-Instruct-Q4_K_M.gguf"),
os.path.expanduser("~/.vitalis/models/LFM2.5-1.2B-Instruct-Q4_K_M.gguf"),
"Llama-3.2-3B-Instruct-Q4_K_M.gguf",
"LFM2.5-1.2B-Instruct-Q4_K_M.gguf",
]
for c in candidates:
if os.path.exists(c):
model_path = c
break
if not model_path:
model_path = candidates[0]
if not os.path.exists(model_path):
logger.critical(f"Target model weights missing at path: {model_path}")
raise FileNotFoundError(f"Model file target missing: {model_path}")
logger.info(f"Initializing local model instance from {model_path}...")
try:
self.llm = Llama(
model_path=model_path,
n_ctx=n_ctx,
n_threads=n_threads,
n_gpu_layers=n_gpu_layers,
verbose=False
)
logger.info("Model hardware acceleration context successfully initialized.")
except Exception as e:
logger.error(f"Failed to load hardware context for GGUF: {str(e)}")
raise e
self.executor = ThreadPoolExecutor(max_workers=1)
def execute_raw(self, prompt: str, max_tokens: int = 1024, temperature: float = 0.2, top_p: float = 0.95) -> str:
try:
response = self.llm(
prompt=prompt,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
stop=["<|endoftext|>", "###", "Instruction:", "Response:"]
)
output_text = response["choices"][0]["text"].strip()
return output_text
except Exception as e:
logger.error(f"Error encountered during raw execution sequence: {str(e)}")
return f"EXECUTION_ERROR: {str(e)}"
async def generate_async(self, prompt: str, max_tokens: int = 1024, temperature: float = 0.2, top_p: float = 0.95) -> str:
loop = asyncio.get_running_loop()
try:
return await loop.run_in_executor(
self.executor,
self.execute_raw,
prompt, max_tokens, temperature, top_p
)
except Exception as e:
logger.error(f"Async worker thread crashed: {str(e)}")
return f"ASYNC_EXECUTION_ERROR: {str(e)}"
def shutdown(self):
self.executor.shutdown(wait=True)
|