llama-index-tools-pastdotdev 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.
- llama_index_tools_pastdotdev-0.1.0/.gitignore +12 -0
- llama_index_tools_pastdotdev-0.1.0/LICENSE +21 -0
- llama_index_tools_pastdotdev-0.1.0/PKG-INFO +121 -0
- llama_index_tools_pastdotdev-0.1.0/README.md +99 -0
- llama_index_tools_pastdotdev-0.1.0/llama_index/tools/pastdotdev/__init__.py +4 -0
- llama_index_tools_pastdotdev-0.1.0/llama_index/tools/pastdotdev/base.py +128 -0
- llama_index_tools_pastdotdev-0.1.0/llama_index/tools/pastdotdev/client.py +311 -0
- llama_index_tools_pastdotdev-0.1.0/pyproject.toml +65 -0
- llama_index_tools_pastdotdev-0.1.0/tests/__init__.py +0 -0
- llama_index_tools_pastdotdev-0.1.0/tests/test_client.py +78 -0
- llama_index_tools_pastdotdev-0.1.0/tests/test_live.py +25 -0
- llama_index_tools_pastdotdev-0.1.0/tests/test_tools_pastdotdev.py +209 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 past.dev
|
|
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,121 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: llama-index-tools-pastdotdev
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: past dev tools for LlamaIndex agents: recall and ingest with the past dev Memory API
|
|
5
|
+
Project-URL: Homepage, https://past.dev
|
|
6
|
+
Project-URL: Documentation, https://past.dev/docs/memory-api/quickstart
|
|
7
|
+
Project-URL: API reference, https://past.dev/docs/memory-api/api-reference
|
|
8
|
+
Project-URL: Repository, https://github.com/pastdotdev/llama-index-pastdotdev
|
|
9
|
+
Author-email: "past.dev" <louis@past.dev>
|
|
10
|
+
Maintainer-email: "past.dev" <louis@past.dev>
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: agent tools,llama-index,llamaindex,memory,past dev,past.dev,tools
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
18
|
+
Requires-Python: <4.0,>=3.10
|
|
19
|
+
Requires-Dist: httpx<1,>=0.27
|
|
20
|
+
Requires-Dist: llama-index-core<0.15,>=0.13.0
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# LlamaIndex Tools Integration: past dev
|
|
24
|
+
|
|
25
|
+
past dev is a memory API for AI agents. This package gives a LlamaIndex agent
|
|
26
|
+
two tools over [past dev](https://past.dev):
|
|
27
|
+
|
|
28
|
+
- **`recall`**: a past dev **Recall**. The agent asks a question and gets the
|
|
29
|
+
ranked memory of the current user, with the date, the kind and the quoted
|
|
30
|
+
evidence of each entry.
|
|
31
|
+
- **`ingest`**: a past dev **Ingest**. The agent keeps one piece of information
|
|
32
|
+
as a data point, so that a later recall can find it.
|
|
33
|
+
|
|
34
|
+
## Install
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install llama-index-tools-pastdotdev
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Get a key
|
|
41
|
+
|
|
42
|
+
1. Sign up at [past.dev](https://past.dev) and open a project.
|
|
43
|
+
2. Make a project API key. It starts with `past_sk_`.
|
|
44
|
+
3. Set it in the environment as `PAST_API_KEY`, or give it to the spec as `api_key`.
|
|
45
|
+
|
|
46
|
+
## Use
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from llama_index.core.agent.workflow import FunctionAgent
|
|
50
|
+
from llama_index.llms.openai import OpenAI
|
|
51
|
+
from llama_index.tools.pastdotdev import PastDevToolSpec
|
|
52
|
+
|
|
53
|
+
past_dev = PastDevToolSpec(
|
|
54
|
+
identity=current_user.id, # your id for the end user, from your own user record
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
agent = FunctionAgent(
|
|
58
|
+
tools=past_dev.to_tool_list(),
|
|
59
|
+
llm=OpenAI(model="gpt-4.1-mini"),
|
|
60
|
+
system_prompt="Recall what you know about the user before you answer.",
|
|
61
|
+
)
|
|
62
|
+
response = await agent.run("What did I decide about the launch date?")
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### The identity
|
|
66
|
+
|
|
67
|
+
past dev keeps memory per identity: your id for the end user. Recall needs an
|
|
68
|
+
identity, and the first ingest for an identity creates it.
|
|
69
|
+
|
|
70
|
+
Set `identity` in the `PastDevToolSpec` constructor, from your trigger or your
|
|
71
|
+
user record. It is not a tool argument: the model cannot see it or change it.
|
|
72
|
+
The same applies to `audience` and to the recall budget. Make one spec for
|
|
73
|
+
each user.
|
|
74
|
+
|
|
75
|
+
## The tools
|
|
76
|
+
|
|
77
|
+
| Tool | Arguments the model gives | API call |
|
|
78
|
+
| --- | --- | --- |
|
|
79
|
+
| `recall` | `query` (required), `occurredFrom`, `occurredTo`, `sort` (`relevance` or `chronological`) | `POST /api/v1/recall` |
|
|
80
|
+
| `ingest` | `content` (required), `timestamp` | `POST /api/v1/ingest` |
|
|
81
|
+
|
|
82
|
+
The argument names are the API's own field names.
|
|
83
|
+
|
|
84
|
+
## Parameters
|
|
85
|
+
|
|
86
|
+
| Parameter | Default | Description |
|
|
87
|
+
| --- | --- | --- |
|
|
88
|
+
| `identity` | (required) | Your id for the end user. |
|
|
89
|
+
| `api_key` | `PAST_API_KEY` | A past dev project API key (`past_sk_...`). |
|
|
90
|
+
| `api_host` | `PAST_API_HOST`, else `https://api.past.dev` | The API base URL. Change it only if you run past dev in your own cloud. |
|
|
91
|
+
| `audience` | none | The audience slug for ingested memory. Without it, the memory is visible to the project. |
|
|
92
|
+
| `level` | `low` (on the server) | The recall evidence budget: `low`, `medium`, `high` or `extra-high`. |
|
|
93
|
+
| `max_tokens` | none | An explicit recall evidence budget in tokens. Use `level` or `max_tokens`, not both. |
|
|
94
|
+
| `limit` | 100 (on the server) | The maximum number of recalled documents. |
|
|
95
|
+
| `label` | `Saved by a LlamaIndex agent` | The label of each data point that the model ingests. |
|
|
96
|
+
| `timeout` | `30` | The HTTP timeout in seconds. |
|
|
97
|
+
| `http_client` | none | An `httpx.Client` to use, for example with a proxy. |
|
|
98
|
+
|
|
99
|
+
## Credits
|
|
100
|
+
|
|
101
|
+
Each recall and each ingest spends credits on your past dev plan. The agent
|
|
102
|
+
decides when to call the tools, so tell it in the system prompt when to use them.
|
|
103
|
+
|
|
104
|
+
## Errors
|
|
105
|
+
|
|
106
|
+
An API error raises `PastDevError`, with the error `code`, the HTTP `status`,
|
|
107
|
+
the message and the `X-Request-Id` of the request. The LlamaIndex agent gives
|
|
108
|
+
the error to the model as the tool output. The spec retries a `429` once, after
|
|
109
|
+
its `Retry-After` delay. It does not retry other errors, for example
|
|
110
|
+
`402 out-of-credits` or a `400`.
|
|
111
|
+
|
|
112
|
+
## Links
|
|
113
|
+
|
|
114
|
+
- [Quickstart](https://past.dev/docs/memory-api/quickstart)
|
|
115
|
+
- [API reference](https://past.dev/docs/memory-api/api-reference)
|
|
116
|
+
- [LlamaIndex tools guide](https://developers.llamaindex.ai/python/framework/module_guides/deploying/agents/tools/)
|
|
117
|
+
- A memory block for the same API: [`llama-index-memory-pastdotdev`](https://pypi.org/project/llama-index-memory-pastdotdev/)
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# LlamaIndex Tools Integration: past dev
|
|
2
|
+
|
|
3
|
+
past dev is a memory API for AI agents. This package gives a LlamaIndex agent
|
|
4
|
+
two tools over [past dev](https://past.dev):
|
|
5
|
+
|
|
6
|
+
- **`recall`**: a past dev **Recall**. The agent asks a question and gets the
|
|
7
|
+
ranked memory of the current user, with the date, the kind and the quoted
|
|
8
|
+
evidence of each entry.
|
|
9
|
+
- **`ingest`**: a past dev **Ingest**. The agent keeps one piece of information
|
|
10
|
+
as a data point, so that a later recall can find it.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install llama-index-tools-pastdotdev
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Get a key
|
|
19
|
+
|
|
20
|
+
1. Sign up at [past.dev](https://past.dev) and open a project.
|
|
21
|
+
2. Make a project API key. It starts with `past_sk_`.
|
|
22
|
+
3. Set it in the environment as `PAST_API_KEY`, or give it to the spec as `api_key`.
|
|
23
|
+
|
|
24
|
+
## Use
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from llama_index.core.agent.workflow import FunctionAgent
|
|
28
|
+
from llama_index.llms.openai import OpenAI
|
|
29
|
+
from llama_index.tools.pastdotdev import PastDevToolSpec
|
|
30
|
+
|
|
31
|
+
past_dev = PastDevToolSpec(
|
|
32
|
+
identity=current_user.id, # your id for the end user, from your own user record
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
agent = FunctionAgent(
|
|
36
|
+
tools=past_dev.to_tool_list(),
|
|
37
|
+
llm=OpenAI(model="gpt-4.1-mini"),
|
|
38
|
+
system_prompt="Recall what you know about the user before you answer.",
|
|
39
|
+
)
|
|
40
|
+
response = await agent.run("What did I decide about the launch date?")
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### The identity
|
|
44
|
+
|
|
45
|
+
past dev keeps memory per identity: your id for the end user. Recall needs an
|
|
46
|
+
identity, and the first ingest for an identity creates it.
|
|
47
|
+
|
|
48
|
+
Set `identity` in the `PastDevToolSpec` constructor, from your trigger or your
|
|
49
|
+
user record. It is not a tool argument: the model cannot see it or change it.
|
|
50
|
+
The same applies to `audience` and to the recall budget. Make one spec for
|
|
51
|
+
each user.
|
|
52
|
+
|
|
53
|
+
## The tools
|
|
54
|
+
|
|
55
|
+
| Tool | Arguments the model gives | API call |
|
|
56
|
+
| --- | --- | --- |
|
|
57
|
+
| `recall` | `query` (required), `occurredFrom`, `occurredTo`, `sort` (`relevance` or `chronological`) | `POST /api/v1/recall` |
|
|
58
|
+
| `ingest` | `content` (required), `timestamp` | `POST /api/v1/ingest` |
|
|
59
|
+
|
|
60
|
+
The argument names are the API's own field names.
|
|
61
|
+
|
|
62
|
+
## Parameters
|
|
63
|
+
|
|
64
|
+
| Parameter | Default | Description |
|
|
65
|
+
| --- | --- | --- |
|
|
66
|
+
| `identity` | (required) | Your id for the end user. |
|
|
67
|
+
| `api_key` | `PAST_API_KEY` | A past dev project API key (`past_sk_...`). |
|
|
68
|
+
| `api_host` | `PAST_API_HOST`, else `https://api.past.dev` | The API base URL. Change it only if you run past dev in your own cloud. |
|
|
69
|
+
| `audience` | none | The audience slug for ingested memory. Without it, the memory is visible to the project. |
|
|
70
|
+
| `level` | `low` (on the server) | The recall evidence budget: `low`, `medium`, `high` or `extra-high`. |
|
|
71
|
+
| `max_tokens` | none | An explicit recall evidence budget in tokens. Use `level` or `max_tokens`, not both. |
|
|
72
|
+
| `limit` | 100 (on the server) | The maximum number of recalled documents. |
|
|
73
|
+
| `label` | `Saved by a LlamaIndex agent` | The label of each data point that the model ingests. |
|
|
74
|
+
| `timeout` | `30` | The HTTP timeout in seconds. |
|
|
75
|
+
| `http_client` | none | An `httpx.Client` to use, for example with a proxy. |
|
|
76
|
+
|
|
77
|
+
## Credits
|
|
78
|
+
|
|
79
|
+
Each recall and each ingest spends credits on your past dev plan. The agent
|
|
80
|
+
decides when to call the tools, so tell it in the system prompt when to use them.
|
|
81
|
+
|
|
82
|
+
## Errors
|
|
83
|
+
|
|
84
|
+
An API error raises `PastDevError`, with the error `code`, the HTTP `status`,
|
|
85
|
+
the message and the `X-Request-Id` of the request. The LlamaIndex agent gives
|
|
86
|
+
the error to the model as the tool output. The spec retries a `429` once, after
|
|
87
|
+
its `Retry-After` delay. It does not retry other errors, for example
|
|
88
|
+
`402 out-of-credits` or a `400`.
|
|
89
|
+
|
|
90
|
+
## Links
|
|
91
|
+
|
|
92
|
+
- [Quickstart](https://past.dev/docs/memory-api/quickstart)
|
|
93
|
+
- [API reference](https://past.dev/docs/memory-api/api-reference)
|
|
94
|
+
- [LlamaIndex tools guide](https://developers.llamaindex.ai/python/framework/module_guides/deploying/agents/tools/)
|
|
95
|
+
- A memory block for the same API: [`llama-index-memory-pastdotdev`](https://pypi.org/project/llama-index-memory-pastdotdev/)
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""past dev tool spec: Recall and Ingest as agent tools."""
|
|
2
|
+
|
|
3
|
+
from typing import Literal, Optional
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
from llama_index.core.tools.tool_spec.base import BaseToolSpec
|
|
7
|
+
|
|
8
|
+
from llama_index.tools.pastdotdev.client import (
|
|
9
|
+
PastDevClient,
|
|
10
|
+
format_recall,
|
|
11
|
+
now_iso,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
__version__ = "0.1.0"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class PastDevToolSpec(BaseToolSpec):
|
|
18
|
+
"""
|
|
19
|
+
past dev tools: recall and ingest.
|
|
20
|
+
|
|
21
|
+
The identity, the audience and the recall budget are fixed when you make
|
|
22
|
+
the spec, from your own user record. The model never sees them and cannot
|
|
23
|
+
change them: the tools take only the query or the content.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
identity: Your id for the end user. The tools read and write as this identity.
|
|
27
|
+
api_key: A past dev project API key (past_sk_...). Defaults to PAST_API_KEY.
|
|
28
|
+
api_host: The API base URL. Defaults to PAST_API_HOST, else https://api.past.dev.
|
|
29
|
+
audience: The audience slug for ingested memory. Omit it for project-visible memory.
|
|
30
|
+
level: The recall evidence budget: low, medium, high or extra-high.
|
|
31
|
+
max_tokens: An explicit recall evidence budget in tokens. Send level or max_tokens, not both.
|
|
32
|
+
limit: The maximum number of recalled documents.
|
|
33
|
+
label: The label of each data point the model ingests.
|
|
34
|
+
timeout: The HTTP timeout in seconds.
|
|
35
|
+
http_client: An httpx.Client to use, for example with a proxy.
|
|
36
|
+
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
spec_functions = ["recall", "ingest"]
|
|
40
|
+
|
|
41
|
+
def __init__(
|
|
42
|
+
self,
|
|
43
|
+
identity: str,
|
|
44
|
+
api_key: Optional[str] = None,
|
|
45
|
+
api_host: Optional[str] = None,
|
|
46
|
+
audience: Optional[str] = None,
|
|
47
|
+
level: Optional[Literal["low", "medium", "high", "extra-high"]] = None,
|
|
48
|
+
max_tokens: Optional[int] = None,
|
|
49
|
+
limit: Optional[int] = None,
|
|
50
|
+
label: Optional[str] = "Saved by a LlamaIndex agent",
|
|
51
|
+
timeout: float = 30.0,
|
|
52
|
+
http_client: Optional[httpx.Client] = None,
|
|
53
|
+
) -> None:
|
|
54
|
+
if not identity or not identity.strip():
|
|
55
|
+
raise ValueError("identity is required: set it from your user record.")
|
|
56
|
+
if level is not None and max_tokens is not None:
|
|
57
|
+
raise ValueError("Set level or max_tokens, never both.")
|
|
58
|
+
self.identity = identity
|
|
59
|
+
self.audience = audience
|
|
60
|
+
self.level = level
|
|
61
|
+
self.max_tokens = max_tokens
|
|
62
|
+
self.limit = limit
|
|
63
|
+
self.label = label
|
|
64
|
+
self._client = PastDevClient(
|
|
65
|
+
api_key=api_key,
|
|
66
|
+
api_host=api_host,
|
|
67
|
+
timeout=timeout,
|
|
68
|
+
user_agent=f"llama-index-tools-pastdotdev/{__version__}",
|
|
69
|
+
http_client=http_client,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
def recall(
|
|
73
|
+
self,
|
|
74
|
+
query: str,
|
|
75
|
+
occurredFrom: Optional[str] = None,
|
|
76
|
+
occurredTo: Optional[str] = None,
|
|
77
|
+
sort: Optional[Literal["relevance", "chronological"]] = None,
|
|
78
|
+
) -> str:
|
|
79
|
+
"""
|
|
80
|
+
Recall what past dev remembers about the current user: earlier conversations, facts, preferences and decisions.
|
|
81
|
+
|
|
82
|
+
Use it before you answer a question that may depend on what happened before.
|
|
83
|
+
Each result has a rank, a date and a kind; evidence lines quote the original source.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
query (str): A natural-language question, for example "What did the user decide about the launch date?".
|
|
87
|
+
occurredFrom (Optional[str]): Only memory that occurred at or after this ISO 8601 date-time.
|
|
88
|
+
occurredTo (Optional[str]): Only memory that occurred at or before this ISO 8601 date-time.
|
|
89
|
+
sort (Optional[str]): "relevance" (the default) or "chronological" (oldest first).
|
|
90
|
+
|
|
91
|
+
"""
|
|
92
|
+
response = self._client.recall(
|
|
93
|
+
query=query,
|
|
94
|
+
identity=self.identity,
|
|
95
|
+
limit=self.limit,
|
|
96
|
+
level=self.level,
|
|
97
|
+
max_tokens=self.max_tokens,
|
|
98
|
+
occurred_from=occurredFrom,
|
|
99
|
+
occurred_to=occurredTo,
|
|
100
|
+
sort=sort,
|
|
101
|
+
)
|
|
102
|
+
return format_recall(response) or "past dev has no memory that matches this query."
|
|
103
|
+
|
|
104
|
+
def ingest(self, content: str, timestamp: Optional[str] = None) -> str:
|
|
105
|
+
"""
|
|
106
|
+
Ingest one piece of information into past dev, so that a later recall can find it.
|
|
107
|
+
|
|
108
|
+
Use it when the user tells you something worth keeping: a fact, a preference or a decision.
|
|
109
|
+
Write the content as a complete, self-contained statement.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
content (str): The text to keep, for example "The user prefers email to phone calls.".
|
|
113
|
+
timestamp (Optional[str]): When it occurred, as an ISO 8601 date-time. Defaults to now.
|
|
114
|
+
|
|
115
|
+
"""
|
|
116
|
+
result = self._client.ingest(
|
|
117
|
+
content=content,
|
|
118
|
+
identity=self.identity,
|
|
119
|
+
timestamp=timestamp or now_iso(),
|
|
120
|
+
audience=self.audience,
|
|
121
|
+
label=self.label,
|
|
122
|
+
metadata={"source": "llama-index"},
|
|
123
|
+
)
|
|
124
|
+
result = result or {}
|
|
125
|
+
source_id = result.get("sourceId")
|
|
126
|
+
if result.get("unchanged"):
|
|
127
|
+
return f"past dev already had this data point (source id {source_id})."
|
|
128
|
+
return f"past dev accepted the data point (source id {source_id}, {result.get('status', 'queued')})."
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
"""A small HTTP client for the past dev Memory API.
|
|
2
|
+
|
|
3
|
+
This file is the same in llama-index-memory-pastdotdev and
|
|
4
|
+
llama-index-tools-pastdotdev, so that each package installs without the other.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import asyncio
|
|
8
|
+
import os
|
|
9
|
+
import time
|
|
10
|
+
from datetime import datetime, timezone
|
|
11
|
+
from typing import Any, Dict, List, Optional
|
|
12
|
+
|
|
13
|
+
import httpx
|
|
14
|
+
|
|
15
|
+
DEFAULT_API_HOST = "https://api.past.dev"
|
|
16
|
+
API_KEY_ENV = "PAST_API_KEY"
|
|
17
|
+
API_HOST_ENV = "PAST_API_HOST"
|
|
18
|
+
RECALL_LEVELS = ("low", "medium", "high", "extra-high")
|
|
19
|
+
RECALL_SORTS = ("relevance", "chronological")
|
|
20
|
+
|
|
21
|
+
# A 429 is retried once, after the Retry-After delay, when that delay is short.
|
|
22
|
+
MAX_RETRY_AFTER_SECONDS = 30.0
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class PastDevError(Exception):
|
|
26
|
+
"""An error response from the past dev Memory API."""
|
|
27
|
+
|
|
28
|
+
def __init__(
|
|
29
|
+
self,
|
|
30
|
+
code: str,
|
|
31
|
+
status: int,
|
|
32
|
+
message: Optional[str] = None,
|
|
33
|
+
request_id: Optional[str] = None,
|
|
34
|
+
data: Any = None,
|
|
35
|
+
) -> None:
|
|
36
|
+
self.code = code
|
|
37
|
+
self.status = status
|
|
38
|
+
self.message = message
|
|
39
|
+
self.request_id = request_id
|
|
40
|
+
self.data = data
|
|
41
|
+
text = f"past dev error {code} (HTTP {status})"
|
|
42
|
+
if message:
|
|
43
|
+
text += f": {message}"
|
|
44
|
+
if request_id:
|
|
45
|
+
text += f" [request id {request_id}]"
|
|
46
|
+
super().__init__(text)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def resolve_api_key(api_key: Optional[str]) -> str:
|
|
50
|
+
key = api_key or os.environ.get(API_KEY_ENV)
|
|
51
|
+
if not key:
|
|
52
|
+
raise ValueError(
|
|
53
|
+
"past dev needs a project API key: pass api_key or set the "
|
|
54
|
+
f"{API_KEY_ENV} environment variable."
|
|
55
|
+
)
|
|
56
|
+
return key
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def resolve_api_host(api_host: Optional[str]) -> str:
|
|
60
|
+
host = api_host or os.environ.get(API_HOST_ENV) or DEFAULT_API_HOST
|
|
61
|
+
return host.rstrip("/")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def to_iso(value: Any) -> Optional[str]:
|
|
65
|
+
"""Return an ISO 8601 string for a datetime or a string, else None."""
|
|
66
|
+
if value is None:
|
|
67
|
+
return None
|
|
68
|
+
if isinstance(value, datetime):
|
|
69
|
+
if value.tzinfo is None:
|
|
70
|
+
value = value.replace(tzinfo=timezone.utc)
|
|
71
|
+
return value.isoformat()
|
|
72
|
+
if isinstance(value, str) and value.strip():
|
|
73
|
+
return value.strip()
|
|
74
|
+
return None
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def now_iso() -> str:
|
|
78
|
+
return datetime.now(timezone.utc).isoformat()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def build_recall_body(
|
|
82
|
+
query: str,
|
|
83
|
+
identity: str,
|
|
84
|
+
limit: Optional[int] = None,
|
|
85
|
+
level: Optional[str] = None,
|
|
86
|
+
max_tokens: Optional[int] = None,
|
|
87
|
+
query_timestamp: Optional[str] = None,
|
|
88
|
+
occurred_from: Optional[str] = None,
|
|
89
|
+
occurred_to: Optional[str] = None,
|
|
90
|
+
sort: Optional[str] = None,
|
|
91
|
+
) -> Dict[str, Any]:
|
|
92
|
+
if level is not None and max_tokens is not None:
|
|
93
|
+
raise ValueError("Send level or max_tokens to recall, never both.")
|
|
94
|
+
if level is not None and level not in RECALL_LEVELS:
|
|
95
|
+
raise ValueError(f"level must be one of {', '.join(RECALL_LEVELS)}.")
|
|
96
|
+
if sort is not None and sort not in RECALL_SORTS:
|
|
97
|
+
raise ValueError(f"sort must be one of {', '.join(RECALL_SORTS)}.")
|
|
98
|
+
body: Dict[str, Any] = {"query": query, "identity": identity}
|
|
99
|
+
optional = {
|
|
100
|
+
"limit": limit,
|
|
101
|
+
"level": level,
|
|
102
|
+
"maxTokens": max_tokens,
|
|
103
|
+
"queryTimestamp": query_timestamp,
|
|
104
|
+
"occurredFrom": occurred_from,
|
|
105
|
+
"occurredTo": occurred_to,
|
|
106
|
+
"sort": sort,
|
|
107
|
+
}
|
|
108
|
+
body.update({key: value for key, value in optional.items() if value is not None})
|
|
109
|
+
return body
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def build_ingest_body(
|
|
113
|
+
content: str,
|
|
114
|
+
identity: str,
|
|
115
|
+
timestamp: Optional[str] = None,
|
|
116
|
+
source_id: Optional[str] = None,
|
|
117
|
+
audience: Optional[str] = None,
|
|
118
|
+
label: Optional[str] = None,
|
|
119
|
+
metadata: Any = None,
|
|
120
|
+
idempotency_key: Optional[str] = None,
|
|
121
|
+
) -> Dict[str, Any]:
|
|
122
|
+
if not content or not content.strip():
|
|
123
|
+
raise ValueError("past dev refuses empty content: send some text to ingest.")
|
|
124
|
+
body: Dict[str, Any] = {"content": content, "identity": identity}
|
|
125
|
+
optional = {
|
|
126
|
+
"timestamp": timestamp,
|
|
127
|
+
"id": source_id,
|
|
128
|
+
"audience": audience,
|
|
129
|
+
"label": label,
|
|
130
|
+
"metadata": metadata,
|
|
131
|
+
"idempotencyKey": idempotency_key,
|
|
132
|
+
}
|
|
133
|
+
body.update({key: value for key, value in optional.items() if value is not None})
|
|
134
|
+
return body
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def format_recall(response: Dict[str, Any]) -> str:
|
|
138
|
+
"""Render a recall response as plain text for a model to read."""
|
|
139
|
+
results = response.get("results") or []
|
|
140
|
+
return "\n\n".join(format_recall_results(results))
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def format_recall_results(results: List[Dict[str, Any]]) -> List[str]:
|
|
144
|
+
rendered = []
|
|
145
|
+
for result in results:
|
|
146
|
+
content = (result.get("content") or "").strip()
|
|
147
|
+
if not content:
|
|
148
|
+
continue
|
|
149
|
+
artifact = result.get("artifact") or {}
|
|
150
|
+
kind = artifact.get("kind") or "memory"
|
|
151
|
+
head = (
|
|
152
|
+
f"[{result.get('rank', len(rendered) + 1)}] {_date(result.get('occurredAt'))} · {kind}"
|
|
153
|
+
)
|
|
154
|
+
superseded = artifact.get("supersededAt")
|
|
155
|
+
if superseded:
|
|
156
|
+
head += f" · superseded {_date(superseded)}"
|
|
157
|
+
lines = [head, content]
|
|
158
|
+
for source in result.get("sources") or []:
|
|
159
|
+
where = source.get("label") or source.get("sourceId") or "source"
|
|
160
|
+
when = _date(source.get("occurredAt"))
|
|
161
|
+
for excerpt in source.get("excerpts") or []:
|
|
162
|
+
lines.append(f' evidence ({where}, {when}): "{excerpt}"')
|
|
163
|
+
rendered.append("\n".join(lines))
|
|
164
|
+
return rendered
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _date(value: Optional[str]) -> str:
|
|
168
|
+
return value[:10] if isinstance(value, str) else "undated"
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class _Base:
|
|
172
|
+
def __init__(
|
|
173
|
+
self,
|
|
174
|
+
api_key: Optional[str] = None,
|
|
175
|
+
api_host: Optional[str] = None,
|
|
176
|
+
timeout: float = 30.0,
|
|
177
|
+
user_agent: str = "llama-index-pastdotdev",
|
|
178
|
+
) -> None:
|
|
179
|
+
self.api_host = resolve_api_host(api_host)
|
|
180
|
+
self._headers = {
|
|
181
|
+
"Authorization": f"Bearer {resolve_api_key(api_key)}",
|
|
182
|
+
"Accept": "application/json",
|
|
183
|
+
"User-Agent": user_agent,
|
|
184
|
+
}
|
|
185
|
+
self.timeout = timeout
|
|
186
|
+
|
|
187
|
+
def _url(self, path: str) -> str:
|
|
188
|
+
return f"{self.api_host}{path}"
|
|
189
|
+
|
|
190
|
+
@staticmethod
|
|
191
|
+
def _retry_delay(response: httpx.Response) -> Optional[float]:
|
|
192
|
+
if response.status_code != 429:
|
|
193
|
+
return None
|
|
194
|
+
header = response.headers.get("Retry-After", "1")
|
|
195
|
+
try:
|
|
196
|
+
delay = float(header)
|
|
197
|
+
except ValueError:
|
|
198
|
+
return None
|
|
199
|
+
return delay if 0 <= delay <= MAX_RETRY_AFTER_SECONDS else None
|
|
200
|
+
|
|
201
|
+
@staticmethod
|
|
202
|
+
def _result(response: httpx.Response) -> Any:
|
|
203
|
+
if response.is_success:
|
|
204
|
+
return response.json() if response.content else None
|
|
205
|
+
request_id = response.headers.get("X-Request-Id")
|
|
206
|
+
try:
|
|
207
|
+
body = response.json()
|
|
208
|
+
except ValueError:
|
|
209
|
+
body = None
|
|
210
|
+
if isinstance(body, dict) and body.get("code"):
|
|
211
|
+
raise PastDevError(
|
|
212
|
+
code=str(body["code"]),
|
|
213
|
+
status=int(body.get("status") or response.status_code),
|
|
214
|
+
message=body.get("debugMessage"),
|
|
215
|
+
request_id=request_id,
|
|
216
|
+
data=body.get("data"),
|
|
217
|
+
)
|
|
218
|
+
raise PastDevError(
|
|
219
|
+
code="http-error",
|
|
220
|
+
status=response.status_code,
|
|
221
|
+
message=response.text[:200] or None,
|
|
222
|
+
request_id=request_id,
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
class PastDevClient(_Base):
|
|
227
|
+
"""Synchronous client."""
|
|
228
|
+
|
|
229
|
+
def __init__(
|
|
230
|
+
self, *args: Any, http_client: Optional[httpx.Client] = None, **kwargs: Any
|
|
231
|
+
) -> None:
|
|
232
|
+
super().__init__(*args, **kwargs)
|
|
233
|
+
self._http = http_client or httpx.Client(timeout=self.timeout)
|
|
234
|
+
|
|
235
|
+
def request(self, method: str, path: str, json: Any = None, params: Any = None) -> Any:
|
|
236
|
+
for attempt in range(2):
|
|
237
|
+
response = self._http.request(
|
|
238
|
+
method, self._url(path), json=json, params=params, headers=self._headers
|
|
239
|
+
)
|
|
240
|
+
delay = self._retry_delay(response)
|
|
241
|
+
if delay is None or attempt == 1:
|
|
242
|
+
return self._result(response)
|
|
243
|
+
time.sleep(delay)
|
|
244
|
+
return None # pragma: no cover
|
|
245
|
+
|
|
246
|
+
def ingest(self, **kwargs: Any) -> Dict[str, Any]:
|
|
247
|
+
return self.request("POST", "/api/v1/ingest", json=build_ingest_body(**kwargs))
|
|
248
|
+
|
|
249
|
+
def recall(self, **kwargs: Any) -> Dict[str, Any]:
|
|
250
|
+
return self.request("POST", "/api/v1/recall", json=build_recall_body(**kwargs))
|
|
251
|
+
|
|
252
|
+
def delete_data_point(self, source_id: str) -> Dict[str, Any]:
|
|
253
|
+
return self.request("DELETE", f"/api/v1/data-points/{_path(source_id)}")
|
|
254
|
+
|
|
255
|
+
def list_audiences(self) -> Dict[str, Any]:
|
|
256
|
+
"""List the project's audiences. It costs no credits, so it also tests a key."""
|
|
257
|
+
return self.request("GET", "/api/v1/audiences")
|
|
258
|
+
|
|
259
|
+
def list_identities(self, after: Optional[str] = None) -> Dict[str, Any]:
|
|
260
|
+
return self.request("GET", "/api/v1/identities", params={"after": after} if after else None)
|
|
261
|
+
|
|
262
|
+
def close(self) -> None:
|
|
263
|
+
self._http.close()
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
class AsyncPastDevClient(_Base):
|
|
267
|
+
"""Asynchronous client."""
|
|
268
|
+
|
|
269
|
+
def __init__(
|
|
270
|
+
self, *args: Any, http_client: Optional[httpx.AsyncClient] = None, **kwargs: Any
|
|
271
|
+
) -> None:
|
|
272
|
+
super().__init__(*args, **kwargs)
|
|
273
|
+
self._http = http_client or httpx.AsyncClient(timeout=self.timeout)
|
|
274
|
+
|
|
275
|
+
async def request(self, method: str, path: str, json: Any = None, params: Any = None) -> Any:
|
|
276
|
+
for attempt in range(2):
|
|
277
|
+
response = await self._http.request(
|
|
278
|
+
method, self._url(path), json=json, params=params, headers=self._headers
|
|
279
|
+
)
|
|
280
|
+
delay = self._retry_delay(response)
|
|
281
|
+
if delay is None or attempt == 1:
|
|
282
|
+
return self._result(response)
|
|
283
|
+
await asyncio.sleep(delay)
|
|
284
|
+
return None # pragma: no cover
|
|
285
|
+
|
|
286
|
+
async def ingest(self, **kwargs: Any) -> Dict[str, Any]:
|
|
287
|
+
return await self.request("POST", "/api/v1/ingest", json=build_ingest_body(**kwargs))
|
|
288
|
+
|
|
289
|
+
async def recall(self, **kwargs: Any) -> Dict[str, Any]:
|
|
290
|
+
return await self.request("POST", "/api/v1/recall", json=build_recall_body(**kwargs))
|
|
291
|
+
|
|
292
|
+
async def delete_data_point(self, source_id: str) -> Dict[str, Any]:
|
|
293
|
+
return await self.request("DELETE", f"/api/v1/data-points/{_path(source_id)}")
|
|
294
|
+
|
|
295
|
+
async def list_audiences(self) -> Dict[str, Any]:
|
|
296
|
+
"""List the project's audiences. It costs no credits, so it also tests a key."""
|
|
297
|
+
return await self.request("GET", "/api/v1/audiences")
|
|
298
|
+
|
|
299
|
+
async def list_identities(self, after: Optional[str] = None) -> Dict[str, Any]:
|
|
300
|
+
return await self.request(
|
|
301
|
+
"GET", "/api/v1/identities", params={"after": after} if after else None
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
async def aclose(self) -> None:
|
|
305
|
+
await self._http.aclose()
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def _path(segment: str) -> str:
|
|
309
|
+
from urllib.parse import quote
|
|
310
|
+
|
|
311
|
+
return quote(segment, safe="")
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[dependency-groups]
|
|
6
|
+
dev = [
|
|
7
|
+
"pytest>=8",
|
|
8
|
+
"pytest-asyncio>=0.23",
|
|
9
|
+
"pytest-cov>=6.1.1",
|
|
10
|
+
"ruff>=0.11",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
[project]
|
|
14
|
+
name = "llama-index-tools-pastdotdev"
|
|
15
|
+
version = "0.1.0"
|
|
16
|
+
description = "past dev tools for LlamaIndex agents: recall and ingest with the past dev Memory API"
|
|
17
|
+
authors = [{name = "past.dev", email = "louis@past.dev"}]
|
|
18
|
+
maintainers = [{name = "past.dev", email = "louis@past.dev"}]
|
|
19
|
+
requires-python = ">=3.10,<4.0"
|
|
20
|
+
readme = "README.md"
|
|
21
|
+
license = "MIT"
|
|
22
|
+
license-files = ["LICENSE"]
|
|
23
|
+
keywords = ["llama-index", "llamaindex", "past dev", "past.dev", "tools", "agent tools", "memory"]
|
|
24
|
+
classifiers = [
|
|
25
|
+
"Development Status :: 4 - Beta",
|
|
26
|
+
"Intended Audience :: Developers",
|
|
27
|
+
"Programming Language :: Python :: 3",
|
|
28
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
29
|
+
]
|
|
30
|
+
dependencies = [
|
|
31
|
+
"llama-index-core>=0.13.0,<0.15",
|
|
32
|
+
"httpx>=0.27,<1",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[project.urls]
|
|
36
|
+
Homepage = "https://past.dev"
|
|
37
|
+
Documentation = "https://past.dev/docs/memory-api/quickstart"
|
|
38
|
+
"API reference" = "https://past.dev/docs/memory-api/api-reference"
|
|
39
|
+
Repository = "https://github.com/pastdotdev/llama-index-pastdotdev"
|
|
40
|
+
|
|
41
|
+
[tool.hatch.build.targets.sdist]
|
|
42
|
+
include = ["llama_index/", "tests/", "README.md", "LICENSE"]
|
|
43
|
+
|
|
44
|
+
[tool.hatch.build.targets.wheel]
|
|
45
|
+
include = ["llama_index/"]
|
|
46
|
+
|
|
47
|
+
[tool.llamahub]
|
|
48
|
+
contains_example = false
|
|
49
|
+
import_path = "llama_index.tools.pastdotdev"
|
|
50
|
+
|
|
51
|
+
[tool.llamahub.class_authors]
|
|
52
|
+
PastDevToolSpec = "pastdotdev"
|
|
53
|
+
|
|
54
|
+
[tool.pytest.ini_options]
|
|
55
|
+
asyncio_mode = "auto"
|
|
56
|
+
markers = ["live: calls the real past dev API; skipped unless PAST_API_KEY is set"]
|
|
57
|
+
|
|
58
|
+
[tool.ruff]
|
|
59
|
+
line-length = 100
|
|
60
|
+
target-version = "py310"
|
|
61
|
+
|
|
62
|
+
[tool.ruff.lint]
|
|
63
|
+
select = ["E", "F", "I", "B"]
|
|
64
|
+
# The formatter wraps code; long lines left are docstrings, which the model reads.
|
|
65
|
+
ignore = ["E501"]
|
|
File without changes
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from typing import List
|
|
3
|
+
|
|
4
|
+
import httpx
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from llama_index.tools.pastdotdev.client import (
|
|
8
|
+
AsyncPastDevClient,
|
|
9
|
+
PastDevClient,
|
|
10
|
+
build_recall_body,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def recorder(requests: List[httpx.Request]) -> httpx.MockTransport:
|
|
15
|
+
def handler(request: httpx.Request) -> httpx.Response:
|
|
16
|
+
requests.append(request)
|
|
17
|
+
if request.method == "DELETE":
|
|
18
|
+
return httpx.Response(202, json={"operationId": "op-1", "ids": ["chat/1"]})
|
|
19
|
+
if request.url.path == "/api/v1/identities":
|
|
20
|
+
return httpx.Response(200, json={"identities": [], "hasMore": False, "endCursor": None})
|
|
21
|
+
return httpx.Response(200, json={"audiences": []})
|
|
22
|
+
|
|
23
|
+
return httpx.MockTransport(handler)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_sync_client_paths() -> None:
|
|
27
|
+
requests: List[httpx.Request] = []
|
|
28
|
+
client = PastDevClient(
|
|
29
|
+
api_key="past_sk_test", http_client=httpx.Client(transport=recorder(requests))
|
|
30
|
+
)
|
|
31
|
+
assert client.delete_data_point("chat/1")["ids"] == ["chat/1"]
|
|
32
|
+
assert client.list_audiences() == {"audiences": []}
|
|
33
|
+
assert client.list_identities(after="cursor-1")["hasMore"] is False
|
|
34
|
+
client.close()
|
|
35
|
+
assert [(r.method, r.url.raw_path.decode()) for r in requests] == [
|
|
36
|
+
("DELETE", "/api/v1/data-points/chat%2F1"),
|
|
37
|
+
("GET", "/api/v1/audiences"),
|
|
38
|
+
("GET", "/api/v1/identities?after=cursor-1"),
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
async def test_async_client_paths() -> None:
|
|
43
|
+
requests: List[httpx.Request] = []
|
|
44
|
+
client = AsyncPastDevClient(
|
|
45
|
+
api_key="past_sk_test", http_client=httpx.AsyncClient(transport=recorder(requests))
|
|
46
|
+
)
|
|
47
|
+
await client.delete_data_point("chat/1")
|
|
48
|
+
await client.list_audiences()
|
|
49
|
+
await client.list_identities()
|
|
50
|
+
await client.aclose()
|
|
51
|
+
assert [(r.method, r.url.raw_path.decode()) for r in requests] == [
|
|
52
|
+
("DELETE", "/api/v1/data-points/chat%2F1"),
|
|
53
|
+
("GET", "/api/v1/audiences"),
|
|
54
|
+
("GET", "/api/v1/identities"),
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_recall_body_uses_api_keys() -> None:
|
|
59
|
+
body = build_recall_body(
|
|
60
|
+
query="q",
|
|
61
|
+
identity="u",
|
|
62
|
+
max_tokens=500,
|
|
63
|
+
query_timestamp="2026-10-01T00:00:00Z",
|
|
64
|
+
occurred_to="2026-09-01T00:00:00Z",
|
|
65
|
+
sort="chronological",
|
|
66
|
+
)
|
|
67
|
+
assert json.loads(json.dumps(body)) == {
|
|
68
|
+
"query": "q",
|
|
69
|
+
"identity": "u",
|
|
70
|
+
"maxTokens": 500,
|
|
71
|
+
"queryTimestamp": "2026-10-01T00:00:00Z",
|
|
72
|
+
"occurredTo": "2026-09-01T00:00:00Z",
|
|
73
|
+
"sort": "chronological",
|
|
74
|
+
}
|
|
75
|
+
with pytest.raises(ValueError):
|
|
76
|
+
build_recall_body(query="q", identity="u", level="huge")
|
|
77
|
+
with pytest.raises(ValueError):
|
|
78
|
+
build_recall_body(query="q", identity="u", sort="random")
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""One smoke test against the real past dev API.
|
|
2
|
+
|
|
3
|
+
It is skipped unless PAST_API_KEY is set. It spends a few credits: one recall.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import os
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
from llama_index.tools.pastdotdev import PastDevToolSpec
|
|
11
|
+
|
|
12
|
+
pytestmark = [
|
|
13
|
+
pytest.mark.live,
|
|
14
|
+
pytest.mark.skipif(not os.environ.get("PAST_API_KEY"), reason="PAST_API_KEY is not set"),
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_live_recall_tool() -> None:
|
|
19
|
+
spec = PastDevToolSpec(identity="llama-index-smoke-test")
|
|
20
|
+
# The connection test: listing audiences costs no credits.
|
|
21
|
+
assert isinstance(spec._client.list_audiences()["audiences"], list)
|
|
22
|
+
|
|
23
|
+
tools = {tool.metadata.name: tool for tool in spec.to_tool_list()}
|
|
24
|
+
output = tools["recall"](query="What is my favourite colour?")
|
|
25
|
+
assert isinstance(output.content, str) and output.content
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from typing import Any, Callable, Dict, List
|
|
3
|
+
|
|
4
|
+
import httpx
|
|
5
|
+
import pytest
|
|
6
|
+
from llama_index.core.tools.tool_spec.base import BaseToolSpec
|
|
7
|
+
|
|
8
|
+
from llama_index.tools.pastdotdev import PastDevError, PastDevToolSpec
|
|
9
|
+
|
|
10
|
+
KEY = "past_sk_test"
|
|
11
|
+
|
|
12
|
+
RECALL_RESPONSE = {
|
|
13
|
+
"asOf": "2026-10-16T12:00:00Z",
|
|
14
|
+
"usedEvidenceTokens": 42,
|
|
15
|
+
"results": [
|
|
16
|
+
{
|
|
17
|
+
"id": "7f1c3a4e-0000-4000-8000-000000000001",
|
|
18
|
+
"rank": 1,
|
|
19
|
+
"occurredAt": "2026-10-01T09:00:00Z",
|
|
20
|
+
"content": "Ada prefers email to phone calls.",
|
|
21
|
+
"artifact": {
|
|
22
|
+
"id": "7f1c3a4e-0000-4000-8000-000000000001",
|
|
23
|
+
"kind": "observation",
|
|
24
|
+
"occurredAt": "2026-10-01T09:00:00Z",
|
|
25
|
+
},
|
|
26
|
+
"sources": [
|
|
27
|
+
{
|
|
28
|
+
"sourceId": "chat-1",
|
|
29
|
+
"occurredAt": "2026-10-01T09:00:00Z",
|
|
30
|
+
"excerpts": ["please email me, never call"],
|
|
31
|
+
}
|
|
32
|
+
],
|
|
33
|
+
}
|
|
34
|
+
],
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
INGEST_RESPONSE = {
|
|
38
|
+
"projectId": 1,
|
|
39
|
+
"ingestionId": "7f1c3a4e-0000-4000-8000-0000000000aa",
|
|
40
|
+
"sourceId": "abc",
|
|
41
|
+
"status": "queued",
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class FakeApi:
|
|
46
|
+
def __init__(self, handler: Callable[[httpx.Request], httpx.Response]) -> None:
|
|
47
|
+
self.handler = handler
|
|
48
|
+
self.requests: List[httpx.Request] = []
|
|
49
|
+
|
|
50
|
+
def __call__(self, request: httpx.Request) -> httpx.Response:
|
|
51
|
+
self.requests.append(request)
|
|
52
|
+
return self.handler(request)
|
|
53
|
+
|
|
54
|
+
def bodies(self, path: str) -> List[Dict[str, Any]]:
|
|
55
|
+
return [json.loads(r.content) for r in self.requests if r.url.path == path]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def default_handler(request: httpx.Request) -> httpx.Response:
|
|
59
|
+
if request.url.path == "/api/v1/recall":
|
|
60
|
+
return httpx.Response(200, json=RECALL_RESPONSE)
|
|
61
|
+
if request.url.path == "/api/v1/ingest":
|
|
62
|
+
return httpx.Response(202, json=INGEST_RESPONSE)
|
|
63
|
+
return httpx.Response(404, json={"code": "not-found", "status": 404})
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def make_spec(api: FakeApi, **kwargs: Any) -> PastDevToolSpec:
|
|
67
|
+
kwargs.setdefault("api_key", KEY)
|
|
68
|
+
kwargs.setdefault("identity", "user-ada")
|
|
69
|
+
return PastDevToolSpec(http_client=httpx.Client(transport=httpx.MockTransport(api)), **kwargs)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_is_a_tool_spec() -> None:
|
|
73
|
+
names = [b.__name__ for b in PastDevToolSpec.__mro__]
|
|
74
|
+
assert BaseToolSpec.__name__ in names
|
|
75
|
+
assert PastDevToolSpec.spec_functions == ["recall", "ingest"]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_requires_identity() -> None:
|
|
79
|
+
with pytest.raises(ValueError):
|
|
80
|
+
make_spec(FakeApi(default_handler), identity="")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_requires_api_key(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
84
|
+
monkeypatch.delenv("PAST_API_KEY", raising=False)
|
|
85
|
+
with pytest.raises(ValueError, match="PAST_API_KEY"):
|
|
86
|
+
PastDevToolSpec(identity="user-ada")
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def test_refuses_level_and_max_tokens_together() -> None:
|
|
90
|
+
with pytest.raises(ValueError):
|
|
91
|
+
make_spec(FakeApi(default_handler), level="low", max_tokens=100)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def test_model_cannot_choose_identity_or_audience() -> None:
|
|
95
|
+
tools = make_spec(FakeApi(default_handler)).to_tool_list()
|
|
96
|
+
assert [tool.metadata.name for tool in tools] == ["recall", "ingest"]
|
|
97
|
+
for tool in tools:
|
|
98
|
+
properties = tool.metadata.get_parameters_dict()["properties"]
|
|
99
|
+
assert "identity" not in properties
|
|
100
|
+
assert "audience" not in properties
|
|
101
|
+
recall, ingest = tools
|
|
102
|
+
assert set(recall.metadata.get_parameters_dict()["properties"]) == {
|
|
103
|
+
"query",
|
|
104
|
+
"occurredFrom",
|
|
105
|
+
"occurredTo",
|
|
106
|
+
"sort",
|
|
107
|
+
}
|
|
108
|
+
assert recall.metadata.get_parameters_dict()["required"] == ["query"]
|
|
109
|
+
assert set(ingest.metadata.get_parameters_dict()["properties"]) == {"content", "timestamp"}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def test_recall_tool_sends_fixed_identity_and_renders_results() -> None:
|
|
113
|
+
api = FakeApi(default_handler)
|
|
114
|
+
spec = make_spec(api, identity="user-ada", level="high", limit=10)
|
|
115
|
+
recall = spec.to_tool_list()[0]
|
|
116
|
+
output = recall(query="How should I contact Ada?", occurredFrom="2026-01-01T00:00:00Z")
|
|
117
|
+
assert api.bodies("/api/v1/recall") == [
|
|
118
|
+
{
|
|
119
|
+
"query": "How should I contact Ada?",
|
|
120
|
+
"identity": "user-ada",
|
|
121
|
+
"limit": 10,
|
|
122
|
+
"level": "high",
|
|
123
|
+
"occurredFrom": "2026-01-01T00:00:00Z",
|
|
124
|
+
}
|
|
125
|
+
]
|
|
126
|
+
assert api.requests[0].headers["Authorization"] == f"Bearer {KEY}"
|
|
127
|
+
assert "Ada prefers email to phone calls." in output.content
|
|
128
|
+
assert '"please email me, never call"' in output.content
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def test_recall_with_no_results_says_so() -> None:
|
|
132
|
+
api = FakeApi(
|
|
133
|
+
lambda request: httpx.Response(
|
|
134
|
+
200, json={"asOf": "2026-10-16T12:00:00Z", "usedEvidenceTokens": 0, "results": []}
|
|
135
|
+
)
|
|
136
|
+
)
|
|
137
|
+
assert "no memory" in make_spec(api).recall(query="Anything?")
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_ingest_tool_sends_content_with_fixed_identity_and_audience() -> None:
|
|
141
|
+
api = FakeApi(default_handler)
|
|
142
|
+
spec = make_spec(api, audience="support-team", api_host="https://memory.example.com/")
|
|
143
|
+
result = spec.ingest(content="Ada prefers email.", timestamp="2026-10-01T09:00:00Z")
|
|
144
|
+
assert api.requests[0].url.host == "memory.example.com"
|
|
145
|
+
assert api.bodies("/api/v1/ingest") == [
|
|
146
|
+
{
|
|
147
|
+
"content": "Ada prefers email.",
|
|
148
|
+
"identity": "user-ada",
|
|
149
|
+
"timestamp": "2026-10-01T09:00:00Z",
|
|
150
|
+
"audience": "support-team",
|
|
151
|
+
"label": "Saved by a LlamaIndex agent",
|
|
152
|
+
"metadata": {"source": "llama-index"},
|
|
153
|
+
}
|
|
154
|
+
]
|
|
155
|
+
assert "source id abc" in result
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def test_ingest_unchanged_says_so() -> None:
|
|
159
|
+
api = FakeApi(lambda request: httpx.Response(200, json={**INGEST_RESPONSE, "unchanged": True}))
|
|
160
|
+
assert "already had" in make_spec(api).ingest(content="Ada prefers email.")
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def test_ingest_refuses_empty_content() -> None:
|
|
164
|
+
api = FakeApi(default_handler)
|
|
165
|
+
with pytest.raises(ValueError):
|
|
166
|
+
make_spec(api).ingest(content=" ")
|
|
167
|
+
assert api.requests == []
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def test_bad_request_is_raised_and_not_retried() -> None:
|
|
171
|
+
api = FakeApi(
|
|
172
|
+
lambda request: httpx.Response(
|
|
173
|
+
400,
|
|
174
|
+
json={
|
|
175
|
+
"code": "recall-range-invalid",
|
|
176
|
+
"status": 400,
|
|
177
|
+
"debugMessage": "Start after end.",
|
|
178
|
+
},
|
|
179
|
+
headers={"X-Request-Id": "req-9"},
|
|
180
|
+
)
|
|
181
|
+
)
|
|
182
|
+
with pytest.raises(PastDevError) as caught:
|
|
183
|
+
make_spec(api).recall(query="x", occurredFrom="2026-02-01", occurredTo="2026-01-01")
|
|
184
|
+
assert caught.value.code == "recall-range-invalid"
|
|
185
|
+
assert "req-9" in str(caught.value)
|
|
186
|
+
assert len(api.requests) == 1
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def test_rate_limit_is_retried_once() -> None:
|
|
190
|
+
calls = {"n": 0}
|
|
191
|
+
|
|
192
|
+
def handler(request: httpx.Request) -> httpx.Response:
|
|
193
|
+
calls["n"] += 1
|
|
194
|
+
if calls["n"] == 1:
|
|
195
|
+
return httpx.Response(
|
|
196
|
+
429, json={"code": "rate-limited", "status": 429}, headers={"Retry-After": "0"}
|
|
197
|
+
)
|
|
198
|
+
return httpx.Response(202, json=INGEST_RESPONSE)
|
|
199
|
+
|
|
200
|
+
api = FakeApi(handler)
|
|
201
|
+
assert "accepted" in make_spec(api).ingest(content="Ada prefers email.")
|
|
202
|
+
assert len(api.requests) == 2
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
async def test_tools_work_async() -> None:
|
|
206
|
+
api = FakeApi(default_handler)
|
|
207
|
+
recall = make_spec(api).to_tool_list()[0]
|
|
208
|
+
output = await recall.acall(query="How should I contact Ada?")
|
|
209
|
+
assert "Ada prefers email" in output.content
|