a
    bg6                     @  s   d dl mZ d dlmZmZmZ d dlZd dlmZ d dl	m
Z
 G dd de
ZG dd	 d	e
ZG d
d de
ZG dd de
ZdS )    )annotations)AnyDictOptionalN)CallbackManagerForToolRun)BaseToolc                   @  sx   e Zd ZU dZdZded< dZded< ded< ded< ded	< d
Zded< dddddddZddddddddZ	dS )CogniswitchKnowledgeRequesta!  Tool that uses the Cogniswitch service to answer questions.

    name: str = "cogniswitch_knowledge_request"
    description: str = (
        "A wrapper around cogniswitch service to answer the question
        from the knowledge base."
        "Input should be a search query."
    )
    Zcogniswitch_knowledge_requeststrnamezYA wrapper around cogniswitch service to 
    answer the question from the knowledge base.descriptioncs_token	OAI_tokenapiKeyz@https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeRequestapi_urlN#Optional[CallbackManagerForToolRun]Dict[str, Any])queryrun_managerreturnc                 C  s   |  | j| j|| j}|S )a  
        Use the tool to answer a query.

        Args:
            query (str): Natural language query,
              that you would like to ask to your knowledge graph.
            run_manager (Optional[CallbackManagerForChainRun]):
            Manager for chain run callbacks.

        Returns:
            Dict[str, Any]: Output dictionary containing
            the 'response' from the service.
        )	answer_csr   r   r   )selfr   r   response r   x/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/tools/cogniswitch/tool.py_run   s    z CogniswitchKnowledgeRequest._rundict)r   r   r   r   r   c                 C  s`   |st d|st d|s$t d|s0t d|||d}d|i}tj| j|d|d}| S )	ac  
        Send a query to the Cogniswitch service and retrieve the response.

        Args:
            cs_token (str): Cogniswitch token.
            OAI_token (str): OpenAI token.
            apiKey (str): OAuth token.
            query (str): Query to be answered.

        Returns:
            dict: Response JSON from the Cogniswitch service.
        zMissing cs_tokenzMissing OpenAI tokenzMissing cogniswitch OAuth tokenzMissing input query)r   platformTokenopenAITokenr   Fheadersverifydata)
ValueErrorrequestspostr   json)r   r   r   r   r   r   r!   r   r   r   r   r   2   s    z%CogniswitchKnowledgeRequest.answer_cs)N)
__name__
__module____qualname____doc__r
   __annotations__r   r   r   r   r   r   r   r   r   
   s   

 r   c                   @  sr   e Zd ZU dZdZded< dZded< ded< ded< ded	< d
Zded< dddddddZdddddZ	dS )CogniswitchKnowledgeStatusae  Tool that uses the Cogniswitch services to get the
     status of the document or url uploaded.

    name: str = "cogniswitch_knowledge_status"
    description: str = (
        "A wrapper around cogniswitch services to know the status of
         the document uploaded from a url or a file. "
        "Input should be a file name or the url link"
    )
    Zcogniswitch_knowledge_statusr	   r
   zlA wrapper around cogniswitch services to know 
    the status of the document uploaded from a url or a file.r   r   r   r   zFhttps://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeSource/statusknowledge_status_urlNr   r   )document_namer   r   c                 C  s   |  |}|S )a  
        Use the tool to know the status of the document uploaded.

        Args:
            document_name (str): name of the document or
            the url uploaded
            run_manager (Optional[CallbackManagerForChainRun]):
            Manager for chain run callbacks.

        Returns:
            Dict[str, Any]: Output dictionary containing
            the 'response' from the service.
        )knowledge_status)r   r-   r   r   r   r   r   r   i   s    
zCogniswitchKnowledgeStatus._runr   )r-   r   c                 C  sL  || j d}| j| j| j d}tj| j||dd}|jdkr>| }t|d }|d}|dkrnd	|d< nX|d
krd|d< nF|dkrd|d< n4|dkrd|d< n"|dkrd|d< n|dkrd|d< d|	 v r|
d d|	 v r|
d d|	 v r
|
d d|	 v r"|
d d|	 v r:|
d |S d|jiS dS )a  
        Use this function to know the status of the document or the URL uploaded
        Args:
            document_name (str): The document name or the url that is uploaded.

        Returns:
            dict: Response JSON from the Cogniswitch service.
        )ZdocNamer   r   r   r   F)r   paramsr       statusr   SUCCESS   
PROCESSING   ZUPLOADED   FAILURE   ZUPLOAD_FAILURE   ZREJECTEDZfilePathZsavedFileNameZintegrationConfigIdZmetaDataZ
docEntryIdmessageN)r   r   r   r#   getr,   status_coder%   r   keyspop)r   r-   r0   r   r   Zsource_infoZsource_datar3   r   r   r   r.   ~   sP    











z+CogniswitchKnowledgeStatus.knowledge_status)N)
r&   r'   r(   r)   r
   r*   r   r,   r   r.   r   r   r   r   r+   S   s   

 r+   c                   @  sz   e Zd ZU dZdZded< dZded< ded< ded< ded	< d
Zded< dddddddddZdddddddZ	dS )CogniswitchKnowledgeSourceFilea  Tool that uses the Cogniswitch services to store data from file.

    name: str = "cogniswitch_knowledge_source_file"
    description: str = (
        "This calls the CogniSwitch services to analyze & store data from a file.
        If the input looks like a file path, assign that string value to file key.
        Assign document name & description only if provided in input."
    )
    Z!cogniswitch_knowledge_source_filer	   r
   z
        This calls the CogniSwitch services to analyze & store data from a file. 
        If the input looks like a file path, assign that string value to file key. 
        Assign document name & description only if provided in input.
        r   r   r   r   zDhttps://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeSource/fileknowledgesource_fileNOptional[str]r   r   )filer-   document_descriptionr   r   c                 C  s$   |sddiS | j |||d}|S dS )a  
        Execute the tool to store the data given from a file.
        This calls the CogniSwitch services to analyze & store data from a file.
        If the input looks like a file path, assign that string value to file key.
        Assign document name & description only if provided in input.

        Args:
            file Optional[str]: The file path of your knowledge
            document_name Optional[str]: Name of your knowledge document
            document_description Optional[str]: Description of your knowledge document
            run_manager (Optional[CallbackManagerForChainRun]):
            Manager for chain run callbacks.

        Returns:
            Dict[str, Any]: Output dictionary containing
            the 'response' from the service.
        r<   No input provided)rD   r-   rE   N
store_data)r   rD   r-   rE   r   r   r   r   r   r      s    z#CogniswitchKnowledgeSourceFile._runr   )rD   r-   rE   r   c                 C  sv   | j | j| jd}|sd}|s"d}|dur8dt|di}||d}tj| j|d||d}|jd	krj| S d
diS dS )a  
        Store data using the Cogniswitch service.
        This calls the CogniSwitch services to analyze & store data from a file.
        If the input looks like a file path, assign that string value to file key.
        Assign document name & description only if provided in input.

        Args:
            file (Optional[str]): file path of your file.
            the current files supported by the files are
            .txt, .pdf, .docx, .doc, .html
            document_name (Optional[str]): Name of the document you are uploading.
            document_description (Optional[str]): Description of the document.

        Returns:
            dict: Response JSON from the Cogniswitch service.
        r/    NrD   rb)ZdocumentNameZdocumentDescriptionF)r   r    r!   filesr1   r<   Bad Request)	r   r   r   openr#   r$   rB   r>   r%   )r   rD   r-   rE   r   rK   r!   r   r   r   r   rH      s.    
z)CogniswitchKnowledgeSourceFile.store_data)NNNN)
r&   r'   r(   r)   r
   r*   r   rB   r   rH   r   r   r   r   rA      s   


    $rA   c                   @  sz   e Zd ZU dZdZded< dZded< ded< ded< ded	< d
Zded< dddddddddZdddddddZ	dS )CogniswitchKnowledgeSourceURLax  Tool that uses the Cogniswitch services to store data from a URL.

    name: str = "cogniswitch_knowledge_source_url"
    description: str = (
        "This calls the CogniSwitch services to analyze & store data from a url.
        the URL is provided in input, assign that value to the url key.
        Assign document name & description only if provided in input"
    )
    Z cogniswitch_knowledge_source_urlr	   r
   z
    This calls the CogniSwitch services to analyze & store data from a url. 
        the URL is provided in input, assign that value to the url key. 
        Assign document name & description only if provided in inputr   r   r   r   zChttps://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeSource/urlknowledgesource_urlNrC   r   r   )urlr-   rE   r   r   c                 C  s    |sddiS | j |||d}|S )a  
        Execute the tool to store the data given from a url.
        This calls the CogniSwitch services to analyze & store data from a url.
        the URL is provided in input, assign that value to the url key.
        Assign document name & description only if provided in input.

        Args:
            url Optional[str]: The website/url link of your knowledge
            document_name Optional[str]: Name of your knowledge document
            document_description Optional[str]: Description of your knowledge document
            run_manager (Optional[CallbackManagerForChainRun]):
            Manager for chain run callbacks.

        Returns:
            Dict[str, Any]: Output dictionary containing
            the 'response' from the service.
        r<   rF   )rP   r-   rE   rG   )r   rP   r-   rE   r   r   r   r   r   r   ?  s    z"CogniswitchKnowledgeSourceURL._runr   )rP   r-   rE   r   c                 C  sh   | j | j| jd}|sd}|s"d}|s.ddiS d|i}tj| j|d|d}|jdkr\| S dd	iS d
S )aE  
        Store data using the Cogniswitch service.
        This calls the CogniSwitch services to analyze & store data from a url.
        the URL is provided in input, assign that value to the url key.
        Assign document name & description only if provided in input.

        Args:
            url (Optional[str]): URL link.
            document_name (Optional[str]): Name of the document you are uploading.
            document_description (Optional[str]): Description of the document.

        Returns:
            dict: Response JSON from the Cogniswitch service.
        r/   rI   r<   rF   rP   Fr   r1   rL   N)r   r   r   r#   r$   rO   r>   r%   )r   rP   r-   rE   r   r!   r   r   r   r   rH   b  s*    
z(CogniswitchKnowledgeSourceURL.store_data)NNNN)
r&   r'   r(   r)   r
   r*   r   rO   r   rH   r   r   r   r   rN   (  s   


    #rN   )
__future__r   typingr   r   r   r#   Zlangchain_core.callbacksr   Zlangchain_core.toolsr   r   r+   rA   rN   r   r   r   r   <module>   s   Idq