langchain-exa 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 LangChain, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ Metadata-Version: 2.1
2
+ Name: langchain-exa
3
+ Version: 0.0.1
4
+ Summary: An integration package connecting Exa and LangChain
5
+ Home-page: https://github.com/langchain-ai/langchain
6
+ License: MIT
7
+ Requires-Python: >=3.8.1,<4.0
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Requires-Dist: exa-py (>=1.0.7,<2.0.0)
15
+ Requires-Dist: langchain-core (>=0.0.12)
16
+ Project-URL: Repository, https://github.com/langchain-ai/langchain
17
+ Project-URL: Source Code, https://github.com/langchain-ai/langchain/tree/master/libs/partners/exa
18
+ Description-Content-Type: text/markdown
19
+
20
+ # langchain-exa
21
+
@@ -0,0 +1 @@
1
+ # langchain-exa
@@ -0,0 +1,12 @@
1
+ from exa_py.api import HighlightsContentsOptions, TextContentsOptions # type: ignore
2
+
3
+ from langchain_exa.retrievers import ExaSearchRetriever
4
+ from langchain_exa.tools import ExaFindSimilarResults, ExaSearchResults
5
+
6
+ __all__ = [
7
+ "ExaSearchResults",
8
+ "ExaSearchRetriever",
9
+ "HighlightsContentsOptions",
10
+ "TextContentsOptions",
11
+ "ExaFindSimilarResults",
12
+ ]
@@ -0,0 +1,18 @@
1
+ import os
2
+ from typing import Dict
3
+
4
+ from exa_py import Exa # type: ignore
5
+ from langchain_core.utils import convert_to_secret_str
6
+
7
+
8
+ def initialize_client(values: Dict) -> Dict:
9
+ """Initialize the client."""
10
+ exa_api_key = values.get("exa_api_key") or os.environ.get("EXA_API_KEY") or ""
11
+ values["exa_api_key"] = convert_to_secret_str(exa_api_key)
12
+ args = {
13
+ "api_key": values["exa_api_key"].get_secret_value(),
14
+ }
15
+ if values.get("exa_base_url"):
16
+ args["base_url"] = values["exa_base_url"]
17
+ values["client"] = Exa(**args)
18
+ return values
File without changes
@@ -0,0 +1,91 @@
1
+ from typing import Any, Dict, List, Literal, Optional, Union
2
+
3
+ from exa_py import Exa # type: ignore
4
+ from exa_py.api import HighlightsContentsOptions, TextContentsOptions # type: ignore
5
+ from langchain_core.callbacks import CallbackManagerForRetrieverRun
6
+ from langchain_core.documents import Document
7
+ from langchain_core.pydantic_v1 import SecretStr, root_validator
8
+ from langchain_core.retrievers import BaseRetriever
9
+
10
+ from langchain_exa._utilities import initialize_client
11
+
12
+
13
+ def _get_metadata(result: Any) -> Dict[str, Any]:
14
+ """Get the metadata from a result object."""
15
+ metadata = {
16
+ "title": result.title,
17
+ "url": result.url,
18
+ "id": result.id,
19
+ "score": result.score,
20
+ "published_date": result.published_date,
21
+ "author": result.author,
22
+ }
23
+ if getattr(result, "highlights"):
24
+ metadata["highlights"] = result.highlights
25
+ if getattr(result, "highlight_scores"):
26
+ metadata["highlight_scores"] = result.highlight_scores
27
+ return metadata
28
+
29
+
30
+ class ExaSearchRetriever(BaseRetriever):
31
+ """Exa Search retriever."""
32
+
33
+ k: int = 10 # num_results
34
+ """The number of search results to return."""
35
+ include_domains: Optional[List[str]] = None
36
+ """A list of domains to include in the search."""
37
+ exclude_domains: Optional[List[str]] = None
38
+ """A list of domains to exclude from the search."""
39
+ start_crawl_date: Optional[str] = None
40
+ """The start date for the crawl (in YYYY-MM-DD format)."""
41
+ end_crawl_date: Optional[str] = None
42
+ """The end date for the crawl (in YYYY-MM-DD format)."""
43
+ start_published_date: Optional[str] = None
44
+ """The start date for when the document was published (in YYYY-MM-DD format)."""
45
+ end_published_date: Optional[str] = None
46
+ """The end date for when the document was published (in YYYY-MM-DD format)."""
47
+ use_autoprompt: Optional[bool] = None
48
+ """Whether to use autoprompt for the search."""
49
+ type: str = "neural"
50
+ """The type of search, 'keyword' or 'neural'. Default: neural"""
51
+ highlights: Optional[Union[HighlightsContentsOptions, bool]] = None
52
+ """Whether to set the page content to the highlights of the results."""
53
+ text_contents_options: Union[TextContentsOptions, Literal[True]] = True
54
+ """How to set the page content of the results"""
55
+
56
+ client: Exa
57
+ exa_api_key: SecretStr
58
+ exa_base_url: Optional[str] = None
59
+
60
+ @root_validator(pre=True)
61
+ def validate_environment(cls, values: Dict) -> Dict:
62
+ """Validate the environment."""
63
+ values = initialize_client(values)
64
+ return values
65
+
66
+ def _get_relevant_documents(
67
+ self, query: str, *, run_manager: CallbackManagerForRetrieverRun
68
+ ) -> List[Document]:
69
+ response = self.client.search_and_contents(
70
+ query,
71
+ num_results=self.k,
72
+ text=self.text_contents_options,
73
+ highlights=self.highlights,
74
+ include_domains=self.include_domains,
75
+ exclude_domains=self.exclude_domains,
76
+ start_crawl_date=self.start_crawl_date,
77
+ end_crawl_date=self.end_crawl_date,
78
+ start_published_date=self.start_published_date,
79
+ end_published_date=self.end_published_date,
80
+ use_autoprompt=self.use_autoprompt,
81
+ )
82
+
83
+ results = response.results
84
+
85
+ return [
86
+ Document(
87
+ page_content=(result.text),
88
+ metadata=_get_metadata(result),
89
+ )
90
+ for result in results
91
+ ]
@@ -0,0 +1,120 @@
1
+ """Tool for the Exa Search API."""
2
+
3
+ from typing import Dict, List, Optional, Union
4
+
5
+ from exa_py import Exa # type: ignore
6
+ from exa_py.api import HighlightsContentsOptions, TextContentsOptions # type: ignore
7
+ from langchain_core.callbacks import (
8
+ CallbackManagerForToolRun,
9
+ )
10
+ from langchain_core.pydantic_v1 import SecretStr, root_validator
11
+ from langchain_core.tools import BaseTool
12
+
13
+ from langchain_exa._utilities import initialize_client
14
+
15
+
16
+ class ExaSearchResults(BaseTool):
17
+ """Tool that queries the Metaphor Search API and gets back json."""
18
+
19
+ name: str = "exa_search_results_json"
20
+ description: str = (
21
+ "A wrapper around Exa Search. "
22
+ "Input should be an Exa-optimized query. "
23
+ "Output is a JSON array of the query results"
24
+ )
25
+ client: Exa
26
+ exa_api_key: SecretStr
27
+
28
+ @root_validator(pre=True)
29
+ def validate_environment(cls, values: Dict) -> Dict:
30
+ """Validate the environment."""
31
+ values = initialize_client(values)
32
+ return values
33
+
34
+ def _run(
35
+ self,
36
+ query: str,
37
+ num_results: int,
38
+ text_contents_options: Optional[Union[TextContentsOptions, bool]] = None,
39
+ highlights: Optional[Union[HighlightsContentsOptions, bool]] = None,
40
+ include_domains: Optional[List[str]] = None,
41
+ exclude_domains: Optional[List[str]] = None,
42
+ start_crawl_date: Optional[str] = None,
43
+ end_crawl_date: Optional[str] = None,
44
+ start_published_date: Optional[str] = None,
45
+ end_published_date: Optional[str] = None,
46
+ use_autoprompt: Optional[bool] = None,
47
+ run_manager: Optional[CallbackManagerForToolRun] = None,
48
+ ) -> Union[List[Dict], str]:
49
+ """Use the tool."""
50
+ try:
51
+ return self.client.search_and_contents(
52
+ query,
53
+ num_results=num_results,
54
+ text=text_contents_options,
55
+ highlights=highlights,
56
+ include_domains=include_domains,
57
+ exclude_domains=exclude_domains,
58
+ start_crawl_date=start_crawl_date,
59
+ end_crawl_date=end_crawl_date,
60
+ start_published_date=start_published_date,
61
+ end_published_date=end_published_date,
62
+ use_autoprompt=use_autoprompt,
63
+ )
64
+ except Exception as e:
65
+ return repr(e)
66
+
67
+
68
+ class ExaFindSimilarResults(BaseTool):
69
+ """Tool that queries the Metaphor Search API and gets back json."""
70
+
71
+ name: str = "exa_find_similar_results_json"
72
+ description: str = (
73
+ "A wrapper around Exa Find Similar. "
74
+ "Input should be an Exa-optimized query. "
75
+ "Output is a JSON array of the query results"
76
+ )
77
+ client: Exa
78
+ exa_api_key: SecretStr
79
+ exa_base_url: Optional[str] = None
80
+
81
+ @root_validator(pre=True)
82
+ def validate_environment(cls, values: Dict) -> Dict:
83
+ """Validate the environment."""
84
+ values = initialize_client(values)
85
+ return values
86
+
87
+ def _run(
88
+ self,
89
+ url: str,
90
+ num_results: int,
91
+ text_contents_options: Optional[Union[TextContentsOptions, bool]] = None,
92
+ highlights: Optional[Union[HighlightsContentsOptions, bool]] = None,
93
+ include_domains: Optional[List[str]] = None,
94
+ exclude_domains: Optional[List[str]] = None,
95
+ start_crawl_date: Optional[str] = None,
96
+ end_crawl_date: Optional[str] = None,
97
+ start_published_date: Optional[str] = None,
98
+ end_published_date: Optional[str] = None,
99
+ exclude_source_domain: Optional[bool] = None,
100
+ category: Optional[str] = None,
101
+ run_manager: Optional[CallbackManagerForToolRun] = None,
102
+ ) -> Union[List[Dict], str]:
103
+ """Use the tool."""
104
+ try:
105
+ return self.client.find_similar_and_contents(
106
+ url,
107
+ num_results=num_results,
108
+ text=text_contents_options,
109
+ highlights=highlights,
110
+ include_domains=include_domains,
111
+ exclude_domains=exclude_domains,
112
+ start_crawl_date=start_crawl_date,
113
+ end_crawl_date=end_crawl_date,
114
+ start_published_date=start_published_date,
115
+ end_published_date=end_published_date,
116
+ exclude_source_domain=exclude_source_domain,
117
+ category=category,
118
+ )
119
+ except Exception as e:
120
+ return repr(e)
@@ -0,0 +1,95 @@
1
+ [tool.poetry]
2
+ name = "langchain-exa"
3
+ version = "0.0.1"
4
+ description = "An integration package connecting Exa and LangChain"
5
+ authors = []
6
+ readme = "README.md"
7
+ repository = "https://github.com/langchain-ai/langchain"
8
+ license = "MIT"
9
+
10
+ [tool.poetry.urls]
11
+ "Source Code" = "https://github.com/langchain-ai/langchain/tree/master/libs/partners/exa"
12
+
13
+ [tool.poetry.dependencies]
14
+ python = ">=3.8.1,<4.0"
15
+ langchain-core = ">=0.0.12"
16
+ exa-py = "^1.0.7"
17
+
18
+ [tool.poetry.group.test]
19
+ optional = true
20
+
21
+ [tool.poetry.group.test.dependencies]
22
+ pytest = "^7.3.0"
23
+ freezegun = "^1.2.2"
24
+ pytest-mock = "^3.10.0"
25
+ syrupy = "^4.0.2"
26
+ pytest-watcher = "^0.3.4"
27
+ pytest-asyncio = "^0.21.1"
28
+ langchain-core = {path = "../../core", develop = true}
29
+
30
+ [tool.poetry.group.codespell]
31
+ optional = true
32
+
33
+ [tool.poetry.group.codespell.dependencies]
34
+ codespell = "^2.2.0"
35
+
36
+ [tool.poetry.group.lint]
37
+ optional = true
38
+
39
+ [tool.poetry.group.lint.dependencies]
40
+ ruff = "^0.1.5"
41
+
42
+ [tool.poetry.group.typing.dependencies]
43
+ mypy = "^0.991"
44
+ langchain-core = {path = "../../core", develop = true}
45
+
46
+ [tool.poetry.group.dev]
47
+ optional = true
48
+
49
+ [tool.poetry.group.dev.dependencies]
50
+ langchain-core = {path = "../../core", develop = true}
51
+
52
+ [tool.poetry.group.test_integration]
53
+ optional = true
54
+
55
+ [tool.poetry.group.test_integration.dependencies]
56
+
57
+
58
+ [tool.ruff]
59
+ select = [
60
+ "E", # pycodestyle
61
+ "F", # pyflakes
62
+ "I", # isort
63
+ ]
64
+
65
+ [tool.mypy]
66
+ disallow_untyped_defs = "True"
67
+
68
+ [tool.coverage.run]
69
+ omit = [
70
+ "tests/*",
71
+ ]
72
+
73
+ [build-system]
74
+ requires = ["poetry-core>=1.0.0"]
75
+ build-backend = "poetry.core.masonry.api"
76
+
77
+ [tool.pytest.ini_options]
78
+ # --strict-markers will raise errors on unknown marks.
79
+ # https://docs.pytest.org/en/7.1.x/how-to/mark.html#raising-errors-on-unknown-marks
80
+ #
81
+ # https://docs.pytest.org/en/7.1.x/reference/reference.html
82
+ # --strict-config any warnings encountered while parsing the `pytest`
83
+ # section of the configuration file raise errors.
84
+ #
85
+ # https://github.com/tophat/syrupy
86
+ # --snapshot-warn-unused Prints a warning on unused snapshots rather than fail the test suite.
87
+ addopts = "--snapshot-warn-unused --strict-markers --strict-config --durations=5"
88
+ # Registering custom markers.
89
+ # https://docs.pytest.org/en/7.1.x/example/markers.html#registering-markers
90
+ markers = [
91
+ "requires: mark tests as requiring a specific library",
92
+ "asyncio: mark tests as requiring asyncio",
93
+ "compile: mark placeholder test used to compile integration tests without running them",
94
+ ]
95
+ asyncio_mode = "auto"