Spaces:
Sleeping
Sleeping
sampathlonka
commited on
Commit
•
903243e
1
Parent(s):
377ed3a
Updated_v2
Browse files
app.py
CHANGED
@@ -23,16 +23,16 @@ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
|
23 |
import tiktoken
|
24 |
from llama_index.core.callbacks import CallbackManager, TokenCountingHandler
|
25 |
from llama_index.core.tools import QueryEngineTool, ToolMetadata
|
26 |
-
from
|
|
|
27 |
|
28 |
# Print all loaded secrets
|
29 |
all_secrets = st.secrets
|
30 |
-
|
31 |
|
32 |
# Access the specific secret
|
33 |
try:
|
34 |
openai_api_key = st.secrets["OPENAI_APIKEY_CS"]
|
35 |
-
#st.write("OpenAI API Key:", openai_api_key)
|
36 |
except KeyError as e:
|
37 |
st.error(f"KeyError: {e}")
|
38 |
|
@@ -42,12 +42,12 @@ try:
|
|
42 |
#st.write("OpenAI API Key:", openai_api_key)
|
43 |
except KeyError as e:
|
44 |
st.error(f"KeyError: {e}")
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
|
49 |
#llm
|
50 |
-
|
51 |
llm_AI4 = OpenAI(temperature=0, model="gpt-4-1106-preview",api_key=openai_api_key, max_tokens=512)
|
52 |
token_counter = TokenCountingHandler(
|
53 |
tokenizer=tiktoken.encoding_for_model("gpt-4-1106-preview").encode
|
@@ -71,12 +71,16 @@ pinecone_index = pc.Index("pod-index")
|
|
71 |
vector_store_pine = PineconeVectorStore(pinecone_index=pinecone_index)
|
72 |
storage_context_pine = StorageContext.from_defaults(vector_store=vector_store_pine)
|
73 |
index_store = VectorStoreIndex.from_vector_store(vector_store_pine,storage_context=storage_context_pine)
|
74 |
-
query_engine_vector = index_store.as_query_engine(similarity_top_k=
|
|
|
|
|
|
|
|
|
75 |
#pandas Engine
|
76 |
-
df_veda_details = pd.read_csv(
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
|
81 |
# Query Engine Tools
|
82 |
query_engine_tools = [
|
@@ -85,7 +89,8 @@ query_engine_tools = [
|
|
85 |
metadata=ToolMetadata(
|
86 |
name="vector_engine",
|
87 |
description=(
|
88 |
-
'''
|
|
|
89 |
They also covers various aspects, including general details about the Vedas, fundamental terminology associated with Vedic literature, \
|
90 |
and detailed information about Vedamantras for each Veda. The Vedamantra details encompass essential elements such as padapatha, rishi, chandah,\
|
91 |
devata, and swarah.This tool is very useful to answer general questions related to vedas.\
|
@@ -98,9 +103,9 @@ query_engine_tools = [
|
|
98 |
),
|
99 |
),
|
100 |
QueryEngineTool(
|
101 |
-
query_engine=
|
102 |
metadata=ToolMetadata(
|
103 |
-
name="
|
104 |
description=(
|
105 |
'''A powerful tool designed to handle queries related to counting information about vedic content document. This document is a .csv file with different columns as follows:\
|
106 |
'mantra_id', 'scripture_name', 'KandahNumber', 'PrapatakNumber','AnuvakNumber', 'MantraNumber', 'DevataName', 'RishiName', 'SwarahName', 'ChandaName',\
|
@@ -115,21 +120,42 @@ query_engine_tools = [
|
|
115 |
'''
|
116 |
),
|
117 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
)
|
119 |
]
|
120 |
-
|
121 |
# tools
|
122 |
mantra_tools = MantraToolSpec().to_tool_list()
|
123 |
description_tools = ScriptureDescriptionToolSpec().to_tool_list()
|
124 |
-
|
|
|
125 |
|
126 |
# context
|
127 |
context = """
|
128 |
You are an expert on Vedas and related scriptures.\
|
129 |
Your role is to respond to questions about vedic scriptures and associated information based on available sources.\
|
130 |
-
For every query, you must use
|
131 |
-
User expect the responses based on vedic scriptures or related vedas.\
|
132 |
Please provide well-informed answers. Don't use prior knowledge.
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
"""
|
134 |
|
135 |
# Function to create ReActAgent instance (change it based on your initialization logic)
|
|
|
23 |
import tiktoken
|
24 |
from llama_index.core.callbacks import CallbackManager, TokenCountingHandler
|
25 |
from llama_index.core.tools import QueryEngineTool, ToolMetadata
|
26 |
+
from Tools import ScriptureDescriptionToolSpec, MantraToolSpec, PadaToolSpec
|
27 |
+
|
28 |
|
29 |
# Print all loaded secrets
|
30 |
all_secrets = st.secrets
|
31 |
+
|
32 |
|
33 |
# Access the specific secret
|
34 |
try:
|
35 |
openai_api_key = st.secrets["OPENAI_APIKEY_CS"]
|
|
|
36 |
except KeyError as e:
|
37 |
st.error(f"KeyError: {e}")
|
38 |
|
|
|
42 |
#st.write("OpenAI API Key:", openai_api_key)
|
43 |
except KeyError as e:
|
44 |
st.error(f"KeyError: {e}")
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
|
49 |
#llm
|
50 |
+
|
51 |
llm_AI4 = OpenAI(temperature=0, model="gpt-4-1106-preview",api_key=openai_api_key, max_tokens=512)
|
52 |
token_counter = TokenCountingHandler(
|
53 |
tokenizer=tiktoken.encoding_for_model("gpt-4-1106-preview").encode
|
|
|
71 |
vector_store_pine = PineconeVectorStore(pinecone_index=pinecone_index)
|
72 |
storage_context_pine = StorageContext.from_defaults(vector_store=vector_store_pine)
|
73 |
index_store = VectorStoreIndex.from_vector_store(vector_store_pine,storage_context=storage_context_pine)
|
74 |
+
query_engine_vector = index_store.as_query_engine(similarity_top_k=10,vector_store_query_mode ='hybrid',alpha=0.6,inlcude_metadata = True)
|
75 |
+
|
76 |
+
VEDAMANTRA_CSV_PATH = r"Data/veda_content_modified_v3.csv"
|
77 |
+
PADA_CSV_PATH = r"Data/Data/term_data_processed_v2.csv"
|
78 |
+
|
79 |
#pandas Engine
|
80 |
+
df_veda_details = pd.read_csv(VEDAMANTRA_CSV_PATH,encoding='utf-8')
|
81 |
+
df_pada_details = pd.read_csv(PADA_CSV_PATH,encoding='utf-8')
|
82 |
+
query_engine_veda = PandasQueryEngine(df=df_veda_details)
|
83 |
+
query_engine_pada = PandasQueryEngine(df=df_pada_details)
|
84 |
|
85 |
# Query Engine Tools
|
86 |
query_engine_tools = [
|
|
|
89 |
metadata=ToolMetadata(
|
90 |
name="vector_engine",
|
91 |
description=(
|
92 |
+
'''
|
93 |
+
Helpful to get semantic information from the documents. These documents containing comprehensive information about the Vedas.\
|
94 |
They also covers various aspects, including general details about the Vedas, fundamental terminology associated with Vedic literature, \
|
95 |
and detailed information about Vedamantras for each Veda. The Vedamantra details encompass essential elements such as padapatha, rishi, chandah,\
|
96 |
devata, and swarah.This tool is very useful to answer general questions related to vedas.\
|
|
|
103 |
),
|
104 |
),
|
105 |
QueryEngineTool(
|
106 |
+
query_engine=query_engine_veda,
|
107 |
metadata=ToolMetadata(
|
108 |
+
name="pandas_engine_vedas",
|
109 |
description=(
|
110 |
'''A powerful tool designed to handle queries related to counting information about vedic content document. This document is a .csv file with different columns as follows:\
|
111 |
'mantra_id', 'scripture_name', 'KandahNumber', 'PrapatakNumber','AnuvakNumber', 'MantraNumber', 'DevataName', 'RishiName', 'SwarahName', 'ChandaName',\
|
|
|
120 |
'''
|
121 |
),
|
122 |
),
|
123 |
+
),
|
124 |
+
QueryEngineTool(
|
125 |
+
query_engine=query_engine_pada,
|
126 |
+
metadata=ToolMetadata(
|
127 |
+
name="pandas_engine_padas",
|
128 |
+
description=(
|
129 |
+
'''A powerful tool designed to handle queries related to counting information about pada or words from vedic documents. This document is a .csv file with different columns as follows:\
|
130 |
+
'Pada', 'scripture_name', 'mantra_id', 'MantraNumber', 'AnuvakNumber', 'PrapatakNumber', 'KandahNumber', 'Pada_position', 'term_index', 'Segmentation', 'Morphology', 'ShuktaNumber',\
|
131 |
+
'ArchikahNumber', 'AdhyayaNumber', 'MandalaNumber', 'ParyayaNumber' \
|
132 |
+
Always provide the final answer after excuting pandas query which is equivalent to user query.\
|
133 |
+
Sample Query:\
|
134 |
+
1. How many padas are there in RigVeda?\
|
135 |
+
2. How many padas present in both rigveda and samaveda?
|
136 |
+
'''
|
137 |
+
),
|
138 |
+
),
|
139 |
)
|
140 |
]
|
|
|
141 |
# tools
|
142 |
mantra_tools = MantraToolSpec().to_tool_list()
|
143 |
description_tools = ScriptureDescriptionToolSpec().to_tool_list()
|
144 |
+
pada_tools = PadaToolSpec().to_tool_list()
|
145 |
+
tools = [*mantra_tools,*pada_tools,*description_tools,*query_engine_tools]
|
146 |
|
147 |
# context
|
148 |
context = """
|
149 |
You are an expert on Vedas and related scriptures.\
|
150 |
Your role is to respond to questions about vedic scriptures and associated information based on available sources.\
|
151 |
+
For every query, you must use tool first. If the input args, kwargs and tools for the given query is same as in the history or retrieved context is sufficient, then use the history as context.
|
|
|
152 |
Please provide well-informed answers. Don't use prior knowledge.
|
153 |
+
If you are not sure about the answer, you can say that you don't have sufficient information.
|
154 |
+
Also, provide three followup questions based on the input query and the answer in the following format.\
|
155 |
+
You may also try the following questions:\n
|
156 |
+
1. Question1\n
|
157 |
+
2. Question2\n
|
158 |
+
3. Question3\n
|
159 |
"""
|
160 |
|
161 |
# Function to create ReActAgent instance (change it based on your initialization logic)
|