code: import boto3
  import os
  import json 
  # from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
  from langchain_community.llms import Bedrock
  from llama_index.node_parser import SimpleNodeParser
  from llama_index.embeddings import LangchainEmbedding
  from langchain_community.embeddings import BedrockEmbeddings
  from llama_index import ServiceContext, OpenAIEmbedding, PromptHelper
  from llama_index import VectorStoreIndex
  from llama_index import download_loader
  from llama_index.postprocessor.cohere_rerank import CohereRerank
  from credentials import cohere_api_key , openai_api_key
  import pandas as pd
  from pandasai import SmartDataframe
  from pandasai.llm import OpenAI
  from llama_index.llms import OpenAI as llama_openai
  from model import *
  from questions import *
  import random
  import logging
  from collections import defaultdict
  import shutil
  import numpy as np
  from llama_index.node_parser import SentenceWindowNodeParser, SimpleNodeParser
  
  
  
  logging.basicConfig(filename='rag.log', encoding='utf-8', level=logging.INFO)
  
  class rag_on_s3:
  
      def __init__(self):
          session = boto3.Session()
          self.bedrock_runtime = boto3.client("bedrock-runtime")
          self.s3 = session.client('s3')
          self.cohere_api_key = cohere_api_key
          self.S3Reader = download_loader("S3Reader")
          self.root_bucket = "demo-industry-specific"
          os.environ['OPENAI_API_KEY'] = openai_api_key
          self.model_configuration("OpenAI")
  
  
      def get_model_name(self):
          model_names = list(models_ids.keys())
          return model_names
  
      def get_industry_names(self):
          return list(industry_specific_bucket.keys())
          
  
      def model_configuration(self,model_name):
          model_id = models_ids.get(model_name)
          model_kwargs = model_parameters.get(model_name)
  
          if model_id == "OpenAI":
  
              llm = llama_openai(model='gpt-3.5-turbo', temperature=0, max_tokens=256)
              embed_model = OpenAIEmbedding()
              
          else:
              llm = Bedrock(
                  client=self.bedrock_runtime,
                  model_id=model_id,
                  model_kwargs=model_kwargs
              )
              # bedrock_embedding = BedrockEmbeddings(
              #     client=self.bedrock_runtime,
              #     model_id="amazon.titan-embed-text-v1",
              # )
              # embed_model = LangchainEmbedding(bedrock_embedding)
              embed_model = OpenAIEmbedding()
  
          self.service_context = ServiceContext.from_defaults(
          llm=llm,
          embed_model=embed_model,
          system_prompt="You are an AI assistant answering questions"
          )
  
          response = f"{model_name} successfully loaded"
          return response
  
  
      def get_indexes(self, industry_name):
          self.industry_name = industry_specific_bucket[industry_name]
  
          logging.info("getting indexes")
          folder_path = "/home/ubuntu/RAG_API/"+self.industry_name+"-index"
          logging.info(folder_path)
          if os.path.exists(folder_path):
              logging.info("folder exists")
              if os.path.isdir(folder_path):
                  logging.info("removing dir")
                  shutil.rmtree(folder_path, ignore_errors=True)
          
          logging.info("creting indexes")
          loader = self.S3Reader(bucket=self.industry_name)
          docs = loader.load_data()
  
          sentence_node_parser = SentenceWindowNodeParser.from_defaults(
              window_size=3,
              window_metadata_key="window",
              original_text_metadata_key="original_text")
          # base_node_parser = SimpleNodeParser()
  
          nodes = sentence_node_parser.get_nodes_from_documents(docs)
          # base_nodes = base_node_parser.get_nodes_from_documents(docs)
      
  
          self.service_context = ServiceContext.from_service_context(
              service_context = self.service_context,
              node_parser = nodes
              )
  
  
          index = VectorStoreIndex(
              nodes,
              service_context=self.service_context)
  
          index.storage_context.persist(folder_path)
          logging.info("indexing successful")
          response = "Index successfully created"
          return response
  
  
  # to handle text data eg : .txt , .pdf, .docx              
      def get_docs(self, industry_name):
          self.industry_name = industry_specific_bucket[industry_name]
          folder_path = "/home/ubuntu/RAG_API/"+self.industry_name+"-index"
          if (os.path.exists(folder_path)==False):
              self.get_indexes(industry_name)
  
          filenames = []
          files = self.s3.list_objects_v2(Bucket=self.industry_name, Delimiter='/')
          for item in files['Contents']:
              f = item['Key']
              filenames.append(f)
          logging.info(filenames)
  
          response = str(len(filenames)) + " Documents sucessfully indexed"
          ques = question_list[industry_name]
          
          ques_list = random.sample(ques, 4)
          total_docs = len(filenames)
  
          filetypes = defaultdict()
          for fn in filenames:
              ftype = fn.split('.')[-1]
              if ftype not in filetypes:
                  filetypes[ftype] = []
              filetypes[ftype].append(fn)
  
          logging.info(filetypes)
          filetype_response = "{total_docs} documents loaded. ".format(total_docs=total_docs)
          for key in filetypes:
              l = len(filetypes[key])
              filetype_response += "{l} {key} files ".format(l=l, key=key)
  
          filetypes["All"] = filenames
          logging.info(filetype_response)
          return filetype_response, ques_list, filetypes
  
  
  
      def open_file(self, filename):
          response = self.s3.generate_presigned_url('get_object',
                                                      Params = {'Bucket': self.industry_name,
                                                                  'Key':filename},
                                                      ExpiresIn=3000)
          return response
  
  
      def query_text(self,query,index,model_name):
          cohere_rerank = CohereRerank(api_key=self.cohere_api_key, top_n=2)
          query_engine = index.as_query_engine(
              similarity_top_k=5,
              node_postprocessors=[cohere_rerank],
              service_context=self.service_context
          )
          if model_name == "Anthropic":
              response = query_engine.query(f"\n\nHuman:{query}\n\nAssistant:")
          else:
              response = query_engine.query(query)
          
          file_names = [details['file_name'] for key, details in response.metadata.items()]
          score = [node_with_score.score for node_with_score in response.source_nodes]
  
  
          logging.info(response.metadata)
          source = set()
          if(score[0] < 0.8 and score[0] > 0.5):
              source.add(file_names[0])
          else:
              for i in range(len(score)):
                  if score[i] >= 0.8 :
                      source.add(file_names[i])
  
          print(score)
          source = list(source)
          if len(source) == 0:
              final_source = "Not Found"
              final_response = "Sorry, I couldn't find the relevant information to the given query !!"
          else : 
              final_source = source
              final_response = response.response
  
          return final_response,final_source
  
  
  # to handle .csv data 
      def display_csv_files(self):
          response = self.s3.list_objects_v2(Bucket="berrywise-database")
          object_keys = [obj['Key'] for obj in response['Contents']]
          return object_keys
  
  
      def get_csv_file(self,csv_file_key):
          directory_path = "/home/ubuntu/RAG_API/csv_file"
          files_in_directory = os.listdir(directory_path)
          for filename in files_in_directory: 
              file_path = os.path.join(directory_path, filename)  # Construct full path
              os.remove(file_path)
          self.s3.download_file('berrywise-database', csv_file_key, "/home/ubuntu/RAG_API/csv_file/"+csv_file_key)
          file_name= os.listdir(directory_path)
          df = pd.read_csv(f"/home/ubuntu/RAG_API/csv_file/{file_name[0]}")
          data = df.head(5)
          columns = df.columns.tolist()
          
          # logging.info(data)
          
          json_output = data.to_json(orient='records', force_ascii=False)
          parsed_json = json.loads(json_output)
          output = json.dumps(parsed_json)
  
              
          logging.info(output)
          print(output)
          return output, columns
          
  
      def csv_query(self,query):
          folder_path = "/home/ubuntu/RAG_API/csv_file"
          file_name= os.listdir(folder_path)
          df = pd.read_csv(f"/home/ubuntu/RAG_API/csv_file/{file_name[0]}")
          llm = OpenAI(api_token=openai_api_key)
          df = SmartDataframe(df, config={"llm": llm})
          result = df.chat(query)
  
          if(type(result) == np.integer):
              return int(result)
  
          if (type(result) == int or type(result) == np.float64): 
              json_output = json.dumps(result, default=str)
              return json_output
  
          if(type(result) == SmartDataframe):
              result = result.to_dict(orient='records')
              return result
  
          return result
          # if (type(result) == int or type(result) == np.float64): 
          #     json_output = json.dumps(result)
          #     return json_output
          # elif type(result) == str : 
          #     json_output = json.dumps(result)
          #     return json_output
          # else: 
          #     json_output = json.dumps(result.to_dict())
          #     # json_output = json.dumps(result)
          #     return json_output
  
      
          # # llm = OpenAI(api_token=openai_api_key)
          # agent = create_pandas_dataframe_agent(OpenAI(temperature=0), df, verbose=True)
          # prompt = {
          #     "tool_names" : ["pandas"],
          #     "tools" : {
          #         "pandas" : {
          #             "df":df.to_dict()
          #         }
          #     },
          #     "input":query
          # }
          # result = agent.run(prompt)
  
  
  
apis: llama_index.postprocessor.cohere_rerank.CohereRerank llama_index.ServiceContext.from_service_context llama_index.OpenAIEmbedding llama_index.ServiceContext.from_defaults llama_index.VectorStoreIndex llama_index.llms.OpenAI llama_index.download_loader llama_index.node_parser.SentenceWindowNodeParser.from_defaults
extract_api: [((974, 1051), 'logging.basicConfig', 'logging.basicConfig', ([], {'filename': '"""rag.log"""', 'encoding': '"""utf-8"""', 'level': 'logging.INFO'}), "(filename='rag.log', encoding='utf-8', level=logging.INFO)\n", (993, 1051), False, 'import logging\n'), ((1113, 1128), 'boto3.Session', 'boto3.Session', ([], {}), '()\n', (1126, 1128), False, 'import boto3\n'), ((1160, 1191), 'boto3.client', 'boto3.client', (['"""bedrock-runtime"""'], {}), "('bedrock-runtime')\n", (1172, 1191), False, 'import boto3\n'), ((1300, 1327), 'llama_index.download_loader', 'download_loader', (['"""S3Reader"""'], {}), "('S3Reader')\n", (1315, 1327), False, 'from llama_index import download_loader\n'), ((2497, 2624), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm': 'llm', 'embed_model': 'embed_model', 'system_prompt': '"""You are an AI assistant answering questions"""'}), "(llm=llm, embed_model=embed_model,\n    system_prompt='You are an AI assistant answering questions')\n", (2525, 2624), False, 'from llama_index import ServiceContext, OpenAIEmbedding, PromptHelper\n'), ((2857, 2888), 'logging.info', 'logging.info', (['"""getting indexes"""'], {}), "('getting indexes')\n", (2869, 2888), False, 'import logging\n'), ((2971, 2996), 'logging.info', 'logging.info', (['folder_path'], {}), '(folder_path)\n', (2983, 2996), False, 'import logging\n'), ((3008, 3035), 'os.path.exists', 'os.path.exists', (['folder_path'], {}), '(folder_path)\n', (3022, 3035), False, 'import os\n'), ((3247, 3278), 'logging.info', 'logging.info', (['"""creting indexes"""'], {}), "('creting indexes')\n", (3259, 3278), False, 'import logging\n'), ((3403, 3535), 'llama_index.node_parser.SentenceWindowNodeParser.from_defaults', 'SentenceWindowNodeParser.from_defaults', ([], {'window_size': '(3)', 'window_metadata_key': '"""window"""', 'original_text_metadata_key': '"""original_text"""'}), "(window_size=3, window_metadata_key=\n    'window', original_text_metadata_key='original_text')\n", (3441, 3535), False, 'from llama_index.node_parser import SentenceWindowNodeParser, SimpleNodeParser\n'), ((3793, 3889), 'llama_index.ServiceContext.from_service_context', 'ServiceContext.from_service_context', ([], {'service_context': 'self.service_context', 'node_parser': 'nodes'}), '(service_context=self.service_context,\n    node_parser=nodes)\n', (3828, 3889), False, 'from llama_index import ServiceContext, OpenAIEmbedding, PromptHelper\n'), ((3946, 4007), 'llama_index.VectorStoreIndex', 'VectorStoreIndex', (['nodes'], {'service_context': 'self.service_context'}), '(nodes, service_context=self.service_context)\n', (3962, 4007), False, 'from llama_index import VectorStoreIndex\n'), ((4093, 4128), 'logging.info', 'logging.info', (['"""indexing successful"""'], {}), "('indexing successful')\n", (4105, 4128), False, 'import logging\n'), ((4751, 4774), 'logging.info', 'logging.info', (['filenames'], {}), '(filenames)\n', (4763, 4774), False, 'import logging\n'), ((4923, 4945), 'random.sample', 'random.sample', (['ques', '(4)'], {}), '(ques, 4)\n', (4936, 4945), False, 'import random\n'), ((5003, 5016), 'collections.defaultdict', 'defaultdict', ([], {}), '()\n', (5014, 5016), False, 'from collections import defaultdict\n'), ((5210, 5233), 'logging.info', 'logging.info', (['filetypes'], {}), '(filetypes)\n', (5222, 5233), False, 'import logging\n'), ((5511, 5542), 'logging.info', 'logging.info', (['filetype_response'], {}), '(filetype_response)\n', (5523, 5542), False, 'import logging\n'), ((6040, 6090), 'llama_index.postprocessor.cohere_rerank.CohereRerank', 'CohereRerank', ([], {'api_key': 'self.cohere_api_key', 'top_n': '(2)'}), '(api_key=self.cohere_api_key, top_n=2)\n', (6052, 6090), False, 'from llama_index.postprocessor.cohere_rerank import CohereRerank\n'), ((6650, 6681), 'logging.info', 'logging.info', (['response.metadata'], {}), '(response.metadata)\n', (6662, 6681), False, 'import logging\n'), ((7642, 7668), 'os.listdir', 'os.listdir', (['directory_path'], {}), '(directory_path)\n', (7652, 7668), False, 'import os\n'), ((7965, 7991), 'os.listdir', 'os.listdir', (['directory_path'], {}), '(directory_path)\n', (7975, 7991), False, 'import os\n'), ((8005, 8065), 'pandas.read_csv', 'pd.read_csv', (['f"""/home/ubuntu/RAG_API/csv_file/{file_name[0]}"""'], {}), "(f'/home/ubuntu/RAG_API/csv_file/{file_name[0]}')\n", (8016, 8065), True, 'import pandas as pd\n'), ((8271, 8294), 'json.loads', 'json.loads', (['json_output'], {}), '(json_output)\n', (8281, 8294), False, 'import json\n'), ((8312, 8335), 'json.dumps', 'json.dumps', (['parsed_json'], {}), '(parsed_json)\n', (8322, 8335), False, 'import json\n'), ((8358, 8378), 'logging.info', 'logging.info', (['output'], {}), '(output)\n', (8370, 8378), False, 'import logging\n'), ((8546, 8569), 'os.listdir', 'os.listdir', (['folder_path'], {}), '(folder_path)\n', (8556, 8569), False, 'import os\n'), ((8583, 8643), 'pandas.read_csv', 'pd.read_csv', (['f"""/home/ubuntu/RAG_API/csv_file/{file_name[0]}"""'], {}), "(f'/home/ubuntu/RAG_API/csv_file/{file_name[0]}')\n", (8594, 8643), True, 'import pandas as pd\n'), ((8658, 8690), 'pandasai.llm.OpenAI', 'OpenAI', ([], {'api_token': 'openai_api_key'}), '(api_token=openai_api_key)\n', (8664, 8690), False, 'from pandasai.llm import OpenAI\n'), ((8704, 8743), 'pandasai.SmartDataframe', 'SmartDataframe', (['df'], {'config': "{'llm': llm}"}), "(df, config={'llm': llm})\n", (8718, 8743), False, 'from pandasai import SmartDataframe\n'), ((1881, 1947), 'llama_index.llms.OpenAI', 'llama_openai', ([], {'model': '"""gpt-3.5-turbo"""', 'temperature': '(0)', 'max_tokens': '(256)'}), "(model='gpt-3.5-turbo', temperature=0, max_tokens=256)\n", (1893, 1947), True, 'from llama_index.llms import OpenAI as llama_openai\n'), ((1974, 1991), 'llama_index.OpenAIEmbedding', 'OpenAIEmbedding', ([], {}), '()\n', (1989, 1991), False, 'from llama_index import ServiceContext, OpenAIEmbedding, PromptHelper\n'), ((2037, 2124), 'langchain_community.llms.Bedrock', 'Bedrock', ([], {'client': 'self.bedrock_runtime', 'model_id': 'model_id', 'model_kwargs': 'model_kwargs'}), '(client=self.bedrock_runtime, model_id=model_id, model_kwargs=\n    model_kwargs)\n', (2044, 2124), False, 'from langchain_community.llms import Bedrock\n'), ((2447, 2464), 'llama_index.OpenAIEmbedding', 'OpenAIEmbedding', ([], {}), '()\n', (2462, 2464), False, 'from llama_index import ServiceContext, OpenAIEmbedding, PromptHelper\n'), ((3049, 3078), 'logging.info', 'logging.info', (['"""folder exists"""'], {}), "('folder exists')\n", (3061, 3078), False, 'import logging\n'), ((3094, 3120), 'os.path.isdir', 'os.path.isdir', (['folder_path'], {}), '(folder_path)\n', (3107, 3120), False, 'import os\n'), ((4457, 4484), 'os.path.exists', 'os.path.exists', (['folder_path'], {}), '(folder_path)\n', (4471, 4484), False, 'import os\n'), ((7738, 7776), 'os.path.join', 'os.path.join', (['directory_path', 'filename'], {}), '(directory_path, filename)\n', (7750, 7776), False, 'import os\n'), ((7812, 7832), 'os.remove', 'os.remove', (['file_path'], {}), '(file_path)\n', (7821, 7832), False, 'import os\n'), ((8940, 8971), 'json.dumps', 'json.dumps', (['result'], {'default': 'str'}), '(result, default=str)\n', (8950, 8971), False, 'import json\n'), ((3138, 3166), 'logging.info', 'logging.info', (['"""removing dir"""'], {}), "('removing dir')\n", (3150, 3166), False, 'import logging\n'), ((3183, 3229), 'shutil.rmtree', 'shutil.rmtree', (['folder_path'], {'ignore_errors': '(True)'}), '(folder_path, ignore_errors=True)\n', (3196, 3229), False, 'import shutil\n')]