generated from langchain-ai/integration-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 272
Add FeedCoop Search API(supported by volcengine) Toolkit, Utilities, and Integration with Tests #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
LotusZhao0521
wants to merge
15
commits into
langchain-ai:main
from
LotusZhao0521:feature/feedcoop-search
Closed
Add FeedCoop Search API(supported by volcengine) Toolkit, Utilities, and Integration with Tests #286
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3dd4f11
feat: add FeedCoop Search API toolkit and utility with integration an…
LotusZhao0521 77e9b2e
refactor: improve type hints and formatting in FeedCoopSearch API uti…
LotusZhao0521 55311b9
feat: add FeedCoopSearchResults tool and update imports in the tools …
LotusZhao0521 3a59cf2
fix: correct spelling of FeedCoopSearchResults in module lookup and a…
LotusZhao0521 27a3163
feat: add FeedCoopSearchResults import and update test imports
LotusZhao0521 c33d228
Update libs/community/langchain_community/tools/feedcoop_search/tool.py
LotusZhao0521 660f4e3
Update libs/community/langchain_community/tools/feedcoop_search/tool.py
LotusZhao0521 20e9186
Update libs/community/tests/unit_tests/utilities/test_feedcoop.py
LotusZhao0521 ccf4011
Update libs/community/langchain_community/tools/feedcoop_search/tool.py
LotusZhao0521 8932c81
Update libs/community/langchain_community/utilities/feedcoop_search.py
LotusZhao0521 4dd7f29
refactor: remove duplicate dependencies and unused variable
LotusZhao0521 516e1d9
refactor: add necessary dependency
LotusZhao0521 0cba20c
Merge branch 'main' into feature/feedcoop-search
LotusZhao0521 c453fda
Merge branch 'main' into feature/feedcoop-search
LotusZhao0521 669263a
Merge branch 'main' into feature/feedcoop-search
LotusZhao0521 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
libs/community/langchain_community/tools/feedcoop_search/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """FeedCoop Search API toolkit.""" | ||
|
|
||
| from langchain_community.tools.feedcoop_search.tool import FeedCoopSearchResults | ||
|
|
||
| __all__ = ["FeedCoopSearchResults"] |
159 changes: 159 additions & 0 deletions
159
libs/community/langchain_community/tools/feedcoop_search/tool.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| """Tool for the FeedCoop search API.""" | ||
|
|
||
| from typing import Any, Dict, List, Literal, Optional, Type, Union | ||
|
|
||
| from langchain_core.callbacks import ( | ||
| AsyncCallbackManagerForToolRun, | ||
| CallbackManagerForToolRun, | ||
| ) | ||
| from langchain_core.tools import BaseTool | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| from langchain_community.utilities.feedcoop_search import FeedCoopSearchAPIWrapper | ||
|
|
||
|
|
||
| class FeedCoopInput(BaseModel): | ||
| """Input for the FeedCoop tool.""" | ||
|
|
||
| query: str = Field(description="search query to look up") | ||
|
|
||
|
|
||
| class FeedCoopSearchResults(BaseTool): | ||
| """Tool that queries the FeedCoop Search API and gets back json. | ||
|
|
||
| Instantiate: | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| pip install -U langchain-community | ||
| export FEEDCOOP_API_KEY="your-api-key" | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from langchain_community.tools import FeedCoopSearchResults | ||
|
|
||
| tool = FeedCoopSearchResults( | ||
| count=10, | ||
| need_content=False, | ||
| need_url=False, | ||
| include_domains=[], | ||
| need_summary=False, | ||
| time_range=None, | ||
| ) | ||
| Invoke directly with args: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| tool.invoke({'query': 'who won the last french open'}) | ||
|
|
||
| .. code-block:: json | ||
|
|
||
| { | ||
| "title": "title", | ||
| "site_name": "火山引擎" | ||
| "url": "https://www.volcengine.com/...", | ||
| "snippet": "snippet", | ||
| "content": "content", | ||
| "summary": "summary", | ||
| "publish_time": "1970-01-01T08:00:00+08:00", | ||
| "logo_url": "https://www.volcengine.com/...", | ||
| "rank_score": 1, | ||
| "auth_info_des": "正常权威", | ||
| "auth_info_level": 2 | ||
| } | ||
| """ | ||
|
|
||
| name: str = "feedcoop_search_results_json" | ||
| description: str = ( | ||
| "A search engine that queries the FeedCoop Search API and gets back json. " | ||
| "Useful for when you need to find information about current events or topics. " | ||
| "Input should be a search query." | ||
| ) | ||
| args_schema: Type[BaseModel] = FeedCoopInput | ||
|
|
||
| count: int = 10 | ||
| """Max search results to return, default is 10""" | ||
| search_type: Literal["web"] = Field(default="web") | ||
| """ | ||
| feedcoop api supports `web` and `web_summary`. | ||
| `web` is only supported for this tool. | ||
| """ | ||
| need_content: bool = False | ||
| """Whether to only return results with body text, default to false""" | ||
| need_url: bool = False | ||
| """Whether to only return the result of the original link, default to false""" | ||
| include_domains: List[str] = [] | ||
| """A list of domains to specifically include in the search results.""" | ||
| need_summary: bool = False | ||
| """Whether to include a summary of the content, default to false""" | ||
| time_range: Optional[Literal["OneDay", "OneWeek", "OneMonth", "OneYear"] | str] = ( | ||
| None | ||
| ) | ||
| """Specify the publication time for the search. | ||
| The following enumeration values are unrestricted if left blank | ||
| OneDay: Within 1 day | ||
| OneWeek: Within 1 week | ||
| OneMonth: Within 1 month | ||
| OneYear: Within 1 year | ||
| YYYY-MM-DD..YYYY-MM-DD: Content published within the period from date A (inclusive) to date B (inclusive), for example "2024-12-30..2025-12-30" | ||
| """ # noqa: E501 | ||
| api_wrapper: FeedCoopSearchAPIWrapper = Field( | ||
| default_factory=FeedCoopSearchAPIWrapper # type: ignore[arg-type] | ||
| ) | ||
| response_format: Literal["content_and_artifact"] = "content_and_artifact" | ||
|
|
||
| def __init__(self, **kwargs: Any): | ||
| """Create api_wrapper with feedcoop_api_key if provided.""" | ||
| if "feedcoop_api_key" in kwargs: | ||
| kwargs["api_wrapper"] = FeedCoopSearchAPIWrapper( | ||
| feedcoop_api_key=kwargs["feedcoop_api_key"] | ||
| ) | ||
| super().__init__(**kwargs) | ||
LotusZhao0521 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def _run( | ||
| self, | ||
| query: str, | ||
| run_manager: Optional[CallbackManagerForToolRun] = None, | ||
| ) -> tuple[Union[str, list[Dict]], Dict]: | ||
| """Use the tool.""" | ||
| try: | ||
| raw_results = self.api_wrapper.raw_results( | ||
| query, | ||
| search_type=self.search_type, | ||
| count=self.count, | ||
| need_content=self.need_content, | ||
| need_url=self.need_url, | ||
| include_domains=self.include_domains, | ||
| need_summary=self.need_summary, | ||
| time_range=self.time_range, | ||
| ) | ||
| except Exception as e: | ||
| return repr(e), {} | ||
LotusZhao0521 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| cleaned_results = self.api_wrapper.clean_results( | ||
| raw_results["Result"]["WebResults"] | ||
| ) | ||
| return cleaned_results, raw_results | ||
|
|
||
| async def _arun( | ||
| self, | ||
| query: str, | ||
| run_manager: Optional[AsyncCallbackManagerForToolRun] = None, | ||
| ) -> tuple[Union[str, list[Dict]], Dict]: | ||
| """Use the tool asynchronously.""" | ||
| try: | ||
| raw_results = await self.api_wrapper.raw_results_async( | ||
| query, | ||
| search_type=self.search_type, | ||
| count=self.count, | ||
| need_content=self.need_content, | ||
| need_url=self.need_url, | ||
| include_domains=self.include_domains, | ||
| need_summary=self.need_summary, | ||
| time_range=self.time_range, | ||
| ) | ||
| except Exception as e: | ||
| return repr(e), {} | ||
| cleaned_results = self.api_wrapper.clean_results( | ||
| raw_results["Result"]["WebResults"] | ||
| ) | ||
| return cleaned_results, raw_results | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.