Azpg ai in database
Skill lestermarch/postgres-ai-experts/skills/azpg-ai-in-database
Call large language models and Azure AI services from inside Azure Database for PostgreSQL Flexible Server using the azure_ai extension — generate vector embeddings (azure_openai.create_embeddings), run LLM text generation (azure_ai.generate), and use Azure Cognitive Services for sentiment, language detection, key-phrase extraction, entity recognition, and summarization (azure_cognitive.*), all in SQL. Use this skill whenever the task involves in-database AI, calling Azure OpenAI or a Language service from SQL, embedding a column of text, enriching or summarizing rows with an LLM, configuring azure_ai.set_setting endpoints/keys, or bulk-generating embeddings for a table — even when the user just says "add embeddings to my products table" or "summarize these reviews in the database". For designing the vector index/RAG retrieval side hand off to azpg-pgvector-rag.From its SKILL.md
npx -y skills add lestermarch/postgres-ai-experts --skill azpg-ai-in-databaseAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
8.3 KB, ~1.8k tokens by cl100k_base, as published. Nobody here has run it
In-database AI with the azure_ai extension on Flexible Server
The azure_ai extension lets SQL call Azure OpenAI and Azure Cognitive Services
directly, so embeddings, LLM generation, and text analytics happen next to the
data — no application round-trip. It adds three schemas:
| Schema | What it calls | Key functions |
|---|---|---|
azure_ai | config + LLM generation | set_setting, get_setting, generate |
azure_openai | Azure OpenAI | create_embeddings |
azure_cognitive | Azure Language service | summarize_abstractive/_extractive, analyze_sentiment, detect_language, extract_key_phrases, recognize_entities |
This skill is read-write and explicit. Inspecting which schemas/functions
exist and whether settings are configured is safe. But every AI call costs money
and adds latency, set_setting stores a secret in the database, and bulk
embedding writes to your table — so configuration and bulk generation are
guarded and run through dry-run-capable scripts. See
Safety protocol.
Retrieval/index design (HNSW, DiskANN, distance operators, RAG query shape) belongs to
azpg-pgvector-rag; this skill produces the embeddings and text. Deep function reference is inreference.md; auth, cost, RBAC, and rate-limit constraints are inazure-constraints.md.
Live instance context (dynamic injection)
PGCONN is a libpq connection string. All read-only.
- Is
azure_aiinstalled? !psql "$PGCONN" -tAc "SELECT extversion FROM pg_extension WHERE extname='azure_ai';" 2>/dev/null || echo "(not installed — needs allow-listing + CREATE EXTENSION)" - Is the OpenAI endpoint configured? (prints the endpoint, never the key)
!
psql "$PGCONN" -tAc "SELECT azure_ai.get_setting('azure_openai.endpoint');" 2>/dev/null || echo "(unset, or you lack azure_ai_settings_manager)" - Is
vectoravailable to store embeddings? !psql "$PGCONN" -tAc "SELECT extversion FROM pg_extension WHERE extname='vector';" 2>/dev/null || echo "(not installed)"
When to use this skill
Trigger on: "generate embeddings for this column", "call Azure OpenAI from SQL",
"summarize/translate/score sentiment on rows in the database", "set up azure_ai",
"embed my whole table", "use an LLM inside Postgres". If the follow-on task is
"now design the ANN index and write the similarity query" hand off to
azpg-pgvector-rag.
Decision flow
Steps 1–2 are read/inspect (safe); steps 3–5 are write/mutate + billable (guarded, explicit).
- Verify prerequisites (safe):
azure_ai(andvectorif storing) installed; caller hasazure_ai_settings_manager. Usescripts/inspect_azure_ai.sql. - Confirm the model deployment (safe): the
deployment_nameargument must be an existing deployment in your Azure OpenAI / Foundry resource. - Configure endpoint + key (guarded, stores a secret): via
scripts/configure_azure_ai.sh. Prefer Microsoft Entra / managed identity over a subscription key where supported — and note managed identity here means the server's system-assigned identity, not a user-assigned one (see azure-constraints.md). - Prototype on ONE row (billable but tiny): run
create_embeddings/generate/ aazure_cognitivecall on a single value and inspect output + dimensions before touching the whole table. - Bulk generate (guarded, billable, writes rows): batch with
scripts/bulk_embed.sh— small batches, resumable,--dry-runshows the SQL and row count without calling the API.
Core calls (verified syntax)
Configuration (members of azure_ai_settings_manager only — all Azure admin
users have it):
SELECT azure_ai.set_setting('azure_openai.endpoint', 'https://<res>.openai.azure.com');
SELECT azure_ai.set_setting('azure_openai.subscription_key', '<key>');
SELECT azure_ai.get_setting('azure_openai.endpoint'); -- readback (key is retrievable too; guard it)
Embeddings — create_embeddings(deployment_name, input [, timeout_ms, throw_on_error]),
returns a float array; cast to vector:
-- one value
SELECT azure_openai.create_embeddings('my-embedding-deploy', 'hello world')::vector;
-- store for a whole column (this WRITES and bills per row — see bulk_embed.sh)
UPDATE products
SET embedding = azure_openai.create_embeddings('my-embedding-deploy', description)::vector
WHERE embedding IS NULL;
LLM generation:
SELECT azure_ai.generate('Summarize this review in one sentence: '|| review_text)
FROM reviews WHERE id = 1;
Azure Cognitive Services (needs azure_cognitive.endpoint + key set):
SELECT azure_cognitive.analyze_sentiment(comment, 'en') FROM feedback LIMIT 1;
SELECT azure_cognitive.summarize_abstractive(bill_text, 'en') FROM bills LIMIT 1;
See reference.md for full argument lists, batch/array forms, and
return-type composites.
Safety protocol
- Reads run automatically. Listing schemas/functions and
get_settingof the endpoint change nothing. Avoid echoingsubscription_keyin logs. - Configuration is a guarded write that stores a secret.
set_settingwrites the key into a config table. Run it viascripts/configure_azure_ai.sh, which reads the key from an env var and never prints it (even in--dry-run). Prefer managed identity where available. - Every AI call bills and adds latency. Always prototype on one row before bulk. State the model + approximate row count so the user consents to the cost.
- Bulk embedding is a guarded, resumable write.
bulk_embed.shprocesses in small batches filtered toWHERE <col> IS NULL, so an interrupted run resumes without re-billing done rows.--dry-runprints the statement and the count of rows that would be processed, and calls no API. throw_on_errordefaults totrue— an API failure raises and rolls back the surrounding transaction. Keep batches transaction-scoped so a mid-batch failure leaves no partial, half-billed state.
Bundled files
reference.md— every function's arguments, batch/array forms, return composites, and end-to-end embed→index→search recipe (index step handed toazpg-pgvector-rag).azure-constraints.md— Entra vs key auth,azure_ai_settings_managerRBAC, cost/rate-limit/throttling behaviour, region and model-deployment requirements.scripts/inspect_azure_ai.sql— read-only; extension present, schemas/functions available, endpoints configured.scripts/configure_azure_ai.sh— guarded; sets endpoint/key from env vars, secret never printed,--dry-run.scripts/bulk_embed.sh— guarded; batched, resumable embedding of a text column,--dry-run, cost preview.examples/enrich_with_llm.md— worked example: add embeddings + an LLM-generated summary column to a table, safely.
What ships with it: 8 files
31.6 KB alongside SKILL.md, 2 of them executable
examples/
- enrich_with_llm.md3.2 KB
scripts/
- bulk_embed.shruns4.4 KB
- configure_azure_ai.shruns3.3 KB
- inspect_azure_ai.sql1.5 KB
- README.md1.7 KB
- azure-constraints.md6.6 KB
- EVALUATION.md4.8 KB
- reference.md5.9 KB