U
    h                     @   sL   d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	 G dd deZ
dS )    )IteratorLiteralOptional)
BaseLoader)Document)get_from_envc                   @   sT   e Zd ZdZdddddeee ee ed ee dddZe	e
 d	d
dZdS )FireCrawlLoadera"
  
    FireCrawlLoader document loader integration

    Setup:
        Install ``firecrawl-py``,``langchain_community`` and set environment variable ``FIRECRAWL_API_KEY``.

        .. code-block:: bash

            pip install -U firecrawl-py langchain_community
            export FIRECRAWL_API_KEY="your-api-key"

    Instantiate:
        .. code-block:: python

            from langchain_community.document_loaders import FireCrawlLoader

            loader = FireCrawlLoader(
                url = "https://firecrawl.dev",
                mode = "crawl"
                # other params = ...
            )

    Lazy load:
        .. code-block:: python

            docs = []
            docs_lazy = loader.lazy_load()

            # async variant:
            # docs_lazy = await loader.alazy_load()

            for doc in docs_lazy:
                docs.append(doc)
            print(docs[0].page_content[:100])
            print(docs[0].metadata)

        .. code-block:: python

            Introducing [Smart Crawl!](https://www.firecrawl.dev/smart-crawl)
             Join the waitlist to turn any web
            {'ogUrl': 'https://www.firecrawl.dev/', 'title': 'Home - Firecrawl', 'robots': 'follow, index', 'ogImage': 'https://www.firecrawl.dev/og.png?123', 'ogTitle': 'Firecrawl', 'sitemap': {'lastmod': '2024-08-12T00:28:16.681Z', 'changefreq': 'weekly'}, 'keywords': 'Firecrawl,Markdown,Data,Mendable,Langchain', 'sourceURL': 'https://www.firecrawl.dev/', 'ogSiteName': 'Firecrawl', 'description': 'Firecrawl crawls and converts any website into clean markdown.', 'ogDescription': 'Turn any website into LLM-ready data.', 'pageStatusCode': 200, 'ogLocaleAlternate': []}


    Async load:
        .. code-block:: python

            docs = await loader.aload()
            print(docs[0].page_content[:100])
            print(docs[0].metadata)

        .. code-block:: python

            Introducing [Smart Crawl!](https://www.firecrawl.dev/smart-crawl)
             Join the waitlist to turn any web
            {'ogUrl': 'https://www.firecrawl.dev/', 'title': 'Home - Firecrawl', 'robots': 'follow, index', 'ogImage': 'https://www.firecrawl.dev/og.png?123', 'ogTitle': 'Firecrawl', 'sitemap': {'lastmod': '2024-08-12T00:28:16.681Z', 'changefreq': 'weekly'}, 'keywords': 'Firecrawl,Markdown,Data,Mendable,Langchain', 'sourceURL': 'https://www.firecrawl.dev/', 'ogSiteName': 'Firecrawl', 'description': 'Firecrawl crawls and converts any website into clean markdown.', 'ogDescription': 'Turn any website into LLM-ready data.', 'pageStatusCode': 200, 'ogLocaleAlternate': []}

    Ncrawl)api_keyapi_urlmodeparamsr	   scrape)urlr
   r   r   r   c                C   sx   zddl m} W n tk
r,   tdY nX |dkrFtd| d|pRtdd}|||d	| _ || _|| _|| _d
S )a  Initialize with API key and url.

        Args:
            url: The url to be crawled.
            api_key: The Firecrawl API key. If not specified will be read from env var
                FIRECRAWL_API_KEY. Get an API key
            api_url: The Firecrawl API URL. If not specified will be read from env var
                FIRECRAWL_API_URL or defaults to https://api.firecrawl.dev.
            mode: The mode to run the loader in. Default is "crawl".
                 Options include "scrape" (single url) and
                 "crawl" (all accessible sub pages).
            params: The parameters to pass to the Firecrawl API.
                Examples include crawlerOptions.
                For more details, visit: https://github.com/mendableai/firecrawl-py
        r   )FirecrawlAppzD`firecrawl` package not found, please run `pip install firecrawl-py`r   Unrecognized mode '%'. Expected one of 'crawl', 'scrape'.r
   ZFIRECRAWL_API_KEY)r
   r   N)	firecrawlr   ImportError
ValueErrorr   r   r   r   )selfr   r
   r   r   r   r    r   R/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/document_loaders/firecrawl.py__init__C   s    

zFireCrawlLoader.__init__)returnc                 c   s   | j dkr"| jj| j| jdg}n2| j dkrB| jj| j| jd}ntd| j  d|D ]V}|di }| jd k	r| jdi dd	kr|d
|d
< t|dd|dV  qXd S )Nr   )r   r	   r   r   metadataZextractorOptionsr   zllm-extractionZllm_extractionZmarkdown )Zpage_contentr   )	r   r   Z
scrape_urlr   r   Z	crawl_urlr   getr   )r   Zfirecrawl_docsdocr   r   r   r   	lazy_loadl   s&    

 zFireCrawlLoader.lazy_load)__name__
__module____qualname____doc__strr   r   dictr   r   r   r    r   r   r   r   r      s   >)r   N)typingr   r   r   Zlangchain_core.document_loadersr   Zlangchain_core.documentsr   Zlangchain_core.utilsr   r   r   r   r   r   <module>   s   