a
    dg>                     @  s   d dl mZ d dlmZmZmZmZmZ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	Zed
gedZdZdZdZdZdZdZdZdZdZd
Z G dd deZ!dS )    )annotations)AnyDictListMappingOptionalcastLLMChain)Chain)BaseLanguageModel)CallbackManagerForChainRun)PromptTemplate)AmazonPersonalizez
Summarize the recommended items for a user from the items list in tag <result> below.
Make correlation into the items in the list and provide a summary.
    <result>
        {result}
    </result>
result)Zinput_variablestemplateintermediate_stepsuser_iditem_id
input_list
filter_arnfilter_valuescontext
promotionsmetadata_columnsc                	   @  s   e Zd ZU dZded< ded< dZded< dZded	< dZded
< eddddZ	eddddZ
eedfdddddd dddZd!ddddddZedddd ZdS )"AmazonPersonalizeChaina&  Chain for retrieving recommendations from Amazon Personalize,
     and summarizing them.

    It only returns recommendations if return_direct=True.
    It can also be used in sequential chains for working with
    the output of Amazon Personalize.

    Example:
        .. code-block:: python

        chain = PersonalizeChain.from_llm(llm=agent_llm, client=personalize_lg,
                                        return_direct=True)

        response = chain.run({'user_id':'1'})

        response = chain.run({'user_id':'1', 'item_id':'234'})
    r   clientr
   summarization_chainFboolreturn_directreturn_intermediate_stepsis_ranking_recipez	List[str])returnc                 C  s   g S )zThis returns an empty list since not there are optional
        input_keys and none is required.

        :meta private:
         selfr#   r#   /var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_experimental/recommenders/amazon_personalize_chain.py
input_keys@   s    z!AmazonPersonalizeChain.input_keysc                 C  s   t gS )z?Will always return result key.

        :meta private:
        )RESULT_OUTPUT_KEYr$   r#   r#   r&   output_keysI   s    z"AmazonPersonalizeChain.output_keysr   r   r   )llmr   prompt_templater!   kwargsr"   c                 K  s"   t ||d}| f |||d|S )a  Initializes the Personalize Chain with LLMAgent, Personalize Client,
                                        Prompts to be used

            Args:
                llm: BaseLanguageModel: The LLM to be used in the Chain
                client: AmazonPersonalize: The client created to support
                                            invoking AmazonPersonalize
                prompt_template: PromptTemplate: The prompt template which can be
                                invoked with the output from Amazon Personalize
                is_ranking_recipe: bool: default: False: specifies
                                if the trained recipe is USER_PERSONALIZED_RANKING

        Example:
            .. code-block:: python

                chain = PersonalizeChain.from_llm(llm=agent_llm,
                                client=personalize_lg, return_direct=True)

                response = chain.run({'user_id':'1'})

                response = chain.run({'user_id':'1', 'item_id':'234'})

                RANDOM_PROMPT_QUERY=" Summarize recommendations in {result}"
                chain = PersonalizeChain.from_llm(llm=agent_llm,
                        client=personalize_lg, prompt_template=PROMPT_TEMPLATE)

        )r*   prompt)r   r   r!   r	   )clsr*   r   r+   r!   r,   r   r#   r#   r&   from_llmQ   s    !zAmazonPersonalizeChain.from_llmNzMapping[str, Any]z$Optional[CallbackManagerForChainRun]zDict[str, Any])inputsrun_managerr"   c              	   C  s  |p
t  }| }|t}|t}|t}|t}|t}	|t	}
|t
}|t}g }|dh | jr| jjt|ttt |||	||d}n| jj||||	||
|d}|d | jr|}n| jt|i|d}|| jj }|d|i t|i}| jr||t< |S )a6  Retrieves recommendations by invoking Amazon Personalize,
                        and invokes an LLM using the default/overridden
        prompt template with the output from Amazon Personalize

            Args:
                inputs: Mapping [str, Any] : Provide input identifiers in a map.
                                                For example - {'user_id','1'} or
                        {'user_id':'1', 'item_id':'123'}. You can also pass the
                                        filter_arn, filter_values as an
                        input.
        zCalling Amazon Personalize)r   r   r   r   r   r   )r   r   r   r   r   r   r   z%Call to Amazon Personalize complete 
)	callbacksr   )r   Zget_noop_managerZ	get_childgetUSER_ID_INPUT_KEYITEM_ID_INPUT_KEYINPUT_LIST_INPUT_KEYFILTER_ARN_INPUT_KEYFILTER_VALUES_INPUT_KEYPROMOTIONS_INPUT_KEYCONTEXT_INPUT_KEYMETADATA_COLUMNS_INPUT_KEYappendr!   r   Zget_personalized_rankingstrr   r   Zget_recommendationsZon_textr   r   r(   Z
output_keyr    INTERMEDIATE_STEPS_KEY)r%   r0   r1   Z_run_managerr2   r   r   r   r   r   r   r   r   r   responseZfinal_resultr   Zchain_resultr#   r#   r&   _call{   sT    







	

zAmazonPersonalizeChain._callr=   c                 C  s   dS )NZamazon_personalize_chainr#   r$   r#   r#   r&   _chain_type   s    z"AmazonPersonalizeChain._chain_type)N)__name__
__module____qualname____doc____annotations__r   r    r!   propertyr'   r)   classmethodSUMMARIZE_PROMPTr/   r@   rA   r#   r#   r#   r&   r   )   s$   
, Cr   N)"
__future__r   typingr   r   r   r   r   r   Zlangchain.chainsr
   Zlangchain.chains.baser   Zlangchain.schema.language_modelr   Z langchain_core.callbacks.managerr   Zlangchain_core.prompts.promptr   Z6langchain_experimental.recommenders.amazon_personalizer   ZSUMMARIZE_PROMPT_QUERYrI   r>   r4   r5   r6   r7   r8   r:   r9   r;   r(   r   r#   r#   r#   r&   <module>   s,    