Files
langchain/libs/community/langchain_community/tools/semanticscholar/tool.py
Harrison Chase 8516a03a02 langchain-community[major]: Upgrade community to pydantic 2 (#26011)
This PR upgrades langchain-community to pydantic 2.


* Most of this PR was auto-generated using code mods with gritql
(https://github.com/eyurtsev/migrate-pydantic/tree/main)
* Subsequently, some code was fixed manually due to accommodate
differences between pydantic 1 and 2

Breaking Changes:

- Use TEXTEMBED_API_KEY and TEXTEMBEB_API_URL for env variables for text
embed integrations:
cbea780492

Other changes:

- Added pydantic_settings as a required dependency for community. This
may be removed if we have enough time to convert the dependency into an
optional one.

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Bagatur <baskaryan@gmail.com>
2024-09-05 14:07:10 -04:00

40 lines
1.2 KiB
Python

"""Tool for the SemanticScholar API."""
from typing import Optional, Type
from langchain_core.callbacks import CallbackManagerForToolRun
from langchain_core.tools import BaseTool
from pydantic import BaseModel, Field
from langchain_community.utilities.semanticscholar import SemanticScholarAPIWrapper
class SemantscholarInput(BaseModel):
"""Input for the SemanticScholar tool."""
query: str = Field(description="search query to look up")
class SemanticScholarQueryRun(BaseTool):
"""Tool that searches the semanticscholar API."""
name: str = "semanticscholar"
description: str = (
"A wrapper around semantischolar.org "
"Useful for when you need to answer to questions"
"from research papers."
"Input should be a search query."
)
api_wrapper: SemanticScholarAPIWrapper = Field(
default_factory=SemanticScholarAPIWrapper # type: ignore[arg-type]
)
args_schema: Type[BaseModel] = SemantscholarInput
def _run(
self,
query: str,
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str:
"""Use the Semantic Scholar tool."""
return self.api_wrapper.run(query)