
    'Ggk;                     z   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZmZmZmZ d dlmZmZmZ d dlmZ d dlmZmZmZ d d	lmZmZmZ d d
l m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1 d Z2dZ3de4fdZ5ddee4   de4fdZ6 G d de'      Z7 G d de      Z8y)    N)ArgumentParser)iglob)Path)AnyCallableDictListOptionalUnioncast)SettingsSimpleDirectoryReaderVectorStoreIndex)BaseEmbedding)RESPONSE_TYPEResponseStreamingResponse)	BaseModelFieldfield_validator)CondenseQuestionChatEngine)IngestionPipeline)LLM)CustomQueryEngine)FnComponent)QueryPipeline)
BaseReader)CompactAndRefine)get_cache_dirc                  T    	 ddl m}   | dd      S # t        $ r t        d      w xY w)Nr   OpenAIzgpt-3.5-turboT)model	streamingz]`llama-index-llms-openai` package not found, please run `pip install llama-index-llms-openai`)llama_index.llms.openair"   ImportErrorr!   s    u/home/kushmeetdev/apache_webroot/langgraph_flaskproject/venv/lib/python3.12/site-packages/llama_index/cli/rag/base.py_try_load_openai_llmr(   !   s8    
2Ot<< 
?
 	

s    'zfiles_history.txtreturnc                  @    t        t        t                     dz        S )Nrag_cli)strr   r        r'   default_ragcli_persist_dirr/   0   s    tMO$y011r.   	query_strc                     | xs dS )N r-   r0   s    r'   query_inputr4   4   s    ?r.   c                   J    e Zd ZU  ed      Zeed<   dedefdZ	dedefdZ
y)	QueryPipelineQueryEngineQuery Pipeline to use for Q&A.descriptionquery_pipeliner0   r)   c                 :    | j                   j                  |      S Nr3   )r:   runselfr0   s     r'   custom_queryz%QueryPipelineQueryEngine.custom_query=   s    ""&&&;;r.   c                 V   K   | j                   j                  |       d {   S 7 wr<   )r:   arunr>   s     r'   acustom_queryz&QueryPipelineQueryEngine.acustom_query@   s&     ((--	-BBBBs    )')N)__name__
__module____qualname__r   r:   r   __annotations__r,   r   r@   rC   r-   r.   r'   r6   r6   8   s?    $)4%NM <c <m <CS C] Cr.   r6   c                   :   e Zd ZU dZ ed      Zeed<    edd      Ze	ed<    ed	e

      Zeed<    edd 
      Zeed<    edd      Zee   ed<    edd      Zee   ed<    edd      Zeeeef      ed<    G d d      Z edd      dedeeef   dee   fd       Z edd      dedeeef   dee   fd       Z	 	 	 	 	 	 d+deee      dee   d e	de	d!e	d"e	d#eeef   ddfd$Zdeddfd%Zd,d&Ze d'e!e"ef   d(ee#g d f      ddfd)       Z$d,d*Z%y)-RagCLIzW
    CLI tool for chatting with output of a IngestionPipeline via a QueryPipeline.
    z,Ingestion pipeline to run for RAG ingestion.r8   ingestion_pipeline:Whether to print out verbose information during execution.F)r9   defaultverbosez(Directory to persist ingestion pipeline.)r9   default_factorypersist_dirz.Language model to use for response generation.c                      t               S N)r(   r-   r.   r'   <lambda>zRagCLI.<lambda>V   s
     4 6 r.   llmr7   Nr:   z Chat engine to use for chatting.chat_enginez5File extractor to use for extracting text from files.file_extractorc                       e Zd ZdZy)RagCLI.ConfigTN)rD   rE   rF   arbitrary_types_allowedr-   r.   r'   ConfigrW   e   s    "&r.   rY   before)modevaluesr)   c                    ||S t        t        |d         }|j                  yt        t        |d         }t	        t
        ddh      }t        t        |d         }d}|j                  %|j                  D ]  }t        |t              s|} n |t        _        |t        _        t        j                  |j                        j                  d	      }	t!        d
|      }
t#        |      }|j%                  ||	|
d       |j'                  dd       |j'                  ddd       |j'                  ddd       |S )zX
        If query_pipeline is not provided, create one from ingestion_pipeline.
        NrJ   rM   outputr0   )fn
output_key
req_paramsrS      )similarity_top_kT)r$   rM   )rM   )query	retriever
summarizerrd   re   rf   nodes)dest_key)r   r   vector_storeboolr   r4   r   transformations
isinstancer   r   rS   embed_modelr   from_vector_storeas_retrieverr   r   add_modulesadd_link)clsr:   r\   rJ   rM   query_componentrS   rm   transformationre   response_synthesizers              r'   &query_pipeline_from_ingestion_pipelinez-RagCLI.query_pipeline_from_ingestion_pipelineh   sU    %!!!"3V<P5QR**2tVI./%x[M
 3u& --9"4"D"D nm<"0K
 *$66++

,,
* 	  0$P 'w7""(&2	
 	5\GLLr.   c                    ||S |j                  d      | j                  d|      |d<   t        t        |d         }|yt	        |      }t        t
        |d         }t        t        |d         }t        j                  |||      S )zQ
        If chat_engine is not provided, create one from query_pipeline.
        Nr:   )r:   r\   )r:   rM   rS   )query_enginerS   rM   )	getrv   r   r   r6   rj   r   r   from_defaults)rr   rT   r\   r:   rx   rM   rS   s          r'   chat_engine_from_query_pipelinez&RagCLI.chat_engine_from_query_pipeline   s     "::&'/'*'Q'Q#F (R (F#$ mV4D-EF!/~NtVI./3u&)77%3
 	
r.   filesquestionchatclearcreate_llamakwargsc                 (  K   |rt         j                  j                  | j                        rht	        d| j                   d      }|j                         j                         dk7  rt        d       yt        j                  d| j                          t        d| j                          || _	        t        t        | j                        }	| j                  rt        d| j                         |Cg }
|D ]  }|
j                  t        |d	
               g }|
D ]  }t         j                  j                  |      }t         j                  j!                  |      rt#        |d	| j$                        }nt#        |gd	| j$                        }|j                  |j'                  |              |	j)                  ||       d{    |	j+                  | j                         t-        | j                   dt.         d      5 }|D ]  }|j1                  t3        |      dz          ! 	 ddd       |r{t5        j6                  d      t        d       nYt9        | j                   dt.               }|j                         st        d       nt-        |      5 }|D ch c]#  }|j                         s|j                         % }}ddd       t;              dk(  rt        d       nt;        |      dkD  rt        d       n|j=                         }d|v rt        d       nt         j                  j                  |      st        d| d       nYt        d| d       ddd d!d"d#d$d%d&d'd(d)d*d+t?        j@                  |       g}t        j                  d,jC                  |             || jE                  |       d{    |r| jG                          d{    yy7 ## 1 sw Y   xY wc c}w # 1 sw Y   2xY w7 B7 *w)-z=
        Entrypoint for local document RAG CLI tool.
        z,Are you sure you want to delete data within z? [y/N] yzAborted.Nzrm -rf zSuccessfully cleared z!Saving/Loading from persist_dir: T)	recursive)	input_dirfilename_as_idrU   )input_filesr   rU   )show_progress)r   	documents)rO   /a
npxzI`npx` is not installed. Please install it by calling `npm install -g npx`zLNo data has been ingested, please specify `--files` to create llama dataset.r      zMultiple files or folders were ingested, which is not supported by create-llama. Please call `llamaindex-cli rag --clear` to clear the cache first, then call `llamaindex-cli rag --files` again with a single folder or file*zGlob pattern is not supported by create-llama. Please call `llamaindex-cli rag --clear` to clear the cache first, then call `llamaindex-cli rag --files` again with a single folder or file.z	The path z does not exist. Please call `llamaindex-cli rag --clear` to clear the cache first, then call `llamaindex-cli rag --files` again with a single folder or file.z%Calling create-llama using data from z ...zcreate-llama@latestz
--frontendz
--templater$   z--frameworkfastapiz--uishadcnz--vector-dbnonez--enginecontextz--files  )$ospathexistsrO   inputstriplowerprintsystemrM   r   r   rJ   extendr   abspathisdirr   rU   	load_datarB   persistopenRAG_HISTORY_FILE_NAMEwriter,   shutilwhichr   lenpopshlexquotejoinhandle_questionstart_chat_repl)r?   r|   r}   r~   rM   r   r   r   responserJ   expanded_filespatternr   _filereaderffilehistory_file_pathlinestored_pathsr   command_argss                         r'   
handle_clizRagCLI.handle_cli   s     ww~~d../ B4CSCSBTT\] >>#))+s2*%		GD$4$4#567)$*:*:);<=!"3T5L5LM<<5t7G7GHN  F%%eGt&DEFI' J.77=='2"''+'+':':F 3%*G'+'+':':F   !1!1!1!HIJ" %))9)UUU&&43C3C&D ))*!,A+BCSI .Q! .DGGCI,-.. ||E"*_ %)D,<,<+=Q?T>U)V$W!(//1L
 /0 TAAB'Sdjjl

'S'ST<(A-P \*Q.h  ,//1$;!!m
 "$!5!"+D6 2m !m "$I$t"TU % 5 , , + - ) & ( - & * )"*5;;t+<*= >,L  IIchh|&<=&&x000&&((( M V. .$ (TT T` 1(sz   GP	O,
?P	%O//A3P"P'O<=O<PC:PPP%P&P/O94P<PP	PPc                 ^  K   | j                   t        d      t        t        | j                         }| j                  |_        t        t
        | j                        }|j                  |      }t        |t              r|j                          y t        t        |      }t        |       y w)Nquery_pipeline is not defined.)r:   
ValueErrorr   r   rM   r   rT   r~   rl   r   print_response_streamr   r   )r?   r}   r:   rT   r   s        r'   r   zRagCLI.handle_question/  s     &=>>mT-@-@A!%5t7G7GH##H-h 12**,Hh/H(Os   B+B-c                    K   | j                   t        d      t        t        | j                        }|j                          yw)z;
        Start a REPL for chatting with the agent.
        Nr   )r:   r   r   r   rT   streaming_chat_repl)r?   rT   s     r'   r   zRagCLI.start_chat_repl=  s?      &=>>5t7G7GH'')s   AAparserinstance_generatorc                 D   r|j                  ddt        dd       |j                  ddt        dd	
       |j                  dddd       |j                  dddd       |j                  ddd       |j                  dddd       |j                  fd       y y )Nz-qz
--questionzThe question you want to ask.F)typehelprequiredz-fz--files+zuThe name of the file(s) or directory you want to ask a question about,such as "file.pdf". Supports globs like "*.py".)r   nargsr   z-cz--chatz&If flag is present, opens a chat REPL.
store_true)r   actionz-vz	--verboserK   z--clearz'Clears out all currently embedded data.z--create-llamaz8Create a LlamaIndex application with your embedded data.)r   r   r   c           	      j    t        j                           j                  di t        |             S )Nr-   )asyncior=   r   vars)argsr   s    r'   rR   z(RagCLI.add_parser_args.<locals>.<lambda>w  s+    '++3&(33Ad4jA# r.   )func)add_argumentr,   set_defaults)rr   r   r   s     `r'   add_parser_argszRagCLI.add_parser_argsF  s     4    F   	 =#	    Q#	    >#   
  O#	       U r.   c                      t        d      }|j                  ddd      }|j                  dd	      } j                  | fd
       |j	                         }|j                  |       y)z*
        Entrypoint for CLI tool.
        zLlamaIndex RAG Q&A tool.r8   commandscommandT)titledestr   ragz8Ask a question to a document / a directory of documents.)r   c                       S rQ   r-   )r?   s   r'   rR   zRagCLI.cli.<locals>.<lambda>  s    d r.   N)r   add_subparsers
add_parserr   
parse_argsr   )r?   r   
subparsersllamarag_parserr   s   `    r'   cliz
RagCLI.cli|  sv      ,FG**9t + 

 %//R 0 
 	_l;  " 			$r.   )NNFFFF)r)   N)&rD   rE   rF   __doc__r   rJ   r   rG   rM   rj   r/   rO   r,   rS   r   r:   r
   r   rT   r   rU   r   r   rY   r   r   rv   r{   r	   r   r   r   classmethodr   r   r   r   r   r-   r.   r'   rI   rI   D   sG    -2B-)  PGT  >2K  D6C  /44/NH]+  9>69K45  7<K7NHT#z/23 
' ' %H5. .*.sCx..	-	 . 6.` ]2

'+CH~
	,	-
 3
4 &*"&"{)S	"{) 3-{) 	{)
 {) {) {) sCx.{) 
{)zc d * 3nc)*3 %Xb(l%;<3 
	3 3jr.   rI   rQ   )9r   r   r   r   argparser   globr   pathlibr   typingr   r   r   r	   r
   r   r   llama_index.corer   r   r   %llama_index.core.base.embeddings.baser   %llama_index.core.base.response.schemar   r   r    llama_index.core.bridge.pydanticr   r   r   llama_index.core.chat_enginer   llama_index.core.ingestionr   llama_index.core.llmsr   llama_index.core.query_enginer   3llama_index.core.query_pipeline.components.functionr   %llama_index.core.query_pipeline.queryr   llama_index.core.readers.baser   &llama_index.core.response_synthesizersr   llama_index.core.utilsr   r(   r   r,   r/   r4   r6   rI   r-   r.   r'   <module>r      s     	   #   C C C 
 @ 
 O N C 8 % ; K ? 4 C 0	
 , 2C 28C= C 	C0 	CHY Hr.   