mem0-haystack 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.
- haystack_integrations/components/retrievers/mem0/__init__.py +7 -0
- haystack_integrations/components/retrievers/mem0/retriever.py +109 -0
- haystack_integrations/components/retrievers/py.typed +0 -0
- haystack_integrations/components/writers/mem0/__init__.py +7 -0
- haystack_integrations/components/writers/mem0/writer.py +91 -0
- haystack_integrations/components/writers/py.typed +0 -0
- haystack_integrations/memory_stores/mem0/__init__.py +11 -0
- haystack_integrations/memory_stores/mem0/errors.py +7 -0
- haystack_integrations/memory_stores/mem0/filters.py +215 -0
- haystack_integrations/memory_stores/mem0/memory_store.py +218 -0
- haystack_integrations/memory_stores/py.typed +0 -0
- haystack_integrations/tools/mem0/__init__.py +8 -0
- haystack_integrations/tools/mem0/retriever_tool.py +173 -0
- haystack_integrations/tools/mem0/writer_tool.py +164 -0
- haystack_integrations/tools/py.typed +0 -0
- mem0_haystack-0.1.0.dist-info/METADATA +40 -0
- mem0_haystack-0.1.0.dist-info/RECORD +19 -0
- mem0_haystack-0.1.0.dist-info/WHEEL +4 -0
- mem0_haystack-0.1.0.dist-info/licenses/LICENSE.txt +201 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from haystack import component, default_from_dict, default_to_dict
|
|
8
|
+
from haystack.dataclasses import ChatMessage
|
|
9
|
+
|
|
10
|
+
from haystack_integrations.memory_stores.mem0.memory_store import Mem0MemoryStore
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@component
|
|
14
|
+
class Mem0MemoryRetriever:
|
|
15
|
+
"""
|
|
16
|
+
Retrieves memories from a Mem0MemoryStore as a list of ChatMessage objects.
|
|
17
|
+
|
|
18
|
+
Use this component in a Haystack Pipeline to fetch relevant memories before passing
|
|
19
|
+
context to a language model or Agent. The returned memories are system messages.
|
|
20
|
+
|
|
21
|
+
Provide either `filters` or at least one Mem0 entity ID (`user_id`, `run_id`, `agent_id`, or `app_id`)
|
|
22
|
+
when running the component. If both are provided, the filters and entity IDs are combined.
|
|
23
|
+
|
|
24
|
+
### Usage example
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from haystack_integrations.components.retrievers.mem0 import Mem0MemoryRetriever
|
|
28
|
+
from haystack_integrations.memory_stores.mem0 import Mem0MemoryStore
|
|
29
|
+
|
|
30
|
+
store = Mem0MemoryStore()
|
|
31
|
+
retriever = Mem0MemoryRetriever(memory_store=store, top_k=3)
|
|
32
|
+
|
|
33
|
+
result = retriever.run(query="What does Alice like?", user_id="alice")
|
|
34
|
+
memories = result["memories"]
|
|
35
|
+
print([message.text for message in memories])
|
|
36
|
+
|
|
37
|
+
# Pass query=None to retrieve all memories in scope.
|
|
38
|
+
all_memories = retriever.run(query=None, user_id="alice")["memories"]
|
|
39
|
+
```
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def __init__(self, *, memory_store: Mem0MemoryStore, top_k: int = 5) -> None:
|
|
43
|
+
"""
|
|
44
|
+
Initialize the Mem0MemoryRetriever.
|
|
45
|
+
|
|
46
|
+
:param memory_store: The Mem0MemoryStore instance to retrieve memories from.
|
|
47
|
+
:param top_k: Default maximum number of memories to return per query.
|
|
48
|
+
"""
|
|
49
|
+
self.memory_store = memory_store
|
|
50
|
+
self.top_k = top_k
|
|
51
|
+
|
|
52
|
+
@component.output_types(memories=list[ChatMessage])
|
|
53
|
+
def run(
|
|
54
|
+
self,
|
|
55
|
+
query: str | None,
|
|
56
|
+
*,
|
|
57
|
+
user_id: str | None = None,
|
|
58
|
+
run_id: str | None = None,
|
|
59
|
+
agent_id: str | None = None,
|
|
60
|
+
app_id: str | None = None,
|
|
61
|
+
filters: dict[str, Any] | None = None,
|
|
62
|
+
top_k: int | None = None,
|
|
63
|
+
) -> dict[str, list[ChatMessage]]:
|
|
64
|
+
"""
|
|
65
|
+
Retrieve memories matching the query from Mem0.
|
|
66
|
+
|
|
67
|
+
:param query: Text query used to search for relevant memories. Pass `None` to retrieve all memories matching
|
|
68
|
+
the scope.
|
|
69
|
+
:param user_id: User ID to scope the search.
|
|
70
|
+
:param run_id: Run ID to scope the search.
|
|
71
|
+
:param agent_id: Agent ID to scope the search.
|
|
72
|
+
:param app_id: App ID to scope the search.
|
|
73
|
+
:param filters: Haystack-style filters to apply. When provided with ID parameters, they are combined.
|
|
74
|
+
Mem0 requires entity IDs inside filters and supports a fixed set of native fields and operators:
|
|
75
|
+
[Search Memories API](https://docs.mem0.ai/api-reference/memory/search-memories) and
|
|
76
|
+
[Memory Filters](https://docs.mem0.ai/platform/features/v2-memory-filters). Fields that are not native
|
|
77
|
+
Mem0 filter fields are treated as Mem0 metadata fields.
|
|
78
|
+
:param top_k: Maximum number of memories to return. Overrides the init-time default.
|
|
79
|
+
:returns: Dictionary with key `memories` containing a list of ChatMessage objects. User-provided
|
|
80
|
+
Mem0 metadata is included in each message's meta. Mem0 retrieval fields such as `memory_id`, `user_id`,
|
|
81
|
+
`score`, and timestamps are included under `meta["mem0"]`.
|
|
82
|
+
"""
|
|
83
|
+
memories = self.memory_store.search_memories(
|
|
84
|
+
query=query,
|
|
85
|
+
filters=filters,
|
|
86
|
+
top_k=top_k if top_k is not None else self.top_k,
|
|
87
|
+
user_id=user_id,
|
|
88
|
+
run_id=run_id,
|
|
89
|
+
agent_id=agent_id,
|
|
90
|
+
app_id=app_id,
|
|
91
|
+
)
|
|
92
|
+
return {"memories": memories}
|
|
93
|
+
|
|
94
|
+
def to_dict(self) -> dict[str, Any]:
|
|
95
|
+
"""Serialize this component to a dictionary."""
|
|
96
|
+
return default_to_dict(
|
|
97
|
+
self,
|
|
98
|
+
memory_store=self.memory_store.to_dict(),
|
|
99
|
+
top_k=self.top_k,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
@classmethod
|
|
103
|
+
def from_dict(cls, data: dict[str, Any]) -> "Mem0MemoryRetriever":
|
|
104
|
+
"""Deserialize this component from a dictionary."""
|
|
105
|
+
if data.get("init_parameters", {}).get("memory_store"):
|
|
106
|
+
data["init_parameters"]["memory_store"] = default_from_dict(
|
|
107
|
+
Mem0MemoryStore, data["init_parameters"]["memory_store"]
|
|
108
|
+
)
|
|
109
|
+
return default_from_dict(cls, data)
|
|
File without changes
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from haystack import component, default_from_dict, default_to_dict
|
|
8
|
+
from haystack.dataclasses import ChatMessage
|
|
9
|
+
|
|
10
|
+
from haystack_integrations.memory_stores.mem0.memory_store import Mem0MemoryStore
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@component
|
|
14
|
+
class Mem0MemoryWriter:
|
|
15
|
+
"""
|
|
16
|
+
Writes ChatMessage objects as memories to a Mem0MemoryStore.
|
|
17
|
+
|
|
18
|
+
Use this component in a Haystack Pipeline to persist conversation messages.
|
|
19
|
+
Scoping IDs (`user_id`, `run_id`, `agent_id`, `app_id`) are runtime parameters so the
|
|
20
|
+
same pipeline instance can serve multiple users or agents. The `infer` setting controls whether
|
|
21
|
+
Mem0 extracts memories from messages or stores message text as-is.
|
|
22
|
+
|
|
23
|
+
### Usage example
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from haystack.dataclasses import ChatMessage
|
|
27
|
+
from haystack_integrations.components.writers.mem0 import Mem0MemoryWriter
|
|
28
|
+
from haystack_integrations.memory_stores.mem0 import Mem0MemoryStore
|
|
29
|
+
|
|
30
|
+
store = Mem0MemoryStore()
|
|
31
|
+
writer = Mem0MemoryWriter(memory_store=store, infer=False)
|
|
32
|
+
|
|
33
|
+
result = writer.run(
|
|
34
|
+
messages=[ChatMessage.from_user("Alice prefers concise Python examples.")],
|
|
35
|
+
user_id="alice",
|
|
36
|
+
)
|
|
37
|
+
print(result["memories_written"])
|
|
38
|
+
```
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
def __init__(self, *, memory_store: Mem0MemoryStore, infer: bool = True) -> None:
|
|
42
|
+
"""
|
|
43
|
+
Initialize the Mem0MemoryWriter.
|
|
44
|
+
|
|
45
|
+
:param memory_store: The Mem0MemoryStore instance to write memories to.
|
|
46
|
+
:param infer: If True, Mem0 extracts memories from messages. If False, Mem0 stores message text as-is.
|
|
47
|
+
"""
|
|
48
|
+
self.memory_store = memory_store
|
|
49
|
+
self.infer = infer
|
|
50
|
+
|
|
51
|
+
@component.output_types(memories_written=int)
|
|
52
|
+
def run(
|
|
53
|
+
self,
|
|
54
|
+
messages: list[ChatMessage],
|
|
55
|
+
*,
|
|
56
|
+
user_id: str | None = None,
|
|
57
|
+
run_id: str | None = None,
|
|
58
|
+
agent_id: str | None = None,
|
|
59
|
+
app_id: str | None = None,
|
|
60
|
+
) -> dict[str, int]:
|
|
61
|
+
"""
|
|
62
|
+
Write messages as memories to the Mem0 store.
|
|
63
|
+
|
|
64
|
+
:param messages: List of ChatMessage objects to store.
|
|
65
|
+
:param user_id: User ID to scope the stored memories.
|
|
66
|
+
:param run_id: Run ID to scope the stored memories.
|
|
67
|
+
:param agent_id: Agent ID to scope the stored memories.
|
|
68
|
+
:param app_id: App ID to scope the stored memories.
|
|
69
|
+
:returns: Dictionary with key `memories_written` containing the count of stored memory items.
|
|
70
|
+
"""
|
|
71
|
+
result = self.memory_store.add_memories(
|
|
72
|
+
messages=messages,
|
|
73
|
+
user_id=user_id,
|
|
74
|
+
run_id=run_id,
|
|
75
|
+
agent_id=agent_id,
|
|
76
|
+
app_id=app_id,
|
|
77
|
+
infer=self.infer,
|
|
78
|
+
)
|
|
79
|
+
count = len(result) if isinstance(result, list) else 0
|
|
80
|
+
return {"memories_written": count}
|
|
81
|
+
|
|
82
|
+
def to_dict(self) -> dict[str, Any]:
|
|
83
|
+
"""Serialize this component to a dictionary."""
|
|
84
|
+
return default_to_dict(self, memory_store=self.memory_store.to_dict(), infer=self.infer)
|
|
85
|
+
|
|
86
|
+
@classmethod
|
|
87
|
+
def from_dict(cls, data: dict[str, Any]) -> "Mem0MemoryWriter":
|
|
88
|
+
"""Deserialize this component from a dictionary."""
|
|
89
|
+
if data.get("init_parameters", {}).get("memory_store"):
|
|
90
|
+
data["init_parameters"]["memory_store"] = Mem0MemoryStore.from_dict(data["init_parameters"]["memory_store"])
|
|
91
|
+
return default_from_dict(cls, data)
|
|
File without changes
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
from haystack_integrations.memory_stores.mem0.errors import Mem0MemoryStoreError
|
|
6
|
+
from haystack_integrations.memory_stores.mem0.memory_store import Mem0MemoryStore
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"Mem0MemoryStore",
|
|
10
|
+
"Mem0MemoryStoreError",
|
|
11
|
+
]
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
"""
|
|
6
|
+
Utilities for translating Haystack filters into Mem0 Platform filters.
|
|
7
|
+
|
|
8
|
+
Mem0 Platform `search` and `get_all` expect entity IDs such as `user_id`, `agent_id`, `app_id`, and `run_id`
|
|
9
|
+
inside the `filters` object. They also reserve a fixed set of top-level filter fields such as `created_at`,
|
|
10
|
+
`categories`, `keywords`, and `memory_ids`.
|
|
11
|
+
|
|
12
|
+
See:
|
|
13
|
+
- [Search Memories API](https://docs.mem0.ai/api-reference/memory/search-memories)
|
|
14
|
+
- [Get Memories API](https://docs.mem0.ai/api-reference/memory/get-memories)
|
|
15
|
+
- [Memory Filters](https://docs.mem0.ai/platform/features/v2-memory-filters)
|
|
16
|
+
|
|
17
|
+
Haystack users usually think of arbitrary fields as metadata filters. To keep that API natural while sending valid
|
|
18
|
+
Mem0 filters, fields that are not native Mem0 filter fields are nested under `metadata`. For example,
|
|
19
|
+
`{"field": "category", "operator": "==", "value": "work"}` becomes `{"metadata": {"category": "work"}}`.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
from haystack.errors import FilterError
|
|
25
|
+
|
|
26
|
+
# Keep this list aligned with the top-level filter keys documented in:
|
|
27
|
+
# https://docs.mem0.ai/api-reference/memory/search-memories
|
|
28
|
+
# https://docs.mem0.ai/platform/features/v2-memory-filters
|
|
29
|
+
_TOP_LEVEL_FILTER_FIELDS = {
|
|
30
|
+
"user_id",
|
|
31
|
+
"agent_id",
|
|
32
|
+
"app_id",
|
|
33
|
+
"run_id",
|
|
34
|
+
"created_at",
|
|
35
|
+
"updated_at",
|
|
36
|
+
"timestamp",
|
|
37
|
+
"categories",
|
|
38
|
+
"metadata",
|
|
39
|
+
"keywords",
|
|
40
|
+
"feedback",
|
|
41
|
+
"feedback_reason",
|
|
42
|
+
"memory_ids",
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _build_search_filters(
|
|
47
|
+
*,
|
|
48
|
+
filters: dict[str, Any] | None = None,
|
|
49
|
+
user_id: str | None = None,
|
|
50
|
+
run_id: str | None = None,
|
|
51
|
+
agent_id: str | None = None,
|
|
52
|
+
app_id: str | None = None,
|
|
53
|
+
) -> dict[str, Any]:
|
|
54
|
+
"""
|
|
55
|
+
Build Mem0 search filters from explicit IDs and optional Haystack-style filters.
|
|
56
|
+
|
|
57
|
+
Mem0 `search` and `get_all` expect entity IDs inside the `filters` object.
|
|
58
|
+
This helper keeps the public store API convenient while ensuring IDs and custom filters are both applied.
|
|
59
|
+
It combines everything as Haystack-style filters first, then normalizes the combined filter to Mem0 format.
|
|
60
|
+
Fields that are not native Mem0 filter fields are treated as Mem0 metadata fields.
|
|
61
|
+
|
|
62
|
+
Mem0 filter reference:
|
|
63
|
+
- [Search Memories API](https://docs.mem0.ai/api-reference/memory/search-memories)
|
|
64
|
+
- [Get Memories API](https://docs.mem0.ai/api-reference/memory/get-memories)
|
|
65
|
+
|
|
66
|
+
:param filters: Haystack-style filters to combine with IDs.
|
|
67
|
+
:param user_id: User ID to scope the search.
|
|
68
|
+
:param run_id: Run ID to scope the search.
|
|
69
|
+
:param agent_id: Agent ID to scope the search.
|
|
70
|
+
:param app_id: App ID to scope the search.
|
|
71
|
+
:returns: Mem0-compatible filters.
|
|
72
|
+
:raises ValueError: If neither filters nor an entity ID is provided.
|
|
73
|
+
"""
|
|
74
|
+
conditions = [
|
|
75
|
+
{"field": key, "operator": "==", "value": value}
|
|
76
|
+
for key, value in {"user_id": user_id, "run_id": run_id, "agent_id": agent_id, "app_id": app_id}.items()
|
|
77
|
+
if value
|
|
78
|
+
]
|
|
79
|
+
|
|
80
|
+
if filters:
|
|
81
|
+
if filters.get("operator", "").upper() == "AND" and "conditions" in filters:
|
|
82
|
+
conditions.extend(filters["conditions"])
|
|
83
|
+
else:
|
|
84
|
+
conditions.append(filters)
|
|
85
|
+
|
|
86
|
+
if not conditions:
|
|
87
|
+
msg = "Either filters or at least one of user_id, run_id, agent_id, or app_id must be provided."
|
|
88
|
+
raise ValueError(msg)
|
|
89
|
+
|
|
90
|
+
combined_filter = conditions[0] if len(conditions) == 1 else {"operator": "AND", "conditions": conditions}
|
|
91
|
+
return normalize_filters(combined_filter)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def normalize_filters(filters: dict[str, Any]) -> dict[str, Any]:
|
|
95
|
+
"""
|
|
96
|
+
Convert Haystack-style filters to the Mem0 filter format.
|
|
97
|
+
|
|
98
|
+
Mem0 Platform supports logical filters (`AND`, `OR`, `NOT`) and comparison operators on native fields.
|
|
99
|
+
It also supports metadata filters under the top-level `metadata` key.
|
|
100
|
+
See [Memory Filters](https://docs.mem0.ai/platform/features/v2-memory-filters) for the list of fields and
|
|
101
|
+
operators.
|
|
102
|
+
|
|
103
|
+
:param filters: Haystack filter dictionary.
|
|
104
|
+
:returns: Equivalent Mem0 filter dictionary. Fields that are not native Mem0 filter fields are nested under
|
|
105
|
+
`metadata`.
|
|
106
|
+
:raises FilterError: If the filter structure or operators are invalid.
|
|
107
|
+
"""
|
|
108
|
+
if not isinstance(filters, dict):
|
|
109
|
+
msg = "Filters must be a dictionary."
|
|
110
|
+
raise FilterError(msg)
|
|
111
|
+
if "field" in filters:
|
|
112
|
+
return _parse_comparison_condition(filters)
|
|
113
|
+
return _parse_logical_condition(filters)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _parse_logical_condition(condition: dict[str, Any]) -> dict[str, Any]:
|
|
117
|
+
"""
|
|
118
|
+
Parse a Haystack logical condition into Mem0's logical filter shape.
|
|
119
|
+
|
|
120
|
+
Mem0 documents `AND`, `OR`, and `NOT` as supported logical operators:
|
|
121
|
+
[Memory Filters](https://docs.mem0.ai/platform/features/v2-memory-filters)
|
|
122
|
+
"""
|
|
123
|
+
if "operator" not in condition:
|
|
124
|
+
msg = f"'operator' key missing in: {condition}"
|
|
125
|
+
raise FilterError(msg)
|
|
126
|
+
if "conditions" not in condition:
|
|
127
|
+
msg = f"'conditions' key missing in: {condition}"
|
|
128
|
+
raise FilterError(msg)
|
|
129
|
+
|
|
130
|
+
operator = condition["operator"].upper()
|
|
131
|
+
if operator not in ("AND", "OR", "NOT"):
|
|
132
|
+
msg = f"Unsupported logical operator: {operator!r}. Use AND, OR, or NOT."
|
|
133
|
+
raise FilterError(msg)
|
|
134
|
+
|
|
135
|
+
converted = [_convert(c) for c in condition["conditions"]]
|
|
136
|
+
return {operator: converted}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _parse_comparison_condition(condition: dict[str, Any]) -> dict[str, Any]:
|
|
140
|
+
"""
|
|
141
|
+
Parse a Haystack comparison condition into Mem0's native or metadata filter shape.
|
|
142
|
+
|
|
143
|
+
Native Mem0 fields are kept at the top level. All other fields are treated as metadata fields because Mem0's
|
|
144
|
+
Platform API expects metadata filters under the `metadata` key:
|
|
145
|
+
[Memory Filters](https://docs.mem0.ai/platform/features/v2-memory-filters)
|
|
146
|
+
"""
|
|
147
|
+
if "field" not in condition:
|
|
148
|
+
msg = f"'field' key missing in: {condition}"
|
|
149
|
+
raise FilterError(msg)
|
|
150
|
+
if "operator" not in condition:
|
|
151
|
+
msg = f"'operator' key missing in: {condition}"
|
|
152
|
+
raise FilterError(msg)
|
|
153
|
+
if "value" not in condition:
|
|
154
|
+
msg = f"'value' key missing in: {condition}"
|
|
155
|
+
raise FilterError(msg)
|
|
156
|
+
|
|
157
|
+
field: str = condition["field"]
|
|
158
|
+
operator: str = condition["operator"]
|
|
159
|
+
value: Any = condition["value"]
|
|
160
|
+
|
|
161
|
+
if field not in _TOP_LEVEL_FILTER_FIELDS:
|
|
162
|
+
return _parse_metadata_comparison_condition(field, operator, value)
|
|
163
|
+
|
|
164
|
+
op_map = {
|
|
165
|
+
"==": lambda f, v: {f: v},
|
|
166
|
+
"!=": lambda f, v: {f: {"ne": v}},
|
|
167
|
+
">": lambda f, v: {f: {"gt": v}},
|
|
168
|
+
">=": lambda f, v: {f: {"gte": v}},
|
|
169
|
+
"<": lambda f, v: {f: {"lt": v}},
|
|
170
|
+
"<=": lambda f, v: {f: {"lte": v}},
|
|
171
|
+
"in": lambda f, v: {f: {"in": v if isinstance(v, list) else [v]}},
|
|
172
|
+
"not in": lambda f, v: {f: {"ne": v}},
|
|
173
|
+
"contains": lambda f, v: {f: {"contains": v}},
|
|
174
|
+
"icontains": lambda f, v: {f: {"icontains": v}},
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if operator not in op_map:
|
|
178
|
+
msg = f"Unsupported filter operator: {operator!r}."
|
|
179
|
+
raise FilterError(msg)
|
|
180
|
+
|
|
181
|
+
return op_map[operator](field, value)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def _parse_metadata_comparison_condition(field: str, operator: str, value: Any) -> dict[str, Any]:
|
|
185
|
+
"""
|
|
186
|
+
Convert a Haystack metadata comparison into Mem0's `metadata` filter shape.
|
|
187
|
+
|
|
188
|
+
Mem0 metadata filters support bare equality, `ne`, and `contains`; operators such as `in`, `gt`, and `lt` are not
|
|
189
|
+
supported for metadata filters.
|
|
190
|
+
For multi-value metadata checks, Mem0 recommends composing equality clauses with `OR`.
|
|
191
|
+
|
|
192
|
+
See [Memory Filters](https://docs.mem0.ai/platform/features/v2-memory-filters).
|
|
193
|
+
"""
|
|
194
|
+
metadata_field = field.removeprefix("metadata.")
|
|
195
|
+
op_map = {
|
|
196
|
+
"==": lambda f, v: {"metadata": {f: v}},
|
|
197
|
+
"!=": lambda f, v: {"metadata": {f: {"ne": v}}},
|
|
198
|
+
"contains": lambda f, v: {"metadata": {f: {"contains": v}}},
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if operator not in op_map:
|
|
202
|
+
msg = (
|
|
203
|
+
f"Unsupported metadata filter operator: {operator!r}. "
|
|
204
|
+
"Mem0 metadata filters support ==, !=, and contains. "
|
|
205
|
+
"For multi-value metadata filters, combine equality conditions with OR."
|
|
206
|
+
)
|
|
207
|
+
raise FilterError(msg)
|
|
208
|
+
|
|
209
|
+
return op_map[operator](metadata_field, value)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def _convert(node: dict[str, Any]) -> dict[str, Any]:
|
|
213
|
+
if "field" in node:
|
|
214
|
+
return _parse_comparison_condition(node)
|
|
215
|
+
return _parse_logical_condition(node)
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from haystack import default_from_dict, default_to_dict, logging
|
|
8
|
+
from haystack.dataclasses.chat_message import ChatMessage
|
|
9
|
+
from haystack.utils import Secret, deserialize_secrets_inplace
|
|
10
|
+
|
|
11
|
+
from haystack_integrations.memory_stores.mem0.errors import Mem0MemoryStoreError
|
|
12
|
+
from haystack_integrations.memory_stores.mem0.filters import _build_search_filters
|
|
13
|
+
from mem0 import MemoryClient
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Mem0MemoryStore:
|
|
19
|
+
"""
|
|
20
|
+
A memory store backed by the Mem0 cloud API.
|
|
21
|
+
|
|
22
|
+
Stores and retrieves ChatMessage-based memories scoped by user_id, run_id, agent_id, or app_id.
|
|
23
|
+
The Mem0 client is created lazily on first use (or explicitly via warm_up()).
|
|
24
|
+
Requires a Mem0 API key set via the MEM0_API_KEY environment variable or passed explicitly.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
*,
|
|
30
|
+
api_key: Secret = Secret.from_env_var("MEM0_API_KEY"),
|
|
31
|
+
) -> None:
|
|
32
|
+
"""
|
|
33
|
+
Initialize the Mem0 memory store.
|
|
34
|
+
|
|
35
|
+
The Mem0 client is not created until warm_up() is called (or the first method that
|
|
36
|
+
needs the client is invoked).
|
|
37
|
+
|
|
38
|
+
:param api_key: The Mem0 API key. Defaults to the MEM0_API_KEY environment variable.
|
|
39
|
+
"""
|
|
40
|
+
self.api_key = api_key
|
|
41
|
+
self._client: MemoryClient | None = None
|
|
42
|
+
|
|
43
|
+
def warm_up(self) -> None:
|
|
44
|
+
"""
|
|
45
|
+
Create the Mem0 client. Called automatically on first use if not called explicitly.
|
|
46
|
+
|
|
47
|
+
Calling this method explicitly is useful when you want to validate the API key
|
|
48
|
+
or pre-connect before the first pipeline run.
|
|
49
|
+
"""
|
|
50
|
+
if self._client is None:
|
|
51
|
+
self._client = MemoryClient(api_key=self.api_key.resolve_value())
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def client(self) -> MemoryClient:
|
|
55
|
+
"""Return the initialized client, calling warm_up() if necessary."""
|
|
56
|
+
self.warm_up()
|
|
57
|
+
return self._client # type: ignore[return-value]
|
|
58
|
+
|
|
59
|
+
def to_dict(self) -> dict[str, Any]:
|
|
60
|
+
"""Serialize the store configuration to a dictionary."""
|
|
61
|
+
return default_to_dict(self, api_key=self.api_key.to_dict())
|
|
62
|
+
|
|
63
|
+
@classmethod
|
|
64
|
+
def from_dict(cls, data: dict[str, Any]) -> "Mem0MemoryStore":
|
|
65
|
+
"""Deserialize the store from a dictionary."""
|
|
66
|
+
if data.get("init_parameters"):
|
|
67
|
+
deserialize_secrets_inplace(data["init_parameters"], keys=["api_key"])
|
|
68
|
+
return default_from_dict(cls, data)
|
|
69
|
+
|
|
70
|
+
def add_memories(
|
|
71
|
+
self,
|
|
72
|
+
*,
|
|
73
|
+
messages: list[ChatMessage],
|
|
74
|
+
user_id: str | None = None,
|
|
75
|
+
run_id: str | None = None,
|
|
76
|
+
agent_id: str | None = None,
|
|
77
|
+
app_id: str | None = None,
|
|
78
|
+
infer: bool = True,
|
|
79
|
+
**kwargs: Any,
|
|
80
|
+
) -> list[dict[str, Any]]:
|
|
81
|
+
"""
|
|
82
|
+
Add ChatMessage memories to Mem0.
|
|
83
|
+
|
|
84
|
+
:param messages: List of ChatMessage objects to store as memories.
|
|
85
|
+
:param user_id: User ID to scope these memories.
|
|
86
|
+
:param run_id: Run ID to scope these memories.
|
|
87
|
+
:param agent_id: Agent ID to scope these memories. Required for Mem0 to store assistant messages.
|
|
88
|
+
:param app_id: App ID to scope these memories.
|
|
89
|
+
:param infer: If True, Mem0 extracts memories from messages. If False, Mem0 stores message text as-is.
|
|
90
|
+
:param kwargs: Additional keyword arguments forwarded to the Mem0 client add method.
|
|
91
|
+
Note: ChatMessage.meta is ignored because Mem0 doesn't support per-message metadata.
|
|
92
|
+
Pass `metadata` as a kwarg to attach metadata to the whole batch instead.
|
|
93
|
+
:returns: List of objects with `memory_id` and `memory` text for each stored memory.
|
|
94
|
+
:raises Mem0MemoryStoreError: If the Mem0 API call fails.
|
|
95
|
+
"""
|
|
96
|
+
ids = self._get_ids(user_id=user_id, run_id=run_id, agent_id=agent_id, app_id=app_id)
|
|
97
|
+
mem0_messages = [{"content": msg.text, "role": msg.role.value} for msg in messages if msg.text]
|
|
98
|
+
if not mem0_messages:
|
|
99
|
+
logger.warning(
|
|
100
|
+
"No valid messages to add after filtering out empty texts. Returning without calling Mem0 API."
|
|
101
|
+
)
|
|
102
|
+
return []
|
|
103
|
+
|
|
104
|
+
added: list[dict[str, Any]] = []
|
|
105
|
+
try:
|
|
106
|
+
status = self.client.add(
|
|
107
|
+
messages=mem0_messages,
|
|
108
|
+
infer=infer,
|
|
109
|
+
**ids,
|
|
110
|
+
**kwargs,
|
|
111
|
+
)
|
|
112
|
+
if status and "results" in status:
|
|
113
|
+
for result in status["results"]:
|
|
114
|
+
data = result.get("data")
|
|
115
|
+
memory_text = data.get("memory") if isinstance(data, dict) else result.get("memory")
|
|
116
|
+
added.append({"memory_id": result.get("id"), "memory": memory_text})
|
|
117
|
+
except Exception as e:
|
|
118
|
+
msg = f"Failed to add memories: {e}"
|
|
119
|
+
raise Mem0MemoryStoreError(msg) from e
|
|
120
|
+
return added
|
|
121
|
+
|
|
122
|
+
def search_memories(
|
|
123
|
+
self,
|
|
124
|
+
*,
|
|
125
|
+
query: str | None = None,
|
|
126
|
+
filters: dict[str, Any] | None = None,
|
|
127
|
+
top_k: int = 5,
|
|
128
|
+
user_id: str | None = None,
|
|
129
|
+
run_id: str | None = None,
|
|
130
|
+
agent_id: str | None = None,
|
|
131
|
+
app_id: str | None = None,
|
|
132
|
+
**kwargs: Any,
|
|
133
|
+
) -> list[ChatMessage]:
|
|
134
|
+
"""
|
|
135
|
+
Search for memories in Mem0.
|
|
136
|
+
|
|
137
|
+
Either `filters` or at least one of `user_id`, `run_id`, `agent_id`, or `app_id` must be provided.
|
|
138
|
+
When both `filters` and IDs are provided, they are combined with an `AND` condition.
|
|
139
|
+
|
|
140
|
+
:param query: Text query to search. If omitted, returns all memories matching the scope.
|
|
141
|
+
:param filters: Haystack-style filters to apply. See
|
|
142
|
+
[Haystack metadata filtering](https://docs.haystack.deepset.ai/docs/metadata-filtering).
|
|
143
|
+
Mem0 requires entity IDs inside filters and supports a fixed set of native fields and operators:
|
|
144
|
+
[Search Memories API](https://docs.mem0.ai/api-reference/memory/search-memories) and
|
|
145
|
+
[Memory Filters](https://docs.mem0.ai/platform/features/v2-memory-filters).
|
|
146
|
+
Fields that are not native Mem0 filter fields are treated as Mem0 metadata fields.
|
|
147
|
+
:param top_k: Maximum number of results to return.
|
|
148
|
+
:param user_id: User ID to scope the search.
|
|
149
|
+
:param run_id: Run ID to scope the search.
|
|
150
|
+
:param agent_id: Agent ID to scope the search.
|
|
151
|
+
:param app_id: App ID to scope the search.
|
|
152
|
+
:param kwargs: Additional keyword arguments forwarded to the Mem0 client.
|
|
153
|
+
:returns: List of ChatMessage (system role) objects containing the retrieved memories. User-provided
|
|
154
|
+
Mem0 metadata is included in each message's meta. Mem0 retrieval fields such as `memory_id`, `user_id`,
|
|
155
|
+
`score`, and timestamps are included under `meta["mem0"]`.
|
|
156
|
+
:raises Mem0MemoryStoreError: If the Mem0 API call fails.
|
|
157
|
+
"""
|
|
158
|
+
mem0_filters = _build_search_filters(
|
|
159
|
+
filters=filters,
|
|
160
|
+
user_id=user_id,
|
|
161
|
+
run_id=run_id,
|
|
162
|
+
agent_id=agent_id,
|
|
163
|
+
app_id=app_id,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
try:
|
|
167
|
+
if not query:
|
|
168
|
+
raw = self.client.get_all(filters=mem0_filters, **kwargs)
|
|
169
|
+
else:
|
|
170
|
+
raw = self.client.search(query=query, top_k=top_k, filters=mem0_filters, **kwargs)
|
|
171
|
+
|
|
172
|
+
result_messages = []
|
|
173
|
+
for memory in raw["results"]:
|
|
174
|
+
meta = dict(memory["metadata"]) if memory["metadata"] else {}
|
|
175
|
+
meta["mem0"] = {
|
|
176
|
+
"memory_id": memory.get("id"),
|
|
177
|
+
"user_id": memory.get("user_id"),
|
|
178
|
+
"agent_id": memory.get("agent_id"),
|
|
179
|
+
"app_id": memory.get("app_id"),
|
|
180
|
+
"run_id": memory.get("run_id"),
|
|
181
|
+
"score": memory.get("score"),
|
|
182
|
+
"score_breakdown": memory.get("score_breakdown"),
|
|
183
|
+
"categories": memory.get("categories"),
|
|
184
|
+
"created_at": memory.get("created_at"),
|
|
185
|
+
"updated_at": memory.get("updated_at"),
|
|
186
|
+
}
|
|
187
|
+
result_messages.append(ChatMessage.from_system(text=memory["memory"], meta=meta))
|
|
188
|
+
return result_messages
|
|
189
|
+
except Exception as e:
|
|
190
|
+
msg = f"Failed to search memories: {e}"
|
|
191
|
+
raise Mem0MemoryStoreError(msg) from e
|
|
192
|
+
|
|
193
|
+
def _get_ids(
|
|
194
|
+
self,
|
|
195
|
+
user_id: str | None = None,
|
|
196
|
+
run_id: str | None = None,
|
|
197
|
+
agent_id: str | None = None,
|
|
198
|
+
app_id: str | None = None,
|
|
199
|
+
) -> dict[str, Any]:
|
|
200
|
+
"""
|
|
201
|
+
Return non-empty Mem0 entity IDs for scoped memory operations.
|
|
202
|
+
|
|
203
|
+
Mem0 requires at least one entity ID when adding or searching memories without explicit filters.
|
|
204
|
+
Valid entity IDs are `user_id`, `run_id`, `agent_id`, and `app_id`.
|
|
205
|
+
|
|
206
|
+
:param user_id: User ID to include in the scope.
|
|
207
|
+
:param run_id: Run ID to include in the scope.
|
|
208
|
+
:param agent_id: Agent ID to include in the scope.
|
|
209
|
+
:param app_id: App ID to include in the scope.
|
|
210
|
+
:returns: Dictionary containing only provided entity IDs.
|
|
211
|
+
:raises ValueError: If no entity ID is provided.
|
|
212
|
+
"""
|
|
213
|
+
if not any([user_id, run_id, agent_id, app_id]):
|
|
214
|
+
msg = "At least one of user_id, run_id, agent_id, or app_id must be provided."
|
|
215
|
+
raise ValueError(msg)
|
|
216
|
+
return {
|
|
217
|
+
k: v for k, v in {"user_id": user_id, "run_id": run_id, "agent_id": agent_id, "app_id": app_id}.items() if v
|
|
218
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
from haystack_integrations.tools.mem0.retriever_tool import Mem0MemoryRetrieverTool
|
|
6
|
+
from haystack_integrations.tools.mem0.writer_tool import Mem0MemoryWriterTool
|
|
7
|
+
|
|
8
|
+
__all__ = ["Mem0MemoryRetrieverTool", "Mem0MemoryWriterTool"]
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
from copy import deepcopy
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from haystack.core.serialization import generate_qualified_class_name
|
|
9
|
+
from haystack.dataclasses import ChatMessage
|
|
10
|
+
from haystack.tools import Tool
|
|
11
|
+
|
|
12
|
+
from haystack_integrations.memory_stores.mem0.memory_store import Mem0MemoryStore
|
|
13
|
+
|
|
14
|
+
_DEFAULT_DESCRIPTION = (
|
|
15
|
+
"Search long-term memories relevant to a query, or return all scoped memories when no query is provided. "
|
|
16
|
+
"Use this tool when stored context from past conversations or facts could help answer the user."
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
_PARAMETERS: dict[str, Any] = {
|
|
20
|
+
"type": "object",
|
|
21
|
+
"properties": {
|
|
22
|
+
"query": {
|
|
23
|
+
"type": ["string", "null"],
|
|
24
|
+
"description": (
|
|
25
|
+
"The search query to find relevant memories. Omit or pass null to return all memories in scope."
|
|
26
|
+
),
|
|
27
|
+
},
|
|
28
|
+
"top_k": {"type": "integer", "description": "Maximum number of memories to return for query searches."},
|
|
29
|
+
},
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
_DEFAULT_INPUTS_FROM_STATE: dict[str, str] = {"user_id": "user_id"}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Mem0MemoryRetrieverTool(Tool):
|
|
36
|
+
"""
|
|
37
|
+
A tool that searches a Mem0MemoryStore for memories.
|
|
38
|
+
|
|
39
|
+
The `user_id` is injected at runtime from Agent State via `inputs_from_state`,
|
|
40
|
+
so a single tool instance can serve many users. The LLM only sees `query` and `top_k` by default.
|
|
41
|
+
If the LLM omits `query` or passes `None`, Mem0 returns all memories matching the injected scope.
|
|
42
|
+
Pass a custom `inputs_from_state` mapping to inject other supported Mem0 entity IDs such as
|
|
43
|
+
`run_id`, `agent_id`, or `app_id`. The mapping keys are Agent State keys and the values are this
|
|
44
|
+
tool's parameter names. For example, use
|
|
45
|
+
`inputs_from_state={"user_id": "user_id", "session_id": "run_id"}` to pass `state["session_id"]`
|
|
46
|
+
to the tool's `run_id` parameter at runtime.
|
|
47
|
+
|
|
48
|
+
### Usage example
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from haystack.components.agents import Agent
|
|
52
|
+
from haystack.components.generators.chat import OpenAIChatGenerator
|
|
53
|
+
from haystack.dataclasses import ChatMessage
|
|
54
|
+
from haystack_integrations.memory_stores.mem0 import Mem0MemoryStore
|
|
55
|
+
from haystack_integrations.tools.mem0 import Mem0MemoryRetrieverTool
|
|
56
|
+
|
|
57
|
+
store = Mem0MemoryStore()
|
|
58
|
+
retrieve_memories = Mem0MemoryRetrieverTool(memory_store=store, top_k=5)
|
|
59
|
+
|
|
60
|
+
agent = Agent(
|
|
61
|
+
chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"),
|
|
62
|
+
tools=[retrieve_memories],
|
|
63
|
+
state_schema={"user_id": {"type": str}, "session_id": {"type": str}},
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
# The Agent can call retrieve_memories with a query for targeted recall,
|
|
67
|
+
# or without a query when it needs all scoped memories.
|
|
68
|
+
result = agent.run(
|
|
69
|
+
messages=[ChatMessage.from_user("What do you remember about me?")],
|
|
70
|
+
user_id="alice",
|
|
71
|
+
session_id="chat-42",
|
|
72
|
+
)
|
|
73
|
+
print(result["last_message"].text)
|
|
74
|
+
```
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
def __init__(
|
|
78
|
+
self,
|
|
79
|
+
*,
|
|
80
|
+
memory_store: Mem0MemoryStore,
|
|
81
|
+
top_k: int = 5,
|
|
82
|
+
name: str = "retrieve_memories",
|
|
83
|
+
description: str = _DEFAULT_DESCRIPTION,
|
|
84
|
+
parameters: dict[str, Any] = _PARAMETERS,
|
|
85
|
+
inputs_from_state: dict[str, str] = _DEFAULT_INPUTS_FROM_STATE,
|
|
86
|
+
) -> None:
|
|
87
|
+
"""
|
|
88
|
+
Initialize the Mem0MemoryRetrieverTool.
|
|
89
|
+
|
|
90
|
+
:param memory_store: The Mem0MemoryStore instance to query.
|
|
91
|
+
:param top_k: Default maximum number of memories to return. The LLM may override this.
|
|
92
|
+
:param name: Tool name exposed to the LLM.
|
|
93
|
+
:param description: Tool description exposed to the LLM.
|
|
94
|
+
:param parameters: JSON schema for the parameters exposed to the LLM. Defaults to optional `query` and `top_k`.
|
|
95
|
+
:param inputs_from_state: Mapping from Agent State keys to this tool's parameter names.
|
|
96
|
+
Defaults to `{"user_id": "user_id"}`, which injects `state["user_id"]` into the `user_id`
|
|
97
|
+
parameter. To pass more Mem0 IDs at runtime, add the state fields to the Agent's
|
|
98
|
+
`state_schema` and map them to the corresponding tool parameters, for example
|
|
99
|
+
`{"user_id": "user_id", "session_id": "run_id", "agent_name": "agent_id", "app_name": "app_id"}`.
|
|
100
|
+
"""
|
|
101
|
+
self.memory_store = memory_store
|
|
102
|
+
self.top_k = top_k
|
|
103
|
+
self._is_warmed_up = False
|
|
104
|
+
super().__init__(
|
|
105
|
+
name=name,
|
|
106
|
+
description=description,
|
|
107
|
+
# We deepcopy to avoid accidental mutations since dicts are mutable and could be shared across instances
|
|
108
|
+
parameters=deepcopy(parameters),
|
|
109
|
+
function=self.retrieve,
|
|
110
|
+
inputs_from_state=dict(inputs_from_state),
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
def warm_up(self) -> None:
|
|
114
|
+
"""Initialize the Mem0 client. Subsequent calls are no-ops."""
|
|
115
|
+
if self._is_warmed_up:
|
|
116
|
+
return
|
|
117
|
+
self.memory_store.warm_up()
|
|
118
|
+
self._is_warmed_up = True
|
|
119
|
+
|
|
120
|
+
def retrieve(
|
|
121
|
+
self,
|
|
122
|
+
query: str | None = None,
|
|
123
|
+
*,
|
|
124
|
+
top_k: int | None = None,
|
|
125
|
+
user_id: str | None = None,
|
|
126
|
+
run_id: str | None = None,
|
|
127
|
+
agent_id: str | None = None,
|
|
128
|
+
app_id: str | None = None,
|
|
129
|
+
) -> str:
|
|
130
|
+
"""
|
|
131
|
+
Retrieve memories relevant to a query, or all memories when no query is provided.
|
|
132
|
+
|
|
133
|
+
:param query: Text query used to search for relevant memories. If omitted or `None`, all memories matching
|
|
134
|
+
the scope are returned.
|
|
135
|
+
:param top_k: Maximum number of memories to return for query searches. Overrides the tool default.
|
|
136
|
+
:param user_id: User ID to scope the search. Injected from Agent State by default.
|
|
137
|
+
:param run_id: Run ID to scope the search. Can be injected with a custom `inputs_from_state` mapping.
|
|
138
|
+
:param agent_id: Agent ID to scope the search. Can be injected with a custom `inputs_from_state` mapping.
|
|
139
|
+
:param app_id: App ID to scope the search. Can be injected with a custom `inputs_from_state` mapping.
|
|
140
|
+
:returns: Retrieved memories formatted for the Agent, or a message when no memories were found.
|
|
141
|
+
"""
|
|
142
|
+
memories: list[ChatMessage] = self.memory_store.search_memories(
|
|
143
|
+
query=query,
|
|
144
|
+
top_k=top_k if top_k is not None else self.top_k,
|
|
145
|
+
user_id=user_id,
|
|
146
|
+
run_id=run_id,
|
|
147
|
+
agent_id=agent_id,
|
|
148
|
+
app_id=app_id,
|
|
149
|
+
)
|
|
150
|
+
if not memories:
|
|
151
|
+
return "No memories found."
|
|
152
|
+
return "\n".join(f"- {m.text}" for m in memories if m.text)
|
|
153
|
+
|
|
154
|
+
def to_dict(self) -> dict[str, Any]:
|
|
155
|
+
"""Serialize this tool to a dictionary."""
|
|
156
|
+
return {
|
|
157
|
+
"type": generate_qualified_class_name(type(self)),
|
|
158
|
+
"data": {
|
|
159
|
+
"memory_store": self.memory_store.to_dict(),
|
|
160
|
+
"top_k": self.top_k,
|
|
161
|
+
"name": self.name,
|
|
162
|
+
"description": self.description,
|
|
163
|
+
"parameters": self.parameters,
|
|
164
|
+
"inputs_from_state": self.inputs_from_state,
|
|
165
|
+
},
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
@classmethod
|
|
169
|
+
def from_dict(cls, data: dict[str, Any]) -> "Mem0MemoryRetrieverTool":
|
|
170
|
+
"""Deserialize this tool from a dictionary."""
|
|
171
|
+
inner = data["data"]
|
|
172
|
+
inner["memory_store"] = Mem0MemoryStore.from_dict(inner["memory_store"])
|
|
173
|
+
return cls(**inner)
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
from copy import deepcopy
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from haystack.core.serialization import generate_qualified_class_name
|
|
9
|
+
from haystack.dataclasses import ChatMessage
|
|
10
|
+
from haystack.tools import Tool
|
|
11
|
+
|
|
12
|
+
from haystack_integrations.memory_stores.mem0.memory_store import Mem0MemoryStore
|
|
13
|
+
|
|
14
|
+
_DEFAULT_DESCRIPTION = (
|
|
15
|
+
"Store a piece of information as a long-term memory. "
|
|
16
|
+
"Use this tool to persist important facts, preferences, or context for future conversations."
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
_PARAMETERS: dict[str, Any] = {
|
|
20
|
+
"type": "object",
|
|
21
|
+
"properties": {
|
|
22
|
+
"text": {"type": "string", "description": "The information to store as a memory."},
|
|
23
|
+
"infer": {
|
|
24
|
+
"type": "boolean",
|
|
25
|
+
"description": "If true, Mem0 extracts memories from the text. If false, Mem0 stores the text as-is.",
|
|
26
|
+
"default": False,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
"required": ["text"],
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
_DEFAULT_INPUTS_FROM_STATE: dict[str, str] = {"user_id": "user_id"}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Mem0MemoryWriterTool(Tool):
|
|
36
|
+
"""
|
|
37
|
+
A tool that writes a memory to a Mem0MemoryStore.
|
|
38
|
+
|
|
39
|
+
The `user_id` is injected at runtime from Agent State via `inputs_from_state`,
|
|
40
|
+
so a single tool instance can serve many users. The LLM only sees `text` and `infer`.
|
|
41
|
+
Pass a custom `inputs_from_state` mapping to inject other supported Mem0 entity IDs such as
|
|
42
|
+
`run_id`, `agent_id`, or `app_id`. The mapping keys are Agent State keys and the values are this
|
|
43
|
+
tool's parameter names. For example, use
|
|
44
|
+
`inputs_from_state={"user_id": "user_id", "session_id": "run_id"}` to pass `state["session_id"]`
|
|
45
|
+
to the tool's `run_id` parameter at runtime.
|
|
46
|
+
|
|
47
|
+
### Usage example
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from haystack.components.agents import Agent
|
|
51
|
+
from haystack.components.generators.chat import OpenAIChatGenerator
|
|
52
|
+
from haystack.dataclasses import ChatMessage
|
|
53
|
+
from haystack_integrations.memory_stores.mem0 import Mem0MemoryStore
|
|
54
|
+
from haystack_integrations.tools.mem0 import Mem0MemoryWriterTool
|
|
55
|
+
|
|
56
|
+
store = Mem0MemoryStore()
|
|
57
|
+
store_memory = Mem0MemoryWriterTool(memory_store=store)
|
|
58
|
+
|
|
59
|
+
agent = Agent(
|
|
60
|
+
chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"),
|
|
61
|
+
tools=[store_memory],
|
|
62
|
+
state_schema={"user_id": {"type": str}, "session_id": {"type": str}},
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
result = agent.run(
|
|
66
|
+
messages=[ChatMessage.from_user("Remember that I prefer concise Python examples.")],
|
|
67
|
+
user_id="alice",
|
|
68
|
+
session_id="chat-42",
|
|
69
|
+
)
|
|
70
|
+
print(result["last_message"].text)
|
|
71
|
+
```
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
def __init__(
|
|
75
|
+
self,
|
|
76
|
+
*,
|
|
77
|
+
memory_store: Mem0MemoryStore,
|
|
78
|
+
name: str = "store_memory",
|
|
79
|
+
description: str = _DEFAULT_DESCRIPTION,
|
|
80
|
+
parameters: dict[str, Any] = _PARAMETERS,
|
|
81
|
+
inputs_from_state: dict[str, str] = _DEFAULT_INPUTS_FROM_STATE,
|
|
82
|
+
) -> None:
|
|
83
|
+
"""
|
|
84
|
+
Initialize the Mem0MemoryWriterTool.
|
|
85
|
+
|
|
86
|
+
:param memory_store: The Mem0MemoryStore instance to write to.
|
|
87
|
+
:param name: Tool name exposed to the LLM.
|
|
88
|
+
:param description: Tool description exposed to the LLM.
|
|
89
|
+
:param parameters: JSON schema for the parameters exposed to the LLM. Defaults to `text` and `infer`.
|
|
90
|
+
:param inputs_from_state: Mapping from Agent State keys to this tool's parameter names.
|
|
91
|
+
Defaults to `{"user_id": "user_id"}`, which injects `state["user_id"]` into the `user_id`
|
|
92
|
+
parameter. To pass more Mem0 IDs at runtime, add the state fields to the Agent's
|
|
93
|
+
`state_schema` and map them to the corresponding tool parameters, for example
|
|
94
|
+
`{"user_id": "user_id", "session_id": "run_id", "agent_name": "agent_id", "app_name": "app_id"}`.
|
|
95
|
+
"""
|
|
96
|
+
self.memory_store = memory_store
|
|
97
|
+
self._is_warmed_up = False
|
|
98
|
+
super().__init__(
|
|
99
|
+
name=name,
|
|
100
|
+
description=description,
|
|
101
|
+
# We deepcopy to avoid accidental mutations since dicts are mutable and could be shared across instances
|
|
102
|
+
parameters=deepcopy(parameters),
|
|
103
|
+
function=self.store,
|
|
104
|
+
inputs_from_state=dict(inputs_from_state),
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
def warm_up(self) -> None:
|
|
108
|
+
"""Initialize the Mem0 client. Subsequent calls are no-ops."""
|
|
109
|
+
if self._is_warmed_up:
|
|
110
|
+
return
|
|
111
|
+
self.memory_store.warm_up()
|
|
112
|
+
self._is_warmed_up = True
|
|
113
|
+
|
|
114
|
+
def store(
|
|
115
|
+
self,
|
|
116
|
+
text: str,
|
|
117
|
+
*,
|
|
118
|
+
infer: bool = False,
|
|
119
|
+
user_id: str | None = None,
|
|
120
|
+
run_id: str | None = None,
|
|
121
|
+
agent_id: str | None = None,
|
|
122
|
+
app_id: str | None = None,
|
|
123
|
+
) -> str:
|
|
124
|
+
"""
|
|
125
|
+
Store text as a memory.
|
|
126
|
+
|
|
127
|
+
:param text: The information to store as a memory.
|
|
128
|
+
:param infer: If True, Mem0 extracts memories from the text. If False, Mem0 stores the text as-is.
|
|
129
|
+
:param user_id: User ID to scope the stored memory. Injected from Agent State by default.
|
|
130
|
+
:param run_id: Run ID to scope the stored memory. Can be injected with a custom `inputs_from_state` mapping.
|
|
131
|
+
:param agent_id: Agent ID to scope the stored memory. Can be injected with a custom `inputs_from_state` mapping.
|
|
132
|
+
:param app_id: App ID to scope the stored memory. Can be injected with a custom `inputs_from_state` mapping.
|
|
133
|
+
:returns: A string indicating how many memory items were stored.
|
|
134
|
+
"""
|
|
135
|
+
result = self.memory_store.add_memories(
|
|
136
|
+
messages=[ChatMessage.from_user(text)],
|
|
137
|
+
infer=infer,
|
|
138
|
+
user_id=user_id,
|
|
139
|
+
run_id=run_id,
|
|
140
|
+
agent_id=agent_id,
|
|
141
|
+
app_id=app_id,
|
|
142
|
+
)
|
|
143
|
+
count = len(result) if isinstance(result, list) else 0
|
|
144
|
+
return f"Stored {count} memory item(s)."
|
|
145
|
+
|
|
146
|
+
def to_dict(self) -> dict[str, Any]:
|
|
147
|
+
"""Serialize this tool to a dictionary."""
|
|
148
|
+
return {
|
|
149
|
+
"type": generate_qualified_class_name(type(self)),
|
|
150
|
+
"data": {
|
|
151
|
+
"memory_store": self.memory_store.to_dict(),
|
|
152
|
+
"name": self.name,
|
|
153
|
+
"description": self.description,
|
|
154
|
+
"parameters": self.parameters,
|
|
155
|
+
"inputs_from_state": self.inputs_from_state,
|
|
156
|
+
},
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
@classmethod
|
|
160
|
+
def from_dict(cls, data: dict[str, Any]) -> "Mem0MemoryWriterTool":
|
|
161
|
+
"""Deserialize this tool from a dictionary."""
|
|
162
|
+
inner = data["data"]
|
|
163
|
+
inner["memory_store"] = Mem0MemoryStore.from_dict(inner["memory_store"])
|
|
164
|
+
return cls(**inner)
|
|
File without changes
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mem0-haystack
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Haystack integration for mem0
|
|
5
|
+
Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/mem0#readme
|
|
6
|
+
Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
|
|
7
|
+
Project-URL: Source, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/mem0
|
|
8
|
+
Author-email: deepset GmbH <info@deepset.ai>
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE.txt
|
|
11
|
+
Keywords: agents,haystack,llm,mem0,memory
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
14
|
+
Classifier: Programming Language :: Python
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
21
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Requires-Dist: haystack-ai>=2.19.0
|
|
24
|
+
Requires-Dist: mem0ai>=0.1.0
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# mem0-haystack
|
|
28
|
+
|
|
29
|
+
[](https://pypi.org/project/mem0-haystack)
|
|
30
|
+
[](https://pypi.org/project/mem0-haystack)
|
|
31
|
+
|
|
32
|
+
- [Changelog](https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/mem0/CHANGELOG.md)
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Contributing
|
|
37
|
+
|
|
38
|
+
Refer to the general [Contribution Guidelines](https://github.com/deepset-ai/haystack-core-integrations/blob/main/CONTRIBUTING.md).
|
|
39
|
+
|
|
40
|
+
To run integration tests locally, export the `MEM0_API_KEY` environment variable.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
haystack_integrations/components/retrievers/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
haystack_integrations/components/retrievers/mem0/__init__.py,sha256=xMrzu_Y6FsHcz3OSAch8gt5tlEVJsOBTYeqgkH-K3lA,237
|
|
3
|
+
haystack_integrations/components/retrievers/mem0/retriever.py,sha256=qS1sLHZbyMpSJgPKrZW00u0ikhCTF3qPwMzzNIbcjEQ,4509
|
|
4
|
+
haystack_integrations/components/writers/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
haystack_integrations/components/writers/mem0/__init__.py,sha256=ExDrh8bx6LIJs5OQzBuaapXIzD3o2ETuKe0-3Q3p1rI,225
|
|
6
|
+
haystack_integrations/components/writers/mem0/writer.py,sha256=IeNm9_4jIz2Q9THhCJE47iz1aHp_9f7a0DPPsKCHrZQ,3416
|
|
7
|
+
haystack_integrations/memory_stores/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
haystack_integrations/memory_stores/mem0/__init__.py,sha256=rAcc2e7rZMaqdWmGb7xmrT83D_HpE89QDVHbvpJ687U,340
|
|
9
|
+
haystack_integrations/memory_stores/mem0/errors.py,sha256=CSPsuQKfPC7FEQCGGgtgjnY7-MHydQ7EM9crwZfJcCc,204
|
|
10
|
+
haystack_integrations/memory_stores/mem0/filters.py,sha256=Jf3GdM6uCdQS4LrxvHz2ukTnPI0rGrFxVj6OQxZhjh8,8384
|
|
11
|
+
haystack_integrations/memory_stores/mem0/memory_store.py,sha256=UQ1XjsM1sRnjIJFJmsHRzoVv1tvT0oj7uSskErke49I,9497
|
|
12
|
+
haystack_integrations/tools/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
|
+
haystack_integrations/tools/mem0/__init__.py,sha256=Sd9pCAw-ZaanUJiubxsJYw_2HRRVMcb23gVHH3Ychg4,336
|
|
14
|
+
haystack_integrations/tools/mem0/retriever_tool.py,sha256=g8Z6UkZUONYoTwKumbMMmqIa0zkndfpGkucGY91yzWQ,7284
|
|
15
|
+
haystack_integrations/tools/mem0/writer_tool.py,sha256=CqfB0PoMSftMtpuzGj-4jqnt4n2Z9Wuige_pZKSXVD0,6513
|
|
16
|
+
mem0_haystack-0.1.0.dist-info/METADATA,sha256=HQ38lFq90JIZEiMAY5DvYfpvwW0qPY1gBoUNeHdraPQ,1828
|
|
17
|
+
mem0_haystack-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
18
|
+
mem0_haystack-0.1.0.dist-info/licenses/LICENSE.txt,sha256=OrAtTu4aPmrTjKDSTzkSPgo_ji6NOMWO2N9JOTFyAzM,11350
|
|
19
|
+
mem0_haystack-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2023-present deepset GmbH
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|