U
    h
                     @   sN   d Z ddlZddlmZmZmZ ddlmZmZ e	e
ZG dd deZdS )z4Utils for interacting with the Semantic Scholar API.    N)AnyDictOptional)	BaseModelroot_validatorc                	   @   s   e Zd ZU dZeed< dZeed< dZeed< dZ	eed< d	Z
ee ed
< dddddddddg	ZeddeedddZeedddZdS )SemanticScholarAPIWrappera  Wrapper around semanticscholar.org API.
    https://github.com/danielnsilva/semanticscholar

    You should have this library installed.

    `pip install semanticscholar`

    Semantic Scholar API can conduct searches and fetch document metadata
    like title, abstract, authors, etc.

    Attributes:
    top_k_results: number of the top-scored document used for the Semantic Scholar tool
    load_max_docs: a limit to the number of loaded documents

    Example:
    .. code-block:: python

    from langchain_community.utilities.semanticscholar import SemanticScholarAPIWrapper
    ss = SemanticScholarAPIWrapper(
        top_k_results = 3,
        load_max_docs = 3
    )
    ss.run("biases in large language models")
    semanticscholar_search   top_k_resultsi,  S2_MAX_QUERY_LENGTHd   load_max_docsi  doc_content_chars_maxtitleabstractZvenueyearZpaperIdZcitationCountZopenAccessPdfauthorsZexternalIdsT)pre)valuesreturnc                 C   sB   z ddl m} | }|j|d< W n tk
r<   tdY nX |S )z7Validate that the python package exists in environment.r   )SemanticScholarr   zfCould not import Semanticscholar python package. Please install it with `pip install semanticscholar`.)Zsemanticscholarr   Zsearch_paperImportError)clsr   r   Zsch r   Q/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/semanticscholar.pyvalidate_environment6   s    
z.SemanticScholarAPIWrapper.validate_environment)queryr   c                 C   s   | j || j| jd}g }|d| j D ]\}ddd t|dg D }|dt|dd d	t|d
d d| dt|dd d	 q&|rd|d| j S dS dS )zRun the Semantic Scholar API.)limitfieldsNz, c                 s   s   | ]}|d  V  qdS )nameNr   ).0authorr   r   r   	<genexpr>L   s    z0SemanticScholarAPIWrapper.run.<locals>.<genexpr>r   zPublished year: r   z
Title: r   z

Authors: z
Abstract: r   
z

zNo results found.)r   r   returned_fieldsr
   joingetattrappendr   )selfr   resultsZ	documentsitemr   r   r   r   runE   s       

4zSemanticScholarAPIWrapper.runN)__name__
__module____qualname____doc__r   __annotations__r
   intr   r   r   r   r$   r   r   r   strr+   r   r   r   r   r      s&   
r   )r/   loggingtypingr   r   r   Zlangchain_core.pydantic_v1r   r   	getLoggerr,   loggerr   r   r   r   r   <module>   s
   
