langchain/libs/community/langchain_community/utilities/google_scholar.py
Bagatur a0c2281540
infra: update mypy 1.10, ruff 0.5 (#23721)
```python
"""python scripts/update_mypy_ruff.py"""
import glob
import tomllib
from pathlib import Path

import toml
import subprocess
import re

ROOT_DIR = Path(__file__).parents[1]


def main():
    for path in glob.glob(str(ROOT_DIR / "libs/**/pyproject.toml"), recursive=True):
        print(path)
        with open(path, "rb") as f:
            pyproject = tomllib.load(f)
        try:
            pyproject["tool"]["poetry"]["group"]["typing"]["dependencies"]["mypy"] = (
                "^1.10"
            )
            pyproject["tool"]["poetry"]["group"]["lint"]["dependencies"]["ruff"] = (
                "^0.5"
            )
        except KeyError:
            continue
        with open(path, "w") as f:
            toml.dump(pyproject, f)
        cwd = "/".join(path.split("/")[:-1])
        completed = subprocess.run(
            "poetry lock --no-update; poetry install --with typing; poetry run mypy . --no-color",
            cwd=cwd,
            shell=True,
            capture_output=True,
            text=True,
        )
        logs = completed.stdout.split("\n")

        to_ignore = {}
        for l in logs:
            if re.match("^(.*)\:(\d+)\: error:.*\[(.*)\]", l):
                path, line_no, error_type = re.match(
                    "^(.*)\:(\d+)\: error:.*\[(.*)\]", l
                ).groups()
                if (path, line_no) in to_ignore:
                    to_ignore[(path, line_no)].append(error_type)
                else:
                    to_ignore[(path, line_no)] = [error_type]
        print(len(to_ignore))
        for (error_path, line_no), error_types in to_ignore.items():
            all_errors = ", ".join(error_types)
            full_path = f"{cwd}/{error_path}"
            try:
                with open(full_path, "r") as f:
                    file_lines = f.readlines()
            except FileNotFoundError:
                continue
            file_lines[int(line_no) - 1] = (
                file_lines[int(line_no) - 1][:-1] + f"  # type: ignore[{all_errors}]\n"
            )
            with open(full_path, "w") as f:
                f.write("".join(file_lines))

        subprocess.run(
            "poetry run ruff format .; poetry run ruff --select I --fix .",
            cwd=cwd,
            shell=True,
            capture_output=True,
            text=True,
        )


if __name__ == "__main__":
    main()

```
2024-07-03 10:33:27 -07:00

131 lines
5.1 KiB
Python

"""Util that calls Google Scholar Search."""
from typing import Dict, Optional
from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator
from langchain_core.utils import get_from_dict_or_env
class GoogleScholarAPIWrapper(BaseModel):
"""Wrapper for Google Scholar API
You can create serpapi key by signing up at: https://serpapi.com/users/sign_up.
The wrapper uses the serpapi python package:
https://serpapi.com/integrations/python#search-google-scholar
To use, you should have the environment variable ``SERP_API_KEY``
set with your API key, or pass `serp_api_key` as a named parameter
to the constructor.
Attributes:
top_k_results: number of results to return from google-scholar query search.
By default it returns top 10 results.
hl: attribute defines the language to use for the Google Scholar search.
It's a two-letter language code.
(e.g., en for English, es for Spanish, or fr for French). Head to the
Google languages page for a full list of supported Google languages:
https://serpapi.com/google-languages
lr: attribute defines one or multiple languages to limit the search to.
It uses lang_{two-letter language code} to specify languages
and | as a delimiter. (e.g., lang_fr|lang_de will only search French
and German pages). Head to the Google lr languages for a full
list of supported languages: https://serpapi.com/google-lr-languages
Example:
.. code-block:: python
from langchain_community.utilities import GoogleScholarAPIWrapper
google_scholar = GoogleScholarAPIWrapper()
google_scholar.run('langchain')
"""
top_k_results: int = 10
hl: str = "en"
lr: str = "lang_en"
serp_api_key: Optional[str] = None
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
@root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""
serp_api_key = get_from_dict_or_env(values, "serp_api_key", "SERP_API_KEY")
values["SERP_API_KEY"] = serp_api_key
try:
from serpapi import GoogleScholarSearch
except ImportError:
raise ImportError(
"google-search-results is not installed. "
"Please install it with `pip install google-search-results"
">=2.4.2`"
)
GoogleScholarSearch.SERP_API_KEY = serp_api_key
values["google_scholar_engine"] = GoogleScholarSearch
return values
def run(self, query: str) -> str:
"""Run query through GoogleSearchScholar and parse result"""
total_results = []
page = 0
while page < max((self.top_k_results - 20), 1):
# We are getting 20 results from every page
# which is the max in order to reduce the number of API CALLS.
# 0 is the first page of results, 20 is the 2nd page of results,
# 40 is the 3rd page of results, etc.
results = (
self.google_scholar_engine( # type: ignore
{
"q": query,
"start": page,
"hl": self.hl,
"num": min(
self.top_k_results, 20
), # if top_k_result is less than 20.
"lr": self.lr,
}
)
.get_dict()
.get("organic_results", [])
)
total_results.extend(results)
if not results: # No need to search for more pages if current page
# has returned no results
break
page += 20
if (
self.top_k_results % 20 != 0 and page > 20 and total_results
): # From the last page we would only need top_k_results%20 results
# if k is not divisible by 20.
results = (
self.google_scholar_engine( # type: ignore
{
"q": query,
"start": page,
"num": self.top_k_results % 20,
"hl": self.hl,
"lr": self.lr,
}
)
.get_dict()
.get("organic_results", [])
)
total_results.extend(results)
if not total_results:
return "No good Google Scholar Result was found"
docs = [
f"Title: {result.get('title','')}\n"
f"Authors: {','.join([author.get('name') for author in result.get('publication_info',{}).get('authors',[])])}\n" # noqa: E501
f"Summary: {result.get('publication_info',{}).get('summary','')}\n"
f"Total-Citations: {result.get('inline_links',{}).get('cited_by',{}).get('total','')}" # noqa: E501
for result in total_results
]
return "\n\n".join(docs)