langflow-recallic 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langflow_recallic-0.1.0/.gitignore +76 -0
- langflow_recallic-0.1.0/LICENSE +201 -0
- langflow_recallic-0.1.0/PKG-INFO +136 -0
- langflow_recallic-0.1.0/README.md +123 -0
- langflow_recallic-0.1.0/pyproject.toml +23 -0
- langflow_recallic-0.1.0/src/langflow_recallic/__init__.py +6 -0
- langflow_recallic-0.1.0/src/langflow_recallic/components/__init__.py +0 -0
- langflow_recallic-0.1.0/src/langflow_recallic/components/recallic/__init__.py +0 -0
- langflow_recallic-0.1.0/src/langflow_recallic/components/recallic/recallic_recall.py +190 -0
- langflow_recallic-0.1.0/src/langflow_recallic/components/recallic/recallic_save.py +125 -0
- langflow_recallic-0.1.0/src/langflow_recallic/extension.json +16 -0
- langflow_recallic-0.1.0/tests/test_langflow_recallic.py +113 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
|
2
|
+
|
|
3
|
+
# dependencies
|
|
4
|
+
/node_modules
|
|
5
|
+
/.pnp
|
|
6
|
+
.pnp.*
|
|
7
|
+
.yarn/*
|
|
8
|
+
!.yarn/patches
|
|
9
|
+
!.yarn/plugins
|
|
10
|
+
!.yarn/releases
|
|
11
|
+
!.yarn/versions
|
|
12
|
+
|
|
13
|
+
# testing
|
|
14
|
+
/coverage
|
|
15
|
+
|
|
16
|
+
# next.js
|
|
17
|
+
/.next/
|
|
18
|
+
/out/
|
|
19
|
+
|
|
20
|
+
# production
|
|
21
|
+
/build
|
|
22
|
+
|
|
23
|
+
# misc
|
|
24
|
+
.DS_Store
|
|
25
|
+
*.pem
|
|
26
|
+
|
|
27
|
+
# debug
|
|
28
|
+
npm-debug.log*
|
|
29
|
+
yarn-debug.log*
|
|
30
|
+
yarn-error.log*
|
|
31
|
+
.pnpm-debug.log*
|
|
32
|
+
|
|
33
|
+
# env files (can opt-in for committing if needed)
|
|
34
|
+
.env*
|
|
35
|
+
|
|
36
|
+
# vercel
|
|
37
|
+
.vercel
|
|
38
|
+
|
|
39
|
+
# typescript
|
|
40
|
+
*.tsbuildinfo
|
|
41
|
+
next-env.d.ts
|
|
42
|
+
|
|
43
|
+
# python
|
|
44
|
+
venv/
|
|
45
|
+
.venv/
|
|
46
|
+
__pycache__/
|
|
47
|
+
*.pyc
|
|
48
|
+
*.pyo
|
|
49
|
+
*.pyd
|
|
50
|
+
.Python
|
|
51
|
+
env/
|
|
52
|
+
pip-log.txt
|
|
53
|
+
pip-delete-this-directory.txt
|
|
54
|
+
|
|
55
|
+
# Databases and Local Models
|
|
56
|
+
*.db
|
|
57
|
+
*.sqlite
|
|
58
|
+
*.sqlite3
|
|
59
|
+
backend/models/
|
|
60
|
+
storage/
|
|
61
|
+
|
|
62
|
+
# Build and IDE
|
|
63
|
+
**/build/
|
|
64
|
+
.vscode/
|
|
65
|
+
.idea/
|
|
66
|
+
*.log
|
|
67
|
+
|
|
68
|
+
# Phase 6 training data (some sources are CC BY-SA; never committed)
|
|
69
|
+
.phase6_data/
|
|
70
|
+
|
|
71
|
+
# built Python packages (python -m build / pyproject-build)
|
|
72
|
+
integrations/*/dist/
|
|
73
|
+
legacy/*/dist/
|
|
74
|
+
|
|
75
|
+
# private submission drafts
|
|
76
|
+
submissions/
|
|
@@ -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 [yyyy] [name of copyright owner]
|
|
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.
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: langflow-recallic
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Recallic memory components for Langflow: self-hosted, no LLM calls for memory, says when it has no record.
|
|
5
|
+
Project-URL: Homepage, https://www.recallic.com
|
|
6
|
+
Project-URL: Source, https://github.com/Recallic/Recallic/tree/main/integrations/langflow
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Requires-Dist: lfx>=1.13
|
|
11
|
+
Requires-Dist: recallic>=0.3.0
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# langflow-recallic: Recallic memory components for Langflow
|
|
15
|
+
|
|
16
|
+
Website: [recallic.com](https://www.recallic.com)
|
|
17
|
+
|
|
18
|
+
Long-term memory for [Langflow](https://www.langflow.org) flows and agents, backed by a self-hosted
|
|
19
|
+
[Recallic](https://github.com/Recallic/Recallic) server. Two components:
|
|
20
|
+
|
|
21
|
+
* **Recallic Recall** looks up what a user told your flow before. When no stored memory answers the question, it
|
|
22
|
+
returns a note saying there is no record (and that the model should not guess about the user's past) instead of
|
|
23
|
+
near-misses.
|
|
24
|
+
* **Recallic Save** stores what the user said, so later runs can recall it.
|
|
25
|
+
|
|
26
|
+
No LLM calls are made for memory: storing and searching use Recallic's local models. Your data stays on your server,
|
|
27
|
+
separated per user and per agent.
|
|
28
|
+
|
|
29
|
+
This integration is Apache-2.0. The Recallic server is source-available.
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
Run Recallic ([quickstart](../../docs/quickstart.md)) and create a key with role `service`, which acts for each of
|
|
34
|
+
your users (or use a user's own key and leave User ID empty):
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
python -m backend.tools.keys create --user my-langflow-app --role service
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Then add the components to Langflow in one of three ways.
|
|
41
|
+
|
|
42
|
+
**1. As a Langflow extension (Langflow 1.13 or newer).** Install the package into the same Python environment as
|
|
43
|
+
Langflow and restart it. The components appear in the sidebar under **Recallic**.
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install langflow-recallic
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
**2. From a components folder.** Copy the `recallic` folder from
|
|
50
|
+
[`src/langflow_recallic/components/`](src/langflow_recallic/components/) into a folder of your own and start Langflow
|
|
51
|
+
with that folder on its components path:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
LANGFLOW_COMPONENTS_PATH=/path/to/my-components langflow run
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**3. Pasted as a custom component.** In a flow, add a **Custom Component**, open its code editor, and replace the
|
|
58
|
+
code with the whole of [`recallic_recall.py`](src/langflow_recallic/components/recallic/recallic_recall.py) (or
|
|
59
|
+
[`recallic_save.py`](src/langflow_recallic/components/recallic/recallic_save.py)). Each file is self-contained: it uses
|
|
60
|
+
the `recallic` Python package when Langflow's environment has it, and plain HTTP when it does not.
|
|
61
|
+
|
|
62
|
+
## Usage
|
|
63
|
+
|
|
64
|
+
A chat flow with memory:
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
Chat Input ──► Recallic Recall (Query) ──► Prompt {memories} ──► Language Model ──► Chat Output
|
|
68
|
+
└───────► Recallic Save (Message)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
1. Connect **Chat Input** to the Query of **Recallic Recall** and to the Message of **Recallic Save**.
|
|
72
|
+
2. Set Recallic URL, Recallic API Key (store it as a Langflow global variable) and User ID on both. In a multi-user
|
|
73
|
+
app, connect the User ID to whatever carries your user's id (for example the session id).
|
|
74
|
+
3. Put `{memories}` in your Prompt and connect the **Memories** output to it. The prompt gets either the memories,
|
|
75
|
+
newest facts first with the date each was said, or the no-record note.
|
|
76
|
+
|
|
77
|
+
Langflow does not promise which of the two branches runs first. That is fine: a stored question is not taken as an
|
|
78
|
+
answer to itself.
|
|
79
|
+
|
|
80
|
+
For an **Agent**, turn on **Tool Mode** on either component and connect it to the Agent's Tools. The agent then
|
|
81
|
+
fills in the Query (or Message) itself.
|
|
82
|
+
|
|
83
|
+
To check the components from Python, outside Langflow:
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
import asyncio
|
|
87
|
+
from langflow_recallic import RecallicRecallComponent, RecallicSaveComponent
|
|
88
|
+
|
|
89
|
+
conn = dict(recallic_url="http://localhost:8000", api_key="rx_...", recallic_user_id="alice")
|
|
90
|
+
print(asyncio.run(RecallicSaveComponent(**conn, message="I moved to Pune in March.").save_message()).text)
|
|
91
|
+
print(asyncio.run(RecallicRecallComponent(**conn, query="Which city did I move to?").recall_message()).text)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Recallic Recall
|
|
95
|
+
|
|
96
|
+
| Input | Default | What it does |
|
|
97
|
+
|---|---|---|
|
|
98
|
+
| Query | required | What to look up, usually the user's latest message. |
|
|
99
|
+
| User ID | empty | Whose memory to read. Needed with a service key; leave empty with a user's own key. |
|
|
100
|
+
| Recallic URL | `http://localhost:8000` | Your Recallic server. |
|
|
101
|
+
| Recallic API Key | required | A Recallic key (a secret field). |
|
|
102
|
+
| Agent ID (advanced) | `langflow` | Recallic agent id; each agent reads back what it wrote (and what any agent shared). |
|
|
103
|
+
| Mode (advanced) | Server default | `cautious` declines more (more "I don't know"); `helpful` answers more counting, ordering and "how long between" questions. |
|
|
104
|
+
| Max Memories (advanced) | 5 | Memories returned per lookup. |
|
|
105
|
+
| Timeout (advanced) | 30 | Seconds per request to Recallic. |
|
|
106
|
+
|
|
107
|
+
| Output | What it returns |
|
|
108
|
+
|---|---|
|
|
109
|
+
| Memories (Message) | The memories as text for a prompt, or the no-record note when nothing answers the query. |
|
|
110
|
+
| Memory Data (Data list) | One Data per memory (`text`, `id`, `timestamp`, `score`, `agent_id`); an empty list when nothing answers. |
|
|
111
|
+
|
|
112
|
+
### Recallic Save
|
|
113
|
+
|
|
114
|
+
Same User ID, Recallic URL, Recallic API Key, Agent ID and Timeout inputs, plus:
|
|
115
|
+
|
|
116
|
+
| Input | Default | What it does |
|
|
117
|
+
|---|---|---|
|
|
118
|
+
| Message | required | What to remember, in the user's words. |
|
|
119
|
+
| Memory Type (advanced) | `private` | `shared` makes it readable by all of the user's agents. |
|
|
120
|
+
|
|
121
|
+
Output **Status** (Message): `Saved.`, `Already remembered.` (an exact repeat is skipped) or `Not stored (...)`.
|
|
122
|
+
|
|
123
|
+
## Notes
|
|
124
|
+
|
|
125
|
+
* The user field is named `recallic_user_id` in code, because Langflow components already have a `user_id` (the
|
|
126
|
+
Langflow user running the flow).
|
|
127
|
+
* Recallic Recall makes one request per run, shared by both of its outputs.
|
|
128
|
+
* Needs Recallic server 0.2.5 or newer. Tested with lfx 1.13.4 (the package that holds Langflow's component classes).
|
|
129
|
+
|
|
130
|
+
## Tests
|
|
131
|
+
|
|
132
|
+
Against a running Recallic: `RECALLIC_URL=http://127.0.0.1:8000 RECALLIC_SERVICE_KEY=rx_... pytest tests`.
|
|
133
|
+
They run the components' outputs against the server and load them the three ways above (installed extension,
|
|
134
|
+
components folder, pasted code).
|
|
135
|
+
|
|
136
|
+
Licence: Apache-2.0.
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# langflow-recallic: Recallic memory components for Langflow
|
|
2
|
+
|
|
3
|
+
Website: [recallic.com](https://www.recallic.com)
|
|
4
|
+
|
|
5
|
+
Long-term memory for [Langflow](https://www.langflow.org) flows and agents, backed by a self-hosted
|
|
6
|
+
[Recallic](https://github.com/Recallic/Recallic) server. Two components:
|
|
7
|
+
|
|
8
|
+
* **Recallic Recall** looks up what a user told your flow before. When no stored memory answers the question, it
|
|
9
|
+
returns a note saying there is no record (and that the model should not guess about the user's past) instead of
|
|
10
|
+
near-misses.
|
|
11
|
+
* **Recallic Save** stores what the user said, so later runs can recall it.
|
|
12
|
+
|
|
13
|
+
No LLM calls are made for memory: storing and searching use Recallic's local models. Your data stays on your server,
|
|
14
|
+
separated per user and per agent.
|
|
15
|
+
|
|
16
|
+
This integration is Apache-2.0. The Recallic server is source-available.
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
Run Recallic ([quickstart](../../docs/quickstart.md)) and create a key with role `service`, which acts for each of
|
|
21
|
+
your users (or use a user's own key and leave User ID empty):
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
python -m backend.tools.keys create --user my-langflow-app --role service
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Then add the components to Langflow in one of three ways.
|
|
28
|
+
|
|
29
|
+
**1. As a Langflow extension (Langflow 1.13 or newer).** Install the package into the same Python environment as
|
|
30
|
+
Langflow and restart it. The components appear in the sidebar under **Recallic**.
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install langflow-recallic
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
**2. From a components folder.** Copy the `recallic` folder from
|
|
37
|
+
[`src/langflow_recallic/components/`](src/langflow_recallic/components/) into a folder of your own and start Langflow
|
|
38
|
+
with that folder on its components path:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
LANGFLOW_COMPONENTS_PATH=/path/to/my-components langflow run
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
**3. Pasted as a custom component.** In a flow, add a **Custom Component**, open its code editor, and replace the
|
|
45
|
+
code with the whole of [`recallic_recall.py`](src/langflow_recallic/components/recallic/recallic_recall.py) (or
|
|
46
|
+
[`recallic_save.py`](src/langflow_recallic/components/recallic/recallic_save.py)). Each file is self-contained: it uses
|
|
47
|
+
the `recallic` Python package when Langflow's environment has it, and plain HTTP when it does not.
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
A chat flow with memory:
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
Chat Input ──► Recallic Recall (Query) ──► Prompt {memories} ──► Language Model ──► Chat Output
|
|
55
|
+
└───────► Recallic Save (Message)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
1. Connect **Chat Input** to the Query of **Recallic Recall** and to the Message of **Recallic Save**.
|
|
59
|
+
2. Set Recallic URL, Recallic API Key (store it as a Langflow global variable) and User ID on both. In a multi-user
|
|
60
|
+
app, connect the User ID to whatever carries your user's id (for example the session id).
|
|
61
|
+
3. Put `{memories}` in your Prompt and connect the **Memories** output to it. The prompt gets either the memories,
|
|
62
|
+
newest facts first with the date each was said, or the no-record note.
|
|
63
|
+
|
|
64
|
+
Langflow does not promise which of the two branches runs first. That is fine: a stored question is not taken as an
|
|
65
|
+
answer to itself.
|
|
66
|
+
|
|
67
|
+
For an **Agent**, turn on **Tool Mode** on either component and connect it to the Agent's Tools. The agent then
|
|
68
|
+
fills in the Query (or Message) itself.
|
|
69
|
+
|
|
70
|
+
To check the components from Python, outside Langflow:
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
import asyncio
|
|
74
|
+
from langflow_recallic import RecallicRecallComponent, RecallicSaveComponent
|
|
75
|
+
|
|
76
|
+
conn = dict(recallic_url="http://localhost:8000", api_key="rx_...", recallic_user_id="alice")
|
|
77
|
+
print(asyncio.run(RecallicSaveComponent(**conn, message="I moved to Pune in March.").save_message()).text)
|
|
78
|
+
print(asyncio.run(RecallicRecallComponent(**conn, query="Which city did I move to?").recall_message()).text)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Recallic Recall
|
|
82
|
+
|
|
83
|
+
| Input | Default | What it does |
|
|
84
|
+
|---|---|---|
|
|
85
|
+
| Query | required | What to look up, usually the user's latest message. |
|
|
86
|
+
| User ID | empty | Whose memory to read. Needed with a service key; leave empty with a user's own key. |
|
|
87
|
+
| Recallic URL | `http://localhost:8000` | Your Recallic server. |
|
|
88
|
+
| Recallic API Key | required | A Recallic key (a secret field). |
|
|
89
|
+
| Agent ID (advanced) | `langflow` | Recallic agent id; each agent reads back what it wrote (and what any agent shared). |
|
|
90
|
+
| Mode (advanced) | Server default | `cautious` declines more (more "I don't know"); `helpful` answers more counting, ordering and "how long between" questions. |
|
|
91
|
+
| Max Memories (advanced) | 5 | Memories returned per lookup. |
|
|
92
|
+
| Timeout (advanced) | 30 | Seconds per request to Recallic. |
|
|
93
|
+
|
|
94
|
+
| Output | What it returns |
|
|
95
|
+
|---|---|
|
|
96
|
+
| Memories (Message) | The memories as text for a prompt, or the no-record note when nothing answers the query. |
|
|
97
|
+
| Memory Data (Data list) | One Data per memory (`text`, `id`, `timestamp`, `score`, `agent_id`); an empty list when nothing answers. |
|
|
98
|
+
|
|
99
|
+
### Recallic Save
|
|
100
|
+
|
|
101
|
+
Same User ID, Recallic URL, Recallic API Key, Agent ID and Timeout inputs, plus:
|
|
102
|
+
|
|
103
|
+
| Input | Default | What it does |
|
|
104
|
+
|---|---|---|
|
|
105
|
+
| Message | required | What to remember, in the user's words. |
|
|
106
|
+
| Memory Type (advanced) | `private` | `shared` makes it readable by all of the user's agents. |
|
|
107
|
+
|
|
108
|
+
Output **Status** (Message): `Saved.`, `Already remembered.` (an exact repeat is skipped) or `Not stored (...)`.
|
|
109
|
+
|
|
110
|
+
## Notes
|
|
111
|
+
|
|
112
|
+
* The user field is named `recallic_user_id` in code, because Langflow components already have a `user_id` (the
|
|
113
|
+
Langflow user running the flow).
|
|
114
|
+
* Recallic Recall makes one request per run, shared by both of its outputs.
|
|
115
|
+
* Needs Recallic server 0.2.5 or newer. Tested with lfx 1.13.4 (the package that holds Langflow's component classes).
|
|
116
|
+
|
|
117
|
+
## Tests
|
|
118
|
+
|
|
119
|
+
Against a running Recallic: `RECALLIC_URL=http://127.0.0.1:8000 RECALLIC_SERVICE_KEY=rx_... pytest tests`.
|
|
120
|
+
They run the components' outputs against the server and load them the three ways above (installed extension,
|
|
121
|
+
components folder, pasted code).
|
|
122
|
+
|
|
123
|
+
Licence: Apache-2.0.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "langflow-recallic"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Recallic memory components for Langflow: self-hosted, no LLM calls for memory, says when it has no record."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
dependencies = ["lfx>=1.13", "recallic>=0.3.0"]
|
|
13
|
+
|
|
14
|
+
[project.urls]
|
|
15
|
+
Homepage = "https://www.recallic.com"
|
|
16
|
+
Source = "https://github.com/Recallic/Recallic/tree/main/integrations/langflow"
|
|
17
|
+
|
|
18
|
+
# Langflow (1.13+) discovers installed extensions through this entry point and the extension.json next to it.
|
|
19
|
+
[project.entry-points."langflow.extensions"]
|
|
20
|
+
recallic = "langflow_recallic"
|
|
21
|
+
|
|
22
|
+
[tool.hatch.build.targets.wheel]
|
|
23
|
+
packages = ["src/langflow_recallic"]
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"""Recallic components for Langflow. Installed with pip, Langflow finds them through extension.json."""
|
|
2
|
+
from langflow_recallic.components.recallic.recallic_recall import (DECLINE_NOTE, RecallicRecallComponent,
|
|
3
|
+
format_memories)
|
|
4
|
+
from langflow_recallic.components.recallic.recallic_save import RecallicSaveComponent
|
|
5
|
+
|
|
6
|
+
__all__ = ["RecallicRecallComponent", "RecallicSaveComponent", "format_memories", "DECLINE_NOTE"]
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"""Recallic Recall: a Langflow component that looks up what a user told your flow before.
|
|
2
|
+
|
|
3
|
+
Self-contained on purpose: this one file can be pasted into Langflow's custom component editor or dropped into a
|
|
4
|
+
components folder. It uses the `recallic` Python SDK when it is installed and plain HTTP otherwise.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import asyncio
|
|
10
|
+
import json
|
|
11
|
+
import urllib.error
|
|
12
|
+
import urllib.request
|
|
13
|
+
from datetime import datetime, timezone
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from lfx.custom.custom_component.component import Component
|
|
17
|
+
from lfx.io import DropdownInput, IntInput, MessageTextInput, Output, SecretStrInput, StrInput
|
|
18
|
+
from lfx.schema.data import Data
|
|
19
|
+
from lfx.schema.message import Message
|
|
20
|
+
|
|
21
|
+
DECLINE_NOTE = (
|
|
22
|
+
"No stored memory of this user answers this. If it depends on something they would have told you before, "
|
|
23
|
+
"say you don't have a record of it rather than guess about their past."
|
|
24
|
+
)
|
|
25
|
+
HEADER = "Things this user told you before (newest facts win; use only what bears on the question):"
|
|
26
|
+
SERVER_DEFAULT = "Server default"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _recallic_post(base_url: str, api_key: str, path: str, body: dict, timeout: float) -> dict:
|
|
30
|
+
"""POST to Recallic and return the response's `data`. Uses the SDK when installed."""
|
|
31
|
+
body = {k: v for k, v in body.items() if v is not None}
|
|
32
|
+
try:
|
|
33
|
+
from recallic.client import MemoryClient
|
|
34
|
+
except ImportError:
|
|
35
|
+
MemoryClient = None # noqa: N806
|
|
36
|
+
if MemoryClient is not None:
|
|
37
|
+
return MemoryClient(api_key, base_url, timeout).request("POST", path, data=body)
|
|
38
|
+
req = urllib.request.Request(
|
|
39
|
+
base_url.rstrip("/") + path,
|
|
40
|
+
data=json.dumps(body).encode(),
|
|
41
|
+
headers={"X-API-Key": api_key, "Content-Type": "application/json"},
|
|
42
|
+
method="POST",
|
|
43
|
+
)
|
|
44
|
+
try:
|
|
45
|
+
with urllib.request.urlopen(req, timeout=timeout) as res: # noqa: S310 (the URL is the user's server)
|
|
46
|
+
return json.loads(res.read().decode()).get("data")
|
|
47
|
+
except urllib.error.HTTPError as e:
|
|
48
|
+
detail = e.read().decode(errors="replace")
|
|
49
|
+
msg = f"Recallic returned HTTP {e.code}: {detail[:300]}"
|
|
50
|
+
raise RuntimeError(msg) from e
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _user(user_id: Any) -> str | None:
|
|
54
|
+
text = str(user_id or "").strip()
|
|
55
|
+
return text or None
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _when(ts: Any) -> str:
|
|
59
|
+
try:
|
|
60
|
+
return datetime.fromtimestamp(float(ts), tz=timezone.utc).strftime("%d %b %Y")
|
|
61
|
+
except (TypeError, ValueError):
|
|
62
|
+
return ""
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _answered(out: dict) -> bool:
|
|
66
|
+
return out.get("status") == "CONSISTENT" and bool(out.get("memories"))
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def format_memories(out: dict, limit: int = 5) -> str:
|
|
70
|
+
"""A Recallic recall result as text for a model: the memories, or the no-record note."""
|
|
71
|
+
if not _answered(out):
|
|
72
|
+
return DECLINE_NOTE
|
|
73
|
+
lines = [HEADER]
|
|
74
|
+
for m in out["memories"][:limit]:
|
|
75
|
+
when = _when(m.get("timestamp"))
|
|
76
|
+
lines.append(f"- {('[' + when + '] ') if when else ''}{m.get('content_resolved') or m.get('content', '')}")
|
|
77
|
+
computed = out.get("computed_answer")
|
|
78
|
+
if computed and computed.get("text"):
|
|
79
|
+
lines.append(
|
|
80
|
+
f"Worked out from these memories: {computed['text']} ({(out.get('receipt') or {}).get('computed', '')})."
|
|
81
|
+
)
|
|
82
|
+
return "\n".join(lines)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class RecallicRecallComponent(Component):
|
|
86
|
+
display_name = "Recallic Recall"
|
|
87
|
+
description = (
|
|
88
|
+
"Looks up what this user told you before, from a self-hosted Recallic server. When no stored memory "
|
|
89
|
+
"answers the question, returns a note saying there is no record instead of near-misses."
|
|
90
|
+
)
|
|
91
|
+
documentation: str = "https://github.com/Recallic/Recallic/tree/main/integrations/langflow"
|
|
92
|
+
icon = "brain"
|
|
93
|
+
name = "RecallicRecall"
|
|
94
|
+
|
|
95
|
+
inputs = [
|
|
96
|
+
MessageTextInput(
|
|
97
|
+
name="query",
|
|
98
|
+
display_name="Query",
|
|
99
|
+
info="What to look up, usually the user's latest message.",
|
|
100
|
+
tool_mode=True,
|
|
101
|
+
required=True,
|
|
102
|
+
),
|
|
103
|
+
MessageTextInput(
|
|
104
|
+
name="recallic_user_id",
|
|
105
|
+
display_name="User ID",
|
|
106
|
+
info="Whose memory to read. Needed with a service key; leave empty with a user's own key.",
|
|
107
|
+
),
|
|
108
|
+
StrInput(
|
|
109
|
+
name="recallic_url",
|
|
110
|
+
display_name="Recallic URL",
|
|
111
|
+
value="http://localhost:8000",
|
|
112
|
+
info="Your Recallic server.",
|
|
113
|
+
),
|
|
114
|
+
SecretStrInput(
|
|
115
|
+
name="api_key",
|
|
116
|
+
display_name="Recallic API Key",
|
|
117
|
+
info="A Recallic key. Role 'service' acts for the User ID; a user key acts for itself.",
|
|
118
|
+
required=True,
|
|
119
|
+
),
|
|
120
|
+
StrInput(
|
|
121
|
+
name="agent_id",
|
|
122
|
+
display_name="Agent ID",
|
|
123
|
+
value="langflow",
|
|
124
|
+
info="Recallic agent id. Each agent reads back what it wrote, and what any agent shared.",
|
|
125
|
+
advanced=True,
|
|
126
|
+
),
|
|
127
|
+
DropdownInput(
|
|
128
|
+
name="mode",
|
|
129
|
+
display_name="Mode",
|
|
130
|
+
options=[SERVER_DEFAULT, "cautious", "balanced", "helpful"],
|
|
131
|
+
value=SERVER_DEFAULT,
|
|
132
|
+
info="'cautious' declines more (more \"I don't know\"); 'helpful' answers more counting, ordering "
|
|
133
|
+
"and 'how long between' questions.",
|
|
134
|
+
advanced=True,
|
|
135
|
+
),
|
|
136
|
+
IntInput(name="limit", display_name="Max Memories", value=5, advanced=True),
|
|
137
|
+
IntInput(name="timeout", display_name="Timeout (seconds)", value=30, advanced=True),
|
|
138
|
+
]
|
|
139
|
+
|
|
140
|
+
outputs = [
|
|
141
|
+
Output(display_name="Memories", name="memories", method="recall_message"),
|
|
142
|
+
Output(display_name="Memory Data", name="memory_data", method="recall_data"),
|
|
143
|
+
]
|
|
144
|
+
|
|
145
|
+
def _recall(self) -> dict:
|
|
146
|
+
"""One recall per run, shared by both outputs."""
|
|
147
|
+
if not str(self.query or "").strip():
|
|
148
|
+
return {"status": "ABSTAINED", "memories": []}
|
|
149
|
+
mode = None if self.mode in (None, "", SERVER_DEFAULT) else self.mode
|
|
150
|
+
body = {
|
|
151
|
+
"query": str(self.query or "")[:2000],
|
|
152
|
+
"limit": int(self.limit or 5),
|
|
153
|
+
"agent_id": self.agent_id or "langflow",
|
|
154
|
+
"user_id": _user(self.recallic_user_id),
|
|
155
|
+
"mode": mode,
|
|
156
|
+
}
|
|
157
|
+
key = json.dumps([self.recallic_url, body], sort_keys=True)
|
|
158
|
+
cached = getattr(self, "_recallic_cache", None)
|
|
159
|
+
if cached and cached[0] == key:
|
|
160
|
+
return cached[1]
|
|
161
|
+
out = _recallic_post(self.recallic_url, self.api_key, "/memory/recall", body, float(self.timeout or 30))
|
|
162
|
+
self._recallic_cache = (key, out)
|
|
163
|
+
return out
|
|
164
|
+
|
|
165
|
+
async def recall_message(self) -> Message:
|
|
166
|
+
out = await asyncio.to_thread(self._recall)
|
|
167
|
+
text = format_memories(out, int(self.limit or 5))
|
|
168
|
+
self.status = text
|
|
169
|
+
return Message(text=text)
|
|
170
|
+
|
|
171
|
+
async def recall_data(self) -> list[Data]:
|
|
172
|
+
out = await asyncio.to_thread(self._recall)
|
|
173
|
+
if not _answered(out):
|
|
174
|
+
self.status = DECLINE_NOTE
|
|
175
|
+
return []
|
|
176
|
+
rows = [
|
|
177
|
+
Data(
|
|
178
|
+
text=m.get("content_resolved") or m.get("content", ""),
|
|
179
|
+
data={
|
|
180
|
+
"id": m.get("id"),
|
|
181
|
+
"timestamp": m.get("timestamp"),
|
|
182
|
+
"score": m.get("score"),
|
|
183
|
+
"agent_id": m.get("agent_id"),
|
|
184
|
+
"source": "recallic",
|
|
185
|
+
},
|
|
186
|
+
)
|
|
187
|
+
for m in out["memories"][: int(self.limit or 5)]
|
|
188
|
+
]
|
|
189
|
+
self.status = rows
|
|
190
|
+
return rows
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""Recallic Save: a Langflow component that stores what a user told your flow, so later runs can recall it.
|
|
2
|
+
|
|
3
|
+
Self-contained on purpose: this one file can be pasted into Langflow's custom component editor or dropped into a
|
|
4
|
+
components folder. It uses the `recallic` Python SDK when it is installed and plain HTTP otherwise.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import asyncio
|
|
10
|
+
import json
|
|
11
|
+
import urllib.error
|
|
12
|
+
import urllib.request
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
from lfx.custom.custom_component.component import Component
|
|
16
|
+
from lfx.io import DropdownInput, IntInput, MessageTextInput, Output, SecretStrInput, StrInput
|
|
17
|
+
from lfx.schema.message import Message
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _recallic_post(base_url: str, api_key: str, path: str, body: dict, timeout: float) -> dict:
|
|
21
|
+
"""POST to Recallic and return the response's `data`. Uses the SDK when installed."""
|
|
22
|
+
body = {k: v for k, v in body.items() if v is not None}
|
|
23
|
+
try:
|
|
24
|
+
from recallic.client import MemoryClient
|
|
25
|
+
except ImportError:
|
|
26
|
+
MemoryClient = None # noqa: N806
|
|
27
|
+
if MemoryClient is not None:
|
|
28
|
+
return MemoryClient(api_key, base_url, timeout).request("POST", path, data=body)
|
|
29
|
+
req = urllib.request.Request(
|
|
30
|
+
base_url.rstrip("/") + path,
|
|
31
|
+
data=json.dumps(body).encode(),
|
|
32
|
+
headers={"X-API-Key": api_key, "Content-Type": "application/json"},
|
|
33
|
+
method="POST",
|
|
34
|
+
)
|
|
35
|
+
try:
|
|
36
|
+
with urllib.request.urlopen(req, timeout=timeout) as res: # noqa: S310 (the URL is the user's server)
|
|
37
|
+
return json.loads(res.read().decode()).get("data")
|
|
38
|
+
except urllib.error.HTTPError as e:
|
|
39
|
+
detail = e.read().decode(errors="replace")
|
|
40
|
+
msg = f"Recallic returned HTTP {e.code}: {detail[:300]}"
|
|
41
|
+
raise RuntimeError(msg) from e
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _user(user_id: Any) -> str | None:
|
|
45
|
+
text = str(user_id or "").strip()
|
|
46
|
+
return text or None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class RecallicSaveComponent(Component):
|
|
50
|
+
display_name = "Recallic Save"
|
|
51
|
+
description = (
|
|
52
|
+
"Stores what the user said (or a fact about them) in a self-hosted Recallic server, so later runs "
|
|
53
|
+
"can recall it. No LLM calls."
|
|
54
|
+
)
|
|
55
|
+
documentation: str = "https://github.com/Recallic/Recallic/tree/main/integrations/langflow"
|
|
56
|
+
icon = "brain"
|
|
57
|
+
name = "RecallicSave"
|
|
58
|
+
|
|
59
|
+
inputs = [
|
|
60
|
+
MessageTextInput(
|
|
61
|
+
name="message",
|
|
62
|
+
display_name="Message",
|
|
63
|
+
info="What to remember, in the user's words, e.g. the user's chat message.",
|
|
64
|
+
tool_mode=True,
|
|
65
|
+
required=True,
|
|
66
|
+
),
|
|
67
|
+
MessageTextInput(
|
|
68
|
+
name="recallic_user_id",
|
|
69
|
+
display_name="User ID",
|
|
70
|
+
info="Whose memory to write. Needed with a service key; leave empty with a user's own key.",
|
|
71
|
+
),
|
|
72
|
+
StrInput(
|
|
73
|
+
name="recallic_url",
|
|
74
|
+
display_name="Recallic URL",
|
|
75
|
+
value="http://localhost:8000",
|
|
76
|
+
info="Your Recallic server.",
|
|
77
|
+
),
|
|
78
|
+
SecretStrInput(
|
|
79
|
+
name="api_key",
|
|
80
|
+
display_name="Recallic API Key",
|
|
81
|
+
info="A Recallic key. Role 'service' acts for the User ID; a user key acts for itself.",
|
|
82
|
+
required=True,
|
|
83
|
+
),
|
|
84
|
+
StrInput(
|
|
85
|
+
name="agent_id",
|
|
86
|
+
display_name="Agent ID",
|
|
87
|
+
value="langflow",
|
|
88
|
+
info="Recallic agent id. Each agent reads back what it wrote, and what any agent shared.",
|
|
89
|
+
advanced=True,
|
|
90
|
+
),
|
|
91
|
+
DropdownInput(
|
|
92
|
+
name="memory_type",
|
|
93
|
+
display_name="Memory Type",
|
|
94
|
+
options=["private", "shared"],
|
|
95
|
+
value="private",
|
|
96
|
+
info="'private': only this agent reads it back. 'shared': all of this user's agents can.",
|
|
97
|
+
advanced=True,
|
|
98
|
+
),
|
|
99
|
+
IntInput(name="timeout", display_name="Timeout (seconds)", value=30, advanced=True),
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
outputs = [
|
|
103
|
+
Output(display_name="Status", name="status_message", method="save_message"),
|
|
104
|
+
]
|
|
105
|
+
|
|
106
|
+
def _save(self) -> dict:
|
|
107
|
+
body = {
|
|
108
|
+
"content": str(self.message or "")[:20000],
|
|
109
|
+
"agent_id": self.agent_id or "langflow",
|
|
110
|
+
"memory_type": self.memory_type or "private",
|
|
111
|
+
"user_id": _user(self.recallic_user_id),
|
|
112
|
+
"metadata": {"speaker": "User", "source": "langflow"},
|
|
113
|
+
}
|
|
114
|
+
return _recallic_post(self.recallic_url, self.api_key, "/memory/store", body, float(self.timeout or 30))
|
|
115
|
+
|
|
116
|
+
async def save_message(self) -> Message:
|
|
117
|
+
if not str(self.message or "").strip():
|
|
118
|
+
text = "Nothing to store (empty message)."
|
|
119
|
+
else:
|
|
120
|
+
status = (await asyncio.to_thread(self._save) or {}).get("status")
|
|
121
|
+
text = {"stored": "Saved.", "skipped_duplicate": "Already remembered."}.get(
|
|
122
|
+
status, f"Not stored ({status})."
|
|
123
|
+
)
|
|
124
|
+
self.status = text
|
|
125
|
+
return Message(text=text)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://schemas.langflow.org/extension/v1.json",
|
|
3
|
+
"id": "recallic",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"name": "Recallic",
|
|
6
|
+
"description": "Long-term memory for flows from a self-hosted Recallic server: recall what a user told you before (or get a no-record note), and save new facts. No LLM calls for memory.",
|
|
7
|
+
"lfx": {"compat": ["1"]},
|
|
8
|
+
"bundles": [
|
|
9
|
+
{
|
|
10
|
+
"name": "recallic",
|
|
11
|
+
"path": "components/recallic",
|
|
12
|
+
"display_name": "Recallic",
|
|
13
|
+
"icon": "brain"
|
|
14
|
+
}
|
|
15
|
+
]
|
|
16
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Live tests against a running Recallic:
|
|
3
|
+
RECALLIC_URL=http://127.0.0.1:8000 RECALLIC_SERVICE_KEY=rx_... pytest tests
|
|
4
|
+
|
|
5
|
+
They build the real Langflow component classes, call their output methods as a flow run does, and check the three
|
|
6
|
+
ways Langflow loads them: installed extension, components folder, and pasted custom-component code.
|
|
7
|
+
"""
|
|
8
|
+
import asyncio
|
|
9
|
+
import os
|
|
10
|
+
import sys
|
|
11
|
+
import uuid
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
import pytest
|
|
15
|
+
from langflow_recallic import DECLINE_NOTE, RecallicRecallComponent, RecallicSaveComponent
|
|
16
|
+
from lfx.schema.message import Message
|
|
17
|
+
|
|
18
|
+
URL, KEY = os.environ.get("RECALLIC_URL"), os.environ.get("RECALLIC_SERVICE_KEY")
|
|
19
|
+
pytestmark = pytest.mark.skipif(not (URL and KEY), reason="needs RECALLIC_URL and RECALLIC_SERVICE_KEY")
|
|
20
|
+
|
|
21
|
+
PKG = Path(__file__).resolve().parents[1] / "src" / "langflow_recallic"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _user():
|
|
25
|
+
return f"lf-{uuid.uuid4().hex[:8]}"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _conn(user, **kw):
|
|
29
|
+
return dict(recallic_user_id=user, recallic_url=URL, api_key=KEY, agent_id="langflow-test", timeout=120, **kw)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _save(user, text, cls=RecallicSaveComponent):
|
|
33
|
+
return asyncio.run(cls(**_conn(user), message=text).save_message())
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _recall(user, query, cls=RecallicRecallComponent, **kw):
|
|
37
|
+
comp = cls(**_conn(user, **kw), query=query)
|
|
38
|
+
|
|
39
|
+
async def both():
|
|
40
|
+
return await comp.recall_message(), await comp.recall_data()
|
|
41
|
+
|
|
42
|
+
return asyncio.run(both())
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_save_recall_and_decline():
|
|
46
|
+
user = _user()
|
|
47
|
+
saved = _save(user, "I moved to Pune in March, and my sister Mira is a pediatrician in Goa.")
|
|
48
|
+
assert isinstance(saved, Message) and saved.text == "Saved."
|
|
49
|
+
assert _save(user, "I moved to Pune in March, and my sister Mira is a pediatrician in Goa.").text \
|
|
50
|
+
== "Already remembered."
|
|
51
|
+
|
|
52
|
+
msg, rows = _recall(user, "Which city did I move to?")
|
|
53
|
+
assert isinstance(msg, Message) and "Pune" in msg.text
|
|
54
|
+
assert rows and "Pune" in rows[0].get_text() and rows[0].data["source"] == "recallic"
|
|
55
|
+
|
|
56
|
+
msg, rows = _recall(user, "What is my brother's name?", mode="cautious")
|
|
57
|
+
assert msg.text == DECLINE_NOTE and "don't have a record" in msg.text
|
|
58
|
+
assert rows == []
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_users_are_kept_apart():
|
|
62
|
+
a, b = _user(), _user()
|
|
63
|
+
_save(a, "My dog is called Bruno.")
|
|
64
|
+
msg, rows = _recall(b, "What is my dog called?")
|
|
65
|
+
assert "Bruno" not in msg.text and rows == []
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_empty_inputs_make_no_call():
|
|
69
|
+
assert _save(_user(), " ").text.startswith("Nothing to store")
|
|
70
|
+
msg, rows = _recall(_user(), "")
|
|
71
|
+
assert msg.text == DECLINE_NOTE and rows == []
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def test_works_without_the_sdk(monkeypatch):
|
|
75
|
+
"""Pasted into a Langflow that lacks the `recallic` package, the components fall back to plain HTTP."""
|
|
76
|
+
monkeypatch.setitem(sys.modules, "recallic.client", None) # makes `from recallic.client import ...` fail
|
|
77
|
+
user = _user()
|
|
78
|
+
assert _save(user, "I work at a bakery in Lisbon.").text == "Saved."
|
|
79
|
+
msg, _ = _recall(user, "Where do I work?")
|
|
80
|
+
assert "Lisbon" in msg.text
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_loads_as_installed_extension_and_components_folder():
|
|
84
|
+
from lfx.extension.loader import discover_inline_bundles, load_extension
|
|
85
|
+
|
|
86
|
+
ext = load_extension(PKG)
|
|
87
|
+
assert not ext.errors
|
|
88
|
+
assert {c.class_name for c in ext.components} == {"RecallicRecallComponent", "RecallicSaveComponent"}
|
|
89
|
+
folder = discover_inline_bundles([PKG / "components"])
|
|
90
|
+
assert {c.class_name for r in folder for c in r.components} == {"RecallicRecallComponent",
|
|
91
|
+
"RecallicSaveComponent"}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def test_pasted_code_builds_and_runs():
|
|
95
|
+
"""What Langflow's custom component editor does with pasted code: build the class from source, then run it."""
|
|
96
|
+
from lfx.custom.validate import create_class, extract_class_name
|
|
97
|
+
|
|
98
|
+
user = _user()
|
|
99
|
+
classes = {}
|
|
100
|
+
for name in ("recallic_save.py", "recallic_recall.py"):
|
|
101
|
+
code = (PKG / "components" / "recallic" / name).read_text()
|
|
102
|
+
classes[name] = create_class(code, extract_class_name(code))
|
|
103
|
+
assert _save(user, "My favourite tea is masala chai.", cls=classes["recallic_save.py"]).text == "Saved."
|
|
104
|
+
msg, _ = _recall(user, "What is my favourite tea?", cls=classes["recallic_recall.py"])
|
|
105
|
+
assert "masala chai" in msg.text
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def test_frontend_node_hides_the_key():
|
|
109
|
+
comp = RecallicRecallComponent(**_conn(_user()), query="x")
|
|
110
|
+
node = comp.to_frontend_node()
|
|
111
|
+
field = node["data"]["node"]["template"]["api_key"]
|
|
112
|
+
assert field["password"] is True
|
|
113
|
+
assert {o["name"] for o in node["data"]["node"]["outputs"]} == {"memories", "memory_data"}
|