dspy-contextfetch 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.
- dspy_contextfetch-0.1.0/.gitignore +3 -0
- dspy_contextfetch-0.1.0/LICENSE +21 -0
- dspy_contextfetch-0.1.0/PKG-INFO +49 -0
- dspy_contextfetch-0.1.0/README.md +32 -0
- dspy_contextfetch-0.1.0/dspy_contextfetch/__init__.py +46 -0
- dspy_contextfetch-0.1.0/pyproject.toml +29 -0
- dspy_contextfetch-0.1.0/tests/test_tools.py +75 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ContextFetch
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: dspy-contextfetch
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: ContextFetch web search and page reading as DSPy tools
|
|
5
|
+
Project-URL: Homepage, https://contextfetch.dev
|
|
6
|
+
Project-URL: Source, https://github.com/lachy3433/dspy-contextfetch
|
|
7
|
+
Author: ContextFetch
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: agents,contextfetch,dspy,tools,web search
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Requires-Dist: contextfetch<1.0.0,>=0.1.0
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# dspy-contextfetch
|
|
19
|
+
|
|
20
|
+
[ContextFetch](https://contextfetch.dev) web search and page reading as [DSPy](https://dspy.ai) tools.
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install dspy-contextfetch
|
|
24
|
+
export CONTEXTFETCH_API_KEY="..." # optional: without a key, requests use the keyless tier
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
import dspy
|
|
29
|
+
from dspy_contextfetch import read_page, web_search
|
|
30
|
+
|
|
31
|
+
dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"))
|
|
32
|
+
agent = dspy.ReAct("question -> answer", tools=[web_search, read_page])
|
|
33
|
+
print(agent(question="What does asyncio.TaskGroup do when a task fails?").answer)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- `web_search(query, num_results=5, include_domains=None, start_published_date=None)`: the top results as
|
|
37
|
+
Markdown, each with title, URL, published date and the parts of the page relevant to the query.
|
|
38
|
+
- `read_page(url, query=None)`: one page as Markdown; with `query`, only the parts relevant to it.
|
|
39
|
+
|
|
40
|
+
Both are plain functions over the [`contextfetch`](https://pypi.org/project/contextfetch/) SDK, so they also
|
|
41
|
+
work with `dspy.Tool(...)` or called directly. Errors from the API raise `contextfetch.ContextFetchError`.
|
|
42
|
+
|
|
43
|
+
## Tests
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
uv run --group test pytest
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The live test runs when `~/.contextfetch_smoke_key` exists.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# dspy-contextfetch
|
|
2
|
+
|
|
3
|
+
[ContextFetch](https://contextfetch.dev) web search and page reading as [DSPy](https://dspy.ai) tools.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pip install dspy-contextfetch
|
|
7
|
+
export CONTEXTFETCH_API_KEY="..." # optional: without a key, requests use the keyless tier
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```python
|
|
11
|
+
import dspy
|
|
12
|
+
from dspy_contextfetch import read_page, web_search
|
|
13
|
+
|
|
14
|
+
dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"))
|
|
15
|
+
agent = dspy.ReAct("question -> answer", tools=[web_search, read_page])
|
|
16
|
+
print(agent(question="What does asyncio.TaskGroup do when a task fails?").answer)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
- `web_search(query, num_results=5, include_domains=None, start_published_date=None)`: the top results as
|
|
20
|
+
Markdown, each with title, URL, published date and the parts of the page relevant to the query.
|
|
21
|
+
- `read_page(url, query=None)`: one page as Markdown; with `query`, only the parts relevant to it.
|
|
22
|
+
|
|
23
|
+
Both are plain functions over the [`contextfetch`](https://pypi.org/project/contextfetch/) SDK, so they also
|
|
24
|
+
work with `dspy.Tool(...)` or called directly. Errors from the API raise `contextfetch.ContextFetchError`.
|
|
25
|
+
|
|
26
|
+
## Tests
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
uv run --group test pytest
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The live test runs when `~/.contextfetch_smoke_key` exists.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""ContextFetch web search and page reading as DSPy tools.
|
|
2
|
+
|
|
3
|
+
Plain functions, so they work anywhere DSPy takes a tool: `dspy.ReAct(sig, tools=[web_search, read_page])`
|
|
4
|
+
or `dspy.Tool(web_search)`. The API key comes from the CONTEXTFETCH_API_KEY environment variable; without
|
|
5
|
+
one, requests use the keyless tier.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from contextfetch import ContextFetch
|
|
11
|
+
|
|
12
|
+
__all__ = ["read_page", "web_search"]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def web_search(
|
|
16
|
+
query: str,
|
|
17
|
+
num_results: int = 5,
|
|
18
|
+
include_domains: list[str] | None = None,
|
|
19
|
+
start_published_date: str | None = None,
|
|
20
|
+
) -> str:
|
|
21
|
+
"""Search the web. Returns the top results as Markdown: title, URL, published date and the parts of
|
|
22
|
+
each page relevant to the query. num_results is 1 to 25; include_domains limits results to those
|
|
23
|
+
domains; start_published_date (YYYY-MM-DD) keeps only pages published on or after that date."""
|
|
24
|
+
response = ContextFetch().search(
|
|
25
|
+
query,
|
|
26
|
+
num_results=num_results,
|
|
27
|
+
include_domains=include_domains,
|
|
28
|
+
start_published_date=start_published_date,
|
|
29
|
+
pruned_context=True,
|
|
30
|
+
)
|
|
31
|
+
results = response.get("results") or []
|
|
32
|
+
if not results:
|
|
33
|
+
return "No results found."
|
|
34
|
+
return "\n\n---\n\n".join(
|
|
35
|
+
f"Title: {r.get('title') or ''}\n"
|
|
36
|
+
f"URL: {r.get('url', '')}\n"
|
|
37
|
+
f"Published: {r.get('publishedDate') or 'unknown'}\n\n"
|
|
38
|
+
f"{r.get('prunedContext') or ''}"
|
|
39
|
+
for r in results
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def read_page(url: str, query: str | None = None) -> str:
|
|
44
|
+
"""Read a web page and return its content as Markdown. With query, return only the parts of the page
|
|
45
|
+
relevant to that question."""
|
|
46
|
+
return ContextFetch().read(url, query=query).markdown
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "dspy-contextfetch"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "ContextFetch web search and page reading as DSPy tools"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [{ name = "ContextFetch" }]
|
|
13
|
+
keywords = ["dspy", "contextfetch", "web search", "tools", "agents"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
18
|
+
]
|
|
19
|
+
dependencies = ["contextfetch>=0.1.0,<1.0.0"]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://contextfetch.dev"
|
|
23
|
+
Source = "https://github.com/lachy3433/dspy-contextfetch"
|
|
24
|
+
|
|
25
|
+
[dependency-groups]
|
|
26
|
+
test = ["pytest>=8", "dspy>=3.0"]
|
|
27
|
+
|
|
28
|
+
[tool.hatch.build.targets.sdist]
|
|
29
|
+
only-include = ["dspy_contextfetch", "tests", "README.md", "LICENSE"]
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
import dspy
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from dspy_contextfetch import read_page, web_search
|
|
8
|
+
|
|
9
|
+
KEY_FILE = Path.home() / ".contextfetch_smoke_key"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@pytest.fixture
|
|
13
|
+
def mocked(monkeypatch):
|
|
14
|
+
monkeypatch.setenv("CONTEXTFETCH_API_KEY", "k")
|
|
15
|
+
# The contextfetch SDK calls urllib.request.urlopen; record each request and answer it.
|
|
16
|
+
api = {"calls": [], "search": {"results": []}}
|
|
17
|
+
|
|
18
|
+
class Res:
|
|
19
|
+
def __init__(self, body, headers=None):
|
|
20
|
+
self.body, self.headers = body, headers or {}
|
|
21
|
+
|
|
22
|
+
def read(self):
|
|
23
|
+
return self.body
|
|
24
|
+
|
|
25
|
+
def __enter__(self):
|
|
26
|
+
return self
|
|
27
|
+
|
|
28
|
+
def __exit__(self, *a):
|
|
29
|
+
return False
|
|
30
|
+
|
|
31
|
+
def urlopen(req, timeout):
|
|
32
|
+
api["calls"].append(req)
|
|
33
|
+
if req.full_url.endswith("/v1/search"):
|
|
34
|
+
return Res(json.dumps(api["search"]).encode())
|
|
35
|
+
return Res(b"# Page")
|
|
36
|
+
|
|
37
|
+
monkeypatch.setattr("urllib.request.urlopen", urlopen)
|
|
38
|
+
return api
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_web_search(mocked):
|
|
42
|
+
mocked["search"] = {"results": [{"title": "T", "url": "https://a.dev", "prunedContext": "body"}]}
|
|
43
|
+
out = web_search("q", num_results=3, include_domains=["a.dev"])
|
|
44
|
+
body = json.loads(mocked["calls"][0].data)
|
|
45
|
+
assert body == {
|
|
46
|
+
"query": "q",
|
|
47
|
+
"numResults": 3,
|
|
48
|
+
"includeDomains": ["a.dev"],
|
|
49
|
+
"contents": {"pruned_context": True},
|
|
50
|
+
}
|
|
51
|
+
assert mocked["calls"][0].headers["Authorization"] == "Bearer k"
|
|
52
|
+
assert out == "Title: T\nURL: https://a.dev\nPublished: unknown\n\nbody"
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_web_search_empty(mocked):
|
|
56
|
+
assert web_search("q") == "No results found."
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_read_page(mocked):
|
|
60
|
+
assert read_page("https://a.dev/x", query="what") == "# Page"
|
|
61
|
+
assert mocked["calls"][0].full_url == "https://api.contextfetch.dev/r/https://a.dev/x?q=what"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def test_dspy_tool_schema():
|
|
65
|
+
tool = dspy.Tool(web_search)
|
|
66
|
+
assert tool.name == "web_search"
|
|
67
|
+
assert set(tool.args) == {"query", "num_results", "include_domains", "start_published_date"}
|
|
68
|
+
assert dspy.Tool(read_page).args["url"] == {"type": "string"}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@pytest.mark.skipif(not KEY_FILE.exists(), reason="no live key")
|
|
72
|
+
def test_live(monkeypatch):
|
|
73
|
+
monkeypatch.setenv("CONTEXTFETCH_API_KEY", KEY_FILE.read_text().strip())
|
|
74
|
+
assert "URL: https://" in web_search("python asyncio TaskGroup", num_results=2)
|
|
75
|
+
assert "TaskGroup" in read_page("https://docs.python.org/3/library/asyncio-task.html", "TaskGroup")
|