a
    bgN                     @   sD   d dl mZmZmZmZ d dlmZ d dlmZ G dd deZ	dS )    )DictListOptionalcast)Document)
BaseLoaderc                	   @   sX   e Zd ZdZdeeeeeeef  eee  eee  dddZ	ee
 dd	d
ZdS )DuckDBLoaderaK  Load from `DuckDB`.

    Each document represents one row of the result. The `page_content_columns`
    are written into the `page_content` of the document. The `metadata_columns`
    are written into the `metadata` of the document. By default, all columns
    are written into the `page_content` and none into the `metadata`.
    :memory:FNquerydatabase	read_onlyconfigpage_content_columnsmetadata_columnsc                 C   s,   || _ || _|| _|pi | _|| _|| _dS )aF  

        Args:
            query: The query to execute.
            database: The database to connect to. Defaults to ":memory:".
            read_only: Whether to open the database in read-only mode.
              Defaults to False.
            config: A dictionary of configuration options to pass to the database.
              Optional.
            page_content_columns: The columns to write into the `page_content`
              of the document. Optional.
            metadata_columns: The columns to write into the `metadata` of the document.
              Optional.
        Nr
   )selfr   r   r   r   r   r    r   /var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/duckdb_loader.py__init__   s    
zDuckDBLoader.__init__)returnc                    s  zdd l }W n ty&   tdY n0 g }|j| j| j| jd}|| j}| }t	t
|j}dd |D  | jd u r }n| j}| jd u rg }n| j}|D ]Hd fdd|D }	 fd	d
|D }
t|	|
d}|| qW d    n1 s0    Y  |S )Nr   zTCould not import duckdb python package. Please install it with `pip install duckdb`.)r   r   r   c                 S   s   g | ]}|d  qS )r   r   ).0cr   r   r   
<listcomp>?       z%DuckDBLoader.load.<locals>.<listcomp>
c                 3   s&   | ]}| d   |  V  qdS )z: Nindexr   columnfield_namesresultr   r   	<genexpr>L   s   z$DuckDBLoader.load.<locals>.<genexpr>c                    s   i | ]}|  | qS r   r   r   r   r   r   
<dictcomp>Q   s   z%DuckDBLoader.load.<locals>.<dictcomp>)page_contentmetadata)duckdbImportErrorconnectr   r   r   executer   Zfetchallr   listdescriptionr   r   joinr   append)r   r&   docsconZquery_resultresultsr+   r   r   r$   r%   docr   r   r   load/   s>    


,zDuckDBLoader.load)r	   FNNN)__name__
__module____qualname____doc__strboolr   r   r   r   r   r2   r   r   r   r   r      s        

r   N)
typingr   r   r   r   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser   r   r   r   r   r   <module>   s   