code: from rag.agents.interface import Pipeline
  from llama_index.core.program import LLMTextCompletionProgram
  import json
  from llama_index.llms.ollama import Ollama
  from typing import List
  from pydantic import create_model
  from rich.progress import Progress, SpinnerColumn, TextColumn
  import requests
  import warnings
  import box
  import yaml
  import timeit
  from rich import print
  from typing import Any
  
  
  warnings.filterwarnings("ignore", category=DeprecationWarning)
  warnings.filterwarnings("ignore", category=UserWarning)
  
  
  # Import config vars
  with open('config.yml', 'r', encoding='utf8') as ymlfile:
      cfg = box.Box(yaml.safe_load(ymlfile))
  
  
  class VProcessorPipeline(Pipeline):
      def run_pipeline(self,
                       payload: str,
                       query_inputs: [str],
                       query_types: [str],
                       query: str,
                       file_path: str,
                       index_name: str,
                       debug: bool = False,
                       local: bool = True) -> Any:
          print(f"\nRunning pipeline with {payload}\n")
  
          start = timeit.default_timer()
  
          if file_path is None:
              raise ValueError("File path is required for vprocessor pipeline")
  
          with open(file_path, "rb") as file:
              files = {'file': (file_path, file, 'image/jpeg')}
  
              data = {
                  'image_url': ''
              }
  
              response = self.invoke_pipeline_step(lambda: requests.post(cfg.VPROCESSOR_OCR_ENDPOINT,
                                                                         data=data,
                                                                         files=files,
                                                                         timeout=180),
                                                   "Running OCR...",
                                                   local)
  
          if response.status_code != 200:
              print('Request failed with status code:', response.status_code)
              print('Response:', response.text)
  
              return "Failed to process file. Please try again."
  
          end = timeit.default_timer()
          print(f"Time to run OCR: {end - start}")
  
          start = timeit.default_timer()
  
          data = response.json()
  
          ResponseModel = self.invoke_pipeline_step(lambda: self.build_response_class(query_inputs, query_types),
                                                    "Building dynamic response class...",
                                                    local)
  
          prompt_template_str = """\
          """ + query + """\
          using this structured data, coming from OCR {document_data}.\
          """
  
          llm_ollama = self.invoke_pipeline_step(lambda: Ollama(model=cfg.LLM_VPROCESSOR,
                                                                base_url=cfg.OLLAMA_BASE_URL_VPROCESSOR,
                                                                temperature=0,
                                                                request_timeout=900),
                                                 "Loading Ollama...",
                                                 local)
  
          program = LLMTextCompletionProgram.from_defaults(
              output_cls=ResponseModel,
              prompt_template_str=prompt_template_str,
              llm=llm_ollama,
              verbose=True,
          )
  
          output = self.invoke_pipeline_step(lambda: program(document_data=data),
                                             "Running inference...",
                                             local)
  
          answer = self.beautify_json(output.model_dump_json())
  
          end = timeit.default_timer()
  
          print(f"\nJSON response:\n")
          print(answer + '\n')
          print('=' * 50)
  
          print(f"Time to retrieve answer: {end - start}")
  
          return answer
  
      def prepare_files(self, file_path, file):
          if file_path is not None:
              with open(file_path, "rb") as file:
                  files = {'file': (file_path, file, 'image/jpeg')}
  
                  data = {
                      'image_url': ''
                  }
          else:
              files = {'file': (file.filename, file.file, file.content_type)}
  
              data = {
                  'image_url': ''
              }
          return data, files
  
  
      # Function to safely evaluate type strings
      def safe_eval_type(self, type_str, context):
          try:
              return eval(type_str, {}, context)
          except NameError:
              raise ValueError(f"Type '{type_str}' is not recognized")
  
      def build_response_class(self, query_inputs, query_types_as_strings):
          # Controlled context for eval
          context = {
              'List': List,
              'str': str,
              'int': int,
              'float': float
              # Include other necessary types or typing constructs here
          }
  
          # Convert string representations to actual types
          query_types = [self.safe_eval_type(type_str, context) for type_str in query_types_as_strings]
  
          # Create fields dictionary
          fields = {name: (type_, ...) for name, type_ in zip(query_inputs, query_types)}
  
          DynamicModel = create_model('DynamicModel', **fields)
  
          return DynamicModel
  
      def invoke_pipeline_step(self, task_call, task_description, local):
          if local:
              with Progress(
                      SpinnerColumn(),
                      TextColumn("[progress.description]{task.description}"),
                      transient=False,
              ) as progress:
                  progress.add_task(description=task_description, total=None)
                  ret = task_call()
          else:
              print(task_description)
              ret = task_call()
  
          return ret
  
  
      def beautify_json(self, result):
          try:
              # Convert and pretty print
              data = json.loads(str(result))
              data = json.dumps(data, indent=4)
              return data
          except (json.decoder.JSONDecodeError, TypeError):
              print("The response is not in JSON format:\n")
              print(result)
  
          return {}
apis: llama_index.core.program.LLMTextCompletionProgram.from_defaults llama_index.llms.ollama.Ollama
extract_api: [((396, 458), 'warnings.filterwarnings', 'warnings.filterwarnings', (['"""ignore"""'], {'category': 'DeprecationWarning'}), "('ignore', category=DeprecationWarning)\n", (419, 458), False, 'import warnings\n'), ((459, 514), 'warnings.filterwarnings', 'warnings.filterwarnings', (['"""ignore"""'], {'category': 'UserWarning'}), "('ignore', category=UserWarning)\n", (482, 514), False, 'import warnings\n'), ((614, 637), 'yaml.safe_load', 'yaml.safe_load', (['ymlfile'], {}), '(ymlfile)\n', (628, 637), False, 'import yaml\n'), ((1029, 1076), 'rich.print', 'print', (['f"""\nRunning pipeline with {payload}\n"""'], {}), '(f"""\nRunning pipeline with {payload}\n""")\n', (1034, 1076), False, 'from rich import print\n'), ((1092, 1114), 'timeit.default_timer', 'timeit.default_timer', ([], {}), '()\n', (1112, 1114), False, 'import timeit\n'), ((2116, 2138), 'timeit.default_timer', 'timeit.default_timer', ([], {}), '()\n', (2136, 2138), False, 'import timeit\n'), ((2147, 2187), 'rich.print', 'print', (['f"""Time to run OCR: {end - start}"""'], {}), "(f'Time to run OCR: {end - start}')\n", (2152, 2187), False, 'from rich import print\n'), ((2205, 2227), 'timeit.default_timer', 'timeit.default_timer', ([], {}), '()\n', (2225, 2227), False, 'import timeit\n'), ((3157, 3296), 'llama_index.core.program.LLMTextCompletionProgram.from_defaults', 'LLMTextCompletionProgram.from_defaults', ([], {'output_cls': 'ResponseModel', 'prompt_template_str': 'prompt_template_str', 'llm': 'llm_ollama', 'verbose': '(True)'}), '(output_cls=ResponseModel,\n    prompt_template_str=prompt_template_str, llm=llm_ollama, verbose=True)\n', (3195, 3296), False, 'from llama_index.core.program import LLMTextCompletionProgram\n'), ((3628, 3650), 'timeit.default_timer', 'timeit.default_timer', ([], {}), '()\n', (3648, 3650), False, 'import timeit\n'), ((3660, 3690), 'rich.print', 'print', (['f"""\nJSON response:\n"""'], {}), '(f"""\nJSON response:\n""")\n', (3665, 3690), False, 'from rich import print\n'), ((3697, 3717), 'rich.print', 'print', (["(answer + '\\n')"], {}), "(answer + '\\n')\n", (3702, 3717), False, 'from rich import print\n'), ((3726, 3741), 'rich.print', 'print', (["('=' * 50)"], {}), "('=' * 50)\n", (3731, 3741), False, 'from rich import print\n'), ((3751, 3799), 'rich.print', 'print', (['f"""Time to retrieve answer: {end - start}"""'], {}), "(f'Time to retrieve answer: {end - start}')\n", (3756, 3799), False, 'from rich import print\n'), ((5158, 5196), 'pydantic.create_model', 'create_model', (['"""DynamicModel"""'], {}), "('DynamicModel', **fields)\n", (5170, 5196), False, 'from pydantic import create_model\n'), ((1927, 1990), 'rich.print', 'print', (['"""Request failed with status code:"""', 'response.status_code'], {}), "('Request failed with status code:', response.status_code)\n", (1932, 1990), False, 'from rich import print\n'), ((2003, 2036), 'rich.print', 'print', (['"""Response:"""', 'response.text'], {}), "('Response:', response.text)\n", (2008, 2036), False, 'from rich import print\n'), ((5657, 5680), 'rich.print', 'print', (['task_description'], {}), '(task_description)\n', (5662, 5680), False, 'from rich import print\n'), ((5884, 5910), 'json.dumps', 'json.dumps', (['data'], {'indent': '(4)'}), '(data, indent=4)\n', (5894, 5910), False, 'import json\n'), ((2719, 2832), 'llama_index.llms.ollama.Ollama', 'Ollama', ([], {'model': 'cfg.LLM_VPROCESSOR', 'base_url': 'cfg.OLLAMA_BASE_URL_VPROCESSOR', 'temperature': '(0)', 'request_timeout': '(900)'}), '(model=cfg.LLM_VPROCESSOR, base_url=cfg.OLLAMA_BASE_URL_VPROCESSOR,\n    temperature=0, request_timeout=900)\n', (2725, 2832), False, 'from llama_index.llms.ollama import Ollama\n'), ((6005, 6051), 'rich.print', 'print', (['"""The response is not in JSON format:\n"""'], {}), "('The response is not in JSON format:\\n')\n", (6010, 6051), False, 'from rich import print\n'), ((6064, 6077), 'rich.print', 'print', (['result'], {}), '(result)\n', (6069, 6077), False, 'from rich import print\n'), ((1457, 1536), 'requests.post', 'requests.post', (['cfg.VPROCESSOR_OCR_ENDPOINT'], {'data': 'data', 'files': 'files', 'timeout': '(180)'}), '(cfg.VPROCESSOR_OCR_ENDPOINT, data=data, files=files, timeout=180)\n', (1470, 1536), False, 'import requests\n'), ((5364, 5379), 'rich.progress.SpinnerColumn', 'SpinnerColumn', ([], {}), '()\n', (5377, 5379), False, 'from rich.progress import Progress, SpinnerColumn, TextColumn\n'), ((5401, 5455), 'rich.progress.TextColumn', 'TextColumn', (['"""[progress.description]{task.description}"""'], {}), "('[progress.description]{task.description}')\n", (5411, 5455), False, 'from rich.progress import Progress, SpinnerColumn, TextColumn\n')]