hindsight-pipecat 0.1.1__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.
- hindsight_pipecat-0.1.1/.gitignore +60 -0
- hindsight_pipecat-0.1.1/CHANGELOG.md +11 -0
- hindsight_pipecat-0.1.1/LICENSE +21 -0
- hindsight_pipecat-0.1.1/PKG-INFO +22 -0
- hindsight_pipecat-0.1.1/README.md +109 -0
- hindsight_pipecat-0.1.1/examples/basic_pipeline.py +98 -0
- hindsight_pipecat-0.1.1/examples/interactive_chat.py +212 -0
- hindsight_pipecat-0.1.1/hindsight_pipecat/__init__.py +46 -0
- hindsight_pipecat-0.1.1/hindsight_pipecat/config.py +72 -0
- hindsight_pipecat-0.1.1/hindsight_pipecat/errors.py +7 -0
- hindsight_pipecat-0.1.1/hindsight_pipecat/memory.py +255 -0
- hindsight_pipecat-0.1.1/pyproject.toml +55 -0
- hindsight_pipecat-0.1.1/tests/__init__.py +0 -0
- hindsight_pipecat-0.1.1/tests/test_live_integration.py +211 -0
- hindsight_pipecat-0.1.1/tests/test_memory.py +319 -0
- hindsight_pipecat-0.1.1/uv.lock +2529 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Python-generated files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[oc]
|
|
4
|
+
build/
|
|
5
|
+
dist/
|
|
6
|
+
wheels/
|
|
7
|
+
*.egg-info
|
|
8
|
+
.mcp.json
|
|
9
|
+
.osgrep
|
|
10
|
+
# Virtual environments
|
|
11
|
+
.venv
|
|
12
|
+
|
|
13
|
+
# Node
|
|
14
|
+
node_modules/
|
|
15
|
+
|
|
16
|
+
# Environment variables and local config
|
|
17
|
+
.env
|
|
18
|
+
docker-compose.yml
|
|
19
|
+
docker-compose.override.yml
|
|
20
|
+
|
|
21
|
+
# IDE
|
|
22
|
+
.idea/
|
|
23
|
+
.vscode/
|
|
24
|
+
*.swp
|
|
25
|
+
*.swo
|
|
26
|
+
|
|
27
|
+
# NLTK data (will be downloaded automatically)
|
|
28
|
+
nltk_data/
|
|
29
|
+
|
|
30
|
+
# Monitoring stack (Prometheus/Grafana binaries and data)
|
|
31
|
+
.monitoring/
|
|
32
|
+
.pgbouncer/
|
|
33
|
+
|
|
34
|
+
# Large benchmark datasets (will be downloaded automatically)
|
|
35
|
+
**/longmemeval_s_cleaned.json
|
|
36
|
+
|
|
37
|
+
# Debug logs
|
|
38
|
+
logs/
|
|
39
|
+
|
|
40
|
+
.DS_Store
|
|
41
|
+
|
|
42
|
+
# Generated docs files
|
|
43
|
+
hindsight-docs/static/llms-full.txt
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
hindsight-dev/benchmarks/locomo/results/
|
|
47
|
+
hindsight-dev/benchmarks/longmemeval/results/
|
|
48
|
+
hindsight-dev/benchmarks/consolidation/results/
|
|
49
|
+
hindsight-dev/benchmarks/perf/results/
|
|
50
|
+
benchmarks/results/
|
|
51
|
+
hindsight-cli/target
|
|
52
|
+
hindsight-clients/rust/target
|
|
53
|
+
.claude/*
|
|
54
|
+
!.claude/skills/
|
|
55
|
+
whats-next.md
|
|
56
|
+
TASK.md
|
|
57
|
+
# Changelog is now tracked in hindsight-docs/src/pages/changelog.md
|
|
58
|
+
# CHANGELOG.md
|
|
59
|
+
|
|
60
|
+
blog-post*
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-04-07)
|
|
4
|
+
|
|
5
|
+
- Initial release: `HindsightMemoryService` FrameProcessor for Pipecat voice AI pipelines
|
|
6
|
+
- Retain/recall/inject loop on each `LLMContextFrame`
|
|
7
|
+
- Fire-and-forget async retain after each complete user+assistant turn
|
|
8
|
+
- Memory injected as `<hindsight_memories>` system message before LLM call
|
|
9
|
+
- Supports both `LLMContextFrame` and deprecated `OpenAILLMContextFrame`
|
|
10
|
+
- Configurable recall budget (`low`, `mid`, `high`) and token limit
|
|
11
|
+
- Global `configure()` helper for shared connection settings
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Vectorize AI, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hindsight-pipecat
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Pipecat integration for Hindsight - persistent memory for voice AI pipelines
|
|
5
|
+
Project-URL: Homepage, https://github.com/vectorize-io/hindsight
|
|
6
|
+
Project-URL: Documentation, https://github.com/vectorize-io/hindsight/tree/main/hindsight-integrations/pipecat
|
|
7
|
+
Project-URL: Repository, https://github.com/vectorize-io/hindsight
|
|
8
|
+
Author-email: Vectorize <support@vectorize.io>
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: agents,ai,hindsight,memory,pipecat,voice
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Requires-Dist: hindsight-client>=0.4.0
|
|
22
|
+
Requires-Dist: pipecat-ai>=0.0.100
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Hindsight Pipecat Integration
|
|
2
|
+
|
|
3
|
+
Persistent long-term memory for [Pipecat](https://github.com/pipecat-ai/pipecat) voice AI pipelines via [Hindsight](https://vectorize.io/hindsight). A single `FrameProcessor` slots between your user context aggregator and LLM service — recalling relevant memories before each turn and retaining conversation content after.
|
|
4
|
+
|
|
5
|
+
## Quick Start
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install hindsight-pipecat
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
from pipecat.pipeline.pipeline import Pipeline
|
|
13
|
+
from hindsight_pipecat import HindsightMemoryService
|
|
14
|
+
|
|
15
|
+
memory = HindsightMemoryService(
|
|
16
|
+
bank_id="user-123",
|
|
17
|
+
hindsight_api_url="http://localhost:8888",
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
pipeline = Pipeline([
|
|
21
|
+
transport.input(),
|
|
22
|
+
stt_service,
|
|
23
|
+
user_aggregator,
|
|
24
|
+
memory, # ← add between user_aggregator and LLM
|
|
25
|
+
llm_service,
|
|
26
|
+
assistant_aggregator,
|
|
27
|
+
tts_service,
|
|
28
|
+
transport.output(),
|
|
29
|
+
])
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Or with [Hindsight Cloud](https://ui.hindsight.vectorize.io/signup):
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
memory = HindsightMemoryService(
|
|
36
|
+
bank_id="user-123",
|
|
37
|
+
hindsight_api_url="https://api.hindsight.vectorize.io",
|
|
38
|
+
api_key="hsk_your_token_here",
|
|
39
|
+
)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## How It Works
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
New turn starts
|
|
46
|
+
└─ OpenAILLMContextFrame arrives
|
|
47
|
+
├─ Retain previous complete turn (user+assistant) — fire-and-forget
|
|
48
|
+
└─ Recall relevant memories for current user query
|
|
49
|
+
└─ Inject as <hindsight_memories> system message
|
|
50
|
+
└─ Forward enriched context to LLM
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
On each `OpenAILLMContextFrame`:
|
|
54
|
+
|
|
55
|
+
1. **Retain** — any new complete user+assistant turn pairs are sent to Hindsight asynchronously (non-blocking)
|
|
56
|
+
2. **Recall** — the latest user message is used as the search query; results are injected as a system message before the LLM sees the context
|
|
57
|
+
3. **Forward** — the enriched context frame is pushed downstream
|
|
58
|
+
|
|
59
|
+
Memory accumulates across calls. By the third or fourth turn, recall starts surfacing useful context that the pipeline didn't have to re-establish.
|
|
60
|
+
|
|
61
|
+
## Prerequisites
|
|
62
|
+
|
|
63
|
+
A running Hindsight instance:
|
|
64
|
+
|
|
65
|
+
**Self-hosted:**
|
|
66
|
+
```bash
|
|
67
|
+
pip install hindsight-all
|
|
68
|
+
export HINDSIGHT_API_LLM_API_KEY=your-api-key
|
|
69
|
+
hindsight-api # starts on http://localhost:8888
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**Hindsight Cloud:** [Sign up](https://ui.hindsight.vectorize.io/signup) — no self-hosting required.
|
|
73
|
+
|
|
74
|
+
## Configuration
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
HindsightMemoryService(
|
|
78
|
+
bank_id="user-123", # Required: memory bank to use
|
|
79
|
+
hindsight_api_url="...", # Hindsight API URL
|
|
80
|
+
api_key="hsk_...", # API key (Hindsight Cloud)
|
|
81
|
+
recall_budget="mid", # "low", "mid", or "high"
|
|
82
|
+
recall_max_tokens=4096, # Max tokens for recall results
|
|
83
|
+
enable_recall=True, # Inject memories before LLM
|
|
84
|
+
enable_retain=True, # Store turns after each exchange
|
|
85
|
+
memory_prefix="Relevant memories from past conversations:\n",
|
|
86
|
+
)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Global configuration
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from hindsight_pipecat import configure
|
|
93
|
+
|
|
94
|
+
configure(
|
|
95
|
+
hindsight_api_url="http://localhost:8888",
|
|
96
|
+
api_key="hsk_...",
|
|
97
|
+
recall_budget="mid",
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
# Now create services without repeating connection details
|
|
101
|
+
memory = HindsightMemoryService(bank_id="user-123")
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Running Tests
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
pip install pytest pytest-asyncio
|
|
108
|
+
pytest tests/ -v
|
|
109
|
+
```
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""Basic Pipecat pipeline with Hindsight persistent memory.
|
|
2
|
+
|
|
3
|
+
This example shows how to add long-term memory to a voice AI pipeline using
|
|
4
|
+
HindsightMemoryService. The processor slots between the user context aggregator
|
|
5
|
+
and the LLM service — recalling relevant memories before each turn and retaining
|
|
6
|
+
conversation content after.
|
|
7
|
+
|
|
8
|
+
Requirements:
|
|
9
|
+
pip install pipecat-hindsight pipecat-ai[deepgram,openai,cartesia,silero,daily]
|
|
10
|
+
|
|
11
|
+
Environment variables:
|
|
12
|
+
DEEPGRAM_API_KEY - Deepgram speech-to-text key
|
|
13
|
+
OPENAI_API_KEY - OpenAI API key
|
|
14
|
+
CARTESIA_API_KEY - Cartesia text-to-speech key
|
|
15
|
+
DAILY_API_KEY - Daily.co WebRTC transport key
|
|
16
|
+
HINDSIGHT_API_URL - Hindsight API URL (default: http://localhost:8888)
|
|
17
|
+
HINDSIGHT_API_KEY - Hindsight API key (required for Hindsight Cloud)
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import asyncio
|
|
21
|
+
import os
|
|
22
|
+
|
|
23
|
+
from pipecat.audio.vad.silero import SileroVADAnalyzer
|
|
24
|
+
from pipecat.pipeline.pipeline import Pipeline
|
|
25
|
+
from pipecat.pipeline.runner import PipelineRunner
|
|
26
|
+
from pipecat.pipeline.task import PipelineParams, PipelineTask
|
|
27
|
+
from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
|
|
28
|
+
from pipecat.services.cartesia.tts import CartesiaTTSService
|
|
29
|
+
from pipecat.services.deepgram.stt import DeepgramSTTService
|
|
30
|
+
from pipecat.services.openai.llm import OpenAILLMService
|
|
31
|
+
from pipecat.transports.services.daily import DailyParams, DailyTransport
|
|
32
|
+
|
|
33
|
+
from hindsight_pipecat import HindsightMemoryService
|
|
34
|
+
|
|
35
|
+
SYSTEM_PROMPT = """You are a friendly voice assistant with long-term memory.
|
|
36
|
+
You remember details from past conversations and use them naturally.
|
|
37
|
+
Keep responses concise — this is a voice interface."""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
async def main() -> None:
|
|
41
|
+
transport = DailyTransport(
|
|
42
|
+
room_url=os.environ["DAILY_ROOM_URL"],
|
|
43
|
+
token=os.environ.get("DAILY_TOKEN", ""),
|
|
44
|
+
bot_name="Hindsight Bot",
|
|
45
|
+
params=DailyParams(
|
|
46
|
+
audio_out_enabled=True,
|
|
47
|
+
transcription_enabled=False,
|
|
48
|
+
vad_enabled=True,
|
|
49
|
+
vad_analyzer=SileroVADAnalyzer(),
|
|
50
|
+
vad_audio_passthrough=True,
|
|
51
|
+
),
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
stt = DeepgramSTTService(api_key=os.environ["DEEPGRAM_API_KEY"])
|
|
55
|
+
tts = CartesiaTTSService(
|
|
56
|
+
api_key=os.environ["CARTESIA_API_KEY"],
|
|
57
|
+
voice_id="79a125e8-cd45-4c13-8a67-188112f4dd22", # British Reading Lady
|
|
58
|
+
)
|
|
59
|
+
llm = OpenAILLMService(api_key=os.environ["OPENAI_API_KEY"], model="gpt-4o-mini")
|
|
60
|
+
|
|
61
|
+
# HindsightMemoryService — add between user aggregator and LLM.
|
|
62
|
+
# bank_id should be stable per user so memories persist across sessions.
|
|
63
|
+
memory = HindsightMemoryService(
|
|
64
|
+
bank_id="demo-user-001",
|
|
65
|
+
hindsight_api_url=os.environ.get("HINDSIGHT_API_URL", "http://localhost:8888"),
|
|
66
|
+
api_key=os.environ.get("HINDSIGHT_API_KEY"),
|
|
67
|
+
recall_budget="mid",
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
context = OpenAILLMContext(messages=[{"role": "system", "content": SYSTEM_PROMPT}])
|
|
71
|
+
context_aggregator = llm.create_context_aggregator(context)
|
|
72
|
+
|
|
73
|
+
pipeline = Pipeline(
|
|
74
|
+
[
|
|
75
|
+
transport.input(),
|
|
76
|
+
stt,
|
|
77
|
+
context_aggregator.user(),
|
|
78
|
+
memory, # ← recall before LLM, retain after each turn
|
|
79
|
+
llm,
|
|
80
|
+
tts,
|
|
81
|
+
transport.output(),
|
|
82
|
+
context_aggregator.assistant(),
|
|
83
|
+
]
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
task = PipelineTask(pipeline, params=PipelineParams(allow_interruptions=True))
|
|
87
|
+
|
|
88
|
+
@transport.event_handler("on_first_participant_joined")
|
|
89
|
+
async def on_first_participant_joined(transport, participant): # type: ignore[misc]
|
|
90
|
+
await transport.capture_participant_transcription(participant["id"])
|
|
91
|
+
await task.queue_frames([context_aggregator.user().get_context_frame()])
|
|
92
|
+
|
|
93
|
+
runner = PipelineRunner()
|
|
94
|
+
await runner.run(task)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
if __name__ == "__main__":
|
|
98
|
+
asyncio.run(main())
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"""Interactive text chat — test HindsightMemoryService by hand.
|
|
2
|
+
|
|
3
|
+
Simulates the Pipecat pipeline at text level so you can have a real conversation
|
|
4
|
+
and watch memory recall/retain happen in real time.
|
|
5
|
+
|
|
6
|
+
Usage:
|
|
7
|
+
export OPENAI_API_KEY=sk-... # optional; falls back to fake assistant
|
|
8
|
+
python examples/interactive_chat.py --bank demo-$USER
|
|
9
|
+
python examples/interactive_chat.py --bank demo-$USER --hindsight-url http://localhost:8888
|
|
10
|
+
|
|
11
|
+
Each turn shows:
|
|
12
|
+
[RECALL] — what Hindsight returned for the current user query
|
|
13
|
+
[INJECT] — the <hindsight_memories> system message added to the LLM context
|
|
14
|
+
[LLM] — the assistant's response (OpenAI or manual)
|
|
15
|
+
[RETAIN] — complete user+assistant turn sent to Hindsight
|
|
16
|
+
|
|
17
|
+
Commands:
|
|
18
|
+
:quit / :q — exit
|
|
19
|
+
:memories — dump all memories in the bank
|
|
20
|
+
:reset — clear context (keeps the Hindsight bank)
|
|
21
|
+
:bank — show current bank id
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
import argparse
|
|
27
|
+
import asyncio
|
|
28
|
+
import json
|
|
29
|
+
import os
|
|
30
|
+
import sys
|
|
31
|
+
import urllib.request
|
|
32
|
+
from unittest.mock import MagicMock
|
|
33
|
+
|
|
34
|
+
# Ensure package is importable when run directly
|
|
35
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
36
|
+
|
|
37
|
+
from hindsight_pipecat import HindsightMemoryService
|
|
38
|
+
from hindsight_pipecat.memory import _MEMORY_MARKER
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
BLUE = "\033[94m"
|
|
42
|
+
GREEN = "\033[92m"
|
|
43
|
+
YELLOW = "\033[93m"
|
|
44
|
+
CYAN = "\033[96m"
|
|
45
|
+
DIM = "\033[2m"
|
|
46
|
+
RESET = "\033[0m"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def banner(label: str, color: str = CYAN) -> None:
|
|
50
|
+
print(f"{color}[{label}]{RESET}", end=" ")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _make_frame(messages: list[dict]) -> MagicMock:
|
|
54
|
+
"""Build a mock OpenAILLMContextFrame (Pipecat's text context carrier)."""
|
|
55
|
+
from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContextFrame
|
|
56
|
+
frame = MagicMock(spec=OpenAILLMContextFrame)
|
|
57
|
+
ctx = MagicMock()
|
|
58
|
+
ctx.messages = messages
|
|
59
|
+
frame.context = ctx
|
|
60
|
+
return frame
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
async def _run_turn(service: HindsightMemoryService, messages: list[dict]) -> list[dict]:
|
|
64
|
+
"""Send messages through the memory service; return possibly-mutated list."""
|
|
65
|
+
frame = _make_frame(messages)
|
|
66
|
+
service.push_frame = MagicMock(return_value=asyncio.sleep(0))
|
|
67
|
+
await service._handle_context_frame(frame)
|
|
68
|
+
return frame.context.messages
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def get_openai_response(messages: list[dict]) -> str | None:
|
|
72
|
+
"""Call OpenAI if available; otherwise return None (caller prompts manually)."""
|
|
73
|
+
try:
|
|
74
|
+
from openai import OpenAI
|
|
75
|
+
except ImportError:
|
|
76
|
+
return None
|
|
77
|
+
|
|
78
|
+
api_key = os.environ.get("OPENAI_API_KEY")
|
|
79
|
+
if not api_key:
|
|
80
|
+
return None
|
|
81
|
+
|
|
82
|
+
client = OpenAI(api_key=api_key)
|
|
83
|
+
resp = client.chat.completions.create(
|
|
84
|
+
model=os.environ.get("OPENAI_MODEL", "gpt-4o-mini"),
|
|
85
|
+
messages=messages, # type: ignore[arg-type]
|
|
86
|
+
max_tokens=200,
|
|
87
|
+
)
|
|
88
|
+
return resp.choices[0].message.content or ""
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def dump_memories(url: str, bank: str, api_key: str | None) -> None:
|
|
92
|
+
req = urllib.request.Request(f"{url}/v1/default/banks/{bank}/memories/list")
|
|
93
|
+
if api_key:
|
|
94
|
+
req.add_header("Authorization", f"Bearer {api_key}")
|
|
95
|
+
try:
|
|
96
|
+
with urllib.request.urlopen(req, timeout=10) as r:
|
|
97
|
+
data = json.loads(r.read())
|
|
98
|
+
items = data.get("items", [])
|
|
99
|
+
print(f"\n{YELLOW}=== Bank '{bank}' — {len(items)} memories ==={RESET}")
|
|
100
|
+
for i, m in enumerate(items, 1):
|
|
101
|
+
print(f"{i}. {m.get('text', '')[:200]}")
|
|
102
|
+
if not items:
|
|
103
|
+
print("(empty)")
|
|
104
|
+
print()
|
|
105
|
+
except Exception as e:
|
|
106
|
+
print(f"{YELLOW}Could not list memories: {e}{RESET}")
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
async def main() -> None:
|
|
110
|
+
parser = argparse.ArgumentParser()
|
|
111
|
+
parser.add_argument("--bank", default=f"demo-{os.environ.get('USER', 'anon')}")
|
|
112
|
+
parser.add_argument("--hindsight-url", default=os.environ.get("HINDSIGHT_API_URL", "http://localhost:8888"))
|
|
113
|
+
parser.add_argument("--hindsight-api-key", default=os.environ.get("HINDSIGHT_API_KEY"))
|
|
114
|
+
parser.add_argument("--system-prompt", default="You are a friendly voice assistant with long-term memory. Use memories naturally; don't dump them verbatim. Keep responses concise.")
|
|
115
|
+
args = parser.parse_args()
|
|
116
|
+
|
|
117
|
+
service = HindsightMemoryService(
|
|
118
|
+
bank_id=args.bank,
|
|
119
|
+
hindsight_api_url=args.hindsight_url,
|
|
120
|
+
api_key=args.hindsight_api_key,
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
messages: list[dict] = [{"role": "system", "content": args.system_prompt}]
|
|
124
|
+
|
|
125
|
+
use_openai = get_openai_response([{"role": "user", "content": "ping"}]) is not None
|
|
126
|
+
llm_mode = "OpenAI" if use_openai else "manual (no OPENAI_API_KEY)"
|
|
127
|
+
|
|
128
|
+
print(f"\n{GREEN}=== Pipecat + Hindsight Interactive Chat ==={RESET}")
|
|
129
|
+
print(f"Bank: {args.bank}")
|
|
130
|
+
print(f"Hindsight: {args.hindsight_url}")
|
|
131
|
+
print(f"LLM mode: {llm_mode}")
|
|
132
|
+
print(f"System: {args.system_prompt[:80]}...")
|
|
133
|
+
print(f"\nType a message. Commands: {DIM}:quit :memories :reset :bank{RESET}\n")
|
|
134
|
+
|
|
135
|
+
turn_number = 0
|
|
136
|
+
while True:
|
|
137
|
+
try:
|
|
138
|
+
user_input = input(f"{BLUE}you> {RESET}").strip()
|
|
139
|
+
except (EOFError, KeyboardInterrupt):
|
|
140
|
+
print()
|
|
141
|
+
break
|
|
142
|
+
|
|
143
|
+
if not user_input:
|
|
144
|
+
continue
|
|
145
|
+
if user_input in (":quit", ":q", ":exit"):
|
|
146
|
+
break
|
|
147
|
+
if user_input == ":memories":
|
|
148
|
+
dump_memories(args.hindsight_url, args.bank, args.hindsight_api_key)
|
|
149
|
+
continue
|
|
150
|
+
if user_input == ":reset":
|
|
151
|
+
messages = [{"role": "system", "content": args.system_prompt}]
|
|
152
|
+
service._last_retained_count = 0
|
|
153
|
+
print(f"{YELLOW}Context reset (Hindsight bank untouched){RESET}")
|
|
154
|
+
continue
|
|
155
|
+
if user_input == ":bank":
|
|
156
|
+
print(f"{YELLOW}bank: {args.bank}{RESET}")
|
|
157
|
+
continue
|
|
158
|
+
|
|
159
|
+
turn_number += 1
|
|
160
|
+
print(f"\n{DIM}── Turn {turn_number} ──{RESET}")
|
|
161
|
+
|
|
162
|
+
# Append the user message — the memory service will run retain/recall on this
|
|
163
|
+
messages.append({"role": "user", "content": user_input})
|
|
164
|
+
|
|
165
|
+
# Run through memory service (recall + inject + retain-any-prior-pair)
|
|
166
|
+
messages = await _run_turn(service, messages)
|
|
167
|
+
|
|
168
|
+
# Show what got recalled/injected
|
|
169
|
+
memory_msg = next(
|
|
170
|
+
(m for m in messages if m.get("role") == "system" and _MEMORY_MARKER in m.get("content", "")),
|
|
171
|
+
None,
|
|
172
|
+
)
|
|
173
|
+
if memory_msg:
|
|
174
|
+
banner("RECALL+INJECT", CYAN)
|
|
175
|
+
content = memory_msg["content"]
|
|
176
|
+
# Show just the memories, not the XML wrapper
|
|
177
|
+
inner = content.replace(_MEMORY_MARKER, "").replace("</hindsight_memories>", "").strip()
|
|
178
|
+
print(inner[:500] + ("..." if len(inner) > 500 else ""))
|
|
179
|
+
else:
|
|
180
|
+
banner("RECALL", DIM)
|
|
181
|
+
print(f"{DIM}(no memories yet — bank is fresh or query didn't match){RESET}")
|
|
182
|
+
|
|
183
|
+
# Get assistant response
|
|
184
|
+
if use_openai:
|
|
185
|
+
banner("LLM", GREEN)
|
|
186
|
+
response = get_openai_response(messages) or "(empty)"
|
|
187
|
+
print(response)
|
|
188
|
+
else:
|
|
189
|
+
banner("LLM (manual)", YELLOW)
|
|
190
|
+
print(f"{DIM}Type what the assistant should say (one line):{RESET}")
|
|
191
|
+
response = input(" ").strip()
|
|
192
|
+
if not response:
|
|
193
|
+
response = "(no response)"
|
|
194
|
+
|
|
195
|
+
messages.append({"role": "assistant", "content": response})
|
|
196
|
+
|
|
197
|
+
# Next turn, the service will retain this completed pair
|
|
198
|
+
banner("RETAIN", GREEN)
|
|
199
|
+
print(f"{DIM}scheduled — will fire on next turn's recall call{RESET}\n")
|
|
200
|
+
|
|
201
|
+
# On exit, run one more turn to flush the last retain
|
|
202
|
+
if turn_number > 0:
|
|
203
|
+
print(f"{DIM}Flushing final turn...{RESET}")
|
|
204
|
+
# A no-op user message to trigger retain of the last pair
|
|
205
|
+
await _run_turn(service, messages)
|
|
206
|
+
print(f"{DIM}Waiting 5s for async retain + fact extraction...{RESET}")
|
|
207
|
+
await asyncio.sleep(5)
|
|
208
|
+
dump_memories(args.hindsight_url, args.bank, args.hindsight_api_key)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
if __name__ == "__main__":
|
|
212
|
+
asyncio.run(main())
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""Hindsight-Pipecat: Persistent memory for voice AI pipelines.
|
|
2
|
+
|
|
3
|
+
Provides a Hindsight-backed FrameProcessor for Pipecat pipelines,
|
|
4
|
+
giving them long-term memory via automatic recall and retain.
|
|
5
|
+
|
|
6
|
+
Basic usage::
|
|
7
|
+
|
|
8
|
+
from pipecat.pipeline.pipeline import Pipeline
|
|
9
|
+
from hindsight_pipecat import HindsightMemoryService
|
|
10
|
+
|
|
11
|
+
memory = HindsightMemoryService(
|
|
12
|
+
bank_id="user-123",
|
|
13
|
+
hindsight_api_url="http://localhost:8888",
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
pipeline = Pipeline([
|
|
17
|
+
transport.input(),
|
|
18
|
+
stt_service,
|
|
19
|
+
user_aggregator,
|
|
20
|
+
memory, # recall before LLM, retain after each turn
|
|
21
|
+
llm_service,
|
|
22
|
+
assistant_aggregator,
|
|
23
|
+
tts_service,
|
|
24
|
+
transport.output(),
|
|
25
|
+
])
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from .config import (
|
|
29
|
+
HindsightPipecatConfig,
|
|
30
|
+
configure,
|
|
31
|
+
get_config,
|
|
32
|
+
reset_config,
|
|
33
|
+
)
|
|
34
|
+
from .errors import HindsightPipecatError
|
|
35
|
+
from .memory import HindsightMemoryService
|
|
36
|
+
|
|
37
|
+
__version__ = "0.1.0"
|
|
38
|
+
|
|
39
|
+
__all__ = [
|
|
40
|
+
"configure",
|
|
41
|
+
"get_config",
|
|
42
|
+
"reset_config",
|
|
43
|
+
"HindsightPipecatConfig",
|
|
44
|
+
"HindsightPipecatError",
|
|
45
|
+
"HindsightMemoryService",
|
|
46
|
+
]
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Global configuration for Hindsight-Pipecat integration."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
|
|
8
|
+
DEFAULT_HINDSIGHT_API_URL = "https://api.hindsight.vectorize.io"
|
|
9
|
+
HINDSIGHT_API_KEY_ENV = "HINDSIGHT_API_KEY"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class HindsightPipecatConfig:
|
|
14
|
+
"""Connection and default settings for the Pipecat integration.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
hindsight_api_url: URL of the Hindsight API server.
|
|
18
|
+
api_key: API key for Hindsight authentication.
|
|
19
|
+
recall_budget: Default recall budget level (low/mid/high).
|
|
20
|
+
recall_max_tokens: Default maximum tokens for recall results.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
hindsight_api_url: str = DEFAULT_HINDSIGHT_API_URL
|
|
24
|
+
api_key: str | None = None
|
|
25
|
+
recall_budget: str = "mid"
|
|
26
|
+
recall_max_tokens: int = 4096
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
_global_config: HindsightPipecatConfig | None = None
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def configure(
|
|
33
|
+
hindsight_api_url: str | None = None,
|
|
34
|
+
api_key: str | None = None,
|
|
35
|
+
recall_budget: str = "mid",
|
|
36
|
+
recall_max_tokens: int = 4096,
|
|
37
|
+
) -> HindsightPipecatConfig:
|
|
38
|
+
"""Configure Hindsight connection and default settings.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
hindsight_api_url: Hindsight API URL (default: production).
|
|
42
|
+
api_key: API key. Falls back to HINDSIGHT_API_KEY env var.
|
|
43
|
+
recall_budget: Default recall budget (low/mid/high).
|
|
44
|
+
recall_max_tokens: Default max tokens for recall.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
The configured HindsightPipecatConfig.
|
|
48
|
+
"""
|
|
49
|
+
global _global_config
|
|
50
|
+
|
|
51
|
+
resolved_url = hindsight_api_url or DEFAULT_HINDSIGHT_API_URL
|
|
52
|
+
resolved_key = api_key or os.environ.get(HINDSIGHT_API_KEY_ENV)
|
|
53
|
+
|
|
54
|
+
_global_config = HindsightPipecatConfig(
|
|
55
|
+
hindsight_api_url=resolved_url,
|
|
56
|
+
api_key=resolved_key,
|
|
57
|
+
recall_budget=recall_budget,
|
|
58
|
+
recall_max_tokens=recall_max_tokens,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
return _global_config
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def get_config() -> HindsightPipecatConfig | None:
|
|
65
|
+
"""Get the current global configuration."""
|
|
66
|
+
return _global_config
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def reset_config() -> None:
|
|
70
|
+
"""Reset global configuration to None."""
|
|
71
|
+
global _global_config
|
|
72
|
+
_global_config = None
|