clawdie-ai/scripts/memory/embed.sh
Sam & Claude 3e404b4332 Add memory pipeline scripts (Phase 2)
Three scripts in scripts/memory/:
- common.sh: shared config, DB connection, dependency checks
- embed.sh: generate 1536d embeddings via OpenRouter API
- chunk.sh: split text on sentence boundaries (~500 chars)
- memory-pg.sh: full CLI for store/search/recent/important/count

Store pipeline: text → chunk → embed → insert (fully automated).
Search: hybrid RRF combining full-text and vector similarity.
All scripts use #!/usr/bin/env bash for FreeBSD compatibility.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 09:30:34 +00:00

47 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env bash
# embed.sh — Generate an embedding vector from text via OpenRouter
#
# Usage:
# ./embed.sh "text to embed"
# echo "text to embed" | ./embed.sh -
#
# Output: JSON array of floats (the embedding vector)
. "$(dirname "$0")/common.sh"
if [ -z "${OPENROUTER_API_KEY:-}" ]; then
echo "Error: OPENROUTER_API_KEY not set" >&2
exit 1
fi
# Read input
if [ "${1:-}" = "-" ]; then
INPUT_TEXT=$(cat)
elif [ -n "${1:-}" ]; then
INPUT_TEXT="$1"
else
echo "Usage: embed.sh <text> | embed.sh -" >&2
exit 1
fi
# Escape for JSON
JSON_INPUT=$(python3 -c "import json,sys; print(json.dumps(sys.argv[1]))" "$INPUT_TEXT")
# Call API
RESPONSE=$(curl -s -X POST "$EMBED_API_URL" \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d "{\"model\": \"$EMBED_MODEL\", \"input\": $JSON_INPUT, \"dimensions\": $EMBED_DIMENSIONS}")
# Extract embedding, error if missing
python3 -c "
import json, sys
r = json.loads(sys.argv[1])
if 'error' in r:
print(f'API error: {r[\"error\"]}', file=sys.stderr)
sys.exit(1)
if 'data' not in r or len(r['data']) == 0:
print(f'Unexpected response: {json.dumps(r)[:200]}', file=sys.stderr)
sys.exit(1)
print(json.dumps(r['data'][0]['embedding']))
" "$RESPONSE"