o
    pDi                     @   s   d dl mZmZ d dlmZ d dlmZmZ d dlm	Z	 d dl
mZ e  dZdZeedZe	jeed	d
ZedddZdZedd ZdZdZedZdedefddZdS )    )defaultdictdeque)load_dotenv)OpenAIEmbeddings
ChatOpenAI)FAISS)ChatPromptTemplatezvector_store/tracwater_faissztext-embedding-3-large)modelT)allow_dangerous_deserializationzgpt-4.1-minig?)r	   temperature   c                   C   s
   t tdS )N)maxlen)r   	MAX_TURNS r   r   (/home/anubhav/Desktop/Tracwater/query.py<lambda>&   s   
 r         a  
You are a domain-specific expert assistant representing TracWater.

You must answer all user questions using ONLY the information provided in the context below and speak strictly in the first person plural (e.g., "we", "our", "us") when referring to TracWater, its products, or services.

Conversation History:
{history}

Context:
{context}

Mandatory Rules:
- ALWAYS refer to TracWater using first-person language ("we", "our", "us").
- NEVER use third-person references such as "they", "the company", or "TracWater provides".
- Do NOT use any external knowledge, assumptions, or hallucinations.
- Base your response strictly on the provided context.
- If the required information is not present in the context, respond EXACTLY with:
  "Sorry, no information for this query is available in TracWater sources. Kindly rephrase or ask another question."
- Keep responses clear, professional, and concise.

User Question:
{question}
q
session_idc           	      C   s   t j| td}dd |D }|s| ddS dd |D }ddd	 |D }d
dd	 t| D }ttj|p9d|| d}|j	}t| 
| |d | |dS )zC
    Perform session-aware RAG query with relevance threshold.
    )kc                 S   s    g | ]\}}|t k r||fqS r   )SIMILARITY_THRESHOLD).0docscorer   r   r   
<listcomp>o   s
    z query_search.<locals>.<listcomp>z2Information not available in the provided sources.)queryanswerc                 S   s   g | ]\}}|qS r   r   )r   r   _r   r   r   r   }   s    z

c                 s   s(    | ]}d |j d  d|j V  qdS )z	[Source: 
source_urlz]
N)metadatapage_content)r   dr   r   r   	<genexpr>   
    
zquery_search.<locals>.<genexpr>
c                 s   s(    | ]}d |d  d|d  V  qdS )zUser: r   z
Assistant: aNr   )r   turnr   r   r   r#      r$   None)historycontextquestion)r   r&   )vectordbsimilarity_search_with_scoreTOP_KjoinSESSION_MEMORYllminvokePROMPTformatcontentappend)	r   r   resultsfiltereddocsr*   r)   responser   r   r   r   query_searchg   s<   

r;   N)collectionsr   r   dotenvr   langchain_openair   r    langchain_community.vectorstoresr   langchain_core.promptsr   
VECTOR_DIREMBEDDING_MODEL
embeddings
load_localr,   r1   r   r0   r   r.   from_templater3   strr;   r   r   r   r   <module>   s0    

	
