Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 6,672 Bytes
63d1774 2a96fbf 63d1774 2f2633c 63d1774 12446b3 63d1774 ef12cbd 63d1774 5f4344d a1180f7 92ca15b 5d00a8f 92ca15b 5d00a8f a1180f7 92ca15b 2a96fbf a1180f7 12446b3 2bdd7bf 12446b3 2bdd7bf a1180f7 92ca15b 2bdd7bf ee431ce a1180f7 92ca15b a1180f7 92ca15b a1180f7 92ca15b a1180f7 92ca15b 5d00a8f 92ca15b 5d00a8f 92ca15b 5d00a8f a1180f7 92ca15b 5d00a8f a1180f7 92ca15b 5d00a8f 92ca15b a1180f7 914dd1a 92ca15b 5d00a8f 63d1774 98834ab 2a96fbf a1180f7 92ca15b 63d1774 5d00a8f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
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
|