llama-index-memory-pastdotdev 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.
- llama_index/memory/pastdotdev/__init__.py +4 -0
- llama_index/memory/pastdotdev/base.py +180 -0
- llama_index/memory/pastdotdev/client.py +311 -0
- llama_index_memory_pastdotdev-0.1.0.dist-info/METADATA +130 -0
- llama_index_memory_pastdotdev-0.1.0.dist-info/RECORD +7 -0
- llama_index_memory_pastdotdev-0.1.0.dist-info/WHEEL +4 -0
- llama_index_memory_pastdotdev-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"""past dev memory block for the LlamaIndex Memory class."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from datetime import datetime
|
|
5
|
+
from typing import Any, Dict, List, Literal, Optional
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
from llama_index.core.base.llms.types import ChatMessage, MessageRole
|
|
9
|
+
from llama_index.core.bridge.pydantic import Field, PrivateAttr, model_validator
|
|
10
|
+
from llama_index.core.memory.memory import BaseMemoryBlock
|
|
11
|
+
|
|
12
|
+
from llama_index.memory.pastdotdev.client import (
|
|
13
|
+
DEFAULT_API_HOST,
|
|
14
|
+
AsyncPastDevClient,
|
|
15
|
+
PastDevError,
|
|
16
|
+
format_recall_results,
|
|
17
|
+
now_iso,
|
|
18
|
+
resolve_api_key,
|
|
19
|
+
to_iso,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
logger = logging.getLogger(__name__)
|
|
23
|
+
|
|
24
|
+
__version__ = "0.1.0"
|
|
25
|
+
|
|
26
|
+
DEFAULT_NAME = "past_dev_memory"
|
|
27
|
+
DEFAULT_DESCRIPTION = (
|
|
28
|
+
"Long-term memory recalled from past dev for the current user. "
|
|
29
|
+
"Each entry has a rank, a date and a kind; evidence lines quote the source."
|
|
30
|
+
)
|
|
31
|
+
DEFAULT_LABEL = "LlamaIndex chat"
|
|
32
|
+
INGESTED_ROLES = (MessageRole.USER, MessageRole.ASSISTANT)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class PastDevMemoryBlock(BaseMemoryBlock[str]):
|
|
36
|
+
"""
|
|
37
|
+
A memory block that keeps long-term memory in past dev.
|
|
38
|
+
|
|
39
|
+
On get, it recalls with the latest user message as the query. On put (the
|
|
40
|
+
messages that the Memory flushes from its short-term queue), it ingests the
|
|
41
|
+
user and assistant text as one data point. The identity is fixed when you
|
|
42
|
+
make the block: the model never chooses it.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
identity: Your id for the end user. Recall reads, and ingest writes, as this identity.
|
|
46
|
+
api_key: A past dev project API key (past_sk_...). Defaults to PAST_API_KEY.
|
|
47
|
+
api_host: The API base URL. Defaults to PAST_API_HOST, else https://api.past.dev.
|
|
48
|
+
audience: The audience slug for ingested memory. Omit it for project-visible memory.
|
|
49
|
+
level: The recall evidence budget: low, medium, high or extra-high.
|
|
50
|
+
max_tokens: An explicit recall evidence budget in tokens. Send level or max_tokens, not both.
|
|
51
|
+
limit: The maximum number of recalled documents.
|
|
52
|
+
label: The label of each ingested data point.
|
|
53
|
+
raise_on_error: When False, an API error is logged and the block returns nothing.
|
|
54
|
+
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
name: str = Field(default=DEFAULT_NAME, description="The name of the memory block.")
|
|
58
|
+
description: Optional[str] = Field(default=DEFAULT_DESCRIPTION)
|
|
59
|
+
identity: str = Field(
|
|
60
|
+
description="The end user's id. Fixed by the builder, never by the model."
|
|
61
|
+
)
|
|
62
|
+
api_key: Optional[str] = Field(default=None, exclude=True, repr=False)
|
|
63
|
+
api_host: Optional[str] = Field(
|
|
64
|
+
default=None, description=f"Defaults to PAST_API_HOST, else {DEFAULT_API_HOST}."
|
|
65
|
+
)
|
|
66
|
+
audience: Optional[str] = Field(default=None)
|
|
67
|
+
level: Optional[Literal["low", "medium", "high", "extra-high"]] = Field(default=None)
|
|
68
|
+
max_tokens: Optional[int] = Field(default=None, ge=1)
|
|
69
|
+
limit: Optional[int] = Field(default=None, ge=1, le=200)
|
|
70
|
+
label: str = Field(default=DEFAULT_LABEL)
|
|
71
|
+
timeout: float = Field(default=30.0, gt=0)
|
|
72
|
+
raise_on_error: bool = Field(default=True)
|
|
73
|
+
|
|
74
|
+
_client: AsyncPastDevClient = PrivateAttr()
|
|
75
|
+
|
|
76
|
+
def __init__(self, http_client: Optional[httpx.AsyncClient] = None, **data: Any) -> None:
|
|
77
|
+
super().__init__(**data)
|
|
78
|
+
self._client = AsyncPastDevClient(
|
|
79
|
+
api_key=resolve_api_key(self.api_key),
|
|
80
|
+
api_host=self.api_host,
|
|
81
|
+
timeout=self.timeout,
|
|
82
|
+
user_agent=f"llama-index-memory-pastdotdev/{__version__}",
|
|
83
|
+
http_client=http_client,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
@model_validator(mode="after")
|
|
87
|
+
def _check(self) -> "PastDevMemoryBlock":
|
|
88
|
+
if not self.identity or not self.identity.strip():
|
|
89
|
+
raise ValueError("identity is required: set it from your user record.")
|
|
90
|
+
if self.level is not None and self.max_tokens is not None:
|
|
91
|
+
raise ValueError("Set level or max_tokens, never both.")
|
|
92
|
+
return self
|
|
93
|
+
|
|
94
|
+
@classmethod
|
|
95
|
+
def class_name(cls) -> str:
|
|
96
|
+
return "PastDevMemoryBlock"
|
|
97
|
+
|
|
98
|
+
async def _aget(self, messages: Optional[List[ChatMessage]] = None, **block_kwargs: Any) -> str:
|
|
99
|
+
query = _latest_user_text(messages or [])
|
|
100
|
+
if not query:
|
|
101
|
+
return ""
|
|
102
|
+
try:
|
|
103
|
+
response = await self._client.recall(
|
|
104
|
+
query=query,
|
|
105
|
+
identity=self.identity,
|
|
106
|
+
limit=self.limit,
|
|
107
|
+
level=self.level,
|
|
108
|
+
max_tokens=self.max_tokens,
|
|
109
|
+
)
|
|
110
|
+
except (PastDevError, httpx.HTTPError) as error:
|
|
111
|
+
if self.raise_on_error:
|
|
112
|
+
raise
|
|
113
|
+
logger.warning("past dev recall failed: %s", error)
|
|
114
|
+
return ""
|
|
115
|
+
return "\n\n".join(format_recall_results(response.get("results") or []))
|
|
116
|
+
|
|
117
|
+
async def _aput(self, messages: List[ChatMessage]) -> None:
|
|
118
|
+
content = _transcript(messages)
|
|
119
|
+
if not content:
|
|
120
|
+
return
|
|
121
|
+
try:
|
|
122
|
+
await self._client.ingest(
|
|
123
|
+
content=content,
|
|
124
|
+
identity=self.identity,
|
|
125
|
+
timestamp=_first_timestamp(messages) or now_iso(),
|
|
126
|
+
audience=self.audience,
|
|
127
|
+
label=self.label,
|
|
128
|
+
metadata=_metadata(messages),
|
|
129
|
+
)
|
|
130
|
+
except (PastDevError, httpx.HTTPError) as error:
|
|
131
|
+
if self.raise_on_error:
|
|
132
|
+
raise
|
|
133
|
+
logger.warning("past dev ingest failed: %s", error)
|
|
134
|
+
|
|
135
|
+
async def atruncate(self, content: str, tokens_to_truncate: int) -> Optional[str]:
|
|
136
|
+
"""Drop the lowest-ranked entries until about tokens_to_truncate tokens are gone."""
|
|
137
|
+
entries = content.split("\n\n") if content else []
|
|
138
|
+
to_remove = tokens_to_truncate * 4 # about four characters a token
|
|
139
|
+
while entries and to_remove > 0:
|
|
140
|
+
to_remove -= len(entries.pop()) + 2
|
|
141
|
+
return "\n\n".join(entries) or None
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _text(message: ChatMessage) -> str:
|
|
145
|
+
return (message.content or "").strip()
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _latest_user_text(messages: List[ChatMessage]) -> str:
|
|
149
|
+
for message in reversed(messages):
|
|
150
|
+
if message.role == MessageRole.USER and _text(message):
|
|
151
|
+
return _text(message)
|
|
152
|
+
return ""
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _transcript(messages: List[ChatMessage]) -> str:
|
|
156
|
+
lines = [
|
|
157
|
+
f"{message.role.value}: {_text(message)}"
|
|
158
|
+
for message in messages
|
|
159
|
+
if message.role in INGESTED_ROLES and _text(message)
|
|
160
|
+
]
|
|
161
|
+
return "\n\n".join(lines)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _first_timestamp(messages: List[ChatMessage]) -> Optional[str]:
|
|
165
|
+
"""Use a timestamp that the application put on a message, if there is one."""
|
|
166
|
+
for message in messages:
|
|
167
|
+
value = message.additional_kwargs.get("timestamp")
|
|
168
|
+
if isinstance(value, (datetime, str)):
|
|
169
|
+
return to_iso(value)
|
|
170
|
+
return None
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def _metadata(messages: List[ChatMessage]) -> Dict[str, Any]:
|
|
174
|
+
metadata: Dict[str, Any] = {"source": "llama-index"}
|
|
175
|
+
for message in messages:
|
|
176
|
+
session_id = message.additional_kwargs.get("session_id")
|
|
177
|
+
if isinstance(session_id, str):
|
|
178
|
+
metadata["sessionId"] = session_id
|
|
179
|
+
break
|
|
180
|
+
return metadata
|
|
@@ -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,130 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: llama-index-memory-pastdotdev
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: past dev memory block for LlamaIndex agents: recall and ingest long-term memory 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 memory,llama-index,llamaindex,long-term memory,memory,past dev,past.dev
|
|
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 Memory Integration: past dev
|
|
24
|
+
|
|
25
|
+
past dev is a memory API for AI agents. This package gives a LlamaIndex agent
|
|
26
|
+
long-term memory in [past dev](https://past.dev), as a memory block for the
|
|
27
|
+
LlamaIndex `Memory` class.
|
|
28
|
+
|
|
29
|
+
- **Get:** the block does a past dev **Recall**. The query is the latest user
|
|
30
|
+
message. The recalled memory goes into the system message, with the date,
|
|
31
|
+
the kind and the quoted evidence of each entry.
|
|
32
|
+
- **Put:** when the `Memory` flushes old messages from its short-term queue,
|
|
33
|
+
the block does a past dev **Ingest** of the user and assistant text, as one
|
|
34
|
+
data point.
|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install llama-index-memory-pastdotdev
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Get a key
|
|
43
|
+
|
|
44
|
+
1. Sign up at [past.dev](https://past.dev) and open a project.
|
|
45
|
+
2. Make a project API key. It starts with `past_sk_`.
|
|
46
|
+
3. Set it in the environment as `PAST_API_KEY`, or give it to the block as `api_key`.
|
|
47
|
+
|
|
48
|
+
## Use
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from llama_index.core.agent.workflow import FunctionAgent
|
|
52
|
+
from llama_index.core.memory import Memory
|
|
53
|
+
from llama_index.llms.openai import OpenAI
|
|
54
|
+
from llama_index.memory.pastdotdev import PastDevMemoryBlock
|
|
55
|
+
|
|
56
|
+
memory = Memory.from_defaults(
|
|
57
|
+
session_id="session-42",
|
|
58
|
+
token_limit=30000,
|
|
59
|
+
memory_blocks=[
|
|
60
|
+
PastDevMemoryBlock(
|
|
61
|
+
identity=current_user.id, # your id for the end user, from your own user record
|
|
62
|
+
)
|
|
63
|
+
],
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
agent = FunctionAgent(tools=[], llm=OpenAI(model="gpt-4.1-mini"))
|
|
67
|
+
response = await agent.run("How should you contact me?", memory=memory)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### The identity
|
|
71
|
+
|
|
72
|
+
past dev keeps memory per identity: your id for the end user. Recall needs an
|
|
73
|
+
identity, and the first ingest for an identity creates it.
|
|
74
|
+
|
|
75
|
+
Set `identity` in your code, from your trigger or your user record. Do not let
|
|
76
|
+
the model choose it. The block has no tool and no prompt that can change the
|
|
77
|
+
identity, so each user reads and writes only their own memory.
|
|
78
|
+
|
|
79
|
+
## Parameters
|
|
80
|
+
|
|
81
|
+
| Parameter | Default | Description |
|
|
82
|
+
| --- | --- | --- |
|
|
83
|
+
| `identity` | (required) | Your id for the end user. |
|
|
84
|
+
| `api_key` | `PAST_API_KEY` | A past dev project API key (`past_sk_...`). |
|
|
85
|
+
| `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. |
|
|
86
|
+
| `audience` | none | The audience slug for ingested memory. Without it, the memory is visible to the project. |
|
|
87
|
+
| `level` | `low` (on the server) | The recall evidence budget: `low`, `medium`, `high` or `extra-high`. |
|
|
88
|
+
| `max_tokens` | none | An explicit recall evidence budget in tokens. Use `level` or `max_tokens`, not both. |
|
|
89
|
+
| `limit` | 100 (on the server) | The maximum number of recalled documents. |
|
|
90
|
+
| `label` | `LlamaIndex chat` | The label of each ingested data point. |
|
|
91
|
+
| `name` | `past_dev_memory` | The name of the block in the memory template. |
|
|
92
|
+
| `priority` | `0` | The `BaseMemoryBlock` truncation priority. `0` means never truncate. |
|
|
93
|
+
| `raise_on_error` | `True` | When `False`, the block logs an API error and continues without memory. |
|
|
94
|
+
|
|
95
|
+
## What the block sends
|
|
96
|
+
|
|
97
|
+
- **Recall** (`POST /api/v1/recall`): `query` (the latest user message),
|
|
98
|
+
`identity`, and `level`, `maxTokens` or `limit` if you set them.
|
|
99
|
+
- **Ingest** (`POST /api/v1/ingest`): `content` (the flushed user and assistant
|
|
100
|
+
messages as `role: text` lines; system and tool messages are not sent),
|
|
101
|
+
`identity`, `timestamp`, `label`, `audience` if you set it, and `metadata`
|
|
102
|
+
with `{"source": "llama-index", "sessionId": ...}`.
|
|
103
|
+
|
|
104
|
+
The `timestamp` is the `timestamp` of the first flushed message, if your
|
|
105
|
+
application puts one in `message.additional_kwargs["timestamp"]`. Otherwise it
|
|
106
|
+
is the time of the flush.
|
|
107
|
+
|
|
108
|
+
## Credits
|
|
109
|
+
|
|
110
|
+
Each recall and each ingest spends credits on your past dev plan. The block
|
|
111
|
+
recalls once each time the agent reads its memory, and ingests once each time
|
|
112
|
+
the `Memory` flushes messages.
|
|
113
|
+
|
|
114
|
+
## Errors
|
|
115
|
+
|
|
116
|
+
An API error raises `PastDevError`, with the error `code`, the HTTP `status`,
|
|
117
|
+
the message and the `X-Request-Id` of the request. The block retries a `429`
|
|
118
|
+
once, after its `Retry-After` delay. It does not retry other errors, for
|
|
119
|
+
example `402 out-of-credits` or a `400`.
|
|
120
|
+
|
|
121
|
+
## Links
|
|
122
|
+
|
|
123
|
+
- [Quickstart](https://past.dev/docs/memory-api/quickstart)
|
|
124
|
+
- [API reference](https://past.dev/docs/memory-api/api-reference)
|
|
125
|
+
- [LlamaIndex memory guide](https://developers.llamaindex.ai/python/framework/module_guides/deploying/agents/memory/)
|
|
126
|
+
- Tools for the same API: [`llama-index-tools-pastdotdev`](https://pypi.org/project/llama-index-tools-pastdotdev/)
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
llama_index/memory/pastdotdev/__init__.py,sha256=km-nJIivsNrewtXO1tpXIive72oL9nM0M1lNzBnDveE,178
|
|
2
|
+
llama_index/memory/pastdotdev/base.py,sha256=1PI3ihG7SUy79Ky4hz3GhJqyEPZxN2CBUJTjmG760YQ,6970
|
|
3
|
+
llama_index/memory/pastdotdev/client.py,sha256=vBNsRYYG5kZQ53BnQbgpR0-COP33ojKXcFEbE5IJ43U,10813
|
|
4
|
+
llama_index_memory_pastdotdev-0.1.0.dist-info/METADATA,sha256=vjHdIvNGrqwOmuw3afGjq2Wkn3aM2YHicpZjv-teqlo,5521
|
|
5
|
+
llama_index_memory_pastdotdev-0.1.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
6
|
+
llama_index_memory_pastdotdev-0.1.0.dist-info/licenses/LICENSE,sha256=IZOT7vW7GYUkxb0mb6kEAwmuFCEDN_0iZZZRvV-W0GI,1065
|
|
7
|
+
llama_index_memory_pastdotdev-0.1.0.dist-info/RECORD,,
|
|
@@ -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.
|