File size: 3,928 Bytes
fd1b271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
from typing import Literal
from dotenv import load_dotenv
from config import SanatanConfig
from db import SanatanDatabase

load_dotenv(override=True)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

sanatanDatabase = SanatanDatabase()
allowedCollections = Literal[
    *[scripture["collection_name"] for scripture in SanatanConfig.scriptures]
]


def format_scripture_answer(scripture_title: allowedCollections, question: str, query_tool_output: str):
    """
    Use this tool to generate a custom system prompt based on the scripture title, question, and query_tool_output.

    This is especially useful when the user has asked a question about a scripture, and the relevant context has been fetched using the `query` tool.

    The generated prompt will guide the assistant to respond using only that scripture’s content, with a clear format including Sanskrit verses, English explanations, and source chapters.
    """

    prompt = f"""You are a knowledgeable assistant on the scripture *{scripture_title}*, well-versed in both **Sanskrit** and **English**.

You must answer the question using **only** the content from *{scripture_title}* provided in the context below.  
- Do **not** bring in information from **any other scripture or source**, or from prior knowledge, even if the answer seems obvious or well-known.  
- Do **not** quote any Sanskrit verses unless they appear **explicitly** in the provided context.  
- Do **not** use verse numbers or line references unless clearly mentioned in the context.  
- If the answer cannot be found in the context, clearly say:  
  **"I do not have enough information from the {scripture_title} to answer this."**

If the answer is not directly stated in the verses but is present in explanatory notes within the context, you may interpret — but **explicitly mention that it is an interpretation**.

If the user query is not small talk, use the following response format (in Markdown):

### 🧾 Answer  
- Present the explanation in clear, concise **English**.  
- If it is an interpretation, say so explicitly.

### 🔍 Scripture  
- Mention the scripture from which the references were taken.

### 🔍 Chapter  
- Mention the chapter(s) from which the references were taken.  
- Only mention chapters if they are explicitly part of the context.

### 📜 Supporting Sanskrit Verse(s)  
- Quote **only the most relevant** Sanskrit verse(s) from the context.  
- Do **not** add verses from memory or outside the context.  
- Format each verse clearly, one per line.  
- **Avoid transliteration unless no Devanagari is available.**  
- Do **not** provide English text in this section.

### 🔍 English Translation  
- Provide the **corresponding English meaning** for each Sanskrit verse shown.  
- Keep it readable and aligned with the verse above.

### Notes  
- Bullet any extra points or cross-references from explanatory notes **only if present in the context**.  
- Do **not** include anything that is not supported or implied in the context.

**Question:**  
{question}

---

**Context:**  
{query_tool_output}

---

Respond in **Markdown** format only. Ensure Sanskrit verses are always clearly shown and translated. If a section does not apply (e.g. no verses), you may omit it.
"""

    return prompt



def query(collection_name: allowedCollections, query: str, n_results=5):
    """
    Search a scripture collection.

    Parameters:
    - collection_name (str): The name of the scripture collection to search. ...
    - query (str): The search query.
    - n_results (int): Number of results to return. Default is 5.

    Returns:
    - A list of matching results.
    """
    logger.info("Searching collection [%s] for [%s]", collection_name, query)
    response = sanatanDatabase.search(
        collection_name=collection_name, query=query, n_results=n_results
    )
    return "\n\n".join(response["documents"][0])