U
    h"                     @   sh   d dl mZmZmZmZmZ d dlmZ d dlm	Z	m
Z
 d dlmZ erTd dlmZ G dd de	ZdS )	    )TYPE_CHECKINGAnyCallableDictOptional)Document)	BaseModelroot_validator)get_from_dict_or_envApifyDatasetLoaderc                	   @   s4  e Zd ZU dZeed< eed< dZee ed< e	dde
e
dd	d
Zddddee
ee
gef ee ee ee ddddZddddee
ee
gef ee ee ee ddddZddddee
ee
gef ee ee ee ddddZddddee
ee
gef ee ee ee ddddZdS )ApifyWrapperzWrapper around Apify.
    To use, you should have the ``apify-client`` python package installed,
    and the environment variable ``APIFY_API_TOKEN`` set with your API key, or pass
    `apify_api_token` as a named parameter to the constructor.
    apify_clientapify_client_asyncNapify_api_tokenT)pre)valuesreturnc           	      C   s   t |dd}zxddlm}m} ||}t|jd }rH|jd  d7  < ||}t|jd }rr|jd  d7  < ||d	< ||d
< W n tk
r   tdY nX |S )zValidate environment.
        Validate that an Apify API token is set and the apify-client
        Python package exists in the current environment.
        r   ZAPIFY_API_TOKENr   )ApifyClientApifyClientAsynchttpx_clientz
user-agentz; Origin/langchainhttpx_async_clientr   r   z`Could not import apify-client Python package. Please install it with `pip install apify-client`.)r
   r   r   r   getattrhttp_clientheadersImportError)	clsr   r   r   r   clientr   Zasync_clientr    r   G/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/apify.pyvalidate_environment   s.       
z!ApifyWrapper.validate_environment)buildmemory_mbytestimeout_secsr   )actor_id	run_inputdataset_mapping_functionr!   r"   r#   r   c          	      C   s6   ddl m} | j|j||||d}||d |dS )a  Run an Actor on the Apify platform and wait for results to be ready.
        Args:
            actor_id (str): The ID or name of the Actor on the Apify platform.
            run_input (Dict): The input object of the Actor that you're trying to run.
            dataset_mapping_function (Callable): A function that takes a single
                dictionary (an Apify dataset item) and converts it to an
                instance of the Document class.
            build (str, optional): Optionally specifies the actor build to run.
                It can be either a build tag or build number.
            memory_mbytes (int, optional): Optional memory limit for the run,
                in megabytes.
            timeout_secs (int, optional): Optional timeout for the run, in seconds.
        Returns:
            ApifyDatasetLoader: A loader that will fetch the records from the
                Actor run's default dataset.
        r   r   r%   r!   r"   r#   defaultDatasetIdZ
dataset_idr&   )$langchain_community.document_loadersr   r   actorcall	selfr$   r%   r&   r!   r"   r#   r   Z
actor_callr   r   r   
call_actor7   s    zApifyWrapper.call_actorc          	         s<   ddl m} | j|j||||dI dH }||d |dS )a  Run an Actor on the Apify platform and wait for results to be ready.
        Args:
            actor_id (str): The ID or name of the Actor on the Apify platform.
            run_input (Dict): The input object of the Actor that you're trying to run.
            dataset_mapping_function (Callable): A function that takes a single
                dictionary (an Apify dataset item) and converts it to
                an instance of the Document class.
            build (str, optional): Optionally specifies the actor build to run.
                It can be either a build tag or build number.
            memory_mbytes (int, optional): Optional memory limit for the run,
                in megabytes.
            timeout_secs (int, optional): Optional timeout for the run, in seconds.
        Returns:
            ApifyDatasetLoader: A loader that will fetch the records from the
                Actor run's default dataset.
        r   r   r'   Nr(   r)   )r*   r   r   r+   r,   r-   r   r   r   acall_actor_   s    zApifyWrapper.acall_actor)task_id
task_inputr&   r!   r"   r#   r   c          	      C   s6   ddl m} | j|j||||d}||d |dS )  Run a saved Actor task on Apify and wait for results to be ready.
        Args:
            task_id (str): The ID or name of the task on the Apify platform.
            task_input (Dict): The input object of the task that you're trying to run.
                Overrides the task's saved input.
            dataset_mapping_function (Callable): A function that takes a single
                dictionary (an Apify dataset item) and converts it to an
                instance of the Document class.
            build (str, optional): Optionally specifies the actor build to run.
                It can be either a build tag or build number.
            memory_mbytes (int, optional): Optional memory limit for the run,
                in megabytes.
            timeout_secs (int, optional): Optional timeout for the run, in seconds.
        Returns:
            ApifyDatasetLoader: A loader that will fetch the records from the
                task run's default dataset.
        r   r   r2   r!   r"   r#   r(   r)   )r*   r   r   taskr,   	r.   r1   r2   r&   r!   r"   r#   r   Z	task_callr   r   r   call_actor_task   s    zApifyWrapper.call_actor_taskc          	         s<   ddl m} | j|j||||dI dH }||d |dS )r3   r   r   r4   Nr(   r)   )r*   r   r   r5   r,   r6   r   r   r   acall_actor_task   s    zApifyWrapper.acall_actor_task)__name__
__module____qualname____doc__r   __annotations__r   r   strr	   r   r    r   r   intr/   r0   r7   r8   r   r   r   r   r      sl   
&../r   N)typingr   r   r   r   r   Zlangchain_core.documentsr   Zlangchain_core.pydantic_v1r   r	   Zlangchain_core.utilsr
   r*   r   r   r   r   r   r   <module>   s   