#!/usr/bin/env bash # embed.sh — Generate an embedding vector from text via the configured embedding endpoint. # # Defaults to local llama-server (EMBED_BASE_URL in .env). # Set EMBED_API_KEY in .env for remote providers that require auth. # # Usage: # ./embed.sh "text to embed" # echo "text to embed" | ./embed.sh - # # Output: JSON array of floats (the embedding vector) . "$(dirname "$0")/common.sh" # Read input if [ "${1:-}" = "-" ]; then INPUT_TEXT=$(cat) elif [ -n "${1:-}" ]; then INPUT_TEXT="$1" else echo "Usage: embed.sh | 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 embedding API (OpenAI-compatible: local llama-server or remote) RESPONSE=$(curl -s -X POST "$EMBED_API_URL" \ -H "Authorization: Bearer ${EMBED_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"