File size: 9,151 Bytes
667959c
7d84684
667959c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
903243e
 
667959c
e36ed6b
 
903243e
667959c
e36ed6b
 
 
 
 
fb032ac
 
 
 
 
 
 
903243e
 
 
667959c
 
903243e
667959c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
903243e
 
 
 
 
667959c
903243e
 
 
 
667959c
 
 
 
 
 
 
 
903243e
 
667959c
 
 
 
 
 
 
 
 
 
 
 
903243e
667959c
903243e
667959c
464a43e
667959c
464a43e
 
667959c
 
 
464a43e
 
 
 
 
 
903243e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
667959c
 
 
 
 
903243e
 
667959c
 
 
 
 
903243e
667959c
903243e
 
 
 
 
 
667959c
 
 
 
 
464a43e
667959c
 
 
 
 
0d061ad
667959c
 
 
0d061ad
667959c
 
 
 
 
 
 
 
 
 
 
 
 
7d84684
 
 
 
 
 
 
 
 
 
87886ae
7d84684
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import streamlit as st
import openai
st.set_page_config(
    page_title="SVARUPA AI",
    layout="centered",  # or "wide"
    initial_sidebar_state="auto"  # or "expanded" or "collapsed"
    )
from llama_index.core import VectorStoreIndex, StorageContext, Document
from llama_index.llms.openai import OpenAI
import os
import pandas as pd
from llama_index.core import Settings
from llama_index.vector_stores.pinecone import PineconeVectorStore
import pinecone
from pinecone import Pinecone, PodSpec
from llama_index.core.query_engine import PandasQueryEngine
from llama_index.core.agent import ReActAgent
from llama_index.core.memory import ChatMemoryBuffer
from sentence_transformers import SentenceTransformer
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
#from llama_index.indices.postprocessor import SimilarityPostprocessor
#from llama_index.postprocessor import SentenceTransformerRerank
import tiktoken
from llama_index.core.callbacks import CallbackManager, TokenCountingHandler
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from Tools import ScriptureDescriptionToolSpec, MantraToolSpec, PadaToolSpec


# Print all loaded secrets
all_secrets = st.secrets


# Access the specific secret
try:
    openai_api_key = st.secrets["OPENAI_APIKEY_CS"]
except KeyError as e:
    st.error(f"KeyError: {e}")

# Access the specific secret
try:
    pinecone_api_key = st.secrets["PINECONE_API_KEY_SAM"]
    #st.write("OpenAI API Key:", openai_api_key)
except KeyError as e:
    st.error(f"KeyError: {e}")   




#llm

llm_AI4 = OpenAI(temperature=0, model="gpt-4-1106-preview",api_key=openai_api_key, max_tokens=512)
token_counter = TokenCountingHandler(
    tokenizer=tiktoken.encoding_for_model("gpt-4-1106-preview").encode
    )

# global settings
Settings.embed_model = HuggingFaceEmbedding(
    model_name="BAAI/bge-large-en-v1.5",
    embed_batch_size=8
)
Settings.llm = llm_AI4 
Settings.chunk_size = 512
Settings.chunk_overlap = 50
Settings.callback_manager = CallbackManager([token_counter])
#memory for bot
memory = ChatMemoryBuffer.from_defaults(token_limit=3900)

#load vector database
pc = Pinecone(api_key=pinecone_api_key)
pinecone_index = pc.Index("pod-index")
vector_store_pine = PineconeVectorStore(pinecone_index=pinecone_index)
storage_context_pine = StorageContext.from_defaults(vector_store=vector_store_pine)
index_store = VectorStoreIndex.from_vector_store(vector_store_pine,storage_context=storage_context_pine)
query_engine_vector = index_store.as_query_engine(similarity_top_k=10,vector_store_query_mode ='hybrid',alpha=0.6,inlcude_metadata = True)

VEDAMANTRA_CSV_PATH = r"Data/veda_content_modified_v3.csv"
PADA_CSV_PATH = r"Data/Data/term_data_processed_v2.csv"

#pandas Engine
df_veda_details = pd.read_csv(VEDAMANTRA_CSV_PATH,encoding='utf-8')
df_pada_details = pd.read_csv(PADA_CSV_PATH,encoding='utf-8')
query_engine_veda = PandasQueryEngine(df=df_veda_details)
query_engine_pada = PandasQueryEngine(df=df_pada_details)

# Query Engine Tools
query_engine_tools = [
    QueryEngineTool(
        query_engine=query_engine_vector,
        metadata=ToolMetadata(
            name="vector_engine",
            description=(
                '''
                Helpful to get semantic information from the documents. These documents containing comprehensive information about the Vedas.\
                They also covers various aspects, including general details about the Vedas, fundamental terminology associated with Vedic literature, \
                and detailed information about Vedamantras for each Veda. The Vedamantra details encompass essential elements such as padapatha, rishi, chandah,\
                devata, and swarah.This tool is very useful to answer general questions related to vedas.\
                Sample Query:\
                1. What is the meaning of devata ?\
                2. What are the different Brahmanas associated with SamaVeda?\
                3. What is the difference between Shruti and Smriti.
               '''
            ),
        ),
    ),
    QueryEngineTool(
        query_engine=query_engine_veda,
        metadata=ToolMetadata(
            name="pandas_engine_vedas",
            description=(
                '''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:\
                'mantra_id', 'scripture_name', 'KandahNumber', 'PrapatakNumber','AnuvakNumber', 'MantraNumber', 'DevataName', 'RishiName', 'SwarahName', 'ChandaName',\
                'padapatha', 'vedamantra', 'AdhyayaNumber', 'ArchikahNumber', 'ArchikahName', 'ShuktaNumber', 'keyShukta', 'ParyayaNumber', 'MandalaNumber'.\
                Always provide the final answer after excuting pandas query which is equivalent to user query.\
                Sample Query:\
                1. How many mantras are there in RigVeda whose swarah is gāndhāraḥ?\
                2. How many different devata present in rigveda?\
                3. Which Kandah has the maximum number of in KrishnaYajurVeda?\
                4. Find the number of mantras from AtharvaVeda whose devata is vācaspatiḥ and chandah is anuṣṭup?\
                5. count the mantras in RigVeda whose swarah is gāndhāraḥ?
               '''
            ),
        ),
    ),
    QueryEngineTool(
        query_engine=query_engine_pada,
        metadata=ToolMetadata(
            name="pandas_engine_padas",
            description=(
                '''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:\
                    'Pada', 'scripture_name', 'mantra_id', 'MantraNumber', 'AnuvakNumber', 'PrapatakNumber', 'KandahNumber', 'Pada_position', 'term_index', 'Segmentation', 'Morphology', 'ShuktaNumber',\
                        'ArchikahNumber', 'AdhyayaNumber', 'MandalaNumber', 'ParyayaNumber' \
                Always provide the final answer after excuting pandas query which is equivalent to user query.\
                Sample Query:\
                1. How many padas are there in RigVeda?\
                2. How many padas present in both rigveda and samaveda?
               '''
            ),
        ),
    )
    ]
# tools
mantra_tools = MantraToolSpec().to_tool_list()
description_tools = ScriptureDescriptionToolSpec().to_tool_list()
pada_tools = PadaToolSpec().to_tool_list()
tools = [*mantra_tools,*pada_tools,*description_tools,*query_engine_tools]

# context
context = """
  You are an expert on Vedas and related scriptures.\
  Your role is to respond to questions about vedic scriptures and associated information based on available sources.\
  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.
  Please provide well-informed answers. Don't use prior knowledge.
  If you are not sure about the answer, you can say that you don't have sufficient information.
  Also, provide three followup questions based on the input query and the answer in the following format.\
  You may also try the following questions:\n
  1. Question1\n
  2. Question2\n
  3. Question3\n
"""

# Function to create ReActAgent instance (change it based on your initialization logic)
@st.cache_resource(show_spinner=False)  # Set allow_output_mutation to True for mutable objects like instances
def create_react_agent():
    return ReActAgent.from_tools(tools, llm=llm_AI4, context=context,  memory = memory, max_iterations = 100,verbose=True)

# Example usage
react_agent_instance = create_react_agent()

# Streamlit Components Initialization
st.title("Veda Bot ")

if "messages" not in st.session_state.keys():
    st.session_state.messages = [
        {"role": "assistant", "content": "Hi. I am AI Assistant. Ask me a question about Vedas!"}
    ]

if "chat_engine" not in st.session_state.keys():
    # Using st.cache_resource for caching the unserializable react_agent
    st.session_state.chat_engine = create_react_agent()

if prompt := st.chat_input("Your question"):
    st.session_state.messages.append({"role": "user", "content": prompt})

for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.write(message["content"])

try:
    if st.session_state.messages[-1]["role"] != "assistant":
        with st.chat_message("assistant"):
            with st.spinner("Thinking..."):
                # Using the cached chat_engine
                response = st.session_state.chat_engine.chat(prompt)
                st.write(response.response)
                message = {"role": "assistant", "content": response.response}
                st.session_state.messages.append(message)

except openai.RateLimitError as e:
    # Handle the RateLimitError
    st.error("You have exceeded your API quota. Please check your plan and billing details.")
except Exception as e:
    # Handle other exceptions if needed
    st.error(f"An unexpected error occurred: {e}")