|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +--- |
| 4 | + |
| 5 | +# Go Memory-Augmented API |
| 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/go-memory-service) |
| 11 | +::: |
| 12 | + |
| 13 | + |
| 14 | +A Go HTTP microservice demonstrating per-user memory isolation with Hindsight. Remembers each user's tech stack, problems solved, and preferences to provide personalized assistance. |
| 15 | + |
| 16 | +## Features |
| 17 | + |
| 18 | +- 🔐 **Per-User Isolation**: Each user gets their own memory bank |
| 19 | +- 🧠 **Context-Aware Responses**: Uses recall + reflect for personalized answers |
| 20 | +- 🏃 **Fire-and-Forget Memory**: Background goroutines store interactions without blocking responses |
| 21 | +- 🏷️ **Tag-Based Partitioning**: Organize memories by type (projects, debugging, preferences) |
| 22 | + |
| 23 | +## Setup |
| 24 | + |
| 25 | +### 1. Start Hindsight |
| 26 | + |
| 27 | +```bash |
| 28 | +export OPENAI_API_KEY=your-key |
| 29 | + |
| 30 | +docker run --rm -it --pull always -p 8888:8888 -p 9999:9999 \ |
| 31 | + -e HINDSIGHT_API_LLM_API_KEY=$OPENAI_API_KEY \ |
| 32 | + -e HINDSIGHT_API_LLM_MODEL=o3-mini \ |
| 33 | + -v $HOME/.hindsight-docker:/home/hindsight/.pg0 \ |
| 34 | + ghcr.io/vectorize-io/hindsight:latest |
| 35 | +``` |
| 36 | + |
| 37 | +### 2. Run the service |
| 38 | + |
| 39 | +```bash |
| 40 | +go run main.go |
| 41 | +``` |
| 42 | + |
| 43 | +### 3. Try it out |
| 44 | + |
| 45 | +```bash |
| 46 | +# Store memories |
| 47 | +curl -s localhost:8080/learn -d '{ |
| 48 | + "user_id": "alice", |
| 49 | + "content": "I am building a Go microservice with gRPC and PostgreSQL", |
| 50 | + "tags": ["project"] |
| 51 | +}' |
| 52 | + |
| 53 | +curl -s localhost:8080/learn -d '{ |
| 54 | + "user_id": "alice", |
| 55 | + "content": "I prefer structured logging with slog over zerolog", |
| 56 | + "tags": ["preferences"] |
| 57 | +}' |
| 58 | + |
| 59 | +# Ask questions (uses recall + reflect) |
| 60 | +curl -s localhost:8080/ask -d '{ |
| 61 | + "user_id": "alice", |
| 62 | + "query": "What tech stack am I using?" |
| 63 | +}' | jq . |
| 64 | + |
| 65 | +# Raw memory recall |
| 66 | +curl -s "localhost:8080/recall/alice?q=database" | jq . |
| 67 | +``` |
| 68 | + |
| 69 | +## API Endpoints |
| 70 | + |
| 71 | +- `POST /learn` - Store new information for a user |
| 72 | +- `POST /ask` - Ask a question using the user's memories |
| 73 | +- `GET /recall/{userID}?q=query` - Direct memory recall |
| 74 | +- `GET /health` - Health check |
| 75 | + |
| 76 | +## Key Patterns |
| 77 | + |
| 78 | +**Per-User Banks**: Each user gets an isolated memory bank (`user-alice`, `user-bob`) |
| 79 | + |
| 80 | +**Async Memory Storage**: Interactions are stored in background goroutines: |
| 81 | + |
| 82 | +```go |
| 83 | +go func() { |
| 84 | + bgCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 85 | + defer cancel() |
| 86 | + |
| 87 | + retainReq := hindsight.RetainRequest{ |
| 88 | + Items: []hindsight.MemoryItem{{ |
| 89 | + Content: interaction, |
| 90 | + Context: *hindsight.NewNullableString(hindsight.PtrString("Q&A interaction")), |
| 91 | + }}, |
| 92 | + } |
| 93 | + client.MemoryAPI.RetainMemories(bgCtx, bankID).RetainRequest(retainReq).Execute() |
| 94 | +}() |
| 95 | +``` |
| 96 | + |
| 97 | +**Tag-Based Filtering**: Partition memories within a bank by type for scoped retrieval |
| 98 | + |
| 99 | +## Learn More |
| 100 | + |
| 101 | +- [Go SDK Documentation](https://hindsight.vectorize.io/sdks/go) |
| 102 | +- [Hindsight Documentation](https://hindsight.vectorize.io) |
0 commit comments