Update backend/prompt_builder.py
#6
by pmrinal2005 - opened
2 unmodified lines3456785 unmodified lines14151617181920214 unmodified lines262728293031323334353628 unmodified lines65666768697071727374759 unmodified lines85868788899091929394959697984 unmodified lines1031041051061071081092 unmodified linesSupports up to 2 multimodal attachments (images or PDFs). For PDFs we extracttext inline (since the vision projector handles images only). If vision isunavailable we degrade gracefully to text-only."""import base64import io5 unmodified linesfrom .model_loader import MMPROJ_PATHSYSTEM_PROMPT = """You are Elysium — a persistent agentic civilization.You ALWAYS respond with a single valid JSON object exactly matching theElysiumResponse schema v1.0.0. No preamble. No markdown fences. JSON only.Decide complexity dynamically:- SIMPLE_REPLY: trivial Q — no agents (council_deliberation.agent_outputs = [])4 unmodified lines- SPECIATION_EVENT: only on unresolved cross-domain tension- Always populate ui_directives (camera_focus_node_id, pulses, threads)- All node_id and edge_id values must be unique strings- Always include 'direct_answer' — a short human-readable answer to surface in toasts.When the user attaches images or PDFs, analyze them, populatemultimodal_perception fields (ocr_extracted_text, image_scene_description,document_type, visual_entities_detected), and reference them in your reasoning."""28 unmodified linesctx = f"\n\n[Hypergraph context]\n{hg_context}" if hg_context else ""attachments = attachments or []# Gather image and pdf attachments separatelyimage_atts = [a for a in attachments if a["kind"] == "image" and a.get("image") is not None]pdf_atts = [a for a in attachments if a["kind"] == "pdf" and a.get("bytes")]# Build inline PDF text blockpdf_block = ""for i, p in enumerate(pdf_atts):pdf_block += f"\n\n[Attached PDF #{i+1}: {p.get('name','document.pdf')}]\n"9 unmodified lines})user_content.append({"type": "text","text": (user_text or "(no text)") + pdf_block + ctx,})return [{"role": "system", "content": SYSTEM_PROMPT},{"role": "user", "content": user_content},]# No vision: include note if user attached images but vision is offnote = ""if image_atts and not MMPROJ_PATH:note = (f"\n\n[Note: user attached {len(image_atts)} image(s) but vision "4 unmodified linesreturn [{"role": "system", "content": SYSTEM_PROMPT},{"role": "user","content": (user_text or "(no text)") + pdf_block + note + ctx},]2 unmodified lines34567891011125 unmodified lines18192021222324252627284 unmodified lines333435363738394041424344454628 unmodified lines7576777879808182839 unmodified lines939495969798991001011021031041051064 unmodified lines1111121131141151161171182 unmodified linesSupports up to 2 multimodal attachments (images or PDFs). For PDFs we extracttext inline (since the vision projector handles images only). If vision isunavailable we degrade gracefully to text-only.FIX: SYSTEM_PROMPT now explicitly forbids <think> blocks, markdown fences,preamble, and any non-JSON token. Even so, server.py still strips <think>defensively because the fine-tuned weights emit it occasionally."""import base64import io5 unmodified linesfrom .model_loader import MMPROJ_PATHSYSTEM_PROMPT = """You are Elysium — a persistent agentic civilization.OUTPUT CONTRACT (strict):• Respond with ONE valid JSON object matching the ElysiumResponse schema v1.0.0.• Output JSON ONLY. No preamble, no postscript, no markdown, no code fences.• Do NOT emit <think>, <reasoning>, or any XML-style tags.• The first character of your output MUST be `{` and the last must be `}`.Decide complexity dynamically:- SIMPLE_REPLY: trivial Q — no agents (council_deliberation.agent_outputs = [])4 unmodified lines- SPECIATION_EVENT: only on unresolved cross-domain tension- Always populate ui_directives (camera_focus_node_id, pulses, threads)- All node_id and edge_id values must be unique strings- Always include 'direct_answer' — a short human-readable answer (one or twosentences) suitable for surfacing in a toast. NEVER place JSON, raw schematext, or system tags inside direct_answer.When the user attaches images or PDFs, analyze them, populatemultimodal_perception fields (ocr_extracted_text, image_scene_description,document_type, visual_entities_detected), and reference them in your reasoningthrough the hypergraph_delta and agent thinking — NOT in direct_answer."""28 unmodified linesctx = f"\n\n[Hypergraph context]\n{hg_context}" if hg_context else ""attachments = attachments or []image_atts = [a for a in attachments if a["kind"] == "image" and a.get("image") is not None]pdf_atts = [a for a in attachments if a["kind"] == "pdf" and a.get("bytes")]pdf_block = ""for i, p in enumerate(pdf_atts):pdf_block += f"\n\n[Attached PDF #{i+1}: {p.get('name','document.pdf')}]\n"9 unmodified lines})user_content.append({"type": "text","text": (user_text or "(no text)") + pdf_block + ctx +"\n\nReturn ONLY the JSON object. No <think> tags. No prose.",})return [{"role": "system", "content": SYSTEM_PROMPT},{"role": "user", "content": user_content},]note = ""if image_atts and not MMPROJ_PATH:note = (f"\n\n[Note: user attached {len(image_atts)} image(s) but vision "4 unmodified linesreturn [{"role": "system", "content": SYSTEM_PROMPT},{"role": "user","content": (user_text or "(no text)") + pdf_block + note + ctx +"\n\nReturn ONLY the JSON object. No <think> tags. No prose."},]
backend/prompt_builder.py+18-9