o
    Zh                     @   sz   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eeeef  defddZG dd	 d	eZG d
d de
ZdS )    )AnyCallableIteratorListOptionalTuple)Document)
BaseLoaderdocsreturnc                 C   s   d dd | D S )z#Default joiner for content columns.
c                 S   s   g | ]}|d  qS )    ).0docr   r   e/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/document_loaders/rocksetdb.py
<listcomp>
   s    z"default_joiner.<locals>.<listcomp>)join)r
   r   r   r   default_joiner   s   r   c                       s*   e Zd ZdZdedef fddZ  ZS )ColumnNotFoundErrorzColumn not found error.missing_keyqueryc                    s   t  d| d|  d S )NzColumn "z" not selected in query:
)super__init__)selfr   r   	__class__r   r   r      s   zColumnNotFoundError.__init__)__name__
__module____qualname____doc__strr   __classcell__r   r   r   r   r      s    r   c                   @   sh   e Zd ZdZdefdededee deee  de	ee
eef  gef f
dd	Zd
ee fddZdS )RocksetLoadera  Load from a `Rockset` database.

    To use, you should have the `rockset` python package installed.

    Example:
        .. code-block:: python

            # This code will load 3 records from the "langchain_demo"
            # collection as Documents, with the `text` column used as
            # the content

            from langchain_community.document_loaders import RocksetLoader
            from rockset import RocksetClient, Regions, models

            loader = RocksetLoader(
                RocksetClient(Regions.usw2a1, "<api key>"),
                models.QueryRequestSql(
                    query="select * from langchain_demo limit 3"
                ),
                ["text"]
            )
        )
    Nclientr   content_keysmetadata_keyscontent_columns_joinerc           	      C   s   zddl m}m} ddlm} W n ty   tdw t||s*tdt| t||s8tdt| || _	|| _
|| _|| _|| _|| _|| _z	| j	d W dS  ty`   Y dS w )	aK  Initialize with Rockset client.

        Args:
            client: Rockset client object.
            query: Rockset query object.
            content_keys: The collection columns to be written into the `page_content`
                of the Documents.
            metadata_keys: The collection columns to be written into the `metadata` of
                the Documents. By default, this is all the keys in the document.
            content_columns_joiner: Method that joins content_keys and its values into a
                string. It's method that takes in a List[Tuple[str, Any]]],
                representing a list of tuples of (column name, column value).
                By default, this is a method that joins each column value with a new
                line. This method is only relevant if there are multiple content_keys.
        r   )QueryPaginatorRocksetClient)QueryRequestSqlz]Could not import rockset client python package. Please install it with `pip install rockset`.z;client should be an instance of rockset.RocksetClient, got zBquery should be an instance of rockset.model.QueryRequestSql, got Z	langchainN)Zrocksetr(   r)   Zrockset.modelsr*   ImportError
isinstance
ValueErrortyper$   r   r%   r'   r&   Z	paginatorZrequest_modelZset_applicationAttributeError)	r   r$   r   r%   r&   r'   r(   r)   r*   r   r   r   r   -   s@   

zRocksetLoader.__init__r   c                 #   s    | j jj| jdj}|D ]: z#t|  fdd| jD | jd ur, fdd| jD n dV  W q tyG } z	t	|j
d | jd }~ww d S )N)sqlc                    s   g | ]}| | fqS r   r   r   colr   r   r   r   o   s    z+RocksetLoader.lazy_load.<locals>.<listcomp>c                    s   i | ]}| | qS r   r   r1   r3   r   r   
<dictcomp>q   s    z+RocksetLoader.lazy_load.<locals>.<dictcomp>)Zpage_contentmetadatar   )r$   ZQueriesr   resultsr   r'   r%   r&   KeyErrorr   args)r   Zquery_resultser   r3   r   	lazy_loadg   s2   

zRocksetLoader.lazy_load)r   r   r   r    r   r   r   r!   r   r   r   r   r   r   r:   r   r   r   r   r#      s     

:r#   N)typingr   r   r   r   r   r   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser	   r!   r   	Exceptionr   r#   r   r   r   r   <module>   s     