o
    g                     @   s   d Z ddlZddlZddlZddlmZ ddlZddlmZ ddlm	  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jd	 dd
lmZ e  G dd dZedkrie Zej  dS dS )zConnect to ChromaDB    N)datetime)Settings)load_dotenv)Chroma)Document)VectorStoreRetriever.)	OPENAI_EFc                   @   sX   e Zd ZdZddefddZ	ddedejfd	d
Z	dd Z
dd ZddefddZdS )ChromaDBzClass to query ChromaDBNpersist_directoryc                 C   s   t d}|dd }|dd }|dkr(tj||tdt ddd	| _n|r2tj|d
| _ntjdd
| _t| _	| jj
d| j	d| _| jj
d| j	d| _| jj
d| j	d| _d S )Nchroma_server:   r   	localhostz1chromadb.auth.token_authn.TokenAuthClientProviderchroma_server_api_key)chroma_client_auth_providerchroma_client_auth_credentials)hostportsettings)pathz./chromacompany_product_description)embedding_functioncompany_overview_descriptionfunds)osgetenvsplitchromadb
HttpClientr   chroma_clientPersistentClientr	   	openai_efget_or_create_collectionproduct_collectionoverview_collectionfund_collection)selfr   server_addressserver_port	server_ip r+   +/var/www/html/XCapMarket/utils/chroma_db.py__init__   s4   

	zChromaDB.__init__
   query_items
collectionc                 K   sD   d|vrd|d< d|vrd|d< |j d||d |d |d}|S )zGet company product descriptionwhereNwhere_document
)query_textsr1   r2   	n_results)queryjoin)r'   r/   r0   r5   kwargsresult_itemsr+   r+   r,   retrive_by_collectionD   s   zChromaDB.retrive_by_collectionc                 K   s   i }dd |  D }t|dkr|dur|S t|dkr#|du r#dS t|dkr/|du r/|S |dur9d|gi}ndg i}|  D ]\}}|d ||i qA|S )aL   "
        Create where filter for ChromaDB query
        kwargs: key value pairs
            - key: metadata field
            - value: dict
                Key: filtering (
                    $eq - equal to (string, int, float)
                    $ne - not equal to (string, int, float)
                    $gt - greater than (int, float)
                    $gte - greater than or equal to (int, float)
                    $lt - less than (int, float)
                    $lte - less than or equal to (int, float)
                )
                Value: value to filter by

        c                 S   s   i | ]\}}|d ur||qS Nr+   ).0kvr+   r+   r,   
<dictcomp>k   s    z0ChromaDB.create_where_filter.<locals>.<dictcomp>r   Nr   z$and)itemslenappend)r'   industry_filterr8   where_filterkeyvaluer+   r+   r,   create_where_filterW   s   zChromaDB.create_where_filterc                 C   s   t | j tdu rtdz|j|d g|| g|d |d |d |d dgd W d
S  tjyd } z*td	|  |j	|d g|| g|d |d |d |d dgd W Y d
}~d
S d
}~ww )zAdd item into collectionFzChromaDB is not availableroot_urlindustrysectorpublic)rI   rJ   rK   company_url)ids	documents	metadataszDuplicate ID: N)

isinstancer    	heartbeatint
ValueErroradderrorsDuplicateIDErrorprintupdate)r'   r0   itemdocument_nameer+   r+   r,   add_item   s6   zChromaDB.add_itemr6   c                 C   st   | j j|gdt| j dd| jiid}g }tt|d d D ]}|t|d d | |d d | d	 q |S )
Nr.   	file_namez$in)r4   r5   r1   rM   r   rN   rO   )page_contentmetadata)r0   r6   rA   
file_namesrangerB   r   )r'   r6   configresultsdocsir+   r+   r,   invoke   s   zChromaDB.invoker;   )r.   )__name__
__module____qualname____doc__strr-   listr   
Collectionr:   rG   r\   rf   r+   r+   r+   r,   r
      s    )
("r
   __main__)rj   loggingr   sysr   r   chromadb.errorsrU   "chromadb.utils.embedding_functionsutilsembedding_functionschromadb.configr   dotenvr   langchain.vectorstoresr   langchain.docstore.documentr   langchain.vectorstores.baser   r   rB   configs.configr	   r
   rg   cdbr    list_collectionsr+   r+   r+   r,   <module>   s,     