a
    ”˜ëjf)  ã                   @  sÐ   d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	m
Z
 ddlmZmZmZmZmZmZ ddlmZmZmZ ddlmZ er�dd	lmZ eee
f ZG d
d„ deƒZG dd„ deƒZG dd„ deƒZdS )z%Base classes for media and documents.é    )ÚannotationsN)ÚBufferedReaderÚBytesIO)ÚPathÚPurePath)ÚTYPE_CHECKINGÚAnyÚLiteralÚOptionalÚUnionÚcast)Ú
ConfigDictÚFieldÚmodel_validator)ÚSerializable)Ú	Generatorc                   @  s8   e Zd ZU dZeddd�Zded< eed�Zded	< dS )
Ú	BaseMediaa]  Use to represent media content.

    Media objects can be used to represent raw data, such as text or binary data.

    LangChain Media objects allow associating metadata and an optional identifier
    with the content.

    The presence of an ID and metadata make it easier to store, index, and search
    over the content in a structured way.
    NT)ÚdefaultZcoerce_numbers_to_strúOptional[str]Úid)Údefault_factoryÚdictÚmetadata)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__annotations__r   r   © r   r   ú\/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/documents/base.pyr      s   
	r   c                	   @  s  e Zd ZU dZdZded< dZded< dZded	< dZd
ed< e	ddd�Z
eddœdd„ƒZedd�edddœdd„ƒƒZddœdd„Zddœdd„Zejddœdd„ƒZeddddd œd!ddd"d#d d$œd%d&„ƒZeddddd'œd(dddd#d d)œd*d+„ƒZddœd,d-„ZdS ).ÚBloba%  Blob represents raw data by either reference or value.

    Provides an interface to materialize the blob in different representations, and
    help to decouple the development of data loaders from the downstream parsing of
    the raw data.

    Inspired by: https://developer.mozilla.org/en-US/docs/Web/API/Blob

    Example: Initialize a blob from in-memory data

        .. code-block:: python

            from langchain_core.documents import Blob

            blob = Blob.from_data("Hello, world!")

            # Read the blob as a string
            print(blob.as_string())

            # Read the blob as bytes
            print(blob.as_bytes())

            # Read the blob as a byte stream
            with blob.as_bytes_io() as f:
                print(f.read())

    Example: Load from memory and specify mime-type and metadata

        .. code-block:: python

            from langchain_core.documents import Blob

            blob = Blob.from_data(
                data="Hello, world!",
                mime_type="text/plain",
                metadata={"source": "https://example.com"},
            )

    Example: Load the blob from a file

        .. code-block:: python

            from langchain_core.documents import Blob

            blob = Blob.from_path("path/to/file.txt")

            # Read the blob as a string
            print(blob.as_string())

            # Read the blob as bytes
            print(blob.as_bytes())

            # Read the blob as a byte stream
            with blob.as_bytes_io() as f:
                print(f.read())

    NzUnion[bytes, str, None]Údatar   Úmimetypezutf-8ÚstrÚencodingzOptional[PathLike]ÚpathT)Zarbitrary_types_allowedÚfrozen©Úreturnc                 C  s4   | j r d| j v r td| j d ƒS | jr0t| jƒS dS )a  The source location of the blob as string if known otherwise none.

        If a path is associated with the blob, it will default to the path location.

        Unless explicitly set via a metadata field called "source", in which
        case that value will be used instead.
        Úsourcer   N)r   r   r%   r#   ©Úselfr   r   r   r)   }   s    	zBlob.sourceÚbefore)Úmodezdict[str, Any]r   )Úvaluesr(   c                 C  s    d|vrd|vrd}t |ƒ‚|S )z,Verify that either data or path is provided.r!   r%   z$Either data or path must be provided)Ú
ValueError)Úclsr.   Úmsgr   r   r   Úcheck_blob_is_validŠ   s    zBlob.check_blob_is_validc                 C  sf   | j du r$| jr$t| jƒj| jd�S t| j tƒr>| j  | j¡S t| j tƒrP| j S d| › �}t	|ƒ‚dS )z«Read data as a string.

        Raises:
            ValueError: If the blob cannot be represented as a string.

        Returns:
            The data as a string.
        N)r$   zUnable to get string for blob )
r!   r%   r   Ú	read_textr$   Ú
isinstanceÚbytesÚdecoder#   r/   ©r+   r1   r   r   r   Ú	as_string“   s    	
zBlob.as_stringr5   c                 C  s`   t | jtƒr| jS t | jtƒr,| j | j¡S | jdu rJ| jrJt| jƒ ¡ S d| › �}t	|ƒ‚dS )z¢Read data as bytes.

        Raises:
            ValueError: If the blob cannot be represented as bytes.

        Returns:
            The data as bytes.
        NzUnable to get bytes for blob )
r4   r!   r5   r#   Úencoder$   r%   r   Ú
read_bytesr/   r7   r   r   r   Úas_bytes¥   s    	
zBlob.as_bytesz5Generator[Union[BytesIO, BufferedReader], None, None]c                 c  sx   t | jtƒrt| jƒV  nZ| jdu rb| jrbt| jƒ d¡�}|V  W d  ƒ qt1 sV0    Y  nd| › �}t|ƒ‚dS )zÂRead data as a byte stream.

        Raises:
            NotImplementedError: If the blob cannot be represented as a byte stream.

        Yields:
            The data as a byte stream.
        NÚrbzUnable to convert blob )r4   r!   r5   r   r%   r   ÚopenÚNotImplementedError)r+   Úfr1   r   r   r   Úas_bytes_io·   s    
&
zBlob.as_bytes_io)r$   Ú	mime_typeÚ
guess_typer   ÚPathLikeÚboolzOptional[dict])r%   r$   rA   rB   r   r(   c                C  sF   |du r$|r$|rt  |¡d nd}n|}| d||||dur>|ni d�S )a   Load the blob from a path like object.

        Args:
            path: path like object to file to be read
            encoding: Encoding to use if decoding the bytes into a string
            mime_type: if provided, will be set as the mime-type of the data
            guess_type: If True, the mimetype will be guessed from the file extension,
                        if a mime-type was not provided
            metadata: Metadata to associate with the blob

        Returns:
            Blob instance
        Nr   ©r!   r"   r$   r%   r   )Ú	mimetypesrB   )r0   r%   r$   rA   rB   r   r"   r   r   r   Ú	from_pathÊ   s    ûzBlob.from_path)r$   rA   r%   r   zUnion[str, bytes])r!   r$   rA   r%   r   r(   c                C  s   | |||||dur|ni d�S )aÍ  Initialize the blob from in-memory data.

        Args:
            data: the in-memory data associated with the blob
            encoding: Encoding to use if decoding the bytes into a string
            mime_type: if provided, will be set as the mime-type of the data
            path: if provided, will be set as the source from which the data came
            metadata: Metadata to associate with the blob

        Returns:
            Blob instance
        NrE   r   )r0   r!   r$   rA   r%   r   r   r   r   Ú	from_dataï   s    ûzBlob.from_datac                 C  s(   dt | ƒ› �}| jr$|d| j› �7 }|S )zReturn the blob representation.zBlob ú )r   r)   )r+   Zstr_reprr   r   r   Ú__repr__  s    zBlob.__repr__)r   r   r   r   r!   r   r"   r$   r%   r   Zmodel_configÚpropertyr)   r   Úclassmethodr2   r8   r;   Ú
contextlibÚcontextmanagerr@   rG   rH   rJ   r   r   r   r   r    1   s>   
:þù$ùr    c                      sr   e Zd ZU dZded< d Zded< ddddœ‡ fd	d
„Zeddœdd„ƒZeddœdd„ƒZ	ddœdd„Z
‡  ZS )ÚDocumenta2  Class for storing a piece of text and associated metadata.

    Example:

        .. code-block:: python

            from langchain_core.documents import Document

            document = Document(
                page_content="Hello, world!", metadata={"source": "https://example.com"}
            )

    r#   Úpage_contentzLiteral['Document']Útyper   ÚNone)rP   Úkwargsr(   c                   s   t ƒ jf d|i|¤Ž dS )z0Pass page_content in as positional or named arg.rP   N)ÚsuperÚ__init__)r+   rP   rS   ©Ú	__class__r   r   rU   (  s    zDocument.__init__rD   r'   c                 C  s   dS )z*Return True as this class is serializable.Tr   ©r0   r   r   r   Úis_lc_serializable.  s    zDocument.is_lc_serializablez	list[str]c                 C  s   g d¢S )zuGet the namespace of the langchain object.

        Returns:
            ["langchain", "schema", "document"]
        )Z	langchainZschemaÚdocumentr   rX   r   r   r   Úget_lc_namespace3  s    zDocument.get_lc_namespacec                 C  s(   | j rd| j› d| j › �S d| j› d�S )z�Override __str__ to restrict it to page_content and metadata.

        Returns:
            A string representation of the Document.
        zpage_content='z' metadata=ú')r   rP   r*   r   r   r   Ú__str__<  s    zDocument.__str__)r   r   r   r   r   rQ   rU   rL   rY   r[   r]   Ú__classcell__r   r   rV   r   rO     s   
rO   )r   Ú
__future__r   rM   rF   Úior   r   Úpathlibr   r   Útypingr   r   r	   r
   r   r   Zpydanticr   r   r   Z langchain_core.load.serializabler   Úcollections.abcr   r#   rC   r   r    rO   r   r   r   r   Ú<module>   s     e