o
    sg,                     @   s  d Z ddlZ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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jd Ze ZG dd dZedkrddlZej d e Z!e"e!#d e"e!#d e"e!#d dS dS )z!Service to recommend acquisitions    N)load_dotenv)ChatPromptTemplate)JsonOutputParser)
ChatOpenAI)OPENAI_API_KEYOPENAI_MODEL_35)CompanyGroups)	Companies)ChromaDB)DynamoDB)load_sic_codesr   c                   @   s   e Zd ZdZdd Z			ddeded	efd
dZ	ddedee	 fddZ
	dded	edefddZdefddZdd ZdS )AcquisitionsRecommenderzClass to recommend acquisitionsc                 C   s$   t tddd| _t ttdd| _d S )Nzgpt-4og?)api_key
model_nametemperatureg?)r   r   openai_llm_4r   openai_llm_3)self r   I/var/www/html/XCapMarket/services/recommender/acquisitions_recommender.py__init__   s   z AcquisitionsRecommender.__init__N   
horizontalproduct_descriptionnum_of_companiesacq_directionc           	   	   C   sb   |du r| j n|}ttd}tg d}||B |B }||| |d| dtt	d}|S )zGet hypothetical companiesNpydantic_object))systemzuYou are an investment banking managing director.
                    Being provided a company's products description.)r   ac  Think about this step by step:

                        1) Use the product description to classify the company into an industry and sector.

                        2) Become an investment banker for that industry and sector.

                        3) Take in the product description and anonymize it

                        5) Generate a syntenic description of a company that would be interest

                        4) Provide 3 example companies with 5 bullet points describing the company's business

                        5) Do not include rational for the provided company
                        r   z{json_structure})humanz{product_description})r    z{pitch_direction})r   z=You should use this dictionary for SIC codes {sic_codes_dict}zd
                    Write product descriptions of companies that would
                    be good z7 acquisition targets for this company.
                )r   json_structurer   pitch_directionsic_codes_dict)
r   r   r	   r   from_messagesinvokeget_format_instructionsjsondumpsr#   )	r   r   open_ai_modelr   r   parserchat_templatechainresponser   r   r   get_hypo_acq_companies)   s"   
z.AcquisitionsRecommender.get_hypo_acq_companiesacq_company_description	companiesc           	      C   sZ   |du r| j n|}ttd}t|}tg d}||B |B }|| ||d}|S )zRegroup companiesNr   ))r   zYou are an investment banking managing director.
                    Being provided with a set of company's url and product descriptions.)r   aE  Think about this step by step:

                        1) Use the product description to classify the companies into relevant groups.

                        2) Create a minimum of 2 groups and maximum of 5 with a recommendation of 3 groups.
                        3) Provide a rational for why the companies were grouped together especially with the provided description of acquiring company. This should be clear and concise and should not be more than 3 sentences.
                        4) In the company urls list, don't include the acquiring company.
                    r   )r    z/companies to group: ***
 {companies_json} 
 ***)r    z+acquiring company: ***
 {acq_company} 
 ***)r!   companies_jsonacq_company)	r   r   r   r'   r(   r   r$   r%   r&   )	r   r/   r0   r)   r*   r1   r+   r,   r-   r   r   r   regroup_companiesb   s   

z)AcquisitionsRecommender.regroup_companiescompany_urlpublicc                 C   s  t  }|jj|gd}|d d }| j|| jd|d}|du r$ddi}n|du r-ddi}nd	}d
g i}	|d D ];}
z%|	d
 dddtt|
d  d iiddtt|
d  d iigi W q7 tyr   t	
d|
d  d Y q7w |j|	|d}g }|d D ]}||j|d |j|d	d qi }|D ]!}t|d d D ]\}}||krq||d d | d||< qq| j|t| | jd}|S )!Get recommendations for a companyids	documentsr   r   )r   r   T$eqFNz$orr0   z$andindustrySIC_codesectorz	SIC code z not found in the dictionaryr5   product_descriptionswherewhere_documentr8   r4   r   r/   r0   r)   )Query_ChromaDBproduct_collectiongetr.   r   appendr#   intKeyErrorloggingerrorcreate_where_filterretrive_by_collection	enumerater3   listvalues)r   r4   r   r5   query_chromacompanyr   recommended_companiespublic_filterindustry_filterrec_companyrA   &companies_similar_to_recommended_groupcompanies_recommendedgroupirecommended_company_urlregrouped_companiesr   r   r   get_recommendations   s   




z+AcquisitionsRecommender.get_recommendationsc                 C   s   t  }|jj|gd}|d d }|jdd|d d d iiddid}g }||j|g|j|d	d
 i }|D ]!}t|d d D ]\}	}
||
krKqB|
|d d |	 d||
< qBq8| j|t|	 | j
d}|S )r6   r7   r9   r   r;   r:   	metadatasTr>   Nr@   r8   rC   rD   )rE   rF   rG   rM   rH   rN   rO   r3   rP   rQ   r   )r   r4   rR   rS   r   rA   rX   rY   rZ   r[   r\   r]   r   r   r   get_public_comps   s>   

z(AcquisitionsRecommender.get_public_compsc                 C   sB   t  }|d D ]}g }|d D ]
}||| q||d< q|S )zNTransform the output of the recommender to the format expected by the frontendgroupsr0   )r   rH   get_item_for_recommender)r   recommender_outputget_itemrZ   new_company_listrS   r   r   r   *transform_recommender_output_for_front_end  s   
zBAcquisitionsRecommender.transform_recommender_output_for_front_end)Nr   r   )N)r   N)__name__
__module____qualname____doc__r   strrI   r.   rP   dictr3   boolr^   r`   rf   r   r   r   r   r      s:    
:
+
Y.r   __main__.zhttps://www.google.comzhttps://www.unity.comzhttps://www.lyft.com)$rj   r'   rK   osdotenvr   langchain.promptsr   langchain_core.output_parsersr   langchain_openair   configs.configr   r   models.comapny_groupsr   models.companiesr	   utils.chroma_dbr
   rE   utils.dynamo_dbr   utils.load_sic_codesr   environr#   r   rg   syspathrH   acq_recommendorprintr^   r   r   r   r   <module>   s8    
  	