
    'j9                        d 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dlmZ dd	lmZ dd
lmZ  e ee                                          j        d         dz  d           dZ G d de	          Z G d de	          Zdedef         fdZ e            Zdededeeeeeef                  f         fdZdS )u   Grounded market-context summariser.

Extracts only claims that are verbatim-supported by the raw search results.
Each claim carries the source URL and a verbatim quote — no quote, no claim.
    N)Path)Literal)load_dotenv)	BaseModel)Agent)OpenAIChatModel)LiteLLMProvider)	write_log   z.envF)overrideu  You are a strict evidence extractor. You receive web search results and a question.

Rules:
- Each claim MUST include a verbatim_quote copied exactly from the source text.
- Each claim MUST include the source_url of the result it came from.
- If you cannot find a verbatim quote to support a claim, DO NOT include it.
- DO NOT use your training data, general knowledge, or inference.
- DO NOT paraphrase and call it a quote — it must be exact text from the source.
- If the search results contain no relevant benchmarks or figures, return an empty list.

Relevance scoring — rate each claim against the user's question:
- high:   claim directly answers or benchmarks the metric asked (e.g. a specific % or rate for that topic)
- medium: claim is related context but not the exact metric (e.g. describes the process, mentions the topic without figures)
- low:    claim is tangentially related — same domain but different metric or time period
c                   D    e Zd ZU eed<   eed<   eed<   ed         ed<   dS )GroundedClaimclaimverbatim_quote
source_url)highmediumlow	relevanceN)__name__
__module____qualname__str__annotations__r        M/var/www/html/ai-enterprise-brain/backend/chanakya/rt_agent/web_summariser.pyr   r   '   sB         JJJOOO.//////r   r   c                   &    e Zd ZU ee         ed<   dS )GroundedSummaryclaimsN)r   r   r   listr   r   r   r   r   r   r   .   s#         r   r   returnc            
      0   t          t          j                            dd          t	          t          j                            dd          t          j                            dd                              } t          | t          t          d          S )	NLITELLM_PROVIDER_MODEL_NAME LITELLM_PROVIDER_BASE_URLLITELLM_API_KEY)api_baseapi_key)providerr   )output_typesystem_promptretries)r   osenvirongetr	   r   r   _SYSTEM)models    r   _make_agentr3   2   s~    

4b99 Z^^$?DDJNN#4b99
 
 
  E O7TUVVVVr   querysearch_resultsc                 H   d|  d| }t                               |ddi          }|j        }|j        st	          d| g dd           dg fS d	 |j        D             }d
                    d |D                       }t	          d| d |j        D             |d           ||fS )a  Extract grounded claims from search results.

    Returns:
        (text_for_llm, structured_claims) where text_for_llm is a bullet-point
        string for the LLM's context and structured_claims is a list of dicts
        with keys: relevance, claim, source_url.
    z
Question: z

Search results:
temperaturer   )model_settingszweb-summariserr%   )r4   r    market_contextc                 \    g | ])}|j                                         |j        |j        d *S ))r   r   r   )r   upperr   r   .0cs     r   
<listcomp>zsummarise.<locals>.<listcomp>P   sD        k''))AG1<XX  r   
c              3   R   K   | ]"}d |d          d|d          d|d          dV  #dS )z- [r   z] r   z
 (source: r   )Nr   r<   s     r   	<genexpr>zsummarise.<locals>.<genexpr>T   s^         	IanHH'
HHaoHHH     r   c                 6    g | ]}|                                 S r   )
model_dumpr<   s     r   r?   zsummarise.<locals>.<listcomp>X   s     ;c;c;cqALLNN;c;c;cr   )_agentrun_syncoutputr    r
   join)r4   r5   user_msgresultsummary
structuredbulletss          r   	summariserO   @   s     IEHHHHH__X}a6H_IIFmG> "erUW$X$XYYY2v   J ii      G %;c;cT[Tb;c;c;cw~    A  A  AJr   )__doc__r.   pathlibr   typingr   dotenvr   pydanticr   pydantic_air   pydantic_ai.models.openair   pydantic_ai.providers.litellmr	   #backend.chanakya.rt_agent.log_utilsr
   __file__resolveparentsr1   r   r   r3   rF   r   tupler!   dictrO   r   r   r   <module>r^      s    
			                               5 5 5 5 5 5 9 9 9 9 9 9 9 9 9 9 9 9 DDNN""$$,Q/&85 I I I I$0 0 0 0 0I 0 0 0         i      WU401 W W W W 
S # %T$sCx.=Q8Q2R      r   