Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Upload folder using huggingface_hub
Browse files- .gitattributes +2 -0
- .github/workflows/update_space.yml +28 -0
- .gitignore +12 -0
- .python-version +1 -0
- README.md +3 -9
- app.py +74 -0
- assets/adiyen_bot.jpg +3 -0
- assets/adiyen_bot.png +3 -0
- assets/avatar_bot.png +0 -0
- assets/avatar_user.png +0 -0
- config.py +108 -0
- db.py +35 -0
- drive_downloader.py +44 -0
- embeddings.py +9 -0
- graph_helper.py +103 -0
- main.py +22 -0
- push_notifications_helper.py +24 -0
- pyproject.toml +21 -0
- requirements.txt +556 -0
- sanatan_assistant.py +98 -0
- serperdev_helper.py +14 -0
- tools.py +28 -0
- uv.lock +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
assets/adiyen_bot.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
assets/adiyen_bot.png filter=lfs diff=lfs merge=lfs -text
|
.github/workflows/update_space.yml
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Run Python script
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches:
|
| 6 |
+
- main
|
| 7 |
+
|
| 8 |
+
jobs:
|
| 9 |
+
build:
|
| 10 |
+
runs-on: ubuntu-latest
|
| 11 |
+
|
| 12 |
+
steps:
|
| 13 |
+
- name: Checkout
|
| 14 |
+
uses: actions/checkout@v2
|
| 15 |
+
|
| 16 |
+
- name: Set up Python
|
| 17 |
+
uses: actions/setup-python@v2
|
| 18 |
+
with:
|
| 19 |
+
python-version: '3.9'
|
| 20 |
+
|
| 21 |
+
- name: Install Gradio
|
| 22 |
+
run: python -m pip install gradio
|
| 23 |
+
|
| 24 |
+
- name: Log in to Hugging Face
|
| 25 |
+
run: python -c 'import huggingface_hub; huggingface_hub.login(token="${{ secrets.hf_token }}")'
|
| 26 |
+
|
| 27 |
+
- name: Deploy to Spaces
|
| 28 |
+
run: gradio deploy
|
.gitignore
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python-generated files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[oc]
|
| 4 |
+
build/
|
| 5 |
+
dist/
|
| 6 |
+
wheels/
|
| 7 |
+
*.egg-info
|
| 8 |
+
|
| 9 |
+
# Virtual environments
|
| 10 |
+
.venv
|
| 11 |
+
.env
|
| 12 |
+
chromadb-store/
|
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.12
|
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji: 😻
|
| 4 |
-
colorFrom: blue
|
| 5 |
-
colorTo: red
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 5.39.0
|
| 8 |
app_file: app.py
|
| 9 |
-
|
|
|
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: sanatan_ai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
app_file: app.py
|
| 4 |
+
sdk: gradio
|
| 5 |
+
sdk_version: 5.38.0
|
| 6 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from config import SanatanConfig
|
| 4 |
+
from drive_downloader import ZipDownloader
|
| 5 |
+
from graph_helper import generate_graph
|
| 6 |
+
import uuid
|
| 7 |
+
import logging
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
logging.basicConfig()
|
| 11 |
+
logger = logging.getLogger()
|
| 12 |
+
logger.setLevel(logging.INFO)
|
| 13 |
+
|
| 14 |
+
graph = generate_graph()
|
| 15 |
+
|
| 16 |
+
def init():
|
| 17 |
+
load_dotenv(override=True)
|
| 18 |
+
|
| 19 |
+
downloader = ZipDownloader(
|
| 20 |
+
service_account_json=os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON")
|
| 21 |
+
)
|
| 22 |
+
# 1. Download zip file
|
| 23 |
+
zip_path = downloader.download_zip_from_drive(
|
| 24 |
+
file_id=os.getenv("CHROMADB_FILE_ID"),
|
| 25 |
+
output_path=SanatanConfig.dbStorePath,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# 2. Extract it
|
| 29 |
+
downloader.unzip(zip_path, extract_to="./")
|
| 30 |
+
|
| 31 |
+
def init_session():
|
| 32 |
+
# Generate a unique ID per browser session
|
| 33 |
+
return str(uuid.uuid4())
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def chat(message, history, thread_id):
|
| 37 |
+
print("received chat message for thread:", thread_id)
|
| 38 |
+
config = {"configurable": {"thread_id": thread_id}}
|
| 39 |
+
response = graph.invoke(
|
| 40 |
+
{"messages": [{"role": "user", "content": message}]}, config=config
|
| 41 |
+
)
|
| 42 |
+
return response["messages"][-1].content
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
thread_id = gr.State(init_session)
|
| 46 |
+
supported_scriptures = "\n - ".join(
|
| 47 |
+
[scripture["title"] for scripture in SanatanConfig.scriptures]
|
| 48 |
+
)
|
| 49 |
+
intro_messages = [
|
| 50 |
+
{"role" : "assistant","content" : f"Namaskaram! I can assist with the following scriptures: \n - {supported_scriptures}"},
|
| 51 |
+
{"role" : "assistant","content" : """
|
| 52 |
+
#### You can ask me questions like:
|
| 53 |
+
- How do all scriptures describe the form of Vishnu?
|
| 54 |
+
- What did Krishna teach in Gita?
|
| 55 |
+
- How did Arjun react on seeing the Vishwarupa form of Krishna?
|
| 56 |
+
- Give some names of Vishnu from Sahasranamam.
|
| 57 |
+
"""}
|
| 58 |
+
]
|
| 59 |
+
chatbot = gr.Chatbot(
|
| 60 |
+
elem_id="chatbot",
|
| 61 |
+
avatar_images=("assets/avatar_user.png", "assets/adiyen_bot.png"),
|
| 62 |
+
value=intro_messages,
|
| 63 |
+
label="Sanatan-Bot",
|
| 64 |
+
show_copy_button=True,
|
| 65 |
+
show_copy_all_button=True,
|
| 66 |
+
type="messages",
|
| 67 |
+
height=800
|
| 68 |
+
)
|
| 69 |
+
chatInterface = gr.ChatInterface(title="Sanatan-AI",
|
| 70 |
+
fn=chat, additional_inputs=[thread_id], chatbot=chatbot
|
| 71 |
+
)
|
| 72 |
+
# initializze database
|
| 73 |
+
init()
|
| 74 |
+
chatInterface.launch()
|
assets/adiyen_bot.jpg
ADDED
|
Git LFS Details
|
assets/adiyen_bot.png
ADDED
|
Git LFS Details
|
assets/avatar_bot.png
ADDED
|
|
assets/avatar_user.png
ADDED
|
|
config.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class SanatanConfig:
|
| 2 |
+
# shuklaYajurVedamPdfPath: str = "./data/shukla-yajur-veda.pdf"
|
| 3 |
+
# shuklaYajurVedamSmallPdfPath: str = "./data/shukla-yajur-veda-small.pdf"
|
| 4 |
+
# vishnuPuranamPdfPath = "./data/vishnu_puranam.pdf"
|
| 5 |
+
# datastores = [{"name": "sanskrit_001", "dbStorePath": "./chromadb-store"}, {"name": "nalayiram", "dbStorePath": "./chromadb-store-4000"}]
|
| 6 |
+
dbStorePath: str = "./chromadb-store"
|
| 7 |
+
# shuklaYajurVedamCollectionName: str = "shukla_yajur_vedam"
|
| 8 |
+
# vishnuPuranamCollectionName: str = "vishnu_puranam"
|
| 9 |
+
# shuklaYajurVedamOutputDir = "./output/shukla_yajur_vedam"
|
| 10 |
+
# vishnuPuranamOutputDir = "./output/vishnu_puranam"
|
| 11 |
+
scriptures = [
|
| 12 |
+
{
|
| 13 |
+
"name": "vishnu_puranam",
|
| 14 |
+
"title": "Sri Vishnu Puranam",
|
| 15 |
+
"output_dir": "./output/vishnu_puranam",
|
| 16 |
+
"collection_name": "vishnu_puranam",
|
| 17 |
+
"pdf_path": "./data/vishnu_puranam.pdf",
|
| 18 |
+
"language": "san+eng",
|
| 19 |
+
"example_labels": [
|
| 20 |
+
"Vishnu's form",
|
| 21 |
+
"About the five elements",
|
| 22 |
+
"About Garuda",
|
| 23 |
+
"Weapons of Vishnu",
|
| 24 |
+
],
|
| 25 |
+
"examples": [
|
| 26 |
+
"describe Vishnu's form",
|
| 27 |
+
"five elements and their significance",
|
| 28 |
+
"What is the significance of Garuda? Show some verses that describe him.",
|
| 29 |
+
"What weapons does Vishnu hold?",
|
| 30 |
+
],
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"name": "shukla_yajur_vedam",
|
| 34 |
+
"title": "Shukla Yajur Vedam",
|
| 35 |
+
"output_dir": "./output/shukla_yajur_vedam",
|
| 36 |
+
"collection_name": "shukla_yajur_vedam",
|
| 37 |
+
"pdf_path": "./data/shukla-yajur-veda.pdf",
|
| 38 |
+
"language": "san+eng",
|
| 39 |
+
"example_labels": [
|
| 40 |
+
"About Vedam",
|
| 41 |
+
"About the five elements",
|
| 42 |
+
"About Brahma",
|
| 43 |
+
],
|
| 44 |
+
"examples": [
|
| 45 |
+
"Gist of Shukla Yajur Vedam. Give me some sanskrit verses.",
|
| 46 |
+
"What is the significance of fire and water. show some sanskrit verses",
|
| 47 |
+
"Brahma",
|
| 48 |
+
],
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"name": "bhagavat_gita",
|
| 52 |
+
"title": "Bhagavat Gita",
|
| 53 |
+
"output_dir": "./output/bhagavat_gita",
|
| 54 |
+
"collection_name": "bhagavat_gita",
|
| 55 |
+
"pdf_path": "./data/bhagavat_gita.pdf",
|
| 56 |
+
"language": "san+eng",
|
| 57 |
+
"example_labels": [
|
| 58 |
+
"About Arjuna",
|
| 59 |
+
"About Karma",
|
| 60 |
+
"About birth and death",
|
| 61 |
+
"About the battle field",
|
| 62 |
+
"About Krishna's form",
|
| 63 |
+
],
|
| 64 |
+
"examples": [
|
| 65 |
+
"Show some verses where Krishna advises Arjuna",
|
| 66 |
+
"What does Krishna say about Karma",
|
| 67 |
+
"What does Krishna say about birth and death",
|
| 68 |
+
"describe the battle field",
|
| 69 |
+
"Vishwarupa",
|
| 70 |
+
],
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"name": "valmiki_ramayanam",
|
| 74 |
+
"title": "Valmiki Ramayanam",
|
| 75 |
+
"output_dir": "./output/valmiki_ramayanam",
|
| 76 |
+
"collection_name": "valmiki_ramayanam",
|
| 77 |
+
"pdf_path": "./data/valmiki_ramayanam.pdf",
|
| 78 |
+
"language": "san+eng",
|
| 79 |
+
"example_labels": [
|
| 80 |
+
"About Jatayu",
|
| 81 |
+
"About Hanuman",
|
| 82 |
+
"About Vali",
|
| 83 |
+
"About Sita",
|
| 84 |
+
"About Ravana",
|
| 85 |
+
],
|
| 86 |
+
"examples": [
|
| 87 |
+
"What is the significance of Jatayu? show some sanskrit verses to support the argument",
|
| 88 |
+
"Show some verses where Hanuman is mentioned",
|
| 89 |
+
"How did Rama kill Vali",
|
| 90 |
+
"How was Sita abducted",
|
| 91 |
+
"How did Rama kill Ravana?",
|
| 92 |
+
],
|
| 93 |
+
},
|
| 94 |
+
{
|
| 95 |
+
"name": "vishnu_sahasranamam",
|
| 96 |
+
"title": "Vishnu Sahasranamam",
|
| 97 |
+
"output_dir": "./output/vishnu_sahasranamam",
|
| 98 |
+
"collection_name": "vishnu_sahasranamam",
|
| 99 |
+
"pdf_path": "./data/vishnu_sahasranamam.pdf",
|
| 100 |
+
"language": "san+eng",
|
| 101 |
+
"example_labels": ["Vanamali", "1000 names", "Sanskrit text search"],
|
| 102 |
+
"examples": [
|
| 103 |
+
"Vanamali",
|
| 104 |
+
"Show some of the 1000 names of Vishnu along with their meaning",
|
| 105 |
+
"show the verse that begins with शुक्लाम्बरधरं",
|
| 106 |
+
],
|
| 107 |
+
},
|
| 108 |
+
]
|
db.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import chromadb
|
| 2 |
+
|
| 3 |
+
from config import SanatanConfig
|
| 4 |
+
from embeddings import get_embedding
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
logger = logging.getLogger(__name__)
|
| 8 |
+
logger.setLevel(logging.INFO)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class SanatanDatabase:
|
| 12 |
+
def __init__(self) -> None:
|
| 13 |
+
self.chroma_client = chromadb.PersistentClient(path=SanatanConfig.dbStorePath)
|
| 14 |
+
|
| 15 |
+
def does_data_exist(self, collection_name: str) -> bool:
|
| 16 |
+
collection = self.chroma_client.get_or_create_collection(name=collection_name)
|
| 17 |
+
num_rows = collection.count()
|
| 18 |
+
logger.info("num_rows in %s = %d", collection_name, num_rows)
|
| 19 |
+
return num_rows > 0
|
| 20 |
+
|
| 21 |
+
def load(self, collection_name: str, ids, documents, embeddings, metadatas):
|
| 22 |
+
collection = self.chroma_client.get_or_create_collection(name=collection_name)
|
| 23 |
+
collection.add(
|
| 24 |
+
ids=ids,
|
| 25 |
+
documents=documents,
|
| 26 |
+
embeddings=embeddings,
|
| 27 |
+
metadatas=metadatas,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
def search(self, collection_name: str, query: str, n_results=2):
|
| 31 |
+
collection = self.chroma_client.get_or_create_collection(name=collection_name)
|
| 32 |
+
response = collection.query(
|
| 33 |
+
query_embeddings=[get_embedding(query)], n_results=n_results
|
| 34 |
+
)
|
| 35 |
+
return response
|
drive_downloader.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import io
|
| 4 |
+
import zipfile
|
| 5 |
+
|
| 6 |
+
from google.oauth2.service_account import Credentials
|
| 7 |
+
from googleapiclient.discovery import build
|
| 8 |
+
from googleapiclient.http import MediaIoBaseDownload
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class ZipDownloader:
|
| 12 |
+
def __init__(self, service_account_json: str):
|
| 13 |
+
self.creds = Credentials.from_service_account_info(json.loads(service_account_json))
|
| 14 |
+
self.service = build("drive", "v3", credentials=self.creds)
|
| 15 |
+
|
| 16 |
+
def download_zip_from_drive(self, file_id: str, output_path: str) -> str:
|
| 17 |
+
"""
|
| 18 |
+
Downloads a ZIP file from Google Drive and saves it locally.
|
| 19 |
+
Returns the path to the downloaded ZIP.
|
| 20 |
+
"""
|
| 21 |
+
request = self.service.files().get_media(fileId=file_id)
|
| 22 |
+
local_zip_path = os.path.join(output_path, "downloaded.zip")
|
| 23 |
+
|
| 24 |
+
os.makedirs(output_path, exist_ok=True)
|
| 25 |
+
fh = io.FileIO(local_zip_path, 'wb')
|
| 26 |
+
downloader = MediaIoBaseDownload(fh, request)
|
| 27 |
+
|
| 28 |
+
print(f"⬇️ Downloading ZIP file from Drive ID: {file_id}")
|
| 29 |
+
done = False
|
| 30 |
+
while not done:
|
| 31 |
+
status, done = downloader.next_chunk()
|
| 32 |
+
print(f" ⏬ Progress: {int(status.progress() * 100)}%")
|
| 33 |
+
|
| 34 |
+
print(f"✅ ZIP downloaded to: {local_zip_path}")
|
| 35 |
+
return local_zip_path
|
| 36 |
+
|
| 37 |
+
def unzip(self, zip_path: str, extract_to: str):
|
| 38 |
+
"""
|
| 39 |
+
Unzips the downloaded ZIP file to a specified directory.
|
| 40 |
+
"""
|
| 41 |
+
print(f"📂 Extracting ZIP: {zip_path} -> {extract_to}")
|
| 42 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 43 |
+
zip_ref.extractall(extract_to)
|
| 44 |
+
print("✅ Extraction complete.")
|
embeddings.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
|
| 4 |
+
# Step 1: Load SentenceTransformer model
|
| 5 |
+
# model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 6 |
+
model = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
| 7 |
+
|
| 8 |
+
def get_embedding(text: str) -> list:
|
| 9 |
+
return model.encode(text).tolist()
|
graph_helper.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Annotated, TypedDict
|
| 2 |
+
from langgraph.graph import StateGraph, START, END
|
| 3 |
+
from langgraph.checkpoint.memory import MemorySaver
|
| 4 |
+
from langgraph.graph.message import add_messages
|
| 5 |
+
from langchain_openai import ChatOpenAI
|
| 6 |
+
from langgraph.graph.state import CompiledStateGraph
|
| 7 |
+
from tools import (
|
| 8 |
+
tool_format_scripture_answer,
|
| 9 |
+
tool_search_db,
|
| 10 |
+
tool_search_web,
|
| 11 |
+
tool_push,
|
| 12 |
+
)
|
| 13 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
| 14 |
+
from langchain_core.messages import SystemMessage, ToolMessage, HumanMessage
|
| 15 |
+
import logging
|
| 16 |
+
|
| 17 |
+
logger = logging.getLogger(__name__)
|
| 18 |
+
logger.setLevel(logging.INFO)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class ChatState(TypedDict):
|
| 22 |
+
messages: Annotated[list[str], add_messages]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def generate_graph() -> CompiledStateGraph:
|
| 26 |
+
memory = MemorySaver()
|
| 27 |
+
tools = [tool_search_web, tool_push, tool_search_db, tool_format_scripture_answer]
|
| 28 |
+
llm = ChatOpenAI(model="gpt-4o-mini").bind_tools(tools)
|
| 29 |
+
|
| 30 |
+
def chatNode(state: ChatState) -> ChatState:
|
| 31 |
+
# logger.info("messages before LLM: %s", str(state["messages"]))
|
| 32 |
+
response = llm.invoke(state["messages"])
|
| 33 |
+
# return {"messages": [response]}
|
| 34 |
+
return {"messages": state["messages"] + [response]}
|
| 35 |
+
|
| 36 |
+
def init_system_prompt_node(state: ChatState) -> ChatState:
|
| 37 |
+
messages = state["messages"] or []
|
| 38 |
+
|
| 39 |
+
# Check if system prompts were already added
|
| 40 |
+
already_has_prompt = any(
|
| 41 |
+
isinstance(m, SystemMessage) and "format_scripture_answer" in m.content
|
| 42 |
+
for m in messages
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
if not already_has_prompt:
|
| 46 |
+
messages += [
|
| 47 |
+
SystemMessage(
|
| 48 |
+
content="⚠️ Do NOT summarize or compress the output from the `query` tool. It will be passed directly to `format_scripture_answer` tool that formats the answer **AS IS**. DO NOT REMOVE SANSKRIT TEXTS"
|
| 49 |
+
),
|
| 50 |
+
SystemMessage(
|
| 51 |
+
content="You MUST call the `format_scripture_answer` tool if the user question is about scripture content and the `query` tool has returned a result."
|
| 52 |
+
),
|
| 53 |
+
SystemMessage(
|
| 54 |
+
content="If the user's question is about any scripture content (even if multiple scriptures), you must use the `tool_search_db`. Only use `tool_search_web` for general non-scriptural questions."
|
| 55 |
+
),
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
return {"messages": messages}
|
| 59 |
+
|
| 60 |
+
# def chatNode(state: ChatState) -> ChatState:
|
| 61 |
+
# messages = state["messages"]
|
| 62 |
+
# system_prompt = None
|
| 63 |
+
# new_messages = []
|
| 64 |
+
|
| 65 |
+
# for m in messages:
|
| 66 |
+
# if isinstance(m, ToolMessage):
|
| 67 |
+
# print("m.name = ", m.name)
|
| 68 |
+
# if m.name == "format_scripture_answer":
|
| 69 |
+
# system_prompt = m.content
|
| 70 |
+
# else:
|
| 71 |
+
# new_messages.append(m)
|
| 72 |
+
|
| 73 |
+
# if system_prompt:
|
| 74 |
+
# full_history = [
|
| 75 |
+
# SystemMessage(content=system_prompt),
|
| 76 |
+
# SystemMessage(
|
| 77 |
+
# content="⚠️ Do NOT summarize or compress the context from the query tool. It will be passed directly to another tool that formats the answer."
|
| 78 |
+
# ),
|
| 79 |
+
# SystemMessage(
|
| 80 |
+
# content="You MUST call the `format_scripture_answer` tool if the user question is about scripture content and the query tool has returned a result."
|
| 81 |
+
# ),
|
| 82 |
+
# ] + new_messages
|
| 83 |
+
# else:
|
| 84 |
+
# full_history = messages
|
| 85 |
+
|
| 86 |
+
# # 🔍 Debug log (optional)
|
| 87 |
+
# # print("\n🧠 LLM Full History:")
|
| 88 |
+
# # for m in full_history:
|
| 89 |
+
# # print(f"- {m.type.upper()}: {m.content[:100]}...\n")
|
| 90 |
+
|
| 91 |
+
# ai_response = llm.invoke(full_history)
|
| 92 |
+
# return {"messages": messages + [ai_response]}
|
| 93 |
+
|
| 94 |
+
graph = StateGraph(ChatState)
|
| 95 |
+
graph.add_node("init", init_system_prompt_node)
|
| 96 |
+
graph.add_node("chat", chatNode)
|
| 97 |
+
graph.add_node("tools", ToolNode(tools))
|
| 98 |
+
graph.add_edge(START, "init")
|
| 99 |
+
graph.add_edge("init", "chat")
|
| 100 |
+
graph.add_conditional_edges("chat", tools_condition, "tools")
|
| 101 |
+
graph.add_edge("tools", "chat")
|
| 102 |
+
|
| 103 |
+
return graph.compile(checkpointer=memory)
|
main.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_openai import ChatOpenAI
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from push_notifications_helper import push
|
| 4 |
+
from langchain.agents import Tool
|
| 5 |
+
|
| 6 |
+
from tools import tool_push, tool_search_web
|
| 7 |
+
|
| 8 |
+
load_dotenv(override=True)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def main():
|
| 12 |
+
print("Hello from sanatan-ai!")
|
| 13 |
+
llm = ChatOpenAI()
|
| 14 |
+
llm_response = llm.invoke("Generate a simple question to ask the user about geography.")
|
| 15 |
+
# print(response)
|
| 16 |
+
response = tool_search_web.invoke(llm_response.content)
|
| 17 |
+
message = "Question: " + llm_response.content + "\nAnswer: " + response
|
| 18 |
+
tool_push.invoke(message)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
if __name__ == "__main__":
|
| 22 |
+
main()
|
push_notifications_helper.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
logger = logging.getLogger()
|
| 7 |
+
logger.setLevel(logging.INFO)
|
| 8 |
+
|
| 9 |
+
load_dotenv(override=True)
|
| 10 |
+
|
| 11 |
+
pushover_token = os.getenv("PUSHOVER_TOKEN")
|
| 12 |
+
pushover_user = os.getenv("PUSHOVER_USER")
|
| 13 |
+
pushover_url = "https://api.pushover.net/1/messages.json"
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def push(text: str):
|
| 17 |
+
"""Send a push notification to the user"""
|
| 18 |
+
logger.info("Sending a push notification for %s", text)
|
| 19 |
+
response = requests.post(
|
| 20 |
+
pushover_url,
|
| 21 |
+
data={"token": pushover_token, "user": pushover_user, "message": text},
|
| 22 |
+
)
|
| 23 |
+
logger.info("response = %s", response.json())
|
| 24 |
+
logger.info("Sent notification")
|
pyproject.toml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "sanatan-ai"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.12"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"chromadb>=1.0.15",
|
| 9 |
+
"dotenv>=0.9.9",
|
| 10 |
+
"google-api-python-client>=2.177.0",
|
| 11 |
+
"google-auth-httplib2>=0.2.0",
|
| 12 |
+
"google-auth-oauthlib>=1.2.2",
|
| 13 |
+
"gradio>=5.39.0",
|
| 14 |
+
"gspread>=6.2.1",
|
| 15 |
+
"langchain>=0.3.27",
|
| 16 |
+
"langchain-community>=0.3.27",
|
| 17 |
+
"langchain-openai>=0.3.28",
|
| 18 |
+
"langgraph>=0.6.2",
|
| 19 |
+
"oauth2client>=4.1.3",
|
| 20 |
+
"sentence-transformers>=5.0.0",
|
| 21 |
+
]
|
requirements.txt
ADDED
|
@@ -0,0 +1,556 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file was autogenerated by uv via the following command:
|
| 2 |
+
# uv pip compile pyproject.toml -o requirements.txt
|
| 3 |
+
aiofiles==24.1.0
|
| 4 |
+
# via gradio
|
| 5 |
+
aiohappyeyeballs==2.6.1
|
| 6 |
+
# via aiohttp
|
| 7 |
+
aiohttp==3.12.15
|
| 8 |
+
# via langchain-community
|
| 9 |
+
aiosignal==1.4.0
|
| 10 |
+
# via aiohttp
|
| 11 |
+
annotated-types==0.7.0
|
| 12 |
+
# via pydantic
|
| 13 |
+
anyio==4.9.0
|
| 14 |
+
# via
|
| 15 |
+
# gradio
|
| 16 |
+
# httpx
|
| 17 |
+
# openai
|
| 18 |
+
# starlette
|
| 19 |
+
# watchfiles
|
| 20 |
+
attrs==25.3.0
|
| 21 |
+
# via
|
| 22 |
+
# aiohttp
|
| 23 |
+
# jsonschema
|
| 24 |
+
# referencing
|
| 25 |
+
backoff==2.2.1
|
| 26 |
+
# via posthog
|
| 27 |
+
bcrypt==4.3.0
|
| 28 |
+
# via chromadb
|
| 29 |
+
brotli==1.1.0
|
| 30 |
+
# via gradio
|
| 31 |
+
build==1.2.2.post1
|
| 32 |
+
# via chromadb
|
| 33 |
+
cachetools==5.5.2
|
| 34 |
+
# via google-auth
|
| 35 |
+
certifi==2025.7.14
|
| 36 |
+
# via
|
| 37 |
+
# httpcore
|
| 38 |
+
# httpx
|
| 39 |
+
# kubernetes
|
| 40 |
+
# requests
|
| 41 |
+
charset-normalizer==3.4.2
|
| 42 |
+
# via requests
|
| 43 |
+
chromadb==1.0.15
|
| 44 |
+
# via sanatan-ai (pyproject.toml)
|
| 45 |
+
click==8.2.1
|
| 46 |
+
# via
|
| 47 |
+
# typer
|
| 48 |
+
# uvicorn
|
| 49 |
+
colorama==0.4.6
|
| 50 |
+
# via
|
| 51 |
+
# build
|
| 52 |
+
# click
|
| 53 |
+
# tqdm
|
| 54 |
+
# uvicorn
|
| 55 |
+
coloredlogs==15.0.1
|
| 56 |
+
# via onnxruntime
|
| 57 |
+
dataclasses-json==0.6.7
|
| 58 |
+
# via langchain-community
|
| 59 |
+
distro==1.9.0
|
| 60 |
+
# via
|
| 61 |
+
# openai
|
| 62 |
+
# posthog
|
| 63 |
+
dotenv==0.9.9
|
| 64 |
+
# via sanatan-ai (pyproject.toml)
|
| 65 |
+
durationpy==0.10
|
| 66 |
+
# via kubernetes
|
| 67 |
+
fastapi==0.116.1
|
| 68 |
+
# via gradio
|
| 69 |
+
ffmpy==0.6.1
|
| 70 |
+
# via gradio
|
| 71 |
+
filelock==3.18.0
|
| 72 |
+
# via
|
| 73 |
+
# huggingface-hub
|
| 74 |
+
# torch
|
| 75 |
+
# transformers
|
| 76 |
+
flatbuffers==25.2.10
|
| 77 |
+
# via onnxruntime
|
| 78 |
+
frozenlist==1.7.0
|
| 79 |
+
# via
|
| 80 |
+
# aiohttp
|
| 81 |
+
# aiosignal
|
| 82 |
+
fsspec==2025.7.0
|
| 83 |
+
# via
|
| 84 |
+
# gradio-client
|
| 85 |
+
# huggingface-hub
|
| 86 |
+
# torch
|
| 87 |
+
google-api-core==2.25.1
|
| 88 |
+
# via google-api-python-client
|
| 89 |
+
google-api-python-client==2.177.0
|
| 90 |
+
# via sanatan-ai (pyproject.toml)
|
| 91 |
+
google-auth==2.40.3
|
| 92 |
+
# via
|
| 93 |
+
# google-api-core
|
| 94 |
+
# google-api-python-client
|
| 95 |
+
# google-auth-httplib2
|
| 96 |
+
# google-auth-oauthlib
|
| 97 |
+
# gspread
|
| 98 |
+
# kubernetes
|
| 99 |
+
google-auth-httplib2==0.2.0
|
| 100 |
+
# via
|
| 101 |
+
# sanatan-ai (pyproject.toml)
|
| 102 |
+
# google-api-python-client
|
| 103 |
+
google-auth-oauthlib==1.2.2
|
| 104 |
+
# via
|
| 105 |
+
# sanatan-ai (pyproject.toml)
|
| 106 |
+
# gspread
|
| 107 |
+
googleapis-common-protos==1.70.0
|
| 108 |
+
# via
|
| 109 |
+
# google-api-core
|
| 110 |
+
# opentelemetry-exporter-otlp-proto-grpc
|
| 111 |
+
gradio==5.39.0
|
| 112 |
+
# via sanatan-ai (pyproject.toml)
|
| 113 |
+
gradio-client==1.11.0
|
| 114 |
+
# via gradio
|
| 115 |
+
greenlet==3.2.3
|
| 116 |
+
# via sqlalchemy
|
| 117 |
+
groovy==0.1.2
|
| 118 |
+
# via gradio
|
| 119 |
+
grpcio==1.74.0
|
| 120 |
+
# via
|
| 121 |
+
# chromadb
|
| 122 |
+
# opentelemetry-exporter-otlp-proto-grpc
|
| 123 |
+
gspread==6.2.1
|
| 124 |
+
# via sanatan-ai (pyproject.toml)
|
| 125 |
+
h11==0.16.0
|
| 126 |
+
# via
|
| 127 |
+
# httpcore
|
| 128 |
+
# uvicorn
|
| 129 |
+
httpcore==1.0.9
|
| 130 |
+
# via httpx
|
| 131 |
+
httplib2==0.22.0
|
| 132 |
+
# via
|
| 133 |
+
# google-api-python-client
|
| 134 |
+
# google-auth-httplib2
|
| 135 |
+
# oauth2client
|
| 136 |
+
httptools==0.6.4
|
| 137 |
+
# via uvicorn
|
| 138 |
+
httpx==0.28.1
|
| 139 |
+
# via
|
| 140 |
+
# chromadb
|
| 141 |
+
# gradio
|
| 142 |
+
# gradio-client
|
| 143 |
+
# langgraph-sdk
|
| 144 |
+
# langsmith
|
| 145 |
+
# openai
|
| 146 |
+
# safehttpx
|
| 147 |
+
httpx-sse==0.4.1
|
| 148 |
+
# via langchain-community
|
| 149 |
+
huggingface-hub==0.34.3
|
| 150 |
+
# via
|
| 151 |
+
# gradio
|
| 152 |
+
# gradio-client
|
| 153 |
+
# sentence-transformers
|
| 154 |
+
# tokenizers
|
| 155 |
+
# transformers
|
| 156 |
+
humanfriendly==10.0
|
| 157 |
+
# via coloredlogs
|
| 158 |
+
idna==3.10
|
| 159 |
+
# via
|
| 160 |
+
# anyio
|
| 161 |
+
# httpx
|
| 162 |
+
# requests
|
| 163 |
+
# yarl
|
| 164 |
+
importlib-metadata==8.7.0
|
| 165 |
+
# via opentelemetry-api
|
| 166 |
+
importlib-resources==6.5.2
|
| 167 |
+
# via chromadb
|
| 168 |
+
jinja2==3.1.6
|
| 169 |
+
# via
|
| 170 |
+
# gradio
|
| 171 |
+
# torch
|
| 172 |
+
jiter==0.10.0
|
| 173 |
+
# via openai
|
| 174 |
+
joblib==1.5.1
|
| 175 |
+
# via scikit-learn
|
| 176 |
+
jsonpatch==1.33
|
| 177 |
+
# via langchain-core
|
| 178 |
+
jsonpointer==3.0.0
|
| 179 |
+
# via jsonpatch
|
| 180 |
+
jsonschema==4.25.0
|
| 181 |
+
# via chromadb
|
| 182 |
+
jsonschema-specifications==2025.4.1
|
| 183 |
+
# via jsonschema
|
| 184 |
+
kubernetes==33.1.0
|
| 185 |
+
# via chromadb
|
| 186 |
+
langchain==0.3.27
|
| 187 |
+
# via
|
| 188 |
+
# sanatan-ai (pyproject.toml)
|
| 189 |
+
# langchain-community
|
| 190 |
+
langchain-community==0.3.27
|
| 191 |
+
# via sanatan-ai (pyproject.toml)
|
| 192 |
+
langchain-core==0.3.72
|
| 193 |
+
# via
|
| 194 |
+
# langchain
|
| 195 |
+
# langchain-community
|
| 196 |
+
# langchain-openai
|
| 197 |
+
# langchain-text-splitters
|
| 198 |
+
# langgraph
|
| 199 |
+
# langgraph-checkpoint
|
| 200 |
+
# langgraph-prebuilt
|
| 201 |
+
langchain-openai==0.3.28
|
| 202 |
+
# via sanatan-ai (pyproject.toml)
|
| 203 |
+
langchain-text-splitters==0.3.9
|
| 204 |
+
# via langchain
|
| 205 |
+
langgraph==0.6.2
|
| 206 |
+
# via sanatan-ai (pyproject.toml)
|
| 207 |
+
langgraph-checkpoint==2.1.1
|
| 208 |
+
# via
|
| 209 |
+
# langgraph
|
| 210 |
+
# langgraph-prebuilt
|
| 211 |
+
langgraph-prebuilt==0.6.2
|
| 212 |
+
# via langgraph
|
| 213 |
+
langgraph-sdk==0.2.0
|
| 214 |
+
# via langgraph
|
| 215 |
+
langsmith==0.4.9
|
| 216 |
+
# via
|
| 217 |
+
# langchain
|
| 218 |
+
# langchain-community
|
| 219 |
+
# langchain-core
|
| 220 |
+
markdown-it-py==3.0.0
|
| 221 |
+
# via rich
|
| 222 |
+
markupsafe==3.0.2
|
| 223 |
+
# via
|
| 224 |
+
# gradio
|
| 225 |
+
# jinja2
|
| 226 |
+
marshmallow==3.26.1
|
| 227 |
+
# via dataclasses-json
|
| 228 |
+
mdurl==0.1.2
|
| 229 |
+
# via markdown-it-py
|
| 230 |
+
mmh3==5.2.0
|
| 231 |
+
# via chromadb
|
| 232 |
+
mpmath==1.3.0
|
| 233 |
+
# via sympy
|
| 234 |
+
multidict==6.6.3
|
| 235 |
+
# via
|
| 236 |
+
# aiohttp
|
| 237 |
+
# yarl
|
| 238 |
+
mypy-extensions==1.1.0
|
| 239 |
+
# via typing-inspect
|
| 240 |
+
networkx==3.5
|
| 241 |
+
# via torch
|
| 242 |
+
numpy==2.3.2
|
| 243 |
+
# via
|
| 244 |
+
# chromadb
|
| 245 |
+
# gradio
|
| 246 |
+
# langchain-community
|
| 247 |
+
# onnxruntime
|
| 248 |
+
# pandas
|
| 249 |
+
# scikit-learn
|
| 250 |
+
# scipy
|
| 251 |
+
# transformers
|
| 252 |
+
oauth2client==4.1.3
|
| 253 |
+
# via sanatan-ai (pyproject.toml)
|
| 254 |
+
oauthlib==3.3.1
|
| 255 |
+
# via
|
| 256 |
+
# kubernetes
|
| 257 |
+
# requests-oauthlib
|
| 258 |
+
onnxruntime==1.22.1
|
| 259 |
+
# via chromadb
|
| 260 |
+
openai==1.98.0
|
| 261 |
+
# via langchain-openai
|
| 262 |
+
opentelemetry-api==1.36.0
|
| 263 |
+
# via
|
| 264 |
+
# chromadb
|
| 265 |
+
# opentelemetry-exporter-otlp-proto-grpc
|
| 266 |
+
# opentelemetry-sdk
|
| 267 |
+
# opentelemetry-semantic-conventions
|
| 268 |
+
opentelemetry-exporter-otlp-proto-common==1.36.0
|
| 269 |
+
# via opentelemetry-exporter-otlp-proto-grpc
|
| 270 |
+
opentelemetry-exporter-otlp-proto-grpc==1.36.0
|
| 271 |
+
# via chromadb
|
| 272 |
+
opentelemetry-proto==1.36.0
|
| 273 |
+
# via
|
| 274 |
+
# opentelemetry-exporter-otlp-proto-common
|
| 275 |
+
# opentelemetry-exporter-otlp-proto-grpc
|
| 276 |
+
opentelemetry-sdk==1.36.0
|
| 277 |
+
# via
|
| 278 |
+
# chromadb
|
| 279 |
+
# opentelemetry-exporter-otlp-proto-grpc
|
| 280 |
+
opentelemetry-semantic-conventions==0.57b0
|
| 281 |
+
# via opentelemetry-sdk
|
| 282 |
+
orjson==3.11.1
|
| 283 |
+
# via
|
| 284 |
+
# chromadb
|
| 285 |
+
# gradio
|
| 286 |
+
# langgraph-sdk
|
| 287 |
+
# langsmith
|
| 288 |
+
ormsgpack==1.10.0
|
| 289 |
+
# via langgraph-checkpoint
|
| 290 |
+
overrides==7.7.0
|
| 291 |
+
# via chromadb
|
| 292 |
+
packaging==25.0
|
| 293 |
+
# via
|
| 294 |
+
# build
|
| 295 |
+
# gradio
|
| 296 |
+
# gradio-client
|
| 297 |
+
# huggingface-hub
|
| 298 |
+
# langchain-core
|
| 299 |
+
# langsmith
|
| 300 |
+
# marshmallow
|
| 301 |
+
# onnxruntime
|
| 302 |
+
# transformers
|
| 303 |
+
pandas==2.3.1
|
| 304 |
+
# via gradio
|
| 305 |
+
pillow==11.3.0
|
| 306 |
+
# via
|
| 307 |
+
# gradio
|
| 308 |
+
# sentence-transformers
|
| 309 |
+
posthog==5.4.0
|
| 310 |
+
# via chromadb
|
| 311 |
+
propcache==0.3.2
|
| 312 |
+
# via
|
| 313 |
+
# aiohttp
|
| 314 |
+
# yarl
|
| 315 |
+
proto-plus==1.26.1
|
| 316 |
+
# via google-api-core
|
| 317 |
+
protobuf==6.31.1
|
| 318 |
+
# via
|
| 319 |
+
# google-api-core
|
| 320 |
+
# googleapis-common-protos
|
| 321 |
+
# onnxruntime
|
| 322 |
+
# opentelemetry-proto
|
| 323 |
+
# proto-plus
|
| 324 |
+
pyasn1==0.6.1
|
| 325 |
+
# via
|
| 326 |
+
# oauth2client
|
| 327 |
+
# pyasn1-modules
|
| 328 |
+
# rsa
|
| 329 |
+
pyasn1-modules==0.4.2
|
| 330 |
+
# via
|
| 331 |
+
# google-auth
|
| 332 |
+
# oauth2client
|
| 333 |
+
pybase64==1.4.2
|
| 334 |
+
# via chromadb
|
| 335 |
+
pydantic==2.11.7
|
| 336 |
+
# via
|
| 337 |
+
# chromadb
|
| 338 |
+
# fastapi
|
| 339 |
+
# gradio
|
| 340 |
+
# langchain
|
| 341 |
+
# langchain-core
|
| 342 |
+
# langgraph
|
| 343 |
+
# langsmith
|
| 344 |
+
# openai
|
| 345 |
+
# pydantic-settings
|
| 346 |
+
pydantic-core==2.33.2
|
| 347 |
+
# via pydantic
|
| 348 |
+
pydantic-settings==2.10.1
|
| 349 |
+
# via langchain-community
|
| 350 |
+
pydub==0.25.1
|
| 351 |
+
# via gradio
|
| 352 |
+
pygments==2.19.2
|
| 353 |
+
# via rich
|
| 354 |
+
pyparsing==3.2.3
|
| 355 |
+
# via httplib2
|
| 356 |
+
pypika==0.48.9
|
| 357 |
+
# via chromadb
|
| 358 |
+
pyproject-hooks==1.2.0
|
| 359 |
+
# via build
|
| 360 |
+
pyreadline3==3.5.4
|
| 361 |
+
# via humanfriendly
|
| 362 |
+
python-dateutil==2.9.0.post0
|
| 363 |
+
# via
|
| 364 |
+
# kubernetes
|
| 365 |
+
# pandas
|
| 366 |
+
# posthog
|
| 367 |
+
python-dotenv==1.1.1
|
| 368 |
+
# via
|
| 369 |
+
# dotenv
|
| 370 |
+
# pydantic-settings
|
| 371 |
+
# uvicorn
|
| 372 |
+
python-multipart==0.0.20
|
| 373 |
+
# via gradio
|
| 374 |
+
pytz==2025.2
|
| 375 |
+
# via pandas
|
| 376 |
+
pyyaml==6.0.2
|
| 377 |
+
# via
|
| 378 |
+
# chromadb
|
| 379 |
+
# gradio
|
| 380 |
+
# huggingface-hub
|
| 381 |
+
# kubernetes
|
| 382 |
+
# langchain
|
| 383 |
+
# langchain-community
|
| 384 |
+
# langchain-core
|
| 385 |
+
# transformers
|
| 386 |
+
# uvicorn
|
| 387 |
+
referencing==0.36.2
|
| 388 |
+
# via
|
| 389 |
+
# jsonschema
|
| 390 |
+
# jsonschema-specifications
|
| 391 |
+
regex==2025.7.34
|
| 392 |
+
# via
|
| 393 |
+
# tiktoken
|
| 394 |
+
# transformers
|
| 395 |
+
requests==2.32.4
|
| 396 |
+
# via
|
| 397 |
+
# google-api-core
|
| 398 |
+
# huggingface-hub
|
| 399 |
+
# kubernetes
|
| 400 |
+
# langchain
|
| 401 |
+
# langchain-community
|
| 402 |
+
# langsmith
|
| 403 |
+
# posthog
|
| 404 |
+
# requests-oauthlib
|
| 405 |
+
# requests-toolbelt
|
| 406 |
+
# tiktoken
|
| 407 |
+
# transformers
|
| 408 |
+
requests-oauthlib==2.0.0
|
| 409 |
+
# via
|
| 410 |
+
# google-auth-oauthlib
|
| 411 |
+
# kubernetes
|
| 412 |
+
requests-toolbelt==1.0.0
|
| 413 |
+
# via langsmith
|
| 414 |
+
rich==14.1.0
|
| 415 |
+
# via
|
| 416 |
+
# chromadb
|
| 417 |
+
# typer
|
| 418 |
+
rpds-py==0.26.0
|
| 419 |
+
# via
|
| 420 |
+
# jsonschema
|
| 421 |
+
# referencing
|
| 422 |
+
rsa==4.9.1
|
| 423 |
+
# via
|
| 424 |
+
# google-auth
|
| 425 |
+
# oauth2client
|
| 426 |
+
ruff==0.12.7
|
| 427 |
+
# via gradio
|
| 428 |
+
safehttpx==0.1.6
|
| 429 |
+
# via gradio
|
| 430 |
+
safetensors==0.5.3
|
| 431 |
+
# via transformers
|
| 432 |
+
scikit-learn==1.7.1
|
| 433 |
+
# via sentence-transformers
|
| 434 |
+
scipy==1.16.1
|
| 435 |
+
# via
|
| 436 |
+
# scikit-learn
|
| 437 |
+
# sentence-transformers
|
| 438 |
+
semantic-version==2.10.0
|
| 439 |
+
# via gradio
|
| 440 |
+
sentence-transformers==5.0.0
|
| 441 |
+
# via sanatan-ai (pyproject.toml)
|
| 442 |
+
setuptools==80.9.0
|
| 443 |
+
# via torch
|
| 444 |
+
shellingham==1.5.4
|
| 445 |
+
# via typer
|
| 446 |
+
six==1.17.0
|
| 447 |
+
# via
|
| 448 |
+
# kubernetes
|
| 449 |
+
# oauth2client
|
| 450 |
+
# posthog
|
| 451 |
+
# python-dateutil
|
| 452 |
+
sniffio==1.3.1
|
| 453 |
+
# via
|
| 454 |
+
# anyio
|
| 455 |
+
# openai
|
| 456 |
+
sqlalchemy==2.0.42
|
| 457 |
+
# via
|
| 458 |
+
# langchain
|
| 459 |
+
# langchain-community
|
| 460 |
+
starlette==0.47.2
|
| 461 |
+
# via
|
| 462 |
+
# fastapi
|
| 463 |
+
# gradio
|
| 464 |
+
sympy==1.14.0
|
| 465 |
+
# via
|
| 466 |
+
# onnxruntime
|
| 467 |
+
# torch
|
| 468 |
+
tenacity==9.1.2
|
| 469 |
+
# via
|
| 470 |
+
# chromadb
|
| 471 |
+
# langchain-community
|
| 472 |
+
# langchain-core
|
| 473 |
+
threadpoolctl==3.6.0
|
| 474 |
+
# via scikit-learn
|
| 475 |
+
tiktoken==0.9.0
|
| 476 |
+
# via langchain-openai
|
| 477 |
+
tokenizers==0.21.4
|
| 478 |
+
# via
|
| 479 |
+
# chromadb
|
| 480 |
+
# transformers
|
| 481 |
+
tomlkit==0.13.3
|
| 482 |
+
# via gradio
|
| 483 |
+
torch==2.7.1
|
| 484 |
+
# via sentence-transformers
|
| 485 |
+
tqdm==4.67.1
|
| 486 |
+
# via
|
| 487 |
+
# chromadb
|
| 488 |
+
# huggingface-hub
|
| 489 |
+
# openai
|
| 490 |
+
# sentence-transformers
|
| 491 |
+
# transformers
|
| 492 |
+
transformers==4.54.1
|
| 493 |
+
# via sentence-transformers
|
| 494 |
+
typer==0.16.0
|
| 495 |
+
# via
|
| 496 |
+
# chromadb
|
| 497 |
+
# gradio
|
| 498 |
+
typing-extensions==4.14.1
|
| 499 |
+
# via
|
| 500 |
+
# aiosignal
|
| 501 |
+
# anyio
|
| 502 |
+
# chromadb
|
| 503 |
+
# fastapi
|
| 504 |
+
# gradio
|
| 505 |
+
# gradio-client
|
| 506 |
+
# huggingface-hub
|
| 507 |
+
# langchain-core
|
| 508 |
+
# openai
|
| 509 |
+
# opentelemetry-api
|
| 510 |
+
# opentelemetry-exporter-otlp-proto-grpc
|
| 511 |
+
# opentelemetry-sdk
|
| 512 |
+
# opentelemetry-semantic-conventions
|
| 513 |
+
# pydantic
|
| 514 |
+
# pydantic-core
|
| 515 |
+
# referencing
|
| 516 |
+
# sentence-transformers
|
| 517 |
+
# sqlalchemy
|
| 518 |
+
# starlette
|
| 519 |
+
# torch
|
| 520 |
+
# typer
|
| 521 |
+
# typing-inspect
|
| 522 |
+
# typing-inspection
|
| 523 |
+
typing-inspect==0.9.0
|
| 524 |
+
# via dataclasses-json
|
| 525 |
+
typing-inspection==0.4.1
|
| 526 |
+
# via
|
| 527 |
+
# pydantic
|
| 528 |
+
# pydantic-settings
|
| 529 |
+
tzdata==2025.2
|
| 530 |
+
# via pandas
|
| 531 |
+
uritemplate==4.2.0
|
| 532 |
+
# via google-api-python-client
|
| 533 |
+
urllib3==2.5.0
|
| 534 |
+
# via
|
| 535 |
+
# kubernetes
|
| 536 |
+
# requests
|
| 537 |
+
uvicorn==0.35.0
|
| 538 |
+
# via
|
| 539 |
+
# chromadb
|
| 540 |
+
# gradio
|
| 541 |
+
watchfiles==1.1.0
|
| 542 |
+
# via uvicorn
|
| 543 |
+
websocket-client==1.8.0
|
| 544 |
+
# via kubernetes
|
| 545 |
+
websockets==15.0.1
|
| 546 |
+
# via
|
| 547 |
+
# gradio-client
|
| 548 |
+
# uvicorn
|
| 549 |
+
xxhash==3.5.0
|
| 550 |
+
# via langgraph
|
| 551 |
+
yarl==1.20.1
|
| 552 |
+
# via aiohttp
|
| 553 |
+
zipp==3.23.0
|
| 554 |
+
# via importlib-metadata
|
| 555 |
+
zstandard==0.23.0
|
| 556 |
+
# via langsmith
|
sanatan_assistant.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from typing import Literal
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from config import SanatanConfig
|
| 5 |
+
from db import SanatanDatabase
|
| 6 |
+
|
| 7 |
+
load_dotenv(override=True)
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
logger.setLevel(logging.INFO)
|
| 10 |
+
|
| 11 |
+
sanatanDatabase = SanatanDatabase()
|
| 12 |
+
allowedCollections = Literal[
|
| 13 |
+
*[scripture["collection_name"] for scripture in SanatanConfig.scriptures]
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def format_scripture_answer(scripture_title: allowedCollections, question: str, query_tool_output: str):
|
| 18 |
+
"""
|
| 19 |
+
Use this tool to generate a custom system prompt based on the scripture title, question, and query_tool_output.
|
| 20 |
+
|
| 21 |
+
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.
|
| 22 |
+
|
| 23 |
+
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.
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
prompt = f"""You are a knowledgeable assistant on the scripture *{scripture_title}*, well-versed in both **Sanskrit** and **English**.
|
| 27 |
+
|
| 28 |
+
You must answer the question using **only** the content from *{scripture_title}* provided in the context below.
|
| 29 |
+
- Do **not** bring in information from **any other scripture or source**, or from prior knowledge, even if the answer seems obvious or well-known.
|
| 30 |
+
- Do **not** quote any Sanskrit verses unless they appear **explicitly** in the provided context.
|
| 31 |
+
- Do **not** use verse numbers or line references unless clearly mentioned in the context.
|
| 32 |
+
- If the answer cannot be found in the context, clearly say:
|
| 33 |
+
**"I do not have enough information from the {scripture_title} to answer this."**
|
| 34 |
+
|
| 35 |
+
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**.
|
| 36 |
+
|
| 37 |
+
If the user query is not small talk, use the following response format (in Markdown):
|
| 38 |
+
|
| 39 |
+
### 🧾 Answer
|
| 40 |
+
- Present the explanation in clear, concise **English**.
|
| 41 |
+
- If it is an interpretation, say so explicitly.
|
| 42 |
+
|
| 43 |
+
### 🔍 Scripture
|
| 44 |
+
- Mention the scripture from which the references were taken.
|
| 45 |
+
|
| 46 |
+
### 🔍 Chapter
|
| 47 |
+
- Mention the chapter(s) from which the references were taken.
|
| 48 |
+
- Only mention chapters if they are explicitly part of the context.
|
| 49 |
+
|
| 50 |
+
### 📜 Supporting Sanskrit Verse(s)
|
| 51 |
+
- Quote **only the most relevant** Sanskrit verse(s) from the context.
|
| 52 |
+
- Do **not** add verses from memory or outside the context.
|
| 53 |
+
- Format each verse clearly, one per line.
|
| 54 |
+
- **Avoid transliteration unless no Devanagari is available.**
|
| 55 |
+
- Do **not** provide English text in this section.
|
| 56 |
+
|
| 57 |
+
### 🔍 English Translation
|
| 58 |
+
- Provide the **corresponding English meaning** for each Sanskrit verse shown.
|
| 59 |
+
- Keep it readable and aligned with the verse above.
|
| 60 |
+
|
| 61 |
+
### Notes
|
| 62 |
+
- Bullet any extra points or cross-references from explanatory notes **only if present in the context**.
|
| 63 |
+
- Do **not** include anything that is not supported or implied in the context.
|
| 64 |
+
|
| 65 |
+
**Question:**
|
| 66 |
+
{question}
|
| 67 |
+
|
| 68 |
+
---
|
| 69 |
+
|
| 70 |
+
**Context:**
|
| 71 |
+
{query_tool_output}
|
| 72 |
+
|
| 73 |
+
---
|
| 74 |
+
|
| 75 |
+
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.
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
return prompt
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def query(collection_name: allowedCollections, query: str, n_results=5):
|
| 83 |
+
"""
|
| 84 |
+
Search a scripture collection.
|
| 85 |
+
|
| 86 |
+
Parameters:
|
| 87 |
+
- collection_name (str): The name of the scripture collection to search. ...
|
| 88 |
+
- query (str): The search query.
|
| 89 |
+
- n_results (int): Number of results to return. Default is 5.
|
| 90 |
+
|
| 91 |
+
Returns:
|
| 92 |
+
- A list of matching results.
|
| 93 |
+
"""
|
| 94 |
+
logger.info("Searching collection [%s] for [%s]", collection_name, query)
|
| 95 |
+
response = sanatanDatabase.search(
|
| 96 |
+
collection_name=collection_name, query=query, n_results=n_results
|
| 97 |
+
)
|
| 98 |
+
return "\n\n".join(response["documents"][0])
|
serperdev_helper.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.utilities import GoogleSerperAPIWrapper
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import logging
|
| 4 |
+
|
| 5 |
+
load_dotenv(override=True)
|
| 6 |
+
logger = logging.getLogger(__name__)
|
| 7 |
+
logger.setLevel(logging.INFO)
|
| 8 |
+
|
| 9 |
+
serper = GoogleSerperAPIWrapper()
|
| 10 |
+
|
| 11 |
+
def search(query: str):
|
| 12 |
+
"""Search the web for any given query"""
|
| 13 |
+
logger.info("Searching the web for %s", query)
|
| 14 |
+
return serper.run(query)
|
tools.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.agents import Tool
|
| 2 |
+
from langchain_core.tools import StructuredTool
|
| 3 |
+
|
| 4 |
+
from config import SanatanConfig
|
| 5 |
+
from push_notifications_helper import push
|
| 6 |
+
from serperdev_helper import search as search_web
|
| 7 |
+
from sanatan_assistant import format_scripture_answer, query
|
| 8 |
+
|
| 9 |
+
tool_push = Tool(
|
| 10 |
+
name="push", description="Send a push notification to the user", func=push
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
allowed_collections = [s["collection_name"] for s in SanatanConfig.scriptures]
|
| 14 |
+
|
| 15 |
+
tool_search_db = StructuredTool.from_function(
|
| 16 |
+
query,
|
| 17 |
+
description=(
|
| 18 |
+
"Search within a specific scripture collection. "
|
| 19 |
+
f"The collection_name must be one of: {', '.join(allowed_collections)}."
|
| 20 |
+
" Use this to find relevant scripture verses or explanations."
|
| 21 |
+
),
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
tool_search_web = Tool(
|
| 25 |
+
name="search_web", description="Search the web for information", func=search_web
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
tool_format_scripture_answer = StructuredTool.from_function(format_scripture_answer)
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|