dspy-contextfetch 0.1.0__py3-none-any.whl
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,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,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,5 @@
|
|
|
1
|
+
dspy_contextfetch/__init__.py,sha256=UyeMJWTXsIGO3oioB0r-0iibCDd4M-xKUgJai-VJPP4,1710
|
|
2
|
+
dspy_contextfetch-0.1.0.dist-info/METADATA,sha256=HOW5C53Yfdt7EWUmtVhVI9GpUZT9dLZKBLNvngJo93c,1805
|
|
3
|
+
dspy_contextfetch-0.1.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
4
|
+
dspy_contextfetch-0.1.0.dist-info/licenses/LICENSE,sha256=RqQqv3BiTnGfaC6tdJ9oSSRKa0fzw4e7wxJwAR9Bab4,1073
|
|
5
|
+
dspy_contextfetch-0.1.0.dist-info/RECORD,,
|
|
@@ -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.
|