
    +#h                         d Z ddlZddlmZ ddlmZmZmZ ddlZddl	Z	ddl
mZ 	 ddlmZ  G d d	e	j                         Zy# e$ rZ ed      edZ[ww xY w)
z%Clarifai as retriver to retrieve hits    N)ThreadPoolExecutor)ListOptionalUniondotdict)SearchzBClarifai is not installed. Install it using `pip install clarifai`c            	            e Zd ZdZ	 	 ddededee   def fdZd Z	 dde	ee
e   f   dee   d	ej                  fd
Z xZS )
ClarifaiRMa  
    Retrieval module uses clarifai to return the Top K relevant pasages for the given query.
    Assuming that you have ingested the source documents into clarifai App, where it is indexed and stored.

    Args:
        clarifai_user_id (str): Clarifai unique user_id.
        clarfiai_app_id (str): Clarifai App ID, where the documents are stored.
        clarifai_pat (str): Clarifai PAT key.
        k (int): Top K documents to retrieve.

    Examples:
        TODO
    clarifai_user_idclarfiai_app_idclarifai_patkc                     || _         || _        ||nt        j                  d   | _        || _        t        | j                  | j                   || j                        | _        t        | %  |       y )NCLARIFAI_PAT)user_idapp_idtop_kpat)r   )
r   r   osenvironr   r   r	   clarifai_searchsuper__init__)selfr   r   r   r   	__class__s        V/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dspy/retrieve/clarifai_rm.pyr   zClarifaiRM.__init__"   sj     &'(4L"**^:T 	 %LLA488 
 	1    c                     dd| j                    i}t        j                  |j                  j                  j
                  j                  |      }|j                  |_        |j
                  }|S )NAuthorizationzKey )headers)	r   requestsgetinputdatatexturlapparent_encodingencoding)r   hitsheaderrequestrequested_texts        r   retrieve_hitszClarifaiRM.retrieve_hits4   sX    !T$((#45,,tzz3377H"44 r   query_or_queriesreturnc                    t        |t              r|gn|}g }|D cg c]  }|s|	 }}|D ]  } | j                  j                  ddd|igi|}|D 	
cg c]  }	|	j                  D ]  }
|
  }}	}
t        d      5 }t        |j                  | j                  |            }ddd       |j                  d D                |S c c}w c c}
}	w # 1 sw Y   0xY w)a  Uses clarifai-python SDK search function and retrieves top_k similar passages for given query,
        Args:
             query_or_queries : single query or list of queries
             k : Top K relevant documents to return

        Returns:
             passages in format of dotdict

        Examples:
        Below is a code snippet that shows how to use Marqo as the default retriver:
         ```python
         import clarifai
         llm = dspy.Clarifai(model=MODEL_URL, api_key="YOUR CLARIFAI_PAT")
         retriever_model = ClarifaiRM(clarifai_user_id="USER_ID", clarfiai_app_id="APP_ID", clarifai_pat="YOUR CLARIFAI_PAT")
         dspy.settings.configure(lm=llm, rm=retriever_model)
         ```
        rankstext_raw
   )max_workersNc              3   6   K   | ]  }t        d |i        yw)	long_textNr   ).0ds     r   	<genexpr>z%ClarifaiRM.forward.<locals>.<genexpr>^   s     G!G[!$45Gs    )

isinstancestrr   queryr*   r   listmapr.   extend)r   r/   r   kwargsqueriespassagesqr>   search_responser%   hitr*   executorresultss                 r   forwardzClarifaiRM.forward;   s    , *C0 ! 	
 %+1++ 	HE8d2288^U@S?T^W]^O %4IDtyyICICIDI#3 Gxx||D,>,>EFGOOGwGG	H  , JG Gs   CCC>&CC	)N   )N)__name__
__module____qualname____doc__r=   r   intr   r.   r   r   dspy
PredictionrJ   __classcell__)r   s   @r   r   r      s}    $ '+  sm	
 $ KO% %c49n 5%:B3-%	%r   r   )rO   r   concurrent.futuresr   typingr   r   r   r"   rQ   	dsp.utilsr   clarifai.client.searchr	   ImportErrorerrRetriever   r;   r   r   <module>r[      s\    + 	 1 ( (   -M M  
Ls   A A	AA