Spaces:
Build error
Build error
File size: 23,740 Bytes
175746c eb1c19a 175746c eb1c19a 175746c 7de545c 175746c e4a41fa 175746c d5ea931 eb1c19a 175746c eb1c19a 175746c e4a41fa eb1c19a 175746c eb1c19a 175746c eb1c19a 7de545c eb1c19a 175746c e4a41fa 175746c e4a41fa 175746c d5ea931 eb1c19a 175746c eb1c19a 175746c bf779b5 175746c | 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 | #!/usr/bin/env python3
"""Headroom CLI - The Context Optimization Layer for LLM Applications.
Usage:
headroom proxy [OPTIONS] Start the optimization proxy server
headroom memory-eval [OPTIONS] Run LoCoMo memory evaluation
headroom --version Show version
headroom --help Show this help message
Examples:
# Start proxy on default port (8787)
headroom proxy
# Start proxy on custom port
headroom proxy --port 8080
# Start with optimization disabled (passthrough mode)
headroom proxy --no-optimize
# Use with Claude Code
ANTHROPIC_BASE_URL=http://localhost:8787 claude
# Run memory evaluation on 3 conversations
headroom memory-eval -n 3
# Run memory evaluation with LLM answering and judging
headroom memory-eval --answer-model gpt-4o --llm-judge
"""
from __future__ import annotations
import argparse
import sys
from collections.abc import Callable
def get_version() -> str:
"""Get the current version."""
try:
from headroom import __version__
return __version__
except ImportError:
return "unknown"
def cmd_proxy(args: argparse.Namespace) -> int:
"""Start the proxy server."""
try:
from headroom.proxy.server import ProxyConfig, run_server
except ImportError as e:
print("Error: Proxy dependencies not installed. Run: pip install headroom[proxy]")
print(f"Details: {e}")
return 1
config = ProxyConfig(
host=args.host,
port=args.port,
optimize=not args.no_optimize,
cache_enabled=not args.no_cache,
rate_limit_enabled=not args.no_rate_limit,
log_file=args.log_file,
budget_limit_usd=args.budget,
# LLMLingua: ON by default (use --no-llmlingua to disable)
llmlingua_enabled=not args.no_llmlingua,
llmlingua_device=args.llmlingua_device,
llmlingua_target_rate=args.llmlingua_rate,
# Code-aware: ON by default (use --no-code-aware to disable)
code_aware_enabled=not args.no_code_aware,
# Memory System
memory_enabled=args.memory,
memory_backend=args.memory_backend,
memory_db_path=args.memory_db_path,
memory_inject_tools=not args.no_memory_tools,
memory_inject_context=not args.no_memory_context,
memory_top_k=args.memory_top_k,
)
memory_status = "DISABLED"
if config.memory_enabled:
memory_status = f"ENABLED ({config.memory_backend})"
print(f"""
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β HEADROOM PROXY β
β The Context Optimization Layer for LLM Applications β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Starting proxy server...
URL: http://{config.host}:{config.port}
Optimization: {"ENABLED" if config.optimize else "DISABLED"}
Caching: {"ENABLED" if config.cache_enabled else "DISABLED"}
Rate Limit: {"ENABLED" if config.rate_limit_enabled else "DISABLED"}
Memory: {memory_status}
Usage with Claude Code:
ANTHROPIC_BASE_URL=http://{config.host}:{config.port} claude
Usage with OpenAI-compatible clients:
OPENAI_BASE_URL=http://{config.host}:{config.port}/v1 your-app
{
""
if not config.memory_enabled
else '''
Memory Requirements:
Set x-headroom-user-id header to identify the user for memory scoping.
'''
}
Endpoints:
GET /health Health check
GET /stats Detailed statistics
GET /metrics Prometheus metrics
POST /v1/messages Anthropic API
POST /v1/chat/completions OpenAI API
Press Ctrl+C to stop.
""")
try:
run_server(config)
except KeyboardInterrupt:
print("\nShutting down...")
return 0
return 0
def cmd_version(args: argparse.Namespace) -> int:
"""Print version information."""
print(f"headroom {get_version()}")
return 0
def cmd_memory_eval(args: argparse.Namespace) -> int:
"""Run LoCoMo memory evaluation."""
# Suppress noisy pydantic warnings from litellm
import warnings
warnings.filterwarnings("ignore", message=".*Pydantic serializer warnings.*")
warnings.filterwarnings("ignore", category=UserWarning, module="pydantic")
try:
from headroom.evals.memory import (
LoCoMoEvaluator,
MemoryEvalConfig,
create_anthropic_judge,
create_litellm_judge,
create_openai_judge,
simple_judge,
)
from headroom.memory import MemoryConfig
except ImportError as e:
print("Error: Memory eval dependencies not installed.")
print("Run: pip install headroom[memory,evals]")
print(f"Details: {e}")
return 1
import asyncio
# Build configuration
categories = None
if args.categories:
categories = [int(c) for c in args.categories.split(",")]
memory_config = MemoryConfig()
eval_config = MemoryEvalConfig(
n_conversations=args.n_conversations,
categories=categories,
skip_adversarial=not args.include_adversarial,
top_k_memories=args.top_k,
llm_judge_enabled=args.llm_judge,
llm_judge_model=args.judge_model,
memory_config=memory_config,
f1_threshold=args.f1_threshold,
extract_memories=not args.no_extract,
extraction_model=args.extraction_model,
pass_all_memories=args.pass_all,
parallel_workers=args.parallel,
debug=args.debug,
)
# Create answer function based on provider
answer_fn = None
if args.answer_model:
try:
import litellm
def answer_fn(question: str, memories: list[str]) -> str:
if not memories:
return "I don't have information about that."
# Format memories - use all if pass_all, else top 10
context = "\n".join(f"- {m}" for m in memories)
prompt = f"""You are answering questions about a conversation between two people based on extracted memories/facts.
## Memories from the conversation:
{context}
## Question: {question}
## Instructions:
1. Find the specific fact(s) in the memories that answer this question
2. Answer with JUST the key information requested - be concise
3. For "when" questions: give the specific date if mentioned (e.g., "7 May 2023", "2022")
4. For "what" questions: give the specific thing/action
5. For "who" questions: give the name
6. If the exact answer is in the memories, use those exact words/dates
7. If you cannot find the answer, say "Information not found"
## Answer (be concise - just the facts):"""
response = litellm.completion(
model=args.answer_model,
messages=[{"role": "user", "content": prompt}],
temperature=0.0,
max_tokens=150,
)
return response.choices[0].message.content or ""
except ImportError:
print("Error: litellm required for --answer-model. Run: pip install litellm")
return 1
# Create LLM judge if enabled
llm_judge_fn: Callable[[str, str, str], tuple[float, str]] | None = None
if args.llm_judge:
# Use answer model for judge if not explicitly set
judge_model = args.judge_model
if args.answer_model and args.judge_model == "gpt-4o":
judge_model = args.answer_model # Match the answer model
if args.judge_provider == "simple":
llm_judge_fn = simple_judge
elif args.judge_provider == "openai":
llm_judge_fn = create_openai_judge(model=judge_model)
elif args.judge_provider == "anthropic":
llm_judge_fn = create_anthropic_judge(model=judge_model)
else:
llm_judge_fn = create_litellm_judge(model=judge_model)
# Determine judge info for display
judge_info = "DISABLED"
if args.llm_judge:
if args.judge_provider == "simple":
judge_info = "ENABLED (rule-based F1)"
else:
jm = args.judge_model
if args.answer_model and args.judge_model == "gpt-4o":
jm = args.answer_model
judge_info = f"ENABLED ({args.judge_provider}: {jm})"
extract_info = (
f"ENABLED ({args.extraction_model})" if not args.no_extract else "DISABLED (raw dialogue)"
)
retrieval_info = "ALL memories (Path A)" if args.pass_all else f"Top-{args.top_k} retrieval"
print(f"""
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β HEADROOM MEMORY EVALUATION β
β LoCoMo Benchmark β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Configuration:
Conversations: {args.n_conversations or "all"}
Categories: {categories or "[1,2,3,4]"}
Retrieval: {retrieval_info}
Memory Extract: {extract_info}
Answer Model: {args.answer_model or "default (retrieval)"}
LLM Judge: {judge_info}
Parallelism: {args.parallel} workers
Debug: {"ENABLED" if args.debug else "DISABLED"}
Running evaluation...
""")
# Run evaluation
evaluator = LoCoMoEvaluator(
answer_fn=answer_fn,
llm_judge_fn=llm_judge_fn,
config=eval_config,
)
try:
result = asyncio.run(evaluator.run())
except KeyboardInterrupt:
print("\nEvaluation interrupted.")
return 1
# Print results
print(result.summary())
# Save results if output path specified
if args.output:
result.save(args.output)
print(f"\nResults saved to: {args.output}")
return 0
def cmd_memory_eval_v2(args: argparse.Namespace) -> int:
"""Run LoCoMo V2 memory evaluation (LLM-controlled tools)."""
# Suppress noisy pydantic warnings from litellm
import warnings
warnings.filterwarnings("ignore", message=".*Pydantic serializer warnings.*")
warnings.filterwarnings("ignore", category=UserWarning, module="pydantic")
try:
from headroom.evals.memory import (
LoCoMoEvaluatorV2,
MemoryEvalConfigV2,
)
except ImportError as e:
print("Error: Memory eval V2 dependencies not installed.")
print("Run: pip install headroom[memory,evals]")
print(f"Details: {e}")
return 1
import asyncio
# Build configuration
categories = None
if args.categories:
categories = [int(c) for c in args.categories.split(",")]
eval_config = MemoryEvalConfigV2(
n_conversations=args.n_conversations,
categories=categories,
skip_adversarial=not args.include_adversarial,
llm_judge_enabled=args.llm_judge,
llm_judge_model=args.judge_model,
f1_threshold=args.f1_threshold,
parallel_workers=args.parallel,
debug=args.debug,
save_model=args.save_model,
answer_model=args.answer_model,
max_search_results=args.max_results,
include_graph_expansion=not args.no_graph,
)
print(f"""
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β HEADROOM MEMORY EVALUATION V2 β
β LLM-Controlled Memory Architecture β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Configuration:
Conversations: {args.n_conversations or "all"}
Categories: {categories or "[1,2,3,4]"}
Save Model: {args.save_model}
Answer Model: {args.answer_model}
Max Results: {args.max_results}
Graph Expansion: {"DISABLED" if args.no_graph else "ENABLED"}
LLM Judge: {"ENABLED" if args.llm_judge else "DISABLED"}
Parallelism: {args.parallel} workers
Debug: {"ENABLED" if args.debug else "DISABLED"}
Key Differences from V1:
- LLM decides WHAT to save (memory_save tool)
- LLM decides HOW to search (memory_search tool)
- Graph expansion enables multi-hop reasoning
Running evaluation...
""")
# Run evaluation
evaluator = LoCoMoEvaluatorV2(
answer_model=args.answer_model,
config=eval_config,
)
try:
result = asyncio.run(evaluator.run())
except KeyboardInterrupt:
print("\nEvaluation interrupted.")
return 1
# Print results
print(result.summary())
# Save results if output path specified
if args.output:
result.save(args.output)
print(f"\nResults saved to: {args.output}")
return 0
def main(argv: list[str] | None = None) -> int:
"""Main CLI entry point."""
parser = argparse.ArgumentParser(
prog="headroom",
description="The Context Optimization Layer for LLM Applications",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
headroom proxy Start proxy on port 8787
headroom proxy --port 8080 Start proxy on port 8080
headroom proxy --no-optimize Passthrough mode (no optimization)
Environment Variables:
ANTHROPIC_API_KEY Your Anthropic API key (for proxying)
OPENAI_API_KEY Your OpenAI API key (for proxying)
Documentation: https://github.com/headroom-sdk/headroom
""",
)
parser.add_argument(
"--version",
"-V",
action="store_true",
help="Show version and exit",
)
subparsers = parser.add_subparsers(dest="command", help="Commands")
# Proxy command
proxy_parser = subparsers.add_parser(
"proxy",
help="Start the optimization proxy server",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
proxy_parser.add_argument(
"--host",
default="127.0.0.1",
help="Host to bind to (default: 127.0.0.1)",
)
proxy_parser.add_argument(
"--port",
"-p",
type=int,
default=8787,
help="Port to bind to (default: 8787)",
)
proxy_parser.add_argument(
"--no-optimize",
action="store_true",
help="Disable optimization (passthrough mode)",
)
proxy_parser.add_argument(
"--no-cache",
action="store_true",
help="Disable semantic caching",
)
proxy_parser.add_argument(
"--no-rate-limit",
action="store_true",
help="Disable rate limiting",
)
proxy_parser.add_argument(
"--log-file",
help="Path to JSONL log file",
)
proxy_parser.add_argument(
"--budget",
type=float,
help="Daily budget limit in USD",
)
# LLMLingua ML-based compression (ON by default if installed)
proxy_parser.add_argument(
"--no-llmlingua",
action="store_true",
help="Disable LLMLingua-2 ML-based compression",
)
proxy_parser.add_argument(
"--llmlingua-device",
choices=["auto", "cuda", "cpu", "mps"],
default="auto",
help="Device for LLMLingua model (default: auto)",
)
proxy_parser.add_argument(
"--llmlingua-rate",
type=float,
default=0.3,
help="LLMLingua compression rate 0.0-1.0 (default: 0.3 = keep 30%%)",
)
# Code-aware compression (ON by default if installed)
proxy_parser.add_argument(
"--no-code-aware",
action="store_true",
help="Disable AST-based code compression",
)
# Memory System
proxy_parser.add_argument(
"--memory",
action="store_true",
help="Enable persistent user memory (requires x-headroom-user-id header)",
)
proxy_parser.add_argument(
"--memory-backend",
choices=["local", "qdrant-neo4j"],
default="local",
help="Memory storage backend: local (SQLite+HNSW) or qdrant-neo4j (default: local)",
)
proxy_parser.add_argument(
"--memory-db-path",
default="headroom_memory.db",
help="Path to memory database file for local backend (default: headroom_memory.db)",
)
proxy_parser.add_argument(
"--no-memory-tools",
action="store_true",
help="Disable automatic memory tool injection",
)
proxy_parser.add_argument(
"--no-memory-context",
action="store_true",
help="Disable automatic memory context injection",
)
proxy_parser.add_argument(
"--memory-top-k",
type=int,
default=10,
help="Number of memories to inject as context (default: 10)",
)
proxy_parser.set_defaults(func=cmd_proxy)
# Memory eval command
eval_parser = subparsers.add_parser(
"memory-eval",
help="Run LoCoMo memory evaluation benchmark",
formatter_class=argparse.RawDescriptionHelpFormatter,
description="""
Run the LoCoMo memory benchmark to evaluate the Headroom memory system.
LoCoMo (Long-term Conversational Memory) tests memory across:
- Single-hop questions (simple fact recall)
- Temporal questions (time-based)
- Multi-hop questions (reasoning across memories)
- Open-domain questions (interpretation required)
Example:
headroom memory-eval --n-conversations 3
headroom memory-eval --answer-model gpt-4o --llm-judge
""",
)
eval_parser.add_argument(
"--n-conversations",
"-n",
type=int,
help="Number of conversations to evaluate (default: all 10)",
)
eval_parser.add_argument(
"--categories",
help="Comma-separated list of categories 1-5 (default: 1,2,3,4)",
)
eval_parser.add_argument(
"--include-adversarial",
action="store_true",
help="Include category 5 (unanswerable questions)",
)
eval_parser.add_argument(
"--top-k",
type=int,
default=10,
help="Number of memories to retrieve per question (default: 10)",
)
eval_parser.add_argument(
"--f1-threshold",
type=float,
default=0.5,
help="F1 score threshold for 'correct' (default: 0.5)",
)
eval_parser.add_argument(
"--answer-model",
help="LLM model for generating answers (e.g., gpt-4o, claude-sonnet-4-20250514)",
)
eval_parser.add_argument(
"--llm-judge",
action="store_true",
help="Use LLM-as-judge scoring",
)
eval_parser.add_argument(
"--judge-provider",
choices=["openai", "anthropic", "litellm", "simple"],
default="litellm",
help="LLM judge provider (default: litellm - uses same model as answer-model)",
)
eval_parser.add_argument(
"--judge-model",
default="gpt-4o",
help="Model for LLM judge (default: gpt-4o)",
)
eval_parser.add_argument(
"--output",
"-o",
help="Path to save JSON results",
)
eval_parser.add_argument(
"--no-extract",
action="store_true",
help="Disable LLM memory extraction (store raw dialogue instead)",
)
eval_parser.add_argument(
"--extraction-model",
default="gpt-4o-mini",
help="Model for memory extraction (default: gpt-4o-mini)",
)
eval_parser.add_argument(
"--pass-all",
action="store_true",
help="Pass ALL memories to LLM (Path A: no retrieval bottleneck)",
)
eval_parser.add_argument(
"--parallel",
type=int,
default=10,
help="Number of parallel workers for LLM calls (default: 10)",
)
eval_parser.add_argument(
"--debug",
action="store_true",
help="Enable debug logging (saved to results JSON)",
)
eval_parser.set_defaults(func=cmd_memory_eval)
# Memory eval V2 command (LLM-controlled tools)
eval_v2_parser = subparsers.add_parser(
"memory-eval-v2",
help="Run LoCoMo V2 evaluation with LLM-controlled memory tools",
formatter_class=argparse.RawDescriptionHelpFormatter,
description="""
Run the LoCoMo V2 memory benchmark with LLM-controlled memory architecture.
This evaluator tests the new architecture where:
- LLM decides what to save (memory_save tool)
- LLM decides when to search (memory_search tool)
- Graph relationships enable multi-hop reasoning
Example:
headroom memory-eval-v2 --n-conversations 3
headroom memory-eval-v2 --answer-model gpt-4o --save-model gpt-4o-mini
""",
)
eval_v2_parser.add_argument(
"--n-conversations",
"-n",
type=int,
help="Number of conversations to evaluate (default: all 10)",
)
eval_v2_parser.add_argument(
"--categories",
help="Comma-separated list of categories 1-5 (default: 1,2,3,4)",
)
eval_v2_parser.add_argument(
"--include-adversarial",
action="store_true",
help="Include category 5 (unanswerable questions)",
)
eval_v2_parser.add_argument(
"--f1-threshold",
type=float,
default=0.5,
help="F1 score threshold for 'correct' (default: 0.5)",
)
eval_v2_parser.add_argument(
"--save-model",
default="gpt-4o-mini",
help="LLM model for deciding what to save (default: gpt-4o-mini)",
)
eval_v2_parser.add_argument(
"--answer-model",
default="gpt-4o",
help="LLM model for answering questions (default: gpt-4o)",
)
eval_v2_parser.add_argument(
"--max-results",
type=int,
default=10,
help="Maximum memories to retrieve per search (default: 10)",
)
eval_v2_parser.add_argument(
"--no-graph",
action="store_true",
help="Disable graph expansion in search",
)
eval_v2_parser.add_argument(
"--llm-judge",
action="store_true",
help="Use LLM-as-judge scoring",
)
eval_v2_parser.add_argument(
"--judge-model",
default="gpt-4o",
help="Model for LLM judge (default: gpt-4o)",
)
eval_v2_parser.add_argument(
"--output",
"-o",
help="Path to save JSON results",
)
eval_v2_parser.add_argument(
"--parallel",
type=int,
default=5,
help="Number of parallel workers for LLM calls (default: 5)",
)
eval_v2_parser.add_argument(
"--debug",
action="store_true",
help="Enable debug logging (saved to results JSON)",
)
eval_v2_parser.set_defaults(func=cmd_memory_eval_v2)
args = parser.parse_args(argv)
if args.version:
return cmd_version(args)
if args.command is None:
parser.print_help()
return 0
result = args.func(args)
return int(result) if result is not None else 0
if __name__ == "__main__":
sys.exit(main())
|