Skip to content

Commit d5403a4

Browse files
authored
doc: update cookbook (#284)
* fix: sync-cookbook now supports new cookbook repo layout Cookbook repository changed structure: - Applications moved from root to applications/ subdirectory - Notebooks remain in notebooks/ directory (unchanged) Updated sync script to: - Look for apps in applications/* instead of root/* - Update GitHub URLs to include applications/ path - Add safety check if applications/ dir doesn't exist * doc: update cookbook * doc: update cookbook * doc: update cookbook
1 parent a24941f commit d5403a4

34 files changed

Lines changed: 6181 additions & 81 deletions

hindsight-dev/hindsight_dev/sync_cookbook.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
55
- Clones the cookbook repo to a temp directory
66
- Converts notebooks/*.ipynb → docs/cookbook/recipes/*.md
7-
- Converts app directories (with README.md) → docs/cookbook/applications/*.md
7+
- Converts applications/*/ directories (with README.md) → docs/cookbook/applications/*.md
88
- Updates sidebars.ts with the new entries
99
1010
Usage: sync-cookbook (after installing hindsight-dev)
1111
1212
Conventions in cookbook repo:
1313
- notebooks/*.ipynb → Recipes (use cases, tutorials)
14-
- Directories with README.md at root → Applications (complete apps)
14+
- applications/*/ directories with README.md → Applications (complete apps)
1515
- Notebook title extracted from first # heading in first markdown cell
1616
- App title extracted from first # heading in README.md
1717
"""
@@ -236,7 +236,13 @@ def process_applications(cookbook_dir: Path, apps_dir: Path) -> list[dict]:
236236
"""Process application directories with README.md."""
237237
apps = []
238238

239-
for entry in sorted(cookbook_dir.iterdir()):
239+
# Applications are now in the applications/ subdirectory
240+
applications_dir = cookbook_dir / "applications"
241+
if not applications_dir.exists():
242+
print(" No applications directory found")
243+
return apps
244+
245+
for entry in sorted(applications_dir.iterdir()):
240246
if not entry.is_dir() or entry.name in IGNORE_DIRS:
241247
continue
242248

@@ -253,7 +259,7 @@ def process_applications(cookbook_dir: Path, apps_dir: Path) -> list[dict]:
253259
readme_content = readme_path.read_text()
254260

255261
# Create application page with frontmatter
256-
app_url = f"https://github.com/vectorize-io/hindsight-cookbook/tree/main/{entry.name}"
262+
app_url = f"https://github.com/vectorize-io/hindsight-cookbook/tree/main/applications/{entry.name}"
257263

258264
frontmatter = f"""---
259265
sidebar_position: {len(apps) + 1}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# Chat Memory App
6+
7+
8+
:::info Complete Application
9+
This is a complete, runnable application demonstrating Hindsight integration.
10+
[**View source on GitHub →**](https://github.com/vectorize-io/hindsight-cookbook/tree/main/applications/chat-memory)
11+
:::
12+
13+
14+
A demo chat application that uses Groq's `qwen/qwen3-32b` model with Hindsight for persistent per-user memory.
15+
16+
## Features
17+
18+
- 🧠 **Persistent Memory**: Each user gets their own memory bank that remembers conversations
19+
- 🚀 **Fast AI**: Powered by Groq's high-speed inference
20+
- 🎯 **Per-User Context**: Isolated memory per user with automatic context retrieval
21+
- 💬 **Real-time Chat**: Instant responses with memory-augmented context
22+
23+
## Setup
24+
25+
### 1. Start Hindsight API
26+
27+
First, start the Hindsight API server using Docker:
28+
29+
```bash
30+
export GROQ_API_KEY=your_groq_api_key_here
31+
32+
# Start Hindsight with Groq as the LLM provider
33+
docker run --rm -it --pull always -p 8888:8888 -p 9999:9999 \
34+
-e HINDSIGHT_API_LLM_PROVIDER=groq \
35+
-e HINDSIGHT_API_LLM_API_KEY=$GROQ_API_KEY \
36+
-e HINDSIGHT_API_LLM_MODEL="openai/gpt-oss-20b" \
37+
-v $HOME/.hindsight-docker:/home/hindsight/.pg0 \
38+
ghcr.io/vectorize-io/hindsight:latest
39+
```
40+
41+
- **API**: http://localhost:8888
42+
- **Control Plane UI**: http://localhost:9999
43+
44+
### 2. Configure Environment
45+
46+
Copy your Groq API key to the environment file:
47+
48+
```bash
49+
# Update .env.local with your Groq API key
50+
echo "GROQ_API_KEY=your_groq_api_key_here" > .env.local
51+
echo "HINDSIGHT_API_URL=http://localhost:8888" >> .env.local
52+
```
53+
54+
If you don't have one, you can get a free Groq API key here: https://console.groq.com/home
55+
56+
### 3. Install Dependencies
57+
58+
```bash
59+
npm install
60+
```
61+
62+
### 4. Run the App
63+
64+
```bash
65+
npm run dev
66+
```
67+
68+
Open http://localhost:3000 in your browser.
69+
70+
## How It Works
71+
72+
1. **User Identity**: Each browser session gets a unique user ID
73+
2. **Memory Bank Creation**: First message creates a personal memory bank in Hindsight
74+
3. **Context Retrieval**: Before responding, relevant memories are retrieved
75+
4. **Memory Augmented Response**: Groq generates responses with memory context
76+
5. **Conversation Storage**: Each conversation is stored for future context
77+
78+
## Architecture
79+
80+
```
81+
User Message
82+
83+
Next.js API Route (/api/chat)
84+
85+
Hindsight.recall() → Get relevant memories
86+
87+
Groq API → Generate response with memory context
88+
89+
Hindsight.retain() → Store conversation
90+
91+
Response to User
92+
```
93+
94+
## Memory Bank Structure
95+
96+
Each user gets their own isolated memory bank with:
97+
- **Name**: "Chat Memory for [userId]"
98+
- **Background**: Conversational AI assistant context
99+
- **Disposition**: Empathetic (4), Low Skepticism (2), Balanced Literalism (3)
100+
101+
## Try It Out
102+
103+
1. **First Conversation**: Tell the assistant about yourself
104+
- "Hi! I'm a software engineer from San Francisco. I love Python and machine learning."
105+
106+
2. **Second Conversation**: Ask what it remembers
107+
- "What do you know about me?"
108+
- "What programming languages do I like?"
109+
110+
3. **Context Building**: Continue sharing preferences
111+
- "I prefer VS Code over other editors"
112+
- "I'm working on a React project"
113+
114+
4. **Memory Verification**: Visit the Hindsight Control Plane at http://localhost:9999 to see stored memories
115+
116+
## Development
117+
118+
- **Groq Model**: Uses `qwen/qwen3-32b` for fast, high-quality responses
119+
- **Memory Storage**: Automatic conversation retention with context categorization
120+
- **Memory Retrieval**: Semantic search with 2048 token budget for relevant context
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# Deliveryman Demo
6+
7+
8+
:::info Complete Application
9+
This is a complete, runnable application demonstrating Hindsight integration.
10+
[**View source on GitHub →**](https://github.com/vectorize-io/hindsight-cookbook/tree/main/applications/deliveryman-demo)
11+
:::
12+
13+
14+
A delivery agent simulation that demonstrates Hindsight's long-term memory capabilities. An AI agent navigates a multi-building office complex to deliver packages, learning employee locations and optimal paths over time through mental models.
15+
16+
## Prerequisites
17+
18+
- Python 3.11+
19+
- Node.js 18+
20+
- [uv](https://docs.astral.sh/uv/) (Python package manager)
21+
22+
## Setup (Fresh Environment)
23+
24+
### 1. Clone Repositories
25+
26+
```bash
27+
# Clone Hindsight (memory engine)
28+
git clone https://github.com/anthropics/hindsight.git
29+
30+
# Clone the cookbook (contains this demo)
31+
git clone https://github.com/anthropics/hindsight-cookbook.git
32+
```
33+
34+
### 2. Start Hindsight API
35+
36+
```bash
37+
cd hindsight
38+
cp .env.example .env
39+
```
40+
41+
Edit `.env` with your LLM configuration:
42+
43+
```bash
44+
HINDSIGHT_API_LLM_PROVIDER=groq
45+
HINDSIGHT_API_LLM_API_KEY=<your-groq-api-key>
46+
HINDSIGHT_API_LLM_MODEL=openai/gpt-oss-120b
47+
HINDSIGHT_API_HOST=0.0.0.0
48+
HINDSIGHT_API_PORT=8888
49+
HINDSIGHT_API_ENABLE_OBSERVATIONS=true
50+
51+
# Retain extraction settings (improves employee/location extraction)
52+
HINDSIGHT_API_RETAIN_EXTRACTION_MODE=custom
53+
HINDSIGHT_API_RETAIN_CUSTOM_INSTRUCTIONS="Delivery agent. Remember employee locations, building layout, and optimal paths."
54+
55+
# Embedded database storage
56+
PG0_DATA_DIR=/tmp/hindsight-data
57+
```
58+
59+
Start the API:
60+
61+
```bash
62+
./scripts/dev/start-api.sh
63+
# Runs on http://localhost:8888
64+
```
65+
66+
### 3. Start Hindsight Control Plane (Optional)
67+
68+
The control plane provides a web UI for inspecting memory banks, facts, and mental models.
69+
70+
```bash
71+
cd hindsight
72+
./scripts/dev/start-control-plane.sh
73+
# Runs on a dynamic port (check terminal output)
74+
```
75+
76+
### 4. Start Demo Backend
77+
78+
```bash
79+
cd hindsight-cookbook/deliveryman-demo/backend
80+
81+
# Create virtual environment and install dependencies
82+
python -m venv .venv
83+
source .venv/bin/activate
84+
pip install -r requirements.txt
85+
```
86+
87+
Create `backend/.env`:
88+
89+
```bash
90+
OPENAI_API_KEY=<your-openai-api-key>
91+
GROQ_API_KEY=<your-groq-api-key>
92+
HINDSIGHT_API_URL=http://localhost:8888
93+
LLM_MODEL=openai/gpt-4o
94+
```
95+
96+
Start the backend:
97+
98+
```bash
99+
./run.sh
100+
# Or manually:
101+
python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 --ws wsproto --reload
102+
```
103+
104+
**Note:** The `--ws wsproto` flag is required for WebSocket support. Without it, connections will fail with error 1006.
105+
106+
### 5. Start Demo Frontend
107+
108+
```bash
109+
cd hindsight-cookbook/deliveryman-demo/frontend
110+
npm install
111+
npm run dev
112+
# Runs on http://localhost:5173
113+
```
114+
115+
### 6. Open the Demo
116+
117+
Navigate to http://localhost:5173 in your browser.
118+
119+
## How It Works
120+
121+
1. The agent receives a delivery task (e.g., "Deliver Package #3954 to Victor Huang")
122+
2. It navigates a multi-building complex with floors, elevators, and sky bridges
123+
3. Along the way it encounters employees and learns their locations
124+
4. After each delivery, the conversation is sent to Hindsight via the **retain** API
125+
5. Hindsight extracts facts (employee locations, building layout) and builds **mental models**
126+
6. On subsequent deliveries, the agent queries Hindsight to recall what it learned
127+
128+
## Architecture
129+
130+
```
131+
Browser (5173) → Frontend (React + Phaser)
132+
↓ WebSocket
133+
Backend (8000) → FastAPI + Delivery Agent
134+
↓ HTTP
135+
Hindsight API (8888) → Memory Engine + PostgreSQL
136+
```
137+
138+
## Troubleshooting
139+
140+
| Problem | Solution |
141+
|---------|----------|
142+
| WebSocket error 1006 | Restart backend with `--ws wsproto` flag |
143+
| Mental models missing employees | Check `HINDSIGHT_API_RETAIN_EXTRACTION_MODE=custom` is set |
144+
| Hindsight connection refused | Verify Hindsight API is running on port 8888 |
145+
| Frontend shows "Disconnected" | Check backend is running on port 8000 |

0 commit comments

Comments
 (0)