Spaces:
Sleeping
Sleeping
Update prompts/prompts.py
Browse files- prompts/prompts.py +95 -49
prompts/prompts.py
CHANGED
|
@@ -23,70 +23,116 @@ def load_prompts():
|
|
| 23 |
|
| 24 |
merged_prompts = {str(k): _to_str(v) for k, v in merged.items()}
|
| 25 |
|
| 26 |
-
print(merged_prompts)
|
| 27 |
|
| 28 |
return {str(k): _to_str(v) for k, v in merged.items()}
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
-
def print_default_prompt_templates(
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
from smolagents import CodeAgent, InferenceClientModel
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
print("end of simple code")
|
| 59 |
-
pt = getattr(agent, "prompt_templates", None)
|
| 60 |
-
if pt is not None:
|
| 61 |
-
def pick(obj, fields):
|
| 62 |
-
return {k: getattr(obj, k) for k in fields if getattr(obj, k, None)}
|
| 63 |
out = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
print ("starting filling out")
|
| 66 |
-
if getattr(pt, "system_prompt", None):
|
| 67 |
-
out["system_prompt"] = pt.system_prompt
|
| 68 |
-
print(pt.system_prompt)
|
| 69 |
-
if getattr(pt, "planning", None):
|
| 70 |
-
out["planning"] = pick(pt.planning, ["plan", "update_plan_pre_messages", "update_plan_post_messages"])
|
| 71 |
-
if getattr(pt, "managed_agent", None):
|
| 72 |
-
out["managed_agent"] = pick(pt.managed_agent, ["task", "report"])
|
| 73 |
-
if getattr(pt, "final_answer", None):
|
| 74 |
-
out["final_answer"] = pick(pt.final_answer, ["pre_messages", "post_messages"])
|
| 75 |
-
print(yaml.safe_dump(out, sort_keys=False, allow_unicode=True))
|
| 76 |
-
return
|
| 77 |
except Exception as e:
|
| 78 |
print(f"[warn] couldn't access agent.prompt_templates directly: {e}")
|
| 79 |
|
| 80 |
-
# Fallback: export prompt
|
| 81 |
try:
|
| 82 |
with tempfile.TemporaryDirectory() as td:
|
| 83 |
-
agent.save(td) #
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
print(f"[error] failed to export defaults: {e}")
|
| 91 |
-
|
| 92 |
-
|
|
|
|
| 23 |
|
| 24 |
merged_prompts = {str(k): _to_str(v) for k, v in merged.items()}
|
| 25 |
|
| 26 |
+
#print(merged_prompts)
|
| 27 |
|
| 28 |
return {str(k): _to_str(v) for k, v in merged.items()}
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
+
def print_default_prompt_templates(agent):
|
| 33 |
+
import tempfile, pathlib, yaml, json, inspect
|
| 34 |
+
from dataclasses import is_dataclass, asdict
|
|
|
|
| 35 |
|
| 36 |
+
def safe_get(obj, attr, default=None):
|
| 37 |
+
try:
|
| 38 |
+
return getattr(obj, attr)
|
| 39 |
+
except Exception:
|
| 40 |
+
return default
|
| 41 |
|
| 42 |
+
def pick(obj, fields):
|
| 43 |
+
out = {}
|
| 44 |
+
for k in fields:
|
| 45 |
+
v = safe_get(obj, k, None)
|
| 46 |
+
# keep falsy-but-valid (e.g., ""), just skip None
|
| 47 |
+
if v is not None:
|
| 48 |
+
out[k] = v
|
| 49 |
+
return out
|
| 50 |
|
| 51 |
+
def normalize_for_yaml(x):
|
| 52 |
+
# Strings, lists, dicts are safe; other objects → string repr
|
| 53 |
+
if isinstance(x, (str, int, float, bool)) or x is None:
|
| 54 |
+
return x
|
| 55 |
+
if isinstance(x, (list, tuple)):
|
| 56 |
+
return [normalize_for_yaml(v) for v in x]
|
| 57 |
+
if isinstance(x, dict):
|
| 58 |
+
return {str(k): normalize_for_yaml(v) for k, v in x.items()}
|
| 59 |
+
# Dataclasses emitted upstream should’ve been dict-ified already
|
| 60 |
+
return str(x)
|
| 61 |
|
| 62 |
+
print("Trying to print the in-memory PromptTemplates object")
|
| 63 |
+
print("agent type:", type(agent))
|
| 64 |
+
print("has prompt_templates:", hasattr(agent, "prompt_templates"))
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
pt = safe_get(agent, "prompt_templates", None)
|
| 68 |
+
print("prompt_templates object:", pt)
|
| 69 |
+
if pt is None:
|
| 70 |
+
raise AttributeError("agent.prompt_templates is None or missing")
|
| 71 |
+
|
| 72 |
+
# If it’s a dataclass, get a dict in one shot
|
| 73 |
+
if is_dataclass(pt):
|
| 74 |
+
out = asdict(pt)
|
| 75 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
out = {}
|
| 77 |
+
sp = safe_get(pt, "system_prompt", None)
|
| 78 |
+
if sp is not None:
|
| 79 |
+
out["system_prompt"] = sp
|
| 80 |
+
|
| 81 |
+
planning = safe_get(pt, "planning", None)
|
| 82 |
+
if planning is not None:
|
| 83 |
+
out["planning"] = pick(planning, [
|
| 84 |
+
"plan",
|
| 85 |
+
"update_plan_pre_messages",
|
| 86 |
+
"update_plan_post_messages",
|
| 87 |
+
])
|
| 88 |
+
|
| 89 |
+
managed = safe_get(pt, "managed_agent", None)
|
| 90 |
+
if managed is not None:
|
| 91 |
+
out["managed_agent"] = pick(managed, ["task", "report"])
|
| 92 |
+
|
| 93 |
+
final_answer = safe_get(pt, "final_answer", None)
|
| 94 |
+
if final_answer is not None:
|
| 95 |
+
out["final_answer"] = pick(final_answer, ["pre_messages", "post_messages"])
|
| 96 |
+
|
| 97 |
+
# Normalize any message objects to YAML-safe structures
|
| 98 |
+
out = normalize_for_yaml(out)
|
| 99 |
+
|
| 100 |
+
# Pretty print quick peeks (safe)
|
| 101 |
+
for path in [
|
| 102 |
+
("system_prompt",),
|
| 103 |
+
("planning", "plan"),
|
| 104 |
+
("planning", "update_plan_pre_messages"),
|
| 105 |
+
("planning", "update_plan_post_messages"),
|
| 106 |
+
("final_answer", "pre_messages"),
|
| 107 |
+
("final_answer", "post_messages"),
|
| 108 |
+
]:
|
| 109 |
+
cur = out
|
| 110 |
+
for key in path:
|
| 111 |
+
if isinstance(cur, dict) and key in cur:
|
| 112 |
+
cur = cur[key]
|
| 113 |
+
else:
|
| 114 |
+
cur = None
|
| 115 |
+
break
|
| 116 |
+
print(".".join(path)+":", (cur if cur is not None else "<absent>"))
|
| 117 |
+
|
| 118 |
+
print("\n---- prompt templates (YAML) ----")
|
| 119 |
+
print(yaml.safe_dump(out, sort_keys=False, allow_unicode=True))
|
| 120 |
+
return
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
except Exception as e:
|
| 123 |
print(f"[warn] couldn't access agent.prompt_templates directly: {e}")
|
| 124 |
|
| 125 |
+
# Fallback: export prompt file and print it
|
| 126 |
try:
|
| 127 |
with tempfile.TemporaryDirectory() as td:
|
| 128 |
+
agent.save(td) # some builds write prompt.yaml (or prompts.yaml)
|
| 129 |
+
# try common filenames
|
| 130 |
+
for fname in ("prompt.yaml", "prompts.yaml", "prompt.yml", "prompts.yml"):
|
| 131 |
+
p = pathlib.Path(td) / fname
|
| 132 |
+
if p.exists():
|
| 133 |
+
print(f"\n---- exported {fname} ----")
|
| 134 |
+
print(p.read_text(encoding="utf-8"))
|
| 135 |
+
return
|
| 136 |
+
print("[warn] agent.save() produced no prompt file I recognize")
|
| 137 |
except Exception as e:
|
| 138 |
print(f"[error] failed to export defaults: {e}")
|
|
|
|
|
|