Spaces:
Sleeping
Sleeping
Update prompts/prompts.py
Browse files- prompts/prompts.py +7 -147
prompts/prompts.py
CHANGED
|
@@ -1,154 +1,14 @@
|
|
| 1 |
import yaml
|
| 2 |
-
import io, contextlib
|
| 3 |
|
| 4 |
def load_prompts():
|
| 5 |
-
|
| 6 |
-
merged = {}
|
| 7 |
-
for path in files:
|
| 8 |
-
with open(path, "r", encoding="utf-8") as f:
|
| 9 |
-
data = yaml.safe_load(f) or {}
|
| 10 |
-
if isinstance(data, dict):
|
| 11 |
-
merged.update(data)
|
| 12 |
-
|
| 13 |
-
def _to_str(v):
|
| 14 |
-
if v is None:
|
| 15 |
-
return "" # avoid literal "None"
|
| 16 |
-
if isinstance(v, str):
|
| 17 |
-
return v
|
| 18 |
-
if isinstance(v, dict) and "content" in v:
|
| 19 |
-
return str(v["content"]) # flatten chat-style entries
|
| 20 |
-
if isinstance(v, (list, tuple)):
|
| 21 |
-
return "\n".join(map(str, v)) # readable multiline
|
| 22 |
-
return str(v)
|
| 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 |
-
|
| 35 |
-
from dataclasses import is_dataclass, asdict
|
| 36 |
-
|
| 37 |
-
with tempfile.TemporaryDirectory() as td:
|
| 38 |
-
agent.save(td)
|
| 39 |
-
for fname in ("prompt.yaml","prompts.yaml","prompt.yml","prompts.yml"):
|
| 40 |
-
p = pathlib.Path(td)/fname
|
| 41 |
-
if p.exists():
|
| 42 |
-
print(f"\n== {fname} ==\n")
|
| 43 |
-
print(p.read_text(encoding="utf-8"))
|
| 44 |
-
break
|
| 45 |
-
|
| 46 |
-
pass
|
| 47 |
-
|
| 48 |
-
def safe_get(obj, attr, default=None):
|
| 49 |
-
try:
|
| 50 |
-
return getattr(obj, attr)
|
| 51 |
-
except Exception:
|
| 52 |
-
return default
|
| 53 |
-
|
| 54 |
-
def pick(obj, fields):
|
| 55 |
-
out = {}
|
| 56 |
-
for k in fields:
|
| 57 |
-
v = safe_get(obj, k, None)
|
| 58 |
-
# keep falsy-but-valid (e.g., ""), just skip None
|
| 59 |
-
if v is not None:
|
| 60 |
-
out[k] = v
|
| 61 |
-
return out
|
| 62 |
-
|
| 63 |
-
def normalize_for_yaml(x):
|
| 64 |
-
# Strings, lists, dicts are safe; other objects → string repr
|
| 65 |
-
if isinstance(x, (str, int, float, bool)) or x is None:
|
| 66 |
-
return x
|
| 67 |
-
if isinstance(x, (list, tuple)):
|
| 68 |
-
return [normalize_for_yaml(v) for v in x]
|
| 69 |
-
if isinstance(x, dict):
|
| 70 |
-
return {str(k): normalize_for_yaml(v) for k, v in x.items()}
|
| 71 |
-
# Dataclasses emitted upstream should’ve been dict-ified already
|
| 72 |
-
return str(x)
|
| 73 |
-
|
| 74 |
-
print("Trying to print the in-memory PromptTemplates object")
|
| 75 |
-
print("agent type:", type(agent))
|
| 76 |
-
print("has prompt_templates:", hasattr(agent, "prompt_templates"))
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
try:
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
# If it’s a dataclass, get a dict in one shot
|
| 89 |
-
if is_dataclass(pt):
|
| 90 |
-
out = asdict(pt)
|
| 91 |
-
else:
|
| 92 |
-
out = {}
|
| 93 |
-
sp = safe_get(pt, "system_prompt", None)
|
| 94 |
-
if sp is not None:
|
| 95 |
-
out["system_prompt"] = sp
|
| 96 |
-
|
| 97 |
-
planning = safe_get(pt, "planning", None)
|
| 98 |
-
if planning is not None:
|
| 99 |
-
out["planning"] = pick(planning, [
|
| 100 |
-
"plan",
|
| 101 |
-
"update_plan_pre_messages",
|
| 102 |
-
"update_plan_post_messages",
|
| 103 |
-
])
|
| 104 |
-
|
| 105 |
-
managed = safe_get(pt, "managed_agent", None)
|
| 106 |
-
if managed is not None:
|
| 107 |
-
out["managed_agent"] = pick(managed, ["task", "report"])
|
| 108 |
-
|
| 109 |
-
final_answer = safe_get(pt, "final_answer", None)
|
| 110 |
-
if final_answer is not None:
|
| 111 |
-
out["final_answer"] = pick(final_answer, ["pre_messages", "post_messages"])
|
| 112 |
-
|
| 113 |
-
# Normalize any message objects to YAML-safe structures
|
| 114 |
-
out = normalize_for_yaml(out)
|
| 115 |
-
|
| 116 |
-
# Pretty print quick peeks (safe)
|
| 117 |
-
for path in [
|
| 118 |
-
("system_prompt",),
|
| 119 |
-
("planning", "plan"),
|
| 120 |
-
("planning", "update_plan_pre_messages"),
|
| 121 |
-
("planning", "update_plan_post_messages"),
|
| 122 |
-
("final_answer", "pre_messages"),
|
| 123 |
-
("final_answer", "post_messages"),
|
| 124 |
-
]:
|
| 125 |
-
cur = out
|
| 126 |
-
for key in path:
|
| 127 |
-
if isinstance(cur, dict) and key in cur:
|
| 128 |
-
cur = cur[key]
|
| 129 |
-
else:
|
| 130 |
-
cur = None
|
| 131 |
-
break
|
| 132 |
-
print(".".join(path)+":", (cur if cur is not None else "<absent>"))
|
| 133 |
-
|
| 134 |
-
print("\n---- prompt templates (YAML) ----")
|
| 135 |
-
print(yaml.safe_dump(out, sort_keys=False, allow_unicode=True))
|
| 136 |
-
return
|
| 137 |
-
|
| 138 |
except Exception as e:
|
| 139 |
-
|
|
|
|
| 140 |
|
| 141 |
-
# Fallback: export prompt file and print it
|
| 142 |
-
try:
|
| 143 |
-
with tempfile.TemporaryDirectory() as td:
|
| 144 |
-
agent.save(td) # some builds write prompt.yaml (or prompts.yaml)
|
| 145 |
-
# try common filenames
|
| 146 |
-
for fname in ("prompt.yaml", "prompts.yaml", "prompt.yml", "prompts.yml"):
|
| 147 |
-
p = pathlib.Path(td) / fname
|
| 148 |
-
if p.exists():
|
| 149 |
-
print(f"\n---- exported {fname} ----")
|
| 150 |
-
print(p.read_text(encoding="utf-8"))
|
| 151 |
-
return
|
| 152 |
-
print("[warn] agent.save() produced no prompt file I recognize")
|
| 153 |
-
except Exception as e:
|
| 154 |
-
print(f"[error] failed to export defaults: {e}")
|
|
|
|
| 1 |
import yaml
|
|
|
|
| 2 |
|
| 3 |
def load_prompts():
|
| 4 |
+
system_prompt_override = "prompts/code_agent.yml"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
try:
|
| 7 |
+
with open(system_prompt_override, 'r', encoding='utf-8') as file:
|
| 8 |
+
return file.read()
|
| 9 |
+
except FileNotFoundError:
|
| 10 |
+
raise FileNotFoundError(f"Prompt file '{system_prompt_override}' not found.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
except Exception as e:
|
| 12 |
+
raise Exception(f"Failed to read prompt file: {e}")
|
| 13 |
+
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|