Improving prompts and tools loading
Browse files
src/demos/stone/__init__.py
CHANGED
|
@@ -3,8 +3,8 @@
|
|
| 3 |
import logging
|
| 4 |
from typing import Any, Dict
|
| 5 |
|
| 6 |
-
# Import a tool from the tools library: use an alias
|
| 7 |
-
from tools_library.sweep_look import SweepLook as SweepLook
|
| 8 |
from reachy_mini_conversation_app.tools import Tool, ToolDependencies
|
| 9 |
|
| 10 |
|
|
|
|
| 3 |
import logging
|
| 4 |
from typing import Any, Dict
|
| 5 |
|
| 6 |
+
# Import a tool from the tools library: use an alias or noqa to avoid linter errors if unused
|
| 7 |
+
from tools_library.sweep_look import SweepLook as SweepLook # noqa: F401
|
| 8 |
from reachy_mini_conversation_app.tools import Tool, ToolDependencies
|
| 9 |
|
| 10 |
|
src/reachy_mini_conversation_app/prompts.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
"""Nothing (for ruff)."""
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
import logging
|
|
@@ -9,12 +7,16 @@ from pathlib import Path
|
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
Args:
|
| 16 |
content: The template content with [<name>] placeholders
|
| 17 |
-
prompts_library_path: Path to the prompts_library directory
|
| 18 |
|
| 19 |
Returns:
|
| 20 |
Expanded content with placeholders replaced by file contents
|
|
@@ -33,7 +35,7 @@ def _expand_prompt_includes(content: str, prompts_library_path: Path) -> str:
|
|
| 33 |
if match:
|
| 34 |
# Extract the name from [<name>]
|
| 35 |
template_name = match.group(1)
|
| 36 |
-
template_file =
|
| 37 |
|
| 38 |
try:
|
| 39 |
if template_file.exists():
|
|
@@ -111,23 +113,21 @@ def get_session_instructions() -> str:
|
|
| 111 |
return SESSION_INSTRUCTIONS
|
| 112 |
|
| 113 |
try:
|
| 114 |
-
# Look for instructions
|
| 115 |
-
|
| 116 |
-
instructions_file = demo_path / "instructions.txt"
|
| 117 |
|
| 118 |
if instructions_file.exists():
|
| 119 |
instructions = instructions_file.read_text(encoding="utf-8").strip()
|
| 120 |
if instructions:
|
| 121 |
-
# Expand [<name>] placeholders with content from
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
logger.info("Loaded instructions from demo '%s'", demo)
|
| 125 |
return expanded_instructions
|
| 126 |
-
logger.warning("Demo '
|
| 127 |
return SESSION_INSTRUCTIONS
|
| 128 |
|
| 129 |
-
logger.warning("Demo
|
| 130 |
return SESSION_INSTRUCTIONS
|
| 131 |
except Exception as e:
|
| 132 |
-
logger.warning("Failed to load instructions from demo '
|
| 133 |
return SESSION_INSTRUCTIONS
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import logging
|
|
|
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
|
| 10 |
+
DEMOS_DIRECTORY = Path(__file__).parent.parent / "demos"
|
| 11 |
+
PROMPTS_LIBRARY_DIRECTORY = Path(__file__).parent.parent / "prompts_library"
|
| 12 |
+
INSTRUCTIONS_FILENAME = "instructions.txt"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _expand_prompt_includes(content: str) -> str:
|
| 16 |
+
"""Expand [<name>] placeholders with content from prompts library files.
|
| 17 |
|
| 18 |
Args:
|
| 19 |
content: The template content with [<name>] placeholders
|
|
|
|
| 20 |
|
| 21 |
Returns:
|
| 22 |
Expanded content with placeholders replaced by file contents
|
|
|
|
| 35 |
if match:
|
| 36 |
# Extract the name from [<name>]
|
| 37 |
template_name = match.group(1)
|
| 38 |
+
template_file = PROMPTS_LIBRARY_DIRECTORY / f"{template_name}.txt"
|
| 39 |
|
| 40 |
try:
|
| 41 |
if template_file.exists():
|
|
|
|
| 113 |
return SESSION_INSTRUCTIONS
|
| 114 |
|
| 115 |
try:
|
| 116 |
+
# Look for instructions in the demo directory
|
| 117 |
+
instructions_file = DEMOS_DIRECTORY / demo / INSTRUCTIONS_FILENAME
|
|
|
|
| 118 |
|
| 119 |
if instructions_file.exists():
|
| 120 |
instructions = instructions_file.read_text(encoding="utf-8").strip()
|
| 121 |
if instructions:
|
| 122 |
+
# Expand [<name>] placeholders with content from prompts library
|
| 123 |
+
expanded_instructions = _expand_prompt_includes(instructions)
|
| 124 |
+
logger.info(f"Loaded instructions from demo '{demo}'")
|
|
|
|
| 125 |
return expanded_instructions
|
| 126 |
+
logger.warning(f"Demo '{demo}' has empty {INSTRUCTIONS_FILENAME}, using default")
|
| 127 |
return SESSION_INSTRUCTIONS
|
| 128 |
|
| 129 |
+
logger.warning(f"Demo {demo} has no {INSTRUCTIONS_FILENAME} file, using default")
|
| 130 |
return SESSION_INSTRUCTIONS
|
| 131 |
except Exception as e:
|
| 132 |
+
logger.warning(f"Failed to load instructions from demo '{demo}': {e}")
|
| 133 |
return SESSION_INSTRUCTIONS
|
src/reachy_mini_conversation_app/tools.py
CHANGED
|
@@ -458,6 +458,7 @@ class DoNothing(Tool):
|
|
| 458 |
|
| 459 |
# Registry & specs (dynamic)
|
| 460 |
def _load_demo_tools() -> None:
|
|
|
|
| 461 |
demo = os.getenv("DEMO")
|
| 462 |
if not demo:
|
| 463 |
print("No DEMO env variable set; using default.")
|
|
@@ -472,7 +473,6 @@ def _load_demo_tools() -> None:
|
|
| 472 |
sys.exit(1)
|
| 473 |
else:
|
| 474 |
print(f"✗ Demo '{demo}' failed due to missing dependency: {e.name}")
|
| 475 |
-
print(f" Install it with: uv pip install {e.name}")
|
| 476 |
sys.exit(1)
|
| 477 |
except Exception as e:
|
| 478 |
print(f"✗ Failed to load demo '{demo}': {e}")
|
|
@@ -486,19 +486,9 @@ ALL_TOOL_SPECS = [tool.spec() for tool in ALL_TOOLS.values()]
|
|
| 486 |
print("ALL_TOOLS:", ALL_TOOLS.keys())
|
| 487 |
|
| 488 |
|
| 489 |
-
def get_tool_specs(
|
| 490 |
-
"""Get tool specs, optionally excluding
|
| 491 |
-
|
| 492 |
-
Args:
|
| 493 |
-
exclude_speak: If True, exclude the 'speak' tool (for realtime mode where speaking is native)
|
| 494 |
-
|
| 495 |
-
Returns:
|
| 496 |
-
List of tool specifications
|
| 497 |
-
|
| 498 |
-
"""
|
| 499 |
-
if exclude_speak:
|
| 500 |
-
return [spec for spec in ALL_TOOL_SPECS if spec.get("name") != "speak"]
|
| 501 |
-
return ALL_TOOL_SPECS
|
| 502 |
|
| 503 |
|
| 504 |
# Dispatcher
|
|
|
|
| 458 |
|
| 459 |
# Registry & specs (dynamic)
|
| 460 |
def _load_demo_tools() -> None:
|
| 461 |
+
"""Load demo-specific tools if DEMO env variable is set."""
|
| 462 |
demo = os.getenv("DEMO")
|
| 463 |
if not demo:
|
| 464 |
print("No DEMO env variable set; using default.")
|
|
|
|
| 473 |
sys.exit(1)
|
| 474 |
else:
|
| 475 |
print(f"✗ Demo '{demo}' failed due to missing dependency: {e.name}")
|
|
|
|
| 476 |
sys.exit(1)
|
| 477 |
except Exception as e:
|
| 478 |
print(f"✗ Failed to load demo '{demo}': {e}")
|
|
|
|
| 486 |
print("ALL_TOOLS:", ALL_TOOLS.keys())
|
| 487 |
|
| 488 |
|
| 489 |
+
def get_tool_specs(exclusion_list = []) -> list[Dict[str, Any]]:
|
| 490 |
+
"""Get tool specs, optionally excluding some tools."""
|
| 491 |
+
return [spec for spec in ALL_TOOL_SPECS if spec.get("name") not in exclusion_list]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
|
| 493 |
|
| 494 |
# Dispatcher
|