
    h                         S SK r S SKrS SKrS SKJrJrJrJr  S SKr	S SK
r
S SKJr  S SKJrJrJrJrJrJr  S SKJr  S SKJr  S SKJrJr  S SKJr  S S	KJr  S S
KJ r   S SK!J"r"  SSK#J$r$J%r%J&r&   " S S5      r'g)    N)ListDictAnyOptional)settings)QSumCountAvgMaxMin)SentenceTransformer)
ChatOpenAI)HumanMessageSystemMessage)RecursiveCharacterTextSplitter)HuggingFaceEmbeddings)FAISS)Document   )DevelopmentApplicationPDFDocumentExtractedPDFDatac                      \ rS rSrS rS\\   4S jrS rS r	SS\
S\S\\   4S	 jjrS\
S
\\   S\
4S jrS\
S\\
\4   4S jrS\
S\4S jrS\
S\\
\4   4S jrS\\
\4   4S jrS\\
\4   4S jrS\\
\4   4S jrS\\
\4   4S jrS\\
\4   4S jrS\\
\4   4S jrS\\
\4   4S jrS\
S\4S jrS\
S\\
\4   4S jrS\\
\4   4S jrS\\
\4   4S jrS\\
\4   4S jrSr g)RAGChatbotService   c                     [        S5      U l        [        SS[        R                  S9U l        [        R                  R                  [        R                  S5      U l
        S U l        [        SS[        S9U l        g )	Nall-MiniLM-L6-v2zgpt-4o-2024-05-13g?)modeltemperatureapi_keyfaiss_index     )
chunk_sizechunk_overlaplength_function)r   embedding_modelr   r   OPENAI_API_KEYllmospathjoin
MEDIA_ROOTvector_store_pathvector_storer   lentext_splitter)selfs    )/var/www/html/shao/shaoApp/rag_service.py__init__RAGChatbotService.__init__   sh    23EF%++

 "$h.A.A=!Q ;
    returnc                    / n[         R                  R                  5       R                  S5      R	                  5       R                  S5      nU GH  nUR                  b  SUR                  S 3OSnSR                  / SPUR                  =(       d    S	 PS
PUR                  =(       d    S	 PSPUR                  =(       d    S	 PSPUR                  =(       d    S	 PSPUR                  =(       d    S	 PSPU PSPUR                  =(       d    S	 PSPUR                  =(       d    S	 PSPUR                  =(       d    S	 PSPUR                   =(       d    S	 PSPUR"                  =(       d    S	 PSPUR$                  =(       d    S	 PSPUR&                  =(       d    S	 PSPUR(                  =(       d    S	 PSPUR*                  =(       d    S PSP5      nUR-                  [/        USUR                  UR                  SS.S95        UR0                  R	                  5        GH4  n UR2                  nU(       Ga  SR                  / SPUR4                  =(       d    S	 PSPUR6                  =(       d    S	 PS PUR8                  =(       d    S PS!PUR:                  =(       d    S PS"PUR<                  =(       d    S PS#PUR>                  =(       d    S PS$PUR@                  =(       d    S PS%PURB                  =(       d    S PS&PURD                  =(       d    S PS'PURF                  =(       d    S PS(PURH                  =(       d    S PS)PURJ                  =(       d    S PS*PURL                  =(       d    S PS+PURN                  =(       d    S PS,PURP                  =(       d    S PS-PURR                  =(       d    S PS.PURT                  =(       d    S PS/PURV                  =(       d    S PS0PURX                  =(       d    S PS1P5      nUR-                  [/        US2UR                  UR4                  UR6                  S3S4.S95        GM4  GM7     GM     U$ ! [Z         a  n	 Sn	A	GMQ  Sn	A	ff = f)5z4Create documents from the database for vectorizationpdf_documents__extracted_dataapplication_idN$,.2fNot specified u   
            **Development Application Summary**
            
            **Application Details:**
            • **Application ID:** Unknownu   
            • **Council:** u   
            • **Decision:** u%   
            • **Lodgement Date:** u&   
            • **Determined Date:** u#   
            • **Project Cost:** uX   
            
            **Development Specifications:**
            • **Category:** u   
            • **Land Use:** u'   
            • **Development Type:** u*   
            • **Number of Dwellings:** u(   
            • **Number of Storeys:** u'   
            • **Number of Places:** u+   
            • **Number of Apartments:** u-   
            • **Number of Subdivisions:** u[   
            
            **Additional Information:**
            • **Application URL:** zNot availablez
            application_metadatametadata)sourcer;   council_nametype)page_contentrB   u   
                        **PDF Document Information**
                        
                        **Document Details:**
                        • **File Name:** u0   
                        • **Document Type:** u~   
                        
                        **Property Information:**
                        • **Land Description:** u8   
                        • **Registered Proprietor:** u/   
                        • **Encumbrances:** uy   
                        
                        **Development Details:**
                        • **Proposed Use:** u.   
                        • **Description:** uv   
                        
                        **Contact Information:**
                        • **Applicant:** u1   
                        • **Contact Person:** u2   
                        • **Contact Address:** u0   
                        • **Contact Email:** u0   
                        • **Contact Phone:** uu   
                        
                        **Site Specifications:**
                        • **Lot Size:** u0   
                        • **Site Coverage:** u-   
                        • **Total Area:** u4   
                        • **Ground Floor Area:** u3   
                        • **First Floor Area:** u5   
                        • **Private Open Space:** u>   
                        • **Secluded Private Open Space:** z
                        pdf_extracted_dataextracted_data)rC   r;   pdf_filedocument_typerE   ).r   objectsselect_relatedprefetch_relatedallorder_bycostr-   r;   rD   decisionlodgement_datedetermined_datecategoryland_usedevelopment_typenumber_of_dwellingsnumber_of_storeysnumber_of_placesnumber_of_apartmentsnumber_of_subdivisionsapplication_urlappendr   pdf_documentsrH   	file_namerJ   land_descriptionregistered_proprietorencumbrancesproposed_usedescriptionapplicant_namecontact_namecontact_addresscontact_emailcontact_phonelot_sizesite_coverage
total_areaground_floor_areafirst_floor_areaposspos	Exception)
r3   	documentsapplicationsappcost_strapp_metadatapdf_doc	extractedpdf_contentes
             r4   create_documents_from_database0RAGChatbotService.create_documents_from_database#   s   	 .55DDFWW+

#%)* 	  C /2hh.B388D/*H   %  &)%7%7%D9$E	F	
 "..;)<
=  #||8y9:% &)%7%7%D9$EF& '*&9&9&FY%GH# $,*-  #||8y9:  #||8y9:' (+';';'Hy&IJ* +.*A*A*NY)OP(  ),(=(=(J'K! L'!" (+';';'Hy&I#"J+#$ ,/+C+C+Py*Q%$R-%& .1-G-G-T9,U'&V&', '*&9&9&L_%M-,N-L2 X)4&)&8&8$'$4$4&	  ,,0021 ' 6 6I ' ' +, ' -4,=,=,J+K	'L0	'
 180E0E0R/S'
T3' 4=3M3M3`Q`2a'b8' 9B8W8W8j[j7k'l/' 09/E/E/X.Y'Z/' 09/E/E/X.Y'Z.' /8.C.C.V-W'X,'" -6,D,D,W+X#'"Y1#'$ 2;1G1G1Z?0[%'$\2%'& 3<2K2K2^1_''&`0''( 1:0G0G0Z?/[)'(\0)'* 1:0G0G0Z?/[+'*\++'0 ,5+=+=+P*Q1'0R01'2 1:0G0G0Z?/[3'2\-3'4 .7-A-A-T_,U5'4V45'6 5>4O4O4bSb3c7'6d37'8 4=3M3M3`Q`2a9'8b59': 6?]]5Uo4V;':W>;'< ?Hnn>_P_=`='<a='B "(()4*>252D2D,3,=,=181F1F(8&	* 	G ! 3S  z 	 ! s   +H,Q&&
Q<7Q<c                    [        S5        U R                  5       nU(       d  [        S5        g[        S[        U5       S35        U R                  R	                  U5      n[        S[        U5       S35        [        SS	S
0S9n[        R                  " X#5      U l        [        R                  " U R                  SS9  U R                  R                  U R                  5        [        SU R                   35        g)z%Build and save the FAISS vector storez&Building vector store from database...zNo documents found in databaseNz
Splitting z documents into chunks...zCreated z chunksr   devicecpu
model_namemodel_kwargsT)exist_okzVector store saved to )printr{   r1   r2   split_documentsr   r   from_documentsr0   r+   makedirsr/   
save_local)r3   rr   
split_docs
embeddingss       r4   build_vector_store$RAGChatbotService.build_vector_store   s    67 779	23 	
3y>**CDE''77	B
Z)12 +)"E*

 "00H 	D**T:$$T%;%;<&t'='=&>?@r7   c                     [         R                  R                  U R                  5      (       a=  [	        SSS0S9n[
        R                  " U R                  USS9U l        [        S5        gg)	z$Load the existing FAISS vector storer   r~   r   r   T)allow_dangerous_deserializationz Vector store loaded successfullyF)	r+   r,   existsr/   r   r   
load_localr0   r   )r3   r   s     r4   load_vector_store#RAGChatbotService.load_vector_store   se    77>>$0011.-&.J !& 0 0&&04!D
 45r7   querykc                 ^  ^ U R                   (       d%  U R                  5       (       d  U R                  5         U R                   (       d  / $ UR                  5       m[	        U4S jS 5       5      (       a  SnO[	        U4S jS 5       5      (       a  SnU R                   R                  XS9nU$ )z0Search for similar documents in the vector storec              3   *   >#    U H	  oT;   v   M     g 7fN ).0wordquery_lowers     r4   	<genexpr>=RAGChatbotService.search_similar_documents.<locals>.<genexpr>   s       r  0qt{"  0q   )
how manycounttotal
statisticssummaryoverviewlist allshow allall applicationapplication idsr#   c              3   *   >#    U H	  oT;   v   M     g 7fr   r   )r   	indicatorr   s     r4   r   r      s       L  <Kik)  <Kr   avenuestreetroaddrivecourtplacecrescent	boulevardwaylanecloseterracegroveparadewaverleyivanhoedarebinmonashstonningtonwyndham	dandenongbanyuleaddresspropertysitelocation   r   )r0   r   r   loweranysimilarity_search)r3   r   r   similar_docsr   s       @r4   search_similar_documents*RAGChatbotService.search_similar_documents   s      ))++'')  I kkm   r  0q  r  r  rA  L  <K  L  L  LA ((::5:Fr7   context_docsc                     SR                  U Vs/ sH  o3R                  PM     sn5      n[        SU S3S9n[        US9nU R                  R                  XV/5      nUR                  $ s  snf )z2Generate response using LLM with retrieved context

a<  
        You are a knowledgeable and friendly urban planning assistant specializing in development applications and council planning data. Your role is to provide clear, engaging, and well-structured responses about development projects, planning decisions, and council information.

        **Response Guidelines:**
        1. **Be conversational and engaging** - Use a friendly, professional tone
        2. **Structure your responses clearly** - Use bullet points, numbered lists, and sections when appropriate
        3. **Highlight key information** - Use **bold** for important details like costs, dates, and decisions
        4. **Provide context** - Explain what the information means in practical terms
        5. **Format numbers nicely** - Use commas for large numbers and proper currency formatting
        6. **Be comprehensive** - Include relevant details from the context
        7. **Suggest follow-up questions** - End with a helpful suggestion for what they might want to know next
        8. **Handle specific properties** - When asked about specific addresses or properties, focus on that property's details

        **Response Structure:**
        - Start with a direct answer to their question
        - Provide supporting details with clear formatting
        - Use bullet points for lists of applications, types, or statistics
        - Highlight key metrics and important dates
        - For property-specific queries, organize information by property details, development specs, contact info, etc.
        - End with a helpful suggestion or related information

        **Property Query Handling:**
        - When asked about specific properties (addresses, street names, etc.), prioritize that property's information
        - Include all available details: land description, development type, number of apartments/dwellings, GFA, bedroom configurations, etc.
        - If specific details aren't available, clearly state what information is missing
        - Focus on the most relevant and detailed information for that specific property

        **Context Information:**
        z

        Remember: You're helping people understand complex planning information in an accessible way. Make the information engaging and easy to understand!
        )content)r-   rF   r   r   r*   invoker   )r3   r   r   doccontextsystem_messageuser_messageresponses           r4   generate_response#RAGChatbotService.generate_response   s~     ++<H<C//<HI ' 4	8 
	 	90 D $E2 88??N#ABS Is   A)messagec                     U R                  U5      (       a  U R                  U5      $ U R                  U5      (       a  U R                  U5      $ U R	                  USS9nU(       d  S/ SS.$ U R                  X5      n/ nU H  nUR                  R                  SS5      UR                  R                  SS5      UR                  R                  S	S5      UR                  R                  S
S5      S.nUR                  U5        M     UU[        U5      S:  a  SS.$ SS.$ ! [         a  nS[        U5       S3/ SS.s SnA$ SnAff = f)z7Main chat method that combines retrieval and generation   r   u-  I apologize, but I couldn't find any relevant information in my database to answer your question. 🤔

                    **Here are some suggestions to help you get better results:**

                    • **Try rephrasing your question** - Use different keywords or be more specific
                    • **Ask about development applications** - I have data on planning applications, council decisions, and development types
                    • **Query specific councils** - Try asking about "City of Darebin" or other councils in the database
                    • **Ask about development types** - I can help with residential, commercial, industrial, or subdivision projects
                    • **Request statistics** - Ask about application counts, costs, or decision trends

                    **💡 Example questions you can try:**
                    - "How many development applications are in the database?"
                    - "What types of developments are most common?"
                    - "Tell me about applications in City of Darebin"
                    - "What are the recent planning decisions?"
                    - "Show me applications with high costs"

                    Feel free to try again with a different approach! I'm here to help you explore the planning data. 🏗️lowr   sources
confidencer;   r@   rD   rJ   rE   r;   rD   rJ   source_type   highmediumug   I'm sorry, but I encountered a technical issue while processing your request. 😔

**What happened:** u  

**What you can do:**
• **Try again** - Sometimes temporary issues resolve themselves
• **Rephrase your question** - Use simpler or different wording
• **Ask something else** - Try a different type of question about development applications
• **Contact support** - If the issue persists, there might be a system problem

I'm here to help you explore the planning data, so please don't hesitate to try again! 🏗️errorN)_is_statistical_question_handle_statistical_question_is_bulk_listing_request_handle_bulk_listing_requestr   r   rB   getr]   r1   rq   str)r3   r   r   r   r   r   source_inforz   s           r4   chatRAGChatbotService.chat  sy   G	,,W5588AA ,,W5588AA  88A8FL!A$  ""') 0 --gDH G#&)ll&6&67G&S$'LL$4$4^Y$O%(\\%5%5oy%Q#&<<#3#3FI#F	 {+ $ %"(+L(9Q(>f  EM   	!F8 _
b % 	s5   &D" &D" D" -B0D" D" "
E	,E>E	E	c                    ^ UR                  5       m/ SQn[        U4S jU 5       5      (       a  g/ SQn[        U4S jU 5       5      $ )z8Check if the question is asking for statistics or countsr   c              3   *   >#    U H	  oT;   v   M     g 7fr   r   )r   r   message_lowers     r4   r   =RAGChatbotService._is_statistical_question.<locals>.<genexpr>Y  s     O;NiM);Nr   F)r   r   r   r   r   r   rs   projectsdevelopmentscouncilsr   r   r   zall applicationsr   c              3   *   >#    U H	  oT;   v   M     g 7fr   r   r   keywordr   s     r4   r   r   d  s     P;Om+;Or   r   r   )r3   r   property_indicatorsstatistical_keywordsr   s       @r4   r   *RAGChatbotService._is_statistical_questionL  sI    
 O;NOOO 
 P;OPPPr7   c                     UR                  5       nSU;   d  SU;   d  SU;   a  U R                  5       $ SU;   d  SU;   a  U R                  5       $ SU;   d  SU;   a  U R                  5       $ SU;   d  S	U;   a  U R	                  5       $ S
U;   d  SU;   d  SU;   a  U R                  5       $ U R                  5       $ ! [         a  nS[        U5       3/ SS.s SnA$ SnAff = f)z9Handle statistical questions with direct database querieszapplication idr   r   councilr   development typeztypes of developmentrQ   approvalrP   valuebudgetz4I encountered an error while retrieving statistics: r   r   N)	r   _get_all_application_ids_get_council_statistics _get_development_type_statistics_get_decision_statistics_get_cost_statistics_get_comprehensive_statisticsrq   r   r3   r   r   rz   s       r4   r   .RAGChatbotService._handle_statistical_questionf  s   	#MMOM  =04E4VZdhuZu4466m+z]/J3355#}48NR_8_<<>>},
m0K4466=(G},DTaHa0022 99;; 	RSVWXSYRZ[% 	s:   1B: B: B: ,B: !B: *B: :
C CC C c                    [         R                  R                  5       n[        R                  R                  5       n[        R                  R                  5       n[         R                  R                  SSS9R                  5       n[        U Vs/ sH  oU(       d  M  UPM     sn5      n[         R                  R                  SSS9R                  5       n[        U Vs/ sH  o(       d  M  UPM     sn5      n	[         R                  R                  SSS9R                  5       n
[        U
 Vs/ sH  o(       d  M  UPM     sn5      n[         R                  R                  SS9R                  5       n[         R                  R                  SS9R                  [        S5      S	9S
   =(       d    SnSUS SU SUS SUS SU	 SU SUS SUS S3nUSSSSS./SS.$ s  snf s  snf s  snf )z%Get comprehensive database statisticsrD   TflatrV   rQ   Fcost__isnullrP   r   r   r   ud   📊 **Development Applications Database Overview** 🏗️

        **📋 Total Applications:** **,uE   ** development applications

        **🏛️ Council Coverage:** **uD   ** councils in the database
        **📄 Document Processing:** **u?   ** PDF documents processed
        **📊 Data Extraction:** **ur   ** documents with extracted data

        **🏗️ Development Diversity:**
        • **Development Types:** **uJ   ** different types of developments
        • **Decision Categories:** **u<   ** different decision outcomes
        • **Cost Data:** **uN   ** applications with cost information
        • **Total Project Value:** **$r=   u  ** across all projects

        **💡 What you can explore:**
        • Ask about specific councils (e.g., "Tell me about City of Darebin")
        • Query development types (e.g., "Show me residential projects")
        • Check decision trends (e.g., "What are the recent approvals?")
        • Explore high-value projects (e.g., "Show me projects over $1 million")

        This database contains comprehensive planning information to help you understand development patterns and trends! 🚀zDatabase StatisticsAll CouncilsStatistical Summarydatabase_queryr   r   r   )r   rK   r   r   r   values_listdistinctr1   filter	aggregater	   )r3   total_applications
total_pdfstotal_extractedr   ccouncil_countdevelopment_typesdtdevelopment_type_count	decisionsddecision_countapplications_with_cost
total_costr   s                   r4   r	  /RAGChatbotService._get_comprehensive_statistics  s   3;;AAC ((..0
*2288:)11==nSW=Xaac61AQ672::FFGY`dFennp!$3D%K3DRb3D%K!L*22>>zPT>U^^`	8Aaa89!7!?!?!F!FTY!F!Z!`!`!b+33:::NXX_bci_jXklstyxy
((:1'= >))6 8))3A 7%%4Q$7 8& '=%= >((6'7 84Q7 8))3D(9 :B. !+@R`  tI  Zj  k  l 
 	
C 7 &L 9s$   	GG	G#,G#.	G(;G(c                    [         R                  R                  S5      R                  [	        S5      S9R                  SS9R                  S5      nSnUSUR                  5        S	3-  nUS
-  nUSS  H  nUSUS    SUS   S S3-  nM     UR                  5       S:  a  USUR                  5       S-
   S3-  nUSSSSS./SS.$ )zGet council-specific statisticsrD   idr   Fcouncil_name__isnull-countu%   🏛️ **Council Statistics** 📊

**Total Councils:** r   z**Applications by Council:**
N
      • **:** r   r   applications
	
... and z more councilszCouncil Statisticsr  r  r  r   r   r   r   rK   valuesannotater
   r  rO   r   )r3   council_statsr   r   s       r4   r  )RAGChatbotService._get_council_statistics  s   .66==nMVV+ W 

&e&
,XXh-? 	 =*=+>+>+@*AFF44$Sb)G&!8 9gg>Nq=QQ`aaH *  2%*]%8%8%:R%?$@OOH !+?Q_  sH  Yi  j  k 
 	
r7   c                    [         R                  R                  S5      R                  [	        S5      S9R                  SS9R                  S5      nSnUSUR                  5        S	3-  nUS
-  nUSS  H  nUSUS    SUS   S S3-  nM     UR                  5       S:  a  USUR                  5       S-
   S3-  nUSSSSS./SS.$ )zGet development type statisticsrV   r)  r*  Fdevelopment_type__isnullr-  u.   🏗️ **Development Type Statistics** 📊

**Total Development Types:** r   z#**Most Common Development Types:**
Nr/  r0  r1  r   r  r2  r3  z more typeszDevelopment Type Statisticsr  r  r  r   r   r   r4  )r3   dev_type_statsr   dev_types       r4   r  2RAGChatbotService._get_development_type_statistics  s   /77>>?QR[[+ \ 

&%&
0(1C 	 F3N4H4H4J3K4PP::&s+H&*<!= >d8GCTUVBWWfggH , !B&*^%9%9%;b%@$AMMH !+HZh  |Q  br  s  t 
 	
r7   c                    [         R                  R                  S5      R                  [	        S5      S9R                  SS9R                  S5      nSnUSUR                  5        S	3-  nUS
-  nUSS  H  nUSUS    SUS   S S3-  nM     UR                  5       S:  a  USUR                  5       S-
   S3-  nUSSSSS./SS.$ )zGet decision statisticsrQ   r)  r*  Fdecision__isnullr-  u#   📋 **Decision Statistics** 📊

z**Total Decision Categories:** r   z**Decision Outcomes:**
Nr/  r0  r1  r   r  r2  r3  z more decision typeszDecision Statisticsr  r  r  r   r   r   r4  )r3   decision_statsr   rQ   s       r4   r  *RAGChatbotService._get_decision_statistics  s   /77>>zJSS+ T 

&%&
((); 	 ;5n6J6J6L5MTRR..&s+H&*!5 6d8G;LQ:O__H , !B&*^%9%9%;b%@$AAUVVH !+@R`  tI  Zj  k  l 
 	
r7   c                    [         R                  R                  SS9nUR                  [	        S5      S9S   =(       d    SnUR                  [        S5      S9S   =(       d    SnUR                  [        S5      S	9S
   =(       d    SnUR                  [        S5      S9S   =(       d    SnSnUSUR                  5       S S3-  nUS-  nUSUS S3-  nUSUS S3-  nUSUS S3-  nUSUS S3-  nUSSSSS./SS.$ )zGet cost statisticsFr  rP   r  r   r   )avgrF  )maxrG  )minrH  u   💰 **Cost Statistics** 📊

z!**Applications with Cost Data:** r  r   z**Financial Overview:**
u   • **Total Project Value:** $r=   
u   • **Average Project Cost:** $u   • **Highest Project Cost:** $u   • **Lowest Project Cost:** $zCost Statisticsr  r  r  r   r   r   )	r   rK   r  r  r	   r   r   r   r   )r3   
cost_statsr&  avg_costmax_costmin_costr   s          r4   r  &RAGChatbotService._get_cost_statistics  sY   +33:::N
))F)<WEJ
''CK'8?D1''CK'8?D1''CK'8?D167
8H8H8J17MTRR//4Z4ERHH5ht_BGG5ht_BGG4XdO2FF !+<n  pE  Vf  g  h 
 	
r7   c           
          [         R                  R                  5       R                  SS9R	                  SS9R                  S5      nUR                  5       nUS:X  a  S/ SS	.$ S
nUSUS S3-  nUS-  nSn/ n[        SX$5       H  nXXd-    nU H  nUR                  (       a  SUR                  S 3OSn	UR                  =(       d    Sn
UR                  =(       d    SnUR                  =(       d    SnUR                  UR                  U
UU	US.5        M     M     [        US5       H)  u  pmX6S SUS    SUS    S3-  nUS-  S:X  d  M$  US-  nM+     [        S U 5       5      n[        S U 5       5      n[        S U 5       5      nUS -  nUS![!        U5      S S3-  nUS"[!        U5       S3-  nUS#[!        U5       S3-  nUS$[!        U5       S3-  nUS%U(       a  US   S   OS& S3-  nUS'U(       a  US(   S   OS& S3-  nUS)-  nUS*U(       a  US   S   OS+ S,3-  nUS--  nUS.-  nUS/-  nUS0S1S2S3S4./S5S	.$ ! ["         a  nS6[%        U5       3/ S7S	.s S8nA$ S8nAff = f)9zPGet all application IDs with comprehensive information using parallel processingF)application_id__isnullr?   )r;   r;   r   u   ❌ **No Application IDs Found**

I couldn't find any application IDs in the database. This might indicate that the data hasn't been imported yet or there's an issue with the data.r   r   u=   📋 **Complete Development Applications Database** 🏗️

u   **📊 Total Applications:** **r  z** applications

u"   **📋 All Application Details:**
d   r<   r=   r>   r@   )r)  r   rQ   rP   rE   r   3d. **r)  z**  | rE   rI     c              3   (   #    U H	  oS    v   M     g7f)r   Nr   r   rt   s     r4   r   =RAGChatbotService._get_all_application_ids.<locals>.<genexpr><  s     C]cy>]   c              3   (   #    U H	  oS    v   M     g7f)rQ   Nr   rV  s     r4   r   rW  =  s     E}
O}rX  c              3   (   #    U H	  oS    v   M     g7f)rE   Nr   rV  s     r4   r   rW  >  s     A=CK=rX  u!   
**📈 Comprehensive Summary:**
u   • **Total Applications:** u   • **Unique Councils:** u   • **Decision Types:** u   • **Development Types:** u   • **First Application:** zN/Au   • **Last Application:** u   
**💡 Next Steps:**
u4   • Ask about specific applications: "Tell me about z
D-101-2023z"
uC   • Get council statistics: "Show me City of Darebin applications"
uD   • Check development types: "What residential projects are there?"
u1   • Explore costs: "Show me high-value projects"
zAll Applicationsr  zComplete Application Databaser  r   r   z=I encountered an error while retrieving all application IDs: r   N)r   rK   rL   r  excluderO   r   rangerP   rD   rQ   rV   r]   r;   	enumeratesetr1   rq   r   )r3   rs   total_countr   
batch_sizeall_apps_dataibatchrt   ru   r   rQ   r>  app_datar   r"  	dev_typesrz   s                     r4   r  *RAGChatbotService._get_all_application_ids  s\   P	199HHJQQ', R g!  h'(	  ',,.Ka !Y!"'  YH9+aH[\\H==H JM1k6$q~6 C69hh388D/2OH!..;)G"||8yH"33FH!((!00#*$, ( (*  ! 7"  ):fD$(8x?O>PPRSS r6Q;$H  ; C]CCHE}EEIA=AAI=>H6s=7I!6LBOOH3CM?"EEH23y>2B"EEH5c)n5ERHHH5P]mA6Ft6Lch5iikllH4P]]25Ft5Lch4iikllH23HOjwP]^_P`aePf  ~J  PK  KO  P  PH`aHabHNOH %/ASa  uT  eu  v  w$   	[\_`a\b[cd% 	s,   A"I %C&I C4I 
I*I%I*%I*c                     [         R                  R                  5       n[        R                  R                  5       n[        R                  R                  5       n[         R                  R                  SSS9R                  5       nUUU[        U5      [        R                  R                  U R                  5      S.$ ! [         a  nS[        U5      0s SnA$ SnAff = f)z'Get statistics about the knowledge baserD   Tr  )r  total_pdf_documentstotal_extracted_datar   vector_store_existsr   N)r   rK   r   r   r   r  r  listr+   r,   r   r/   rq   r   )r3   r  r  r  r   rz   s         r4   get_statistics RAGChatbotService.get_statistics[  s    	%!7!?!?!E!E!G$,,224J.66<<>O-55AA.W[A\eegH '9'1(7 N')ww~~d6L6L'M   	%SV$$	%s   B=C   
C!
CC!C!c                 V   ^ / SQnUR                  5       m[        U4S jU 5       5      $ )z8Check if the question is asking for bulk listing of data)
r   r   zall ofeveryzcomplete listz	full listzentire databasezall recordszall entriesz	all itemsc              3   *   >#    U H	  oT;   v   M     g 7fr   r   r   s     r4   r   =RAGChatbotService._is_bulk_listing_request.<locals>.<genexpr>u  s     I=m+=r   r   )r3   r   bulk_keywordsr   s      @r4   r   *RAGChatbotService._is_bulk_listing_requestn  s(    
  I=IIIr7   c                 ^    UR                  5       nSU;   d  SU;   a  U R                  5       $ SU;   a  U R                  5       $ SU;   d  SU;   a  U R                  5       $ SU;   a  U R	                  5       $ U R                  5       $ ! [         a  nS[        U5       3/ SS	.s S
nA$ S
nAff = f)z<Handle bulk listing requests with optimized database queriesapplicationr)  r   r   rE   rQ   zBI encountered an error while processing the bulk listing request: r   r   N)r   r  _get_all_councils_list_get_all_development_types_list_get_all_decisions_listr	  rq   r   r
  s       r4   r   .RAGChatbotService._handle_bulk_listing_requestw  s    	#MMOM -1F4466m+2244#}4-8O;;==},3355 99;; 	`adefag`hi% 	s4   +B B B  B 6B 
B,B'!B,'B,c                     [         R                  R                  S5      R                  [	        S5      S9R                  SS9R                  S5      nSnUSUR                  5        S	3-  nUS
-  n[        US5       H  u  p4X#S SUS    SUS   S S3-  nM     USSSSS./SS.$ ! [         a  nS[        U5       3/ SS.s SnA$ SnAff = f)z(Get all councils with application countsrD   r)  r*  Fr+  r-  u+   🏛️ **All Councils in Database** 📊

r.  r   z***Council List with Application Counts:**
r   2drS  ** - r   r  r2  zCouncil Listr  zCouncil Databaser  r   r   r   zError retrieving council list: r   Nr   rK   r5  r6  r
   r  rO   r   r^  rq   r   )r3   r   r   rc  r   rz   s         r4   rw  (RAGChatbotService._get_all_councils_list  s   	-55<<^LUUDk V f%f0(1C  GH.x~~/?.@EEHEEH'!4
fD)@(AwwGWXYFZZijj 5 %/=~  qC  Td  e  f$ 
  	=c!fXF% 	   B$B' '
C1CCCc                     [         R                  R                  S5      R                  [	        S5      S9R                  SS9R                  S5      nSnUSUR                  5        S	3-  nUS
-  n[        US5       H  u  p4X#S SUS    SUS   S S3-  nM     USSSSS./SS.$ ! [         a  nS[        U5       3/ SS.s SnA$ SnAff = f)z%Get all development types with countsrV   r)  r*  Fr:  r-  u4   🏗️ **All Development Types in Database** 📊

r<  r   z/**Development Types with Application Counts:**
r   r|  rS  r}  r   r  r2  zDevelopment Typesr  zDevelopment Type Databaser  r   r   r   z$Error retrieving development types: r   Nr~  )r3   rf  r   rc  r>  rz   s         r4   rx  1RAGChatbotService._get_all_development_types_list  s!   	.66==>PQZZDk [ fef4XXh5G  PH7	8I7J$OOHJJH(A6fD2D)E(FeHU\L]^_K``opp  7 %/BTb  vQ  br  s  t$ 
  	B3q6(K% 	r  c                     [         R                  R                  S5      R                  [	        S5      S9R                  SS9R                  S5      nSnUSUR                  5        S	3-  nUS
-  n[        US5       H  u  p4X#S SUS    SUS   S S3-  nM     USSSSS./SS.$ ! [         a  nS[        U5       3/ SS.s SnA$ SnAff = f)z"Get all decision types with countsrQ   r)  r*  FrA  r-  u.   📋 **All Decision Types in Database** 📊

z**Total Decision Types:** r   z,**Decision Types with Application Counts:**
r   r|  rS  r}  r   r  r2  zDecision Typesr  zDecision Databaser  r   r   r   z!Error retrieving decision types: r   Nr~  )r3   r"  r   rc  rQ   rz   s         r4   ry  )RAGChatbotService._get_all_decisions_list  s   	.66==jIRRDk S fef,XXh-?  JH4Y__5F4GtLLHGGH(A6fD*)=(>eHWDUVWCXXghh  7 %/?Q_  sF  Wg  h  i$ 
  	?AxH% 	r  )r(   r*   r2   r0   r/   N)r   )!__name__
__module____qualname____firstlineno__r5   r   r   r{   r   r   r   intr   r   r   r   r   boolr   r   r	  r  r  r  r  r  rm  r   r   rw  rx  ry  __static_attributes__r   r7   r4   r   r      s   
fX fPA< c c $x. 2- s - $x. - S - ^IC IDcN IVQ Q Q4C DcN 6,
tCH~ ,
\
c3h 
,
$sCx. 
,
$sCx. 
,
d38n 
,R$sCx. Rh%S#X %&J J JC DcN 2S#X 4c3h 4c3h r7   r   )(r+   jsonpickletypingr   r   r   r   numpynpfaissdjango.confr   django.db.modelsr   r	   r
   r   r   r   sentence_transformersr   langchain_openair   langchain.schemar   r   langchain.text_splitterr   langchain_community.embeddingsr    langchain_community.vectorstoresr   langchain.docstore.documentr   modelsr   r   r   r   r   r7   r4   <module>r     sJ    	   , ,     9 9 5 ' 8 B @ 2 0 I II Ir7   