Skip to content

Commit 8617cfa

Browse files
committed
✨ add chat --test flag for simple model healthcheck.
1 parent 4c6874e commit 8617cfa

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

merle/chat.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,75 @@ def get_conversation_summary(self) -> dict[str, Any]:
410410
}
411411

412412

413+
def run_health_check(
414+
base_url: str,
415+
auth_token: str,
416+
model: str,
417+
debug: bool = False,
418+
system_prompt: str | None = None,
419+
context_window_size: int | None = None,
420+
) -> tuple[bool, str]:
421+
"""
422+
Run a health check by sending 'hello' and waiting for a response.
423+
424+
Args:
425+
base_url: Base URL of the deployed API
426+
auth_token: Authentication token
427+
model: Model name to use
428+
debug: Show debug and info log messages if True
429+
system_prompt: Optional system prompt for conversation context
430+
context_window_size: Optional context window size (defaults to 2048 if not provided)
431+
432+
Returns:
433+
Tuple of (success: bool, message: str)
434+
"""
435+
# Configure logging level based on debug flag
436+
if not debug:
437+
logging.getLogger("merle").setLevel(logging.WARNING)
438+
logging.getLogger("httpx").setLevel(logging.WARNING)
439+
else:
440+
logging.getLogger("merle").setLevel(logging.INFO)
441+
logging.getLogger("httpx").setLevel(logging.INFO)
442+
443+
if context_window_size is None:
444+
context_window_size = 2048
445+
446+
client = ChatClient(
447+
base_url, auth_token, model, system_prompt=system_prompt, context_window_size=context_window_size
448+
)
449+
450+
print(f"Testing connection to {model} at {base_url}...")
451+
print("Sending: hello")
452+
print("-" * 40)
453+
454+
try:
455+
response = client.chat("hello", prompt="Response: ")
456+
print() # Newline after response
457+
print("-" * 40)
458+
459+
if response:
460+
print("Status: OK")
461+
return True, response
462+
except httpx.HTTPStatusError as e:
463+
print(f"\nStatus: FAILED (HTTP {e.response.status_code})")
464+
return False, f"HTTP error {e.response.status_code}"
465+
except httpx.ConnectError as e:
466+
print("\nStatus: FAILED (connection error)")
467+
logger.debug(f"Connection error details: {e}")
468+
return False, f"Connection error: {e}"
469+
except httpx.TimeoutException as e:
470+
print("\nStatus: FAILED (timeout)")
471+
logger.debug(f"Timeout details: {e}")
472+
return False, f"Timeout: {e}"
473+
except Exception as e:
474+
print(f"\nStatus: FAILED ({type(e).__name__})")
475+
logger.exception(f"Health check error: {e}")
476+
return False, str(e)
477+
else:
478+
print("Status: FAILED (empty response)")
479+
return False, "Empty response received"
480+
481+
413482
def run_interactive_chat( # noqa: PLR0915, PLR0912, C901
414483
base_url: str,
415484
auth_token: str,

merle/cli.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pathlib import Path
1010

1111
from merle import __version__
12-
from merle.chat import run_interactive_chat
12+
from merle.chat import run_health_check, run_interactive_chat
1313
from merle.functions import (
1414
get_default_project_name,
1515
get_project_cache_dir,
@@ -759,6 +759,18 @@ def handle_chat(args: argparse.Namespace) -> int:
759759
if context_window_size:
760760
logger.info(f"Context window size: {context_window_size} tokens")
761761

762+
# Check if test mode is requested
763+
if getattr(args, "test", False):
764+
success, _ = run_health_check(
765+
base_url=deployment_url,
766+
auth_token=auth_token,
767+
model=model_name,
768+
debug=args.debug,
769+
system_prompt=system_prompt,
770+
context_window_size=context_window_size,
771+
)
772+
return 0 if success else 1
773+
762774
# Start interactive chat
763775
run_interactive_chat(
764776
base_url=deployment_url,
@@ -985,6 +997,11 @@ def main() -> int:
985997
action="store_true",
986998
help="Show debug and info log messages during chat",
987999
)
1000+
chat_parser.add_argument(
1001+
"--test",
1002+
action="store_true",
1003+
help="Send a test 'hello' message and exit (health check mode)",
1004+
)
9881005
chat_parser.add_argument(
9891006
"--project",
9901007
help="Project name to prefix cache directory (allows multiple projects to share the same base cache)",

0 commit comments

Comments
 (0)