
    j=                     B   S SK r S SKrS SKJr  S SKJr  S SKJr  S SKJ	r	  Sr
S rSS jr\S	:X  aQ  S
rSr \" \\5      u  rrrr\" S\5        \" S\S S35        \" S\ 35        \" S5        \ H  r\" S\ 35        M     gg! \ a  r\" S\ 35         SrCgSrCff = f)    N)OllamaEmbeddings)FAISS)ChatGroq)PromptTemplate8gsk_CEh3itIpUAkEkEKsUDqVWGdyb3FYoTjqmXNTBHOSxJFK3obGTzXZc                 X   [        SS9nSU  3n[        R                  R                  U5      (       d  [	        SU  35      eS n[        R
                  " U5       H  n[        R                  R                  X$5      n[        R                  R                  U5      (       d  MH  [        R                  " XQSS9nUR                  R                  R                  5        H  nXGR                  S'   M     Uc  UnM  UR                  U5        M     Uc  [	        SU  35      e[        S	U  35        U$ )
Nznomic-embed-text)modelzmy_embeddings/zNo embeddings found for client T)allow_dangerous_deserializationreference_idz%No valid embeddings found for client z!All embeddings loaded for client )r   ospathexistsFileNotFoundErrorlistdirjoinisdirr   
load_localdocstore_dictvaluesmetadata
merge_fromprint)	client_id
embeddings
client_dirall_vectorsr   folder_pathvectorsdocs           C/var/www/eduai.edurigo.com/question_generate/production/chatting.pyload_all_client_embeddingsr"      s   !(:;J!)-J77>>*%%"A) MNNK

:.ggll:<77==%%&&{`deG ''--446/;^, 7 "%&&w/ / "G	{ STT	-i[
9:    c                    [         R                   " 5       n [        U 5      n[	        S[
        US9nSn[        USS/S	9nUR                  S
S0S9n	U	R                  U5      n
SR                  U
 Vs/ s H  oR                  PM     sn5      nUR                  XS9nUR                  U5      n[         R                   " 5       nX-
  nUR                  n[        S U
 5       5      [        UR                  5       5      -   nU
 Vs/ s H  oR                   R#                  SS5      PM      nnUUUU4$ ! [         a  n[        SU 35        S/ S4s S nA$ S nAff = fs  snf s  snf )NzError: zDI couldn't access the necessary information to answer your question.r   zLlama3-8b-8192)
model_namegroq_api_key
max_tokensa  You are a highly intelligent and professional AI assistant. Use the following context to answer the question. If the answer is not in the context, say "I don't have enough information to answer that question."

Context: {context}

Question: {question}

Answer: contextquestion)templateinput_variablesk   )search_kwargsz

)r(   r)   c              3   h   #    U  H(  n[        UR                  R                  5       5      v   M*     g 7f)N)lenpage_contentsplit).0r    s     r!   	<genexpr>"answer_question.<locals>.<genexpr>S   s'     Ls3++11344s   02r   Unknown)timer"   r   r   r   GROQ_API_KEYr   as_retrieverinvoker   r1   formatcontentsumr0   r2   r   get)r   r)   r'   
start_timer   ellmr*   prompt	retrieversource_docsr    r(   formatted_promptresponseend_timeprocessing_timeanswertotal_tokenssource_reference_idss                       r!   answer_questionrL   (   sw   J],Y7
 #!CH "J/F $$C8$<I""8,K kk{C{++{CDG }}W}Hzz*+Hyy{H+OFLLLsS[SaSaScOddL T__S^CLL,,^YGS^_'FFW  ]smUWY[\\\]8 D `s)   D. 0E%E.
E8E
EE__main__   zWhat are the types of leaves?zAnswer:z
Chatting process took z.2fz secondsz#Total tokens (question + context): z
Source documents:z- zAn error occurred: )   )r   r7   langchain_ollamar    langchain_community.vectorstoresr   langchain_groqr   langchain_core.promptsr   r8   r"   rL   __name__r   r)   rI   rD   rJ   rH   r   r    	Exceptionr@    r#   r!   <module>rW      s    
  - 2 # 1I80Gh zI.H	)=LYX`=a:\?i ((=XFG3L>BC#$CBse*    )#A3'(()s   AB B	BB