o
    phs                  
   @   s  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	 d dl
mZ dZdd	 ZdddZedkrdZdZz6eee\ZZZZede ededd ede  ed eD ]Zedejdd  q^W dS  ey Z zede  W Y dZ[dS dZ[ww dS )    N)OllamaEmbeddings)FAISS)ChatGroq)RetrievalQA)PromptTemplate8gsk_CEh3itIpUAkEkEKsUDqVWGdyb3FYoTjqmXNTBHOSxJFK3obGTzXZc                 C   s   t dd}d|  }tj|std|  d }t|D ]1}tj||}tj|rOtj	||dd}|j
j D ]}||jd< q;|d u rJ|}q|| q|d u r[td|  td	|   |S )
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doc r    @/var/www/eduai.edurigo.com/question_generate/staging/chatting.pyload_all_client_embeddings   s&   


r"      c              
   C   s   t   }zt| }W n ty( } ztd|  dg dfW  Y d }~S d }~ww tdt|d}d}t|ddgd	}tj|d
|j	ddiddd|id}	|	
d|i}
t   }|| }|
d }|
d }tdd |D t|  }dd |D }||||fS )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stuffk   )search_kwargsTprompt)llm
chain_type	retrieverreturn_source_documentschain_type_kwargsqueryresultsource_documentsc                 s   s    | ]
}t |j V  qd S )N)lenpage_contentsplit.0r   r    r    r!   	<genexpr>S   s    z"answer_question.<locals>.<genexpr>c                 S   s   g | ]	}|j d dqS )r
   Unknown)r   getr;   r    r    r!   
<listcomp>V   s    z#answer_question.<locals>.<listcomp>)timer"   r   r   r   GROQ_API_KEYr   r   from_chain_typeas_retrieverinvokesumr8   r:   )r   r(   r&   
start_timer   er0   r)   r/   qa_chainr6   end_timeprocessing_timeanswersource_docstotal_tokenssource_reference_idsr    r    r!   answer_question)   sB   rP   __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- sourcezUnknown sourcezAn error occurred: )r#   )r   rA   langchain_community.embeddingsr    langchain_community.vectorstoresr   langchain_groqr   langchain.chainsr   langchain.promptsr   rB   r"   rP   __name__r   r(   rL   rM   rN   rK   r   r   r   r?   	ExceptionrH   r    r    r    r!   <module>   s6   
3
