Spaces:
Sleeping
Sleeping
sampathlonka
commited on
Commit
•
1e534f0
1
Parent(s):
61424c8
app files
Browse files- src/FunctionTools.py +249 -0
- src/app.py +149 -0
src/FunctionTools.py
ADDED
@@ -0,0 +1,249 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import chardet
|
3 |
+
import streamlit as st
|
4 |
+
import pandas as pd
|
5 |
+
from llama_index.core.tools.tool_spec.base import BaseToolSpec
|
6 |
+
|
7 |
+
class ScriptureDescriptionToolSpec(BaseToolSpec):
|
8 |
+
'''
|
9 |
+
Purpose: Obtains the description or summary about vedas, mandalas, kandas, shuktas, archakah, adhyaya, and other scriptural elements.
|
10 |
+
Returns: A dictionary containing the description or basic information about the specified scriptural element.
|
11 |
+
Sample query:
|
12 |
+
1. Describe the first kandah, second shukta from Atharvaveda?
|
13 |
+
2. Summarize ShuklaYajurVeda?
|
14 |
+
3. What is the difference between ShuklaYajurVeda and KrishnaYajurVeda?
|
15 |
+
'''
|
16 |
+
# Define the functions that we export to the LLM
|
17 |
+
spec_functions = ["get_description"]
|
18 |
+
|
19 |
+
with open("Data/scripture_descriptions.csv", 'rb') as f:
|
20 |
+
result = chardet.detect(f.read())
|
21 |
+
|
22 |
+
encoding = result['encoding']
|
23 |
+
df = pd.read_csv("Data/scripture_descriptions.csv", encoding=encoding)
|
24 |
+
|
25 |
+
@st.cache_data
|
26 |
+
def get_description(_self, level_0, level_1:int=None, level_2:int=None, level_3:int=None):
|
27 |
+
"""
|
28 |
+
To get the description or basic information about vedas/mandalas/kandas/shukatas/archakah/adhyaya and others.
|
29 |
+
"""
|
30 |
+
try:
|
31 |
+
if level_3 is not None:
|
32 |
+
# Case with Level-2 specified
|
33 |
+
result = _self.df[(_self.df['scripture_name'].str.lower() == level_0.lower())
|
34 |
+
& (_self.df['level_1'] == str(level_1))
|
35 |
+
& (_self.df['level_2'] == str(level_2)) & (_self.df['level_3'] == str(level_3))]
|
36 |
+
elif level_2 is not None:
|
37 |
+
# Case with Level-2 specified
|
38 |
+
result = _self.df[(_self.df['scripture_name'].str.lower() == level_0.lower())
|
39 |
+
& (_self.df['level_1'] == str(level_1)) & (_self.df['level_2'] == str(level_2))]
|
40 |
+
elif level_1 is not None:
|
41 |
+
# Case with Level-1 specified
|
42 |
+
result = _self.df[(_self.df['scripture_name'].str.lower() == level_0.lower())
|
43 |
+
& (_self.df['level_1'] == str(level_1))]
|
44 |
+
else:
|
45 |
+
# Case with only Level-0 specified
|
46 |
+
result = _self.df[_self.df['scripture_name'].str.lower() == level_0.lower()]
|
47 |
+
|
48 |
+
return result.iloc[0].to_dict()
|
49 |
+
except IndexError as e:
|
50 |
+
return json.dumps({"error": f"Failed to get scripture description. {e}"})
|
51 |
+
|
52 |
+
class MantraToolSpec(BaseToolSpec):
|
53 |
+
'''
|
54 |
+
To obtain translations or meaning of vedamantras from RigVeda and AtharvaVeda using the function `get_translation`.
|
55 |
+
The mantra details such as vedamantra, padapatha, rishi, chandah, devata, and swarah from the vedas accessible through the function `get_vedamantra_details`.
|
56 |
+
The mantra summary like anvaya, adhibautic, ahyatmic, adhidaivic meaning of vedamantra accessible using the function 'get_vedamantra_summary'
|
57 |
+
Sample Query:
|
58 |
+
1. What is the vedamantra of the mantra from Rigveda, first mandala, first shukta, and first mantra?
|
59 |
+
2. What is the devata of the vedamantra from Rigveda, first mandala, first shukta, and first mantra?
|
60 |
+
3. What is the meaning of the vedamantra from Rigveda, first mandala, first shukta, and first mantra written by Tulsi Ram?
|
61 |
+
4. What is the (adhibautic) meaning of the vedamantra from RigVeda, first mandala, first shukta, and first mantra?
|
62 |
+
'''
|
63 |
+
spec_functions = ["get_translation", "get_vedamantra_details", "get_vedamantra_summary"]
|
64 |
+
|
65 |
+
TRANSLATION_CSV_PATH = 'Data/trans_Rig_Ath_index_v2.csv'
|
66 |
+
VEDAMANTRA_CSV_PATH = "Data/veda_content_modified_v3.csv"
|
67 |
+
|
68 |
+
def __init__(self):
|
69 |
+
super().__init__()
|
70 |
+
self.df_translation = pd.read_csv(self.TRANSLATION_CSV_PATH, encoding='utf-8')
|
71 |
+
self.df_vedamantra = pd.read_csv(self.VEDAMANTRA_CSV_PATH, encoding='utf-8')
|
72 |
+
|
73 |
+
@st.cache_data
|
74 |
+
def get_translation(_self, mantraid=None, scripture_name=None, MahatmaName=None, KandahNumber=None,
|
75 |
+
MandalaNumber=None, ArchikahNumber=None, ShuktaNumber=None,
|
76 |
+
AnvayaNumber=None, PrapatakNumber=None, MantraNumber=None,
|
77 |
+
AnuvakNumber=None, AdhyayaNumber=None):
|
78 |
+
"""
|
79 |
+
Get the translation of mantras from RigVeda and AtharvaVeda.
|
80 |
+
Sample Query:
|
81 |
+
1. What is the translation of Tulsi Ram of the vedamantra from Rigveda, first mandala, first shukta, and first mantra?
|
82 |
+
2. What is the translation or adhibautic meaning of the vedamantra from RigVeda, first mandala, first shukta, and first mantra?
|
83 |
+
3. What is the subject of the mantra 1.1.84.1?
|
84 |
+
"""
|
85 |
+
try:
|
86 |
+
if mantraid is None:
|
87 |
+
scripture_name_lower = scripture_name.lower() if scripture_name is not None else False
|
88 |
+
if scripture_name_lower == 'rigveda':
|
89 |
+
details = _self.df_translation[
|
90 |
+
(_self.df_translation['scripture_name'].str.lower() == scripture_name_lower)
|
91 |
+
& (_self.df_translation['MandalaNumber'] == MandalaNumber)
|
92 |
+
& (_self.df_translation['ShuktaNumber'] == int(ShuktaNumber))
|
93 |
+
& (_self.df_translation['MantraNumber'] == int(MantraNumber))
|
94 |
+
].to_dict(orient='records')
|
95 |
+
elif scripture_name_lower == 'atharvaveda':
|
96 |
+
details = _self.df_translation[
|
97 |
+
(_self.df_translation['scripture_name'].str.lower() == scripture_name_lower)
|
98 |
+
& (_self.df_translation['KandahNumber'] == KandahNumber)
|
99 |
+
& (_self.df_translation['ShuktaNumber'] == ShuktaNumber)
|
100 |
+
& (_self.df_translation['MantraNumber'] == MantraNumber)].to_dict(orient='records')
|
101 |
+
elif scripture_name_lower == 'samaveda':
|
102 |
+
details = _self.df_translation[
|
103 |
+
(_self.df_translation['scripture_name'].str.lower() == scripture_name_lower)
|
104 |
+
& (_self.df_translation['ArchikahNumber'] == ArchikahNumber)
|
105 |
+
& (_self.df_translation['ShuktaNumber'] == ShuktaNumber)
|
106 |
+
& (_self.df_translation['MantraNumber'] == MantraNumber)].to_dict(orient='records')
|
107 |
+
elif scripture_name_lower == 'krishnayajurveda':
|
108 |
+
details = _self.df_translation[
|
109 |
+
(_self.df_translation['scripture_name'].str.lower() == scripture_name_lower)
|
110 |
+
& (_self.df_translation['PrapatakNumber'] == PrapatakNumber)
|
111 |
+
& (_self.df_translation['AnuvakNumber'] == AnuvakNumber)
|
112 |
+
& (_self.df_translation['MantraNumber'] == MantraNumber)].to_dict(orient='records')
|
113 |
+
else:
|
114 |
+
details = _self.df_translation[
|
115 |
+
(_self.df_translation['scripture_name'].str.lower() == scripture_name_lower)
|
116 |
+
& (_self.df_translation['AdhyayaNumber'] == AdhyayaNumber)
|
117 |
+
& (_self.df_translation['MantraNumber'] == MantraNumber)
|
118 |
+
].to_dict(orient='records')
|
119 |
+
else:
|
120 |
+
details = _self.df_translation[_self.df_translation['mantra_id'] == mantraid].to_dict(orient='records')
|
121 |
+
|
122 |
+
if MahatmaName is not None:
|
123 |
+
for item in details:
|
124 |
+
if item['MahatmaName'] == MahatmaName:
|
125 |
+
return item
|
126 |
+
else:
|
127 |
+
return details
|
128 |
+
except Exception as e:
|
129 |
+
return json.dumps({"error": f"Failed to get translation. {e}"})
|
130 |
+
|
131 |
+
@st.cache_data
|
132 |
+
def get_vedamantra_details(_self, mantraid=None, scripture_name=None, KandahNumber=None,
|
133 |
+
MandalaNumber=None, ArchikahNumber=None, ShuktaNumber=None,
|
134 |
+
AnvayaNumber=None, PrapatakNumber=None, MantraNumber=None,
|
135 |
+
AnuvakNumber=None, AdhyayaNumber=None):
|
136 |
+
"""
|
137 |
+
To obtain the vedamantra details such as vedamantra, padapata, devata, rishi, swarah, and chandah.
|
138 |
+
1. What is the vedamantra of the mantra from Rigveda, first mandala, first shukta, and first mantra?
|
139 |
+
2. What is the devata of the vedamantra from Rigveda, first mandala, first shukta, and first mantra?
|
140 |
+
"""
|
141 |
+
try:
|
142 |
+
if mantraid is None:
|
143 |
+
scripture_name_lower = scripture_name.lower() if scripture_name is not None else False
|
144 |
+
|
145 |
+
if scripture_name_lower == 'rigveda':
|
146 |
+
conditions = (_self.df_vedamantra['scripture_name'].str.lower() == scripture_name_lower) & \
|
147 |
+
(_self.df_vedamantra['MandalaNumber'] == MandalaNumber) & \
|
148 |
+
(_self.df_vedamantra['ShuktaNumber'] == ShuktaNumber) & \
|
149 |
+
(_self.df_vedamantra['MantraNumber'] == str(MantraNumber))
|
150 |
+
details = _self.df_vedamantra[conditions]['mantra_json'].values
|
151 |
+
vedamantra_details = json.loads(details[0])['mantraHeader']['language'][1]['mandala']['shukta']['mantra']
|
152 |
+
|
153 |
+
elif scripture_name_lower == 'atharvaveda':
|
154 |
+
conditions = (_self.df_vedamantra['scripture_name'].str.lower() == scripture_name_lower) & \
|
155 |
+
(_self.df_vedamantra['KandahNumber'] == KandahNumber) & \
|
156 |
+
(_self.df_vedamantra['ShuktaNumber'] == ShuktaNumber) & \
|
157 |
+
(_self.df_vedamantra['MantraNumber'] == str(MantraNumber))
|
158 |
+
details = _self.df_vedamantra[conditions]['mantra_json'].values
|
159 |
+
vedamantra_details = json.loads(details[0])['mantraHeader']['language'][1]['kandah']['shukta']['mantra']
|
160 |
+
elif scripture_name_lower == 'samaveda':
|
161 |
+
conditions = (_self.df_vedamantra['scripture_name'].str.lower() == scripture_name_lower) & \
|
162 |
+
(_self.df_vedamantra['ArchikahNumber'] == ArchikahNumber) & \
|
163 |
+
(_self.df_vedamantra['ShuktaNumber'] == ShuktaNumber) & \
|
164 |
+
(_self.df_vedamantra['MantraNumber'] == str(MantraNumber))
|
165 |
+
details = _self.df_vedamantra[conditions]['mantra_json'].values
|
166 |
+
vedamantra_details = json.loads(details[0])['mantraHeader']['language'][1]['archikah']
|
167 |
+
elif scripture_name_lower == 'krishnayajurveda':
|
168 |
+
conditions = (_self.df_vedamantra['scripture_name'].str.lower() == scripture_name_lower) & \
|
169 |
+
(_self.df_vedamantra['PrapatakNumber'] == PrapatakNumber) & \
|
170 |
+
(_self.df_vedamantra['AnuvakNumber'] == AnuvakNumber) & \
|
171 |
+
(_self.df_vedamantra['MantraNumber'] == str(MantraNumber))
|
172 |
+
details = _self.df_vedamantra[conditions]['mantra_json'].values
|
173 |
+
vedamantra_details = json.loads(details[0])['mantraHeader']['language'][1]['kandah']['prapatak']['anuvak']
|
174 |
+
else:
|
175 |
+
conditions = (_self.df_vedamantra['scripture_name'].str.lower() == scripture_name_lower) & \
|
176 |
+
(_self.df_vedamantra['AdhyayaNumber'] == AdhyayaNumber) & \
|
177 |
+
(_self.df_vedamantra['MantraNumber'] == str(MantraNumber))
|
178 |
+
details = _self.df_vedamantra[conditions]['mantra_json'].values
|
179 |
+
vedamantra_details = json.loads(details[0])['mantraHeader']['language'][1]['adhyaya']['mantra']
|
180 |
+
|
181 |
+
else:
|
182 |
+
# Handle case when mantraid is provided
|
183 |
+
details = _self.df_vedamantra[_self.df_vedamantra['mantra_number'] == mantraid]['mantra_json'].values
|
184 |
+
vedamantra_details = json.loads(details[0])['mantraHeader']['language'][1]
|
185 |
+
|
186 |
+
return vedamantra_details
|
187 |
+
except Exception as e:
|
188 |
+
return json.dumps({"error": f"Failed to get vedamantra details. {str(e)}"})
|
189 |
+
|
190 |
+
@st.cache_data
|
191 |
+
def get_vedamantra_summary(_self, mantraid=None, scripture_name=None, KandahNumber=None,
|
192 |
+
MandalaNumber=None, ArchikahNumber=None, ShuktaNumber=None,
|
193 |
+
AnvayaNumber=None, PrapatakNumber=None, MantraNumber=None,
|
194 |
+
AnuvakNumber=None, AdhyayaNumber=None):
|
195 |
+
'''
|
196 |
+
To obtain the vedamantra summary like anvaya, translation, adhibautic, adhyatmic, adhidaivic meaning of the mantra.
|
197 |
+
1. What is the adhibautic meaning of the mantra from AtharvaVeda, first kandah, first shukta, and first mantra?
|
198 |
+
2. What is the anvaya of the vedamantra from Rigveda, first mandala, first shukta, and first mantra?
|
199 |
+
'''
|
200 |
+
try:
|
201 |
+
if mantraid is None:
|
202 |
+
scripture_name_lower = scripture_name.lower() if scripture_name is not None else False
|
203 |
+
if scripture_name_lower == 'rigveda':
|
204 |
+
details = _self.df_vedamantra[
|
205 |
+
(_self.df_vedamantra['scripture_name'].str.lower() == scripture_name_lower)
|
206 |
+
& (_self.df_vedamantra['MandalaNumber'] == MandalaNumber)
|
207 |
+
& (_self.df_vedamantra['ShuktaNumber'] == ShuktaNumber)
|
208 |
+
& (_self.df_vedamantra['MantraNumber'] == str(MantraNumber))
|
209 |
+
]['mantra_json'].values
|
210 |
+
elif scripture_name_lower == 'atharvaveda':
|
211 |
+
details = _self.df_vedamantra[
|
212 |
+
(_self.df_vedamantra['scripture_name'].str.lower() == scripture_name_lower)
|
213 |
+
& (_self.df_vedamantra['KandahNumber'] == KandahNumber)
|
214 |
+
& (_self.df_vedamantra['ShuktaNumber'] == ShuktaNumber)
|
215 |
+
& (_self.df_vedamantra['MantraNumber'] == str(MantraNumber))
|
216 |
+
]['mantra_json'].values
|
217 |
+
elif scripture_name_lower == 'samaveda':
|
218 |
+
details = _self.df_vedamantra[
|
219 |
+
(_self.df_vedamantra['scripture_name'].str.lower() == scripture_name_lower)
|
220 |
+
& (_self.df_vedamantra['ArchikahNumber'] == ArchikahNumber)
|
221 |
+
& (_self.df_vedamantra['ShuktaNumber'] == ShuktaNumber)
|
222 |
+
& (_self.df_vedamantra['MantraNumber'] == str(MantraNumber))
|
223 |
+
]['mantra_json'].values
|
224 |
+
elif scripture_name_lower == 'krishnayajurveda':
|
225 |
+
details = _self.df_vedamantra[
|
226 |
+
(_self.df_vedamantra['scripture_name'].str.lower() == scripture_name_lower)
|
227 |
+
& (_self.df_vedamantra['PrapatakNumber'] == PrapatakNumber)
|
228 |
+
& (_self.df_vedamantra['AnuvakNumber'] == AnuvakNumber)
|
229 |
+
& (_self.df_vedamantra['MantraNumber'] == str(MantraNumber))
|
230 |
+
]['mantra_json'].values
|
231 |
+
else:
|
232 |
+
details = _self.df_vedamantra[
|
233 |
+
(_self.df_vedamantra['scripture_name'].str.lower() == scripture_name_lower)
|
234 |
+
& (_self.df_vedamantra['AdhyayaNumber'] == AdhyayaNumber)
|
235 |
+
& (_self.df_vedamantra['MantraNumber'] == str(MantraNumber))
|
236 |
+
]['mantra_json'].values
|
237 |
+
else:
|
238 |
+
details = _self.df_vedamantra[_self.df_vedamantra['mantra_number'] == mantraid]['mantra_json'].values
|
239 |
+
|
240 |
+
jsonDict = json.loads(details[0])
|
241 |
+
mantraSummary = jsonDict['mantraSummary']['language']
|
242 |
+
mantraSummary_IAST = jsonDict['mantraSummary']['language'][1]
|
243 |
+
vedamantra_summary = {"Roman-IAST summary of vedamantra": mantraSummary_IAST}
|
244 |
+
for item in mantraSummary:
|
245 |
+
if item['languageName'] == 'English':
|
246 |
+
vedamantra_summary.update({"English summary of vedamantra": item})
|
247 |
+
return vedamantra_summary
|
248 |
+
except Exception as e:
|
249 |
+
return json.dumps({"error": f"Failed to get vedamantra summary. {e}"})
|
src/app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
st.set_page_config(
|
3 |
+
page_title="SVARUPA AI",
|
4 |
+
layout="centered", # or "wide"
|
5 |
+
initial_sidebar_state="auto" # or "expanded" or "collapsed"
|
6 |
+
)
|
7 |
+
from llama_index.core import VectorStoreIndex, StorageContext, Document
|
8 |
+
from llama_index.llms.openai import OpenAI
|
9 |
+
import os
|
10 |
+
import pandas as pd
|
11 |
+
from llama_index.core import Settings
|
12 |
+
from llama_index.vector_stores.pinecone import PineconeVectorStore
|
13 |
+
import pinecone
|
14 |
+
from pinecone import Pinecone, PodSpec
|
15 |
+
from llama_index.core.query_engine import PandasQueryEngine
|
16 |
+
from llama_index.core.agent import ReActAgent
|
17 |
+
from llama_index.core.memory import ChatMemoryBuffer
|
18 |
+
from sentence_transformers import SentenceTransformer
|
19 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
20 |
+
#from llama_index.indices.postprocessor import SimilarityPostprocessor
|
21 |
+
#from llama_index.postprocessor import SentenceTransformerRerank
|
22 |
+
import tiktoken
|
23 |
+
from llama_index.core.callbacks import CallbackManager, TokenCountingHandler
|
24 |
+
from llama_index.core.tools import QueryEngineTool, ToolMetadata
|
25 |
+
from FunctionTools import ScriptureDescriptionToolSpec, MantraToolSpec
|
26 |
+
|
27 |
+
|
28 |
+
#load keys
|
29 |
+
openai_api_key = st.secrets["OPENAI_APIKEY_CS"]
|
30 |
+
pinecone_api_key = st.secrets["PINECONE_API_KEY_SAM"]
|
31 |
+
|
32 |
+
#llm
|
33 |
+
llm_AI4 = OpenAI(temperature=0, model="gpt-4-1106-preview",api_key=openai_api_key, max_tokens=512)
|
34 |
+
token_counter = TokenCountingHandler(
|
35 |
+
tokenizer=tiktoken.encoding_for_model("gpt-4-1106-preview").encode
|
36 |
+
)
|
37 |
+
|
38 |
+
# global settings
|
39 |
+
Settings.embed_model = HuggingFaceEmbedding(
|
40 |
+
model_name="BAAI/bge-large-en-v1.5",
|
41 |
+
embed_batch_size=8
|
42 |
+
)
|
43 |
+
Settings.llm = llm_AI4
|
44 |
+
Settings.chunk_size = 512
|
45 |
+
Settings.chunk_overlap = 50
|
46 |
+
Settings.callback_manager = CallbackManager([token_counter])
|
47 |
+
#memory for bot
|
48 |
+
memory = ChatMemoryBuffer.from_defaults(token_limit=3900)
|
49 |
+
|
50 |
+
#load vector database
|
51 |
+
pc = Pinecone(api_key=pinecone_api_key)
|
52 |
+
pinecone_index = pc.Index("pod-index")
|
53 |
+
vector_store_pine = PineconeVectorStore(pinecone_index=pinecone_index)
|
54 |
+
storage_context_pine = StorageContext.from_defaults(vector_store=vector_store_pine)
|
55 |
+
index_store = VectorStoreIndex.from_vector_store(vector_store_pine,storage_context=storage_context_pine)
|
56 |
+
query_engine_vector = index_store.as_query_engine(similarity_top_k=5,vector_store_query_mode ='hybrid',alpha=0.6)
|
57 |
+
#pandas Engine
|
58 |
+
df_veda_details = pd.read_csv("Data/veda_content_details.csv",encoding='utf-8')
|
59 |
+
query_engine_pandas = PandasQueryEngine(df=df_veda_details)
|
60 |
+
|
61 |
+
# Query Engine Tools
|
62 |
+
query_engine_tools = [
|
63 |
+
QueryEngineTool(
|
64 |
+
query_engine=query_engine_vector,
|
65 |
+
metadata=ToolMetadata(
|
66 |
+
name="vector_engine",
|
67 |
+
description=(
|
68 |
+
'''Helpful to get semantic information from the documents. These documents containing comprehensive information about the Vedas.\
|
69 |
+
They also covers various aspects, including general details about the Vedas, fundamental terminology associated with Vedic literature, \
|
70 |
+
and detailed information about Vedamantras for each Veda. The Vedamantra details encompass essential elements such as padapatha, rishi, chandah,\
|
71 |
+
devata, and swarah.This tool is very useful to answer general questions related to vedas.\
|
72 |
+
Sample Query:\
|
73 |
+
1. What is the meaning of devata ?\
|
74 |
+
2. What are the different Brahmanas associated with SamaVeda?\
|
75 |
+
3. What is the difference between Shruti and Smriti.
|
76 |
+
'''
|
77 |
+
),
|
78 |
+
),
|
79 |
+
),
|
80 |
+
QueryEngineTool(
|
81 |
+
query_engine=query_engine_pandas,
|
82 |
+
metadata=ToolMetadata(
|
83 |
+
name="pandas_engine",
|
84 |
+
description=(
|
85 |
+
'''Helpful to answer the queries related to count from the documents. This document is a .csv file with different columns containing comprehensive information about the Vedas.\
|
86 |
+
The column names as follows:\
|
87 |
+
'mantra_id', 'scripture_name', 'KandahNumber', 'PrapatakNumber','AnuvakNumber', 'MantraNumber', 'DevataName', 'RishiName', 'SwarahName', 'ChandaName',\
|
88 |
+
'padapatha', 'vedamantra', 'AdhyayaNumber', 'ArchikahNumber', 'ArchikahName', 'ShuktaNumber', 'keyShukta', 'ParyayaNumber', 'MandalaNumber'
|
89 |
+
''This tool is very useful to answer questions related to vedas on.\
|
90 |
+
Sample Query:\
|
91 |
+
1. How many mantras are there in RigVeda whose swarah is gāndhāraḥ?\
|
92 |
+
2. How many different devata present in rigveda?\
|
93 |
+
3. Which Kandah has the maximum number of in KrishnaYajurVeda?
|
94 |
+
4. How many mantras are there in RigVeda?
|
95 |
+
'''
|
96 |
+
),
|
97 |
+
),
|
98 |
+
)
|
99 |
+
]
|
100 |
+
|
101 |
+
# tools
|
102 |
+
mantra_tools = MantraToolSpec().to_tool_list()
|
103 |
+
description_tools = ScriptureDescriptionToolSpec().to_tool_list()
|
104 |
+
tools = [*mantra_tools,*description_tools,*query_engine_tools]
|
105 |
+
|
106 |
+
# context
|
107 |
+
context = """
|
108 |
+
You are an expert on Vedas and related scriptures.\
|
109 |
+
Your role is to respond to questions about vedic scriptures and associated information based on available sources.\
|
110 |
+
For every query, you must use either any one of the tool or use available history/context.
|
111 |
+
Please provide well-informed answers. Don't use prior knowledge.
|
112 |
+
"""
|
113 |
+
|
114 |
+
# Function to create ReActAgent instance (change it based on your initialization logic)
|
115 |
+
@st.cache_resource(show_spinner=False) # Set allow_output_mutation to True for mutable objects like instances
|
116 |
+
def create_react_agent():
|
117 |
+
return ReActAgent.from_tools(tools, llm=llm_AI4, context=context, verbose=True)
|
118 |
+
|
119 |
+
# Example usage
|
120 |
+
react_agent_instance = create_react_agent()
|
121 |
+
|
122 |
+
# Streamlit Components Initialization
|
123 |
+
st.title("Svarupa Bot ")
|
124 |
+
|
125 |
+
if "messages" not in st.session_state.keys():
|
126 |
+
st.session_state.messages = [
|
127 |
+
{"role": "assistant", "content": "Hi. I am Svarupa AI Assistant. Ask me a question about Vedas!"}
|
128 |
+
]
|
129 |
+
|
130 |
+
if "chat_engine" not in st.session_state.keys():
|
131 |
+
# Using st.cache_resource for caching the unserializable react_agent
|
132 |
+
st.session_state.chat_engine = create_react_agent()
|
133 |
+
|
134 |
+
if prompt := st.chat_input("Your question"):
|
135 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
136 |
+
|
137 |
+
for message in st.session_state.messages:
|
138 |
+
with st.chat_message(message["role"]):
|
139 |
+
st.write(message["content"])
|
140 |
+
|
141 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
142 |
+
with st.chat_message("assistant"):
|
143 |
+
with st.spinner("Thinking..."):
|
144 |
+
# Using the cached chat_engine
|
145 |
+
response = st.session_state.chat_engine.chat(prompt)
|
146 |
+
st.write(response.response)
|
147 |
+
message = {"role": "assistant", "content": response.response}
|
148 |
+
st.session_state.messages.append(message)
|
149 |
+
|