langchain-octen 0.1.0__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.
- langchain_octen-0.1.0/LICENSE +21 -0
- langchain_octen-0.1.0/PKG-INFO +129 -0
- langchain_octen-0.1.0/README.md +102 -0
- langchain_octen-0.1.0/langchain_octen/__init__.py +6 -0
- langchain_octen-0.1.0/langchain_octen/_utilities.py +155 -0
- langchain_octen-0.1.0/langchain_octen/py.typed +0 -0
- langchain_octen-0.1.0/langchain_octen/tools.py +158 -0
- langchain_octen-0.1.0/pyproject.toml +65 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Octen AI
|
|
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,129 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: langchain-octen
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: LangChain integration for Octen Web Search
|
|
5
|
+
License: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: langchain,octen,search,web-search,ai
|
|
8
|
+
Author: Octen Team
|
|
9
|
+
Author-email: support@octen.ai
|
|
10
|
+
Requires-Python: >=3.9,<4.0
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
22
|
+
Requires-Dist: langchain-core (>=0.3.15,<2.0.0)
|
|
23
|
+
Requires-Dist: octen (>=0.2.0,<1.0.0)
|
|
24
|
+
Project-URL: Repository, https://github.com/octen-ai/langchain-octen
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# langchain-octen
|
|
28
|
+
|
|
29
|
+
[](https://pypi.org/project/langchain-octen/)
|
|
30
|
+
[](https://opensource.org/licenses/MIT)
|
|
31
|
+
|
|
32
|
+
LangChain integration for [Octen Web Search](https://octen.ai). Use Octen's fast, real-time web search inside LangChain agents and chains.
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install langchain-octen
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Setup
|
|
41
|
+
|
|
42
|
+
Get an API key from [octen.ai](https://octen.ai) and set it as an environment variable:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
export OCTEN_API_KEY="your-api-key"
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Or pass it directly when creating the tool:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from langchain_octen import OctenSearchResults
|
|
52
|
+
|
|
53
|
+
tool = OctenSearchResults(octen_api_key="your-api-key")
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
### Basic Search
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from langchain_octen import OctenSearchResults
|
|
62
|
+
|
|
63
|
+
tool = OctenSearchResults(max_results=5)
|
|
64
|
+
results = tool.invoke({"query": "latest AI research papers"})
|
|
65
|
+
for r in results:
|
|
66
|
+
print(r["title"], r["url"])
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### With Domain Filtering
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
results = tool.invoke({
|
|
73
|
+
"query": "machine learning",
|
|
74
|
+
"include_domains": ["arxiv.org", "papers.nips.cc"],
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### In a LangChain Agent
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from langchain_octen import OctenSearchResults
|
|
82
|
+
from langgraph.prebuilt import create_react_agent
|
|
83
|
+
|
|
84
|
+
tool = OctenSearchResults(max_results=5)
|
|
85
|
+
agent = create_react_agent(llm, [tool])
|
|
86
|
+
response = agent.invoke({"messages": [("user", "What happened in AI today?")]})
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Async Support
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
import asyncio
|
|
93
|
+
from langchain_octen import OctenSearchResults
|
|
94
|
+
|
|
95
|
+
tool = OctenSearchResults(max_results=3)
|
|
96
|
+
|
|
97
|
+
async def main():
|
|
98
|
+
results = await tool.ainvoke({"query": "Python 3.13 new features"})
|
|
99
|
+
print(results)
|
|
100
|
+
|
|
101
|
+
asyncio.run(main())
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Configuration
|
|
105
|
+
|
|
106
|
+
| Parameter | Type | Default | Description |
|
|
107
|
+
|---|---|---|---|
|
|
108
|
+
| `octen_api_key` | `str` | env `OCTEN_API_KEY` | Octen API key |
|
|
109
|
+
| `max_results` | `int` | `5` | Number of results (1-100) |
|
|
110
|
+
| `search_type` | `str` | `None` | `auto`, `keyword`, or `semantic` |
|
|
111
|
+
| `include_domains` | `list[str]` | `None` | Restrict to these domains |
|
|
112
|
+
| `exclude_domains` | `list[str]` | `None` | Exclude these domains |
|
|
113
|
+
| `include_text` | `list[str]` | `None` | Text that must appear in results |
|
|
114
|
+
| `exclude_text` | `list[str]` | `None` | Text to exclude from results |
|
|
115
|
+
| `time_basis` | `str` | `None` | `auto`, `published`, or `crawled` |
|
|
116
|
+
| `start_time` | `str` | `None` | Start time (ISO 8601) |
|
|
117
|
+
| `end_time` | `str` | `None` | End time (ISO 8601) |
|
|
118
|
+
| `format` | `str` | `None` | `text` or `markdown` |
|
|
119
|
+
| `safesearch` | `str` | `None` | `off` or `strict` |
|
|
120
|
+
| `highlight_enabled` | `bool` | `None` | Return highlighted snippets |
|
|
121
|
+
| `highlight_max_tokens` | `int` | `None` | Max tokens for highlights |
|
|
122
|
+
| `full_content_enabled` | `bool` | `None` | Return full page content |
|
|
123
|
+
| `full_content_max_tokens` | `int` | `None` | Max tokens for full content |
|
|
124
|
+
| `timeout` | `float` | `None` | Request timeout in seconds |
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
MIT
|
|
129
|
+
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# langchain-octen
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/langchain-octen/)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
LangChain integration for [Octen Web Search](https://octen.ai). Use Octen's fast, real-time web search inside LangChain agents and chains.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pip install langchain-octen
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Setup
|
|
15
|
+
|
|
16
|
+
Get an API key from [octen.ai](https://octen.ai) and set it as an environment variable:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
export OCTEN_API_KEY="your-api-key"
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Or pass it directly when creating the tool:
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from langchain_octen import OctenSearchResults
|
|
26
|
+
|
|
27
|
+
tool = OctenSearchResults(octen_api_key="your-api-key")
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
### Basic Search
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from langchain_octen import OctenSearchResults
|
|
36
|
+
|
|
37
|
+
tool = OctenSearchResults(max_results=5)
|
|
38
|
+
results = tool.invoke({"query": "latest AI research papers"})
|
|
39
|
+
for r in results:
|
|
40
|
+
print(r["title"], r["url"])
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### With Domain Filtering
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
results = tool.invoke({
|
|
47
|
+
"query": "machine learning",
|
|
48
|
+
"include_domains": ["arxiv.org", "papers.nips.cc"],
|
|
49
|
+
})
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### In a LangChain Agent
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from langchain_octen import OctenSearchResults
|
|
56
|
+
from langgraph.prebuilt import create_react_agent
|
|
57
|
+
|
|
58
|
+
tool = OctenSearchResults(max_results=5)
|
|
59
|
+
agent = create_react_agent(llm, [tool])
|
|
60
|
+
response = agent.invoke({"messages": [("user", "What happened in AI today?")]})
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Async Support
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
import asyncio
|
|
67
|
+
from langchain_octen import OctenSearchResults
|
|
68
|
+
|
|
69
|
+
tool = OctenSearchResults(max_results=3)
|
|
70
|
+
|
|
71
|
+
async def main():
|
|
72
|
+
results = await tool.ainvoke({"query": "Python 3.13 new features"})
|
|
73
|
+
print(results)
|
|
74
|
+
|
|
75
|
+
asyncio.run(main())
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Configuration
|
|
79
|
+
|
|
80
|
+
| Parameter | Type | Default | Description |
|
|
81
|
+
|---|---|---|---|
|
|
82
|
+
| `octen_api_key` | `str` | env `OCTEN_API_KEY` | Octen API key |
|
|
83
|
+
| `max_results` | `int` | `5` | Number of results (1-100) |
|
|
84
|
+
| `search_type` | `str` | `None` | `auto`, `keyword`, or `semantic` |
|
|
85
|
+
| `include_domains` | `list[str]` | `None` | Restrict to these domains |
|
|
86
|
+
| `exclude_domains` | `list[str]` | `None` | Exclude these domains |
|
|
87
|
+
| `include_text` | `list[str]` | `None` | Text that must appear in results |
|
|
88
|
+
| `exclude_text` | `list[str]` | `None` | Text to exclude from results |
|
|
89
|
+
| `time_basis` | `str` | `None` | `auto`, `published`, or `crawled` |
|
|
90
|
+
| `start_time` | `str` | `None` | Start time (ISO 8601) |
|
|
91
|
+
| `end_time` | `str` | `None` | End time (ISO 8601) |
|
|
92
|
+
| `format` | `str` | `None` | `text` or `markdown` |
|
|
93
|
+
| `safesearch` | `str` | `None` | `off` or `strict` |
|
|
94
|
+
| `highlight_enabled` | `bool` | `None` | Return highlighted snippets |
|
|
95
|
+
| `highlight_max_tokens` | `int` | `None` | Max tokens for highlights |
|
|
96
|
+
| `full_content_enabled` | `bool` | `None` | Return full page content |
|
|
97
|
+
| `full_content_max_tokens` | `int` | `None` | Max tokens for full content |
|
|
98
|
+
| `timeout` | `float` | `None` | Request timeout in seconds |
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
|
|
102
|
+
MIT
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""Octen Search API wrapper for LangChain."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from typing import Any, Dict, List, Optional
|
|
7
|
+
|
|
8
|
+
from pydantic import BaseModel, SecretStr, model_validator
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class OctenSearchAPIWrapper(BaseModel):
|
|
12
|
+
"""Wrapper around the Octen Search API.
|
|
13
|
+
|
|
14
|
+
Uses the ``octen`` Python SDK to execute web searches.
|
|
15
|
+
A new client is created and closed per call to stay Pydantic-serialization safe.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
octen_api_key: SecretStr
|
|
19
|
+
max_results: int = 5
|
|
20
|
+
search_type: Optional[str] = None
|
|
21
|
+
include_domains: Optional[List[str]] = None
|
|
22
|
+
exclude_domains: Optional[List[str]] = None
|
|
23
|
+
include_text: Optional[List[str]] = None
|
|
24
|
+
exclude_text: Optional[List[str]] = None
|
|
25
|
+
time_basis: Optional[str] = None
|
|
26
|
+
start_time: Optional[str] = None
|
|
27
|
+
end_time: Optional[str] = None
|
|
28
|
+
highlight_enabled: Optional[bool] = None
|
|
29
|
+
highlight_max_tokens: Optional[int] = None
|
|
30
|
+
format: Optional[str] = None
|
|
31
|
+
safesearch: Optional[str] = None
|
|
32
|
+
full_content_enabled: Optional[bool] = None
|
|
33
|
+
full_content_max_tokens: Optional[int] = None
|
|
34
|
+
timeout: Optional[float] = None
|
|
35
|
+
|
|
36
|
+
@model_validator(mode="before")
|
|
37
|
+
@classmethod
|
|
38
|
+
def validate_environment(cls, values: Dict[str, Any]) -> Dict[str, Any]:
|
|
39
|
+
"""Read OCTEN_API_KEY from environment if not provided."""
|
|
40
|
+
if not values.get("octen_api_key"):
|
|
41
|
+
api_key = os.environ.get("OCTEN_API_KEY", "")
|
|
42
|
+
if not api_key:
|
|
43
|
+
raise ValueError(
|
|
44
|
+
"OCTEN_API_KEY must be provided as a parameter or set as an "
|
|
45
|
+
"environment variable."
|
|
46
|
+
)
|
|
47
|
+
values["octen_api_key"] = SecretStr(api_key)
|
|
48
|
+
return values
|
|
49
|
+
|
|
50
|
+
def _build_search_kwargs(
|
|
51
|
+
self,
|
|
52
|
+
query: str,
|
|
53
|
+
*,
|
|
54
|
+
include_domains: Optional[List[str]] = None,
|
|
55
|
+
exclude_domains: Optional[List[str]] = None,
|
|
56
|
+
) -> Dict[str, Any]:
|
|
57
|
+
"""Build kwargs dict for the SDK search call."""
|
|
58
|
+
kwargs: Dict[str, Any] = {"query": query, "count": self.max_results}
|
|
59
|
+
|
|
60
|
+
if self.search_type is not None:
|
|
61
|
+
kwargs["search_type"] = self.search_type
|
|
62
|
+
|
|
63
|
+
# Merge per-call domain filters with instance defaults
|
|
64
|
+
inc = include_domains or self.include_domains
|
|
65
|
+
exc = exclude_domains or self.exclude_domains
|
|
66
|
+
if inc:
|
|
67
|
+
kwargs["include_domains"] = inc
|
|
68
|
+
if exc:
|
|
69
|
+
kwargs["exclude_domains"] = exc
|
|
70
|
+
|
|
71
|
+
if self.include_text:
|
|
72
|
+
kwargs["include_text"] = self.include_text
|
|
73
|
+
if self.exclude_text:
|
|
74
|
+
kwargs["exclude_text"] = self.exclude_text
|
|
75
|
+
if self.time_basis is not None:
|
|
76
|
+
kwargs["time_basis"] = self.time_basis
|
|
77
|
+
if self.start_time is not None:
|
|
78
|
+
kwargs["start_time"] = self.start_time
|
|
79
|
+
if self.end_time is not None:
|
|
80
|
+
kwargs["end_time"] = self.end_time
|
|
81
|
+
if self.format is not None:
|
|
82
|
+
kwargs["format"] = self.format
|
|
83
|
+
if self.safesearch is not None:
|
|
84
|
+
kwargs["safesearch"] = self.safesearch
|
|
85
|
+
if self.timeout is not None:
|
|
86
|
+
kwargs["timeout"] = self.timeout
|
|
87
|
+
|
|
88
|
+
# Highlight options
|
|
89
|
+
if self.highlight_enabled is not None:
|
|
90
|
+
from octen.models.search import HighlightOptions
|
|
91
|
+
|
|
92
|
+
highlight_kwargs: Dict[str, Any] = {"enable": self.highlight_enabled}
|
|
93
|
+
if self.highlight_max_tokens is not None:
|
|
94
|
+
highlight_kwargs["max_tokens"] = self.highlight_max_tokens
|
|
95
|
+
kwargs["highlight"] = HighlightOptions(**highlight_kwargs)
|
|
96
|
+
|
|
97
|
+
# Full content options
|
|
98
|
+
if self.full_content_enabled is not None:
|
|
99
|
+
from octen.models.search import FullContentOptions
|
|
100
|
+
|
|
101
|
+
fc_kwargs: Dict[str, Any] = {"enable": self.full_content_enabled}
|
|
102
|
+
if self.full_content_max_tokens is not None:
|
|
103
|
+
fc_kwargs["max_tokens"] = self.full_content_max_tokens
|
|
104
|
+
kwargs["full_content"] = FullContentOptions(**fc_kwargs)
|
|
105
|
+
|
|
106
|
+
return kwargs
|
|
107
|
+
|
|
108
|
+
@staticmethod
|
|
109
|
+
def _format_results(raw_results: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
110
|
+
"""Normalize raw result dicts, omitting None-valued fields."""
|
|
111
|
+
cleaned: List[Dict[str, Any]] = []
|
|
112
|
+
for r in raw_results:
|
|
113
|
+
entry = {k: v for k, v in r.items() if v is not None}
|
|
114
|
+
cleaned.append(entry)
|
|
115
|
+
return cleaned
|
|
116
|
+
|
|
117
|
+
def results(
|
|
118
|
+
self,
|
|
119
|
+
query: str,
|
|
120
|
+
*,
|
|
121
|
+
include_domains: Optional[List[str]] = None,
|
|
122
|
+
exclude_domains: Optional[List[str]] = None,
|
|
123
|
+
) -> List[Dict[str, Any]]:
|
|
124
|
+
"""Execute a synchronous search and return cleaned results."""
|
|
125
|
+
from octen import Octen
|
|
126
|
+
|
|
127
|
+
kwargs = self._build_search_kwargs(
|
|
128
|
+
query, include_domains=include_domains, exclude_domains=exclude_domains
|
|
129
|
+
)
|
|
130
|
+
client = Octen(api_key=self.octen_api_key.get_secret_value())
|
|
131
|
+
try:
|
|
132
|
+
response = client.search.search(**kwargs)
|
|
133
|
+
return self._format_results(response.results)
|
|
134
|
+
finally:
|
|
135
|
+
client.close()
|
|
136
|
+
|
|
137
|
+
async def results_async(
|
|
138
|
+
self,
|
|
139
|
+
query: str,
|
|
140
|
+
*,
|
|
141
|
+
include_domains: Optional[List[str]] = None,
|
|
142
|
+
exclude_domains: Optional[List[str]] = None,
|
|
143
|
+
) -> List[Dict[str, Any]]:
|
|
144
|
+
"""Execute an asynchronous search and return cleaned results."""
|
|
145
|
+
from octen import AsyncOcten
|
|
146
|
+
|
|
147
|
+
kwargs = self._build_search_kwargs(
|
|
148
|
+
query, include_domains=include_domains, exclude_domains=exclude_domains
|
|
149
|
+
)
|
|
150
|
+
client = AsyncOcten(api_key=self.octen_api_key.get_secret_value())
|
|
151
|
+
try:
|
|
152
|
+
response = await client.search.search(**kwargs)
|
|
153
|
+
return self._format_results(response.results)
|
|
154
|
+
finally:
|
|
155
|
+
await client.close()
|
|
File without changes
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"""Octen Search tool for LangChain."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Dict, List, Optional, Type
|
|
6
|
+
|
|
7
|
+
from langchain_core.callbacks import (
|
|
8
|
+
AsyncCallbackManagerForToolRun,
|
|
9
|
+
CallbackManagerForToolRun,
|
|
10
|
+
)
|
|
11
|
+
from langchain_core.tools import BaseTool, ToolException
|
|
12
|
+
from pydantic import BaseModel, Field, SecretStr
|
|
13
|
+
|
|
14
|
+
from langchain_octen._utilities import OctenSearchAPIWrapper
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class OctenSearchInput(BaseModel):
|
|
18
|
+
"""Input schema for the Octen search tool.
|
|
19
|
+
|
|
20
|
+
Only exposes fields that are safe for an LLM to control per-query.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
query: str = Field(description="The search query string.")
|
|
24
|
+
include_domains: Optional[List[str]] = Field(
|
|
25
|
+
default=None,
|
|
26
|
+
description="Optional list of domains to restrict results to.",
|
|
27
|
+
)
|
|
28
|
+
exclude_domains: Optional[List[str]] = Field(
|
|
29
|
+
default=None,
|
|
30
|
+
description="Optional list of domains to exclude from results.",
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class OctenSearchResults(BaseTool):
|
|
35
|
+
"""Tool that queries the Octen Web Search API and returns structured results.
|
|
36
|
+
|
|
37
|
+
Setup:
|
|
38
|
+
Install ``langchain-octen`` and set the ``OCTEN_API_KEY`` environment variable.
|
|
39
|
+
|
|
40
|
+
.. code-block:: bash
|
|
41
|
+
|
|
42
|
+
pip install langchain-octen
|
|
43
|
+
export OCTEN_API_KEY="your-api-key"
|
|
44
|
+
|
|
45
|
+
Instantiate:
|
|
46
|
+
.. code-block:: python
|
|
47
|
+
|
|
48
|
+
from langchain_octen import OctenSearchResults
|
|
49
|
+
|
|
50
|
+
tool = OctenSearchResults(max_results=5)
|
|
51
|
+
|
|
52
|
+
Invoke directly:
|
|
53
|
+
.. code-block:: python
|
|
54
|
+
|
|
55
|
+
tool.invoke({"query": "LangChain Python framework"})
|
|
56
|
+
|
|
57
|
+
Use within an agent:
|
|
58
|
+
.. code-block:: python
|
|
59
|
+
|
|
60
|
+
from langgraph.prebuilt import create_react_agent
|
|
61
|
+
|
|
62
|
+
agent = create_react_agent(llm, [tool])
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
name: str = "octen_search"
|
|
66
|
+
description: str = (
|
|
67
|
+
"A web search engine powered by Octen. "
|
|
68
|
+
"Useful for finding up-to-date information on the internet. "
|
|
69
|
+
"Input should be a search query string."
|
|
70
|
+
)
|
|
71
|
+
args_schema: Type[BaseModel] = OctenSearchInput
|
|
72
|
+
handle_tool_error: bool = True
|
|
73
|
+
|
|
74
|
+
api_wrapper: OctenSearchAPIWrapper = Field(default=None, exclude=True) # type: ignore[assignment]
|
|
75
|
+
|
|
76
|
+
# --- Config fields (forwarded to OctenSearchAPIWrapper) ---
|
|
77
|
+
octen_api_key: Optional[SecretStr] = None
|
|
78
|
+
max_results: int = 5
|
|
79
|
+
search_type: Optional[str] = None
|
|
80
|
+
include_domains: Optional[List[str]] = None
|
|
81
|
+
exclude_domains: Optional[List[str]] = None
|
|
82
|
+
include_text: Optional[List[str]] = None
|
|
83
|
+
exclude_text: Optional[List[str]] = None
|
|
84
|
+
time_basis: Optional[str] = None
|
|
85
|
+
start_time: Optional[str] = None
|
|
86
|
+
end_time: Optional[str] = None
|
|
87
|
+
highlight_enabled: Optional[bool] = None
|
|
88
|
+
highlight_max_tokens: Optional[int] = None
|
|
89
|
+
format: Optional[str] = None
|
|
90
|
+
safesearch: Optional[str] = None
|
|
91
|
+
full_content_enabled: Optional[bool] = None
|
|
92
|
+
full_content_max_tokens: Optional[int] = None
|
|
93
|
+
timeout: Optional[float] = None
|
|
94
|
+
|
|
95
|
+
def __init__(self, **kwargs: Any) -> None:
|
|
96
|
+
"""Initialize OctenSearchResults, creating the underlying API wrapper."""
|
|
97
|
+
super().__init__(**kwargs)
|
|
98
|
+
wrapper_kwargs: Dict[str, Any] = {}
|
|
99
|
+
if self.octen_api_key is not None:
|
|
100
|
+
wrapper_kwargs["octen_api_key"] = self.octen_api_key
|
|
101
|
+
wrapper_kwargs["max_results"] = self.max_results
|
|
102
|
+
for field_name in (
|
|
103
|
+
"search_type",
|
|
104
|
+
"include_domains",
|
|
105
|
+
"exclude_domains",
|
|
106
|
+
"include_text",
|
|
107
|
+
"exclude_text",
|
|
108
|
+
"time_basis",
|
|
109
|
+
"start_time",
|
|
110
|
+
"end_time",
|
|
111
|
+
"highlight_enabled",
|
|
112
|
+
"highlight_max_tokens",
|
|
113
|
+
"format",
|
|
114
|
+
"safesearch",
|
|
115
|
+
"full_content_enabled",
|
|
116
|
+
"full_content_max_tokens",
|
|
117
|
+
"timeout",
|
|
118
|
+
):
|
|
119
|
+
val = getattr(self, field_name)
|
|
120
|
+
if val is not None:
|
|
121
|
+
wrapper_kwargs[field_name] = val
|
|
122
|
+
self.api_wrapper = OctenSearchAPIWrapper(**wrapper_kwargs)
|
|
123
|
+
|
|
124
|
+
def _run(
|
|
125
|
+
self,
|
|
126
|
+
query: str,
|
|
127
|
+
include_domains: Optional[List[str]] = None,
|
|
128
|
+
exclude_domains: Optional[List[str]] = None,
|
|
129
|
+
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
130
|
+
) -> List[Dict[str, Any]]:
|
|
131
|
+
"""Synchronously execute search."""
|
|
132
|
+
try:
|
|
133
|
+
results = self.api_wrapper.results(
|
|
134
|
+
query, include_domains=include_domains, exclude_domains=exclude_domains
|
|
135
|
+
)
|
|
136
|
+
except Exception as e:
|
|
137
|
+
return [{"error": str(e)}]
|
|
138
|
+
if not results:
|
|
139
|
+
raise ToolException(f"No results found for query: {query!r}")
|
|
140
|
+
return results
|
|
141
|
+
|
|
142
|
+
async def _arun(
|
|
143
|
+
self,
|
|
144
|
+
query: str,
|
|
145
|
+
include_domains: Optional[List[str]] = None,
|
|
146
|
+
exclude_domains: Optional[List[str]] = None,
|
|
147
|
+
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
|
|
148
|
+
) -> List[Dict[str, Any]]:
|
|
149
|
+
"""Asynchronously execute search."""
|
|
150
|
+
try:
|
|
151
|
+
results = await self.api_wrapper.results_async(
|
|
152
|
+
query, include_domains=include_domains, exclude_domains=exclude_domains
|
|
153
|
+
)
|
|
154
|
+
except Exception as e:
|
|
155
|
+
return [{"error": str(e)}]
|
|
156
|
+
if not results:
|
|
157
|
+
raise ToolException(f"No results found for query: {query!r}")
|
|
158
|
+
return results
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["poetry-core>=1.0.0"]
|
|
3
|
+
build-backend = "poetry.core.masonry.api"
|
|
4
|
+
|
|
5
|
+
[tool.poetry]
|
|
6
|
+
name = "langchain-octen"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "LangChain integration for Octen Web Search"
|
|
9
|
+
authors = ["Octen Team <support@octen.ai>"]
|
|
10
|
+
license = "MIT"
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
repository = "https://github.com/octen-ai/langchain-octen"
|
|
13
|
+
keywords = ["langchain", "octen", "search", "web-search", "ai"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.9",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
25
|
+
]
|
|
26
|
+
packages = [{ include = "langchain_octen" }]
|
|
27
|
+
|
|
28
|
+
[tool.poetry.dependencies]
|
|
29
|
+
python = ">=3.9,<4.0"
|
|
30
|
+
langchain-core = ">=0.3.15,<2.0.0"
|
|
31
|
+
octen = ">=0.2.0,<1.0.0"
|
|
32
|
+
|
|
33
|
+
[tool.poetry.group.test.dependencies]
|
|
34
|
+
pytest = ">=7.4,<9"
|
|
35
|
+
pytest-asyncio = ">=0.23,<1"
|
|
36
|
+
pytest-socket = ">=0.7,<1"
|
|
37
|
+
|
|
38
|
+
[tool.poetry.group.dev.dependencies]
|
|
39
|
+
ruff = ">=0.5,<1"
|
|
40
|
+
mypy = ">=1.10,<2"
|
|
41
|
+
|
|
42
|
+
[tool.ruff]
|
|
43
|
+
target-version = "py39"
|
|
44
|
+
line-length = 100
|
|
45
|
+
|
|
46
|
+
[tool.ruff.lint]
|
|
47
|
+
select = ["E", "F", "I", "W"]
|
|
48
|
+
|
|
49
|
+
[tool.mypy]
|
|
50
|
+
python_version = "3.9"
|
|
51
|
+
strict = true
|
|
52
|
+
warn_return_any = true
|
|
53
|
+
warn_unused_configs = true
|
|
54
|
+
|
|
55
|
+
[[tool.mypy.overrides]]
|
|
56
|
+
module = ["octen", "octen.*"]
|
|
57
|
+
ignore_missing_imports = true
|
|
58
|
+
follow_untyped_imports = true
|
|
59
|
+
|
|
60
|
+
[[tool.mypy.overrides]]
|
|
61
|
+
module = "langchain_octen._utilities"
|
|
62
|
+
disallow_untyped_calls = false
|
|
63
|
+
|
|
64
|
+
[tool.pytest.ini_options]
|
|
65
|
+
asyncio_mode = "auto"
|