-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
community: Add you.com tool, add async to retriever, add async testing, add You tool doc #18032
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a181845
:art: add async methods and tests
scottnath 19f620b
:sparkles: add YouSearchTool and tests
scottnath 592d40c
Merge remote-tracking branch 'upstream/master' into you-tests-tool
scottnath fe6d5d4
:art: add you tool integration doc
scottnath 981daf4
Merge remote-tracking branch 'upstream/master' into you-tests-tool
scottnath 5a7c889
Update you.py
scottnath f724b12
Update tool.py
scottnath aafd68c
Update tool.py
scottnath caa0fc5
Update tool.py
scottnath 8a53dd5
Update test_imports.py
scottnath 214f1c6
Merge branch 'master' into you-tests-tool
scottnath 476acd8
test fixes
scottnath 6517670
Merge remote-tracking branch 'upstream/master' into you-tests-tool
scottnath 2f0cc90
Update you.py
scottnath File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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,8 @@ | ||
"""You.com API toolkit.""" | ||
|
||
|
||
from langchain_community.tools.you.tool import YouSearchTool | ||
|
||
__all__ = [ | ||
"YouSearchTool", | ||
] |
This file contains 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,43 @@ | ||
from typing import List, Optional, Type | ||
|
||
from langchain_core.callbacks import ( | ||
AsyncCallbackManagerForToolRun, | ||
CallbackManagerForToolRun, | ||
) | ||
from langchain_core.documents import Document | ||
from langchain_core.pydantic_v1 import BaseModel, Field | ||
from langchain_core.tools import BaseTool | ||
|
||
from langchain_community.utilities.you import YouSearchAPIWrapper | ||
|
||
|
||
class YouInput(BaseModel): | ||
query: str = Field(description="should be a search query") | ||
|
||
|
||
class YouSearchTool(BaseTool): | ||
"""Tool that searches the you.com API""" | ||
|
||
name = "you_search" | ||
description = ( | ||
"The YOU APIs make LLMs and search experiences more factual and" | ||
"up to date with realtime web data." | ||
) | ||
args_schema: Type[BaseModel] = YouInput | ||
api_wrapper: YouSearchAPIWrapper = Field(default_factory=YouSearchAPIWrapper) | ||
|
||
def _run( | ||
self, | ||
query: str, | ||
run_manager: Optional[CallbackManagerForToolRun] = None, | ||
) -> List[Document]: | ||
"""Use the you.com tool.""" | ||
return self.api_wrapper.results(query) | ||
|
||
async def _arun( | ||
self, | ||
query: str, | ||
run_manager: Optional[AsyncCallbackManagerForToolRun] = None, | ||
) -> List[Document]: | ||
"""Use the you.com tool asynchronously.""" | ||
return await self.api_wrapper.results_async(query) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,87 @@ | ||
from unittest.mock import AsyncMock, patch | ||
|
||
import pytest | ||
import responses | ||
|
||
from langchain_community.tools.you import YouSearchTool | ||
from langchain_community.utilities.you import YouSearchAPIWrapper | ||
|
||
from ..utilities.test_you import ( | ||
LIMITED_PARSED_OUTPUT, | ||
MOCK_PARSED_OUTPUT, | ||
MOCK_RESPONSE_RAW, | ||
NEWS_RESPONSE_PARSED, | ||
NEWS_RESPONSE_RAW, | ||
TEST_ENDPOINT, | ||
) | ||
|
||
|
||
class TestYouSearchTool: | ||
@responses.activate | ||
def test_invoke(self) -> None: | ||
responses.add( | ||
responses.GET, f"{TEST_ENDPOINT}/search", json=MOCK_RESPONSE_RAW, status=200 | ||
) | ||
query = "Test query text" | ||
you_tool = YouSearchTool(api_wrapper=YouSearchAPIWrapper(ydc_api_key="test")) | ||
results = you_tool.invoke(query) | ||
expected_result = MOCK_PARSED_OUTPUT | ||
assert results == expected_result | ||
|
||
@responses.activate | ||
def test_invoke_max_docs(self) -> None: | ||
responses.add( | ||
responses.GET, f"{TEST_ENDPOINT}/search", json=MOCK_RESPONSE_RAW, status=200 | ||
) | ||
query = "Test query text" | ||
you_tool = YouSearchTool( | ||
api_wrapper=YouSearchAPIWrapper(ydc_api_key="test", k=2) | ||
) | ||
results = you_tool.invoke(query) | ||
expected_result = [MOCK_PARSED_OUTPUT[0], MOCK_PARSED_OUTPUT[1]] | ||
assert results == expected_result | ||
|
||
@responses.activate | ||
def test_invoke_limit_snippets(self) -> None: | ||
responses.add( | ||
responses.GET, f"{TEST_ENDPOINT}/search", json=MOCK_RESPONSE_RAW, status=200 | ||
) | ||
query = "Test query text" | ||
you_tool = YouSearchTool( | ||
api_wrapper=YouSearchAPIWrapper(ydc_api_key="test", n_snippets_per_hit=1) | ||
) | ||
results = you_tool.invoke(query) | ||
expected_result = LIMITED_PARSED_OUTPUT | ||
assert results == expected_result | ||
|
||
@responses.activate | ||
def test_invoke_news(self) -> None: | ||
responses.add( | ||
responses.GET, f"{TEST_ENDPOINT}/news", json=NEWS_RESPONSE_RAW, status=200 | ||
) | ||
|
||
query = "Test news text" | ||
you_tool = YouSearchTool( | ||
api_wrapper=YouSearchAPIWrapper(ydc_api_key="test", endpoint_type="news") | ||
) | ||
results = you_tool.invoke(query) | ||
expected_result = NEWS_RESPONSE_PARSED | ||
assert results == expected_result | ||
|
||
@pytest.mark.asyncio | ||
async def test_ainvoke(self) -> None: | ||
you_tool = YouSearchTool(api_wrapper=YouSearchAPIWrapper(ydc_api_key="test")) | ||
|
||
# Mock response object to simulate aiohttp response | ||
mock_response = AsyncMock() | ||
mock_response.__aenter__.return_value = ( | ||
mock_response # Make the context manager return itself | ||
) | ||
mock_response.__aexit__.return_value = None # No value needed for exit | ||
mock_response.status = 200 | ||
mock_response.json = AsyncMock(return_value=MOCK_RESPONSE_RAW) | ||
|
||
# Patch the aiohttp.ClientSession object | ||
with patch("aiohttp.ClientSession.get", return_value=mock_response): | ||
results = await you_tool.ainvoke("test query") | ||
assert results == MOCK_PARSED_OUTPUT |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the type hint to ignore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hwchase17 development artifact. Removed
thanks