Small C/PGXS extension that lets you call Jigsaw Stack APIs from SQL.
- Create a
.envnext todocker-compose.yml:JIGSAW_API_KEY=sk_...JIGSAW_BASE_URL=https://api.jigsawstack.comJIGSAW_ROUTES={"summary":"v1/ai/summary","sentiment":"v1/ai/sentiment"}
- Start:
docker compose up -d(builds the image, installs the extension). - Connect:
psql -h 127.0.0.1 -U postgres -d postgres - Verify:
SHOW shared_preload_libraries;must includejigsaw_pg.
- Prereqs:
apt-get install -y postgresql-server-dev-15 libcurl4-openssl-dev - Build:
make USE_PGXS=1 - Install:
sudo make USE_PGXS=1 install - In psql:
CREATE EXTENSION jigsaw_pg;
- GUCs (also read from environment with precedence):
jigsaw.api_key(envJIGSAW_API_KEY)jigsaw.base_url(envJIGSAW_BASE_URL, defaulthttps://api.jigsawstack.com)jigsaw.routes(envJIGSAW_ROUTES, JSON mapping endpoint keys to paths)jigsaw.timeout_ms(envJIGSAW_TIMEOUT_MS, default 30000)
- Example (SQL):
ALTER SYSTEM SET jigsaw.base_url = 'https://api.jigsawstack.com';ALTER SYSTEM SET jigsaw.routes = '{"summary":"v1/ai/summary","sentiment":"v1/ai/sentiment"}';SELECT pg_reload_conf();
jigsaw.call(endpoint_key text, payload jsonb) returns jsonb- Posts
payloadto the route resolved fromjigsaw.routes[endpoint_key]or usesendpoint_keyas the path. - Example:
SELECT jigsaw.call('summary', '{"text":"Hello","type":"points","max_points":3}'::jsonb);
- Posts
jigsaw.summary(text text, url text, type text default 'text', max_points int default 2, max_characters int default 0) returns jsonb- Builds the correct JSON for
v1/ai/summary. - Example:
SELECT jigsaw.summary('Hello world', NULL, 'points', 3, 0);
- Builds the correct JSON for
jigsaw.sentiment(text text) returns jsonb- Calls
v1/ai/sentimentwith{ "text": ... }. - Example:
SELECT jigsaw.sentiment('I love this product'); - CALL can call all the api endpoints from Jigsaw Stack. At the moment only summary and sentiment can be called with a named function.
- Calls
- Base vs route: you can set base to
.../v1and routes withoutv1/, or base without/v1and routes withv1/.... The extension normalizes to avoid double/v1. - Ensure env is applied:
docker compose exec db env | grep JIGSAW. - GUCs show after load:
SHOW jigsaw.base_url; SHOW jigsaw.routes;(library is preloaded viashared_preload_libraries). - Inspect the effective URL:
SELECT jigsaw.resolve('summary');