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 = "Data/veda_content_modified_v3.csv" PADA_CSV_PATH = "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 follow-up questions based on the input query and the context in the following format. ***** You may also try the following questions: 1. Question1 2. Question2 3. Question3 """ # 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}")