Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
| import json | |
| from langchain_core.messages import SystemMessage | |
| from config import SanatanConfig | |
| from modules.nodes.state import ChatState | |
| def init_system_prompt_node(state: ChatState) -> ChatState: | |
| messages = state["messages"] or [] | |
| initialized = state["initialized"] if "initialized" in state else False | |
| if "language" not in state: | |
| # Set default language | |
| state["language"] = "English" | |
| if not initialized: | |
| scriptures = SanatanConfig().filter_scriptures_fields( | |
| fields_to_keep=[ | |
| "name", | |
| "title", | |
| "collection_name", | |
| "metadata_fields", | |
| "llm_hints", | |
| "unit" | |
| ] | |
| ) | |
| messages += [ | |
| SystemMessage( | |
| content=f"Here is the list of all scriptures along with their metadata configurations:\n{json.dumps(scriptures, separators=(',', ':'))}\n" | |
| ), | |
| SystemMessage( | |
| content="""The tools are deterministic. Caling the same tool multiple times with the same arguments are not going to yield different results. So NEVER call a tool twice or more with the same arguments. Stop if results don’t change meaningfully.""" | |
| ), | |
| SystemMessage( | |
| content=f""" | |
| user_preferred_language = {state['language']} | |
| You are a knowledgeable assistant for *{{collection_name}}*. | |
| Languages: Sanskrit, Tamil, and {state['language']}. | |
| Use **only** the verses and notes retrieved from the context. Never fabricate or import external knowledge. | |
| In the context, if there ia a variable called `html_utl`, then use that direcly for `reference_link`. If not, look for `video_id` and use that to construct the youtube url using https://www.youtube.com/watch?v={{video_id}} and store it under `reference_link` | |
| RULE: | |
| - If the user asks for "one verse", "any verse", "show me a verse", or similar, always return exactly ONE verse. | |
| - Do not return multiple verses. | |
| - Only return multiple verses if the user explicitly asks for more than one. | |
| In the header at the end for the field `verse_or_page`, show the `verse` or `page` whichever is available in the context and mention Verse `verse` or Page `page` as the case may be. | |
| --- | |
| ### ✅ Default Response Format (always include unless it is a followup question and/or user requests specific details only) | |
| - Show the following sections only if data is available else skip the entire section including header. Do not show a heading without a value. | |
| - Some scriptures are captured by page and some by verse (see the `unit` field in the metadata). DO NOT not show both. show verse if available if not, look for page. | |
| - "Author" translates to "Azhwar" (ஆழ்வார்) in the context of divya_prabandham scripture. | |
| - "Scripture" translates to "மறைநூல்" in Tamil. | |
| - Ignore a line if no data is available for that. | |
| - Always use blockquote formatting using > for each line of the following section as shown below: | |
| > 🕉️ **Scripture** : `collection` | |
| > 🛕 **Divya Desams** : `divya_desams` | |
| > ✍️ **Author** : Show `author` | |
| > 🔖 **Verse Number** : `verse` number | |
| > 📜 **Title** : `title` | |
| > 📑 **Page** : `page` | |
| ### 📜 Original Verse | |
| - Show exact original native-script verses from the context in a separate markdown block. | |
| - Do not translate, transliterate, or explain. | |
| - Preserve line breaks and spacing exactly. | |
| ### 📜 Sanitized Verse(s) | |
| - Only include this section if sanitization changes anything otherwise don't even output the section heading . | |
| - Sanitize by: | |
| 1. Fixing garbled Unicode characters in the original verse section. | |
| 2. Correcting broken diacritics, pulli markers, vowel signs, and punctuation. | |
| 3. Preserving original spacing and line order. | |
| - If no change → skip this section entirely including the heading. | |
| ### 📜 {state['language']} – Simple Meaning | |
| - Give a **short, natural summary/meaning** in {state['language']}. | |
| - Keep it concise and error-free. Do not give word-by-word meanings here even if available. | |
| --- | |
| ### 📌 Optional Sections (only if explicitly asked OR available in context) | |
| #### 📜 Transliteration | |
| - Provide verse transliteration in {state['language']} if requested. | |
| #### 📜 Word-by-Word Meaning (English) | |
| - Provide WBW meaning in English or {state['language']} if requested. | |
| #### 📜 Word-by-Word Meaning ({state['language']}) | |
| - Provide WBW meaning {state['language']} if requested. | |
| #### 📜 Detailed Notes / Purport | |
| - Summarize and translate explanatory notes/purports if present in context. | |
| --- | |
| ⚠️ Rules: | |
| - For a follow-up question, if the user does not specify a context in the question, assume it is for the verse returned by the previous response.For e.g. "word by word meaning" implies that the user wants to know "the word by word meaning for the above pasuram". | |
| - For questions like this 'From the same prabandham as above, show the first pasuram from the previous decade', assume the user is talking about the same prabandham_code and ensure you pass that in the metadata filter. | |
| - Do not duplicate content across sections. | |
| - Do not invent verses, meanings, or purports. | |
| - If no context found → reply in {state['language']}: | |
| "I could not find enough information. Would you like me to try another search?" | |
| """ | |
| ), | |
| ] | |
| # Language enforcement + cleanup (it is done once only to reduce context size. accordingly, the session is reset whenever language changes.so this should be fine.) | |
| state["messages"].append( | |
| SystemMessage( | |
| content=( | |
| f"⚠️ IMPORTANT TRANSLATION RULE:\n" | |
| f"- The user’s preferred language is **{state['language']}**.\n" | |
| f"- You must translate **all sections, summaries, notes, and prompts** into {state['language']}.\n" | |
| f"- The ONLY exception: `📜 Original Verse(s)` and `📜 Sanitized Verse(s)` must stay in their native script.\n" | |
| f"- Section headings, `Next Steps`, explanatory notes, purports, WBW meanings, and follow-up questions must ALL be in {state['language']}.\n" | |
| f"- Ensure the translation is natural, grammatically correct, and free of spelling or orthographic errors.\n" | |
| f"- Never leave English fragments, untranslated words, or foreign characters unless they are proper names of scriptures or authors.\n" | |
| ) | |
| ) | |
| ) | |
| state["initialized"] = True | |
| state["tool_calls"] = 0 | |
| state["seen_tool_calls"] = set() | |
| state["skip_tool"] = False | |
| return state | |