Allow and deny using pgvector as a vector database
The AI Semantic Response Guard policy analyzes the full response from an LLM service and filters it based on semantic similarity to configured allow or deny patterns.
Deny rules take precedence over allow rules. Responses matching a deny pattern are blocked, even if they also match an allow pattern. Responses not matching any allow pattern are blocked when allow rules are set.
Prerequisites
-
An existing AI Model.
-
A PostgreSQL database with pgvector extension installed and reachable from AI Gateway.
-
Port
5432, or your custom PostgreSQL port, is open and reachable from AI Gateway.
Environment variables
-
OPENAI_API_KEY: Your OpenAI API key -
PGVECTOR_HOST: The host where your pgvector-enabled PostgreSQL instance runs -
PGVECTOR_USER: Database user for pgvector -
PGVECTOR_PASSWORD: Database password for pgvector
ai_gateway_policies:
- ref: allow-and-deny-responses-pgvector
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: allow-and-deny-responses-pgvector
display_name: Allow and deny using pgvector as a vector database
type: ai-semantic-response-guard
enabled: true
config:
embeddings:
auth:
header_name: Authorization
header_value: Bearer $OPENAI_API_KEY
model:
name: text-embedding-3-small
provider: openai
search:
threshold: 0.7
vectordb:
strategy: pgvector
distance_metric: cosine
threshold: 0.7
dimensions: 1024
pgvector:
host: !env PGVECTOR_HOST
port: 5432
database: kong-pgvector
user: !env PGVECTOR_USER
password: !env PGVECTOR_PASSWORD
ssl: false
ssl_required: false
ssl_verify: false
ssl_version: tlsv1_2
timeout: 5000
rules:
allow_responses:
- Troubleshooting networks and connectivity issues
- Managing cloud platforms (AWS, Azure, GCP)
- Security hardening and incident response strategies
- DevOps pipelines, automation, and observability
- Software engineering concepts and language syntax
- IT governance, compliance, and regulatory guidance
- Continuous integration and deployment practices
- Writing documentation and explaining technical concepts
- Operating system administration and configuration
- Best practices for collaboration and productivity tools
deny_responses:
- Unauthorized penetration testing or hacking tutorials
- Methods for bypassing software licensing or DRM
- Step-by-step instructions for exploiting vulnerabilities
- Techniques to evade or disable security controls
- Collecting or exposing personal or employee data
- Using AI for impersonation, phishing, or fraud
- Manipulative social engineering techniques
- Advice on breaking internal IT or security policies
- Entertainment, dating, or other non-work topics
- Political, religious, or otherwise sensitive discussions unrelated to work
global: falseMake sure to replace the following placeholders with your own values:
-
AI_GATEWAY_ID: Theidof your AI Gateway. -
!env OPENAI_API_KEY: Your OpenAI API key -
!env PGVECTOR_HOST: The host where your pgvector-enabled PostgreSQL instance runs -
!env PGVECTOR_USER: Database user for pgvector -
!env PGVECTOR_PASSWORD: Database password for pgvector
curl -X POST https://{region}.api.konghq.com/v1/ai-gateways/{AIGatewayId}/policies \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"name": "allow-and-deny-responses-pgvector",
"display_name": "Allow and deny using pgvector as a vector database",
"type": "ai-semantic-response-guard",
"enabled": true,
"config": {
"embeddings": {
"auth": {
"header_name": "Authorization",
"header_value": "Bearer '$OPENAI_API_KEY'"
},
"model": {
"name": "text-embedding-3-small",
"provider": "openai"
}
},
"search": {
"threshold": 0.7
},
"vectordb": {
"strategy": "pgvector",
"distance_metric": "cosine",
"threshold": 0.7,
"dimensions": 1024,
"pgvector": {
"host": "'$PGVECTOR_HOST'",
"port": 5432,
"database": "kong-pgvector",
"user": "'$PGVECTOR_USER'",
"password": "'$PGVECTOR_PASSWORD'",
"ssl": false,
"ssl_required": false,
"ssl_verify": false,
"ssl_version": "tlsv1_2",
"timeout": 5000
}
},
"rules": {
"allow_responses": [
"Troubleshooting networks and connectivity issues",
"Managing cloud platforms (AWS, Azure, GCP)",
"Security hardening and incident response strategies",
"DevOps pipelines, automation, and observability",
"Software engineering concepts and language syntax",
"IT governance, compliance, and regulatory guidance",
"Continuous integration and deployment practices",
"Writing documentation and explaining technical concepts",
"Operating system administration and configuration",
"Best practices for collaboration and productivity tools"
],
"deny_responses": [
"Unauthorized penetration testing or hacking tutorials",
"Methods for bypassing software licensing or DRM",
"Step-by-step instructions for exploiting vulnerabilities",
"Techniques to evade or disable security controls",
"Collecting or exposing personal or employee data",
"Using AI for impersonation, phishing, or fraud",
"Manipulative social engineering techniques",
"Advice on breaking internal IT or security policies",
"Entertainment, dating, or other non-work topics",
"Political, religious, or otherwise sensitive discussions unrelated to work"
]
}
},
"global": false
}
'Make sure to replace the following placeholders with your own values:
-
region: Geographic region where your Kong Konnect is hosted and operates. -
KONNECT_TOKEN: Your Personal Access Token (PAT) associated with your Konnect account. -
AIGatewayId: Theidof the AI Gateway.
See the Konnect AI Gateway API reference to learn about region-specific URLs and personal access tokens.
Prerequisite: Configure your Personal Access Token
terraform {
required_providers {
konnect = {
source = "kong/konnect"
}
}
}
provider "konnect" {
personal_access_token = "$KONNECT_TOKEN"
server_url = "https://us.api.konghq.com/"
}Add the following to your Terraform configuration:
resource "konnect_ai_gateway_policy" "my_policy" {
name = "allow-and-deny-responses-pgvector"
display_name = "Allow and deny using pgvector as a vector database"
type = "ai-semantic-response-guard"
enabled = true
global = false
config = jsonencode({
embeddings = {
auth = {
header_name = "Authorization"
header_value = "Bearer var.openai_api_key"
}
model = {
name = "text-embedding-3-small"
provider = "openai"
}
}
search = {
threshold = 0.7
}
vectordb = {
strategy = "pgvector"
distance_metric = "cosine"
threshold = 0.7
dimensions = 1024
pgvector = {
host = var.pgvector_host
port = 5432
database = "kong-pgvector"
user = var.pgvector_user
password = var.pgvector_password
ssl = false
ssl_required = false
ssl_verify = false
ssl_version = "tlsv1_2"
timeout = 5000
}
}
rules = {
allow_responses = ["Troubleshooting networks and connectivity issues", "Managing cloud platforms (AWS, Azure, GCP)", "Security hardening and incident response strategies", "DevOps pipelines, automation, and observability", "Software engineering concepts and language syntax", "IT governance, compliance, and regulatory guidance", "Continuous integration and deployment practices", "Writing documentation and explaining technical concepts", "Operating system administration and configuration", "Best practices for collaboration and productivity tools"]
deny_responses = ["Unauthorized penetration testing or hacking tutorials", "Methods for bypassing software licensing or DRM", "Step-by-step instructions for exploiting vulnerabilities", "Techniques to evade or disable security controls", "Collecting or exposing personal or employee data", "Using AI for impersonation, phishing, or fraud", "Manipulative social engineering techniques", "Advice on breaking internal IT or security policies", "Entertainment, dating, or other non-work topics", "Political, religious, or otherwise sensitive discussions unrelated to work"]
}
})
gateway_id = konnect_ai_gateway.my_ai_gateway.id
}This example requires the following variables to be added to your manifest. You can specify values at runtime by setting TF_VAR_name=value.
variable "openai_api_key" {
type = string
}
variable "pgvector_host" {
type = string
}
variable "pgvector_user" {
type = string
}
variable "pgvector_password" {
type = string
}ai_gateway_policies:
- ref: allow-and-deny-responses-pgvector
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: allow-and-deny-responses-pgvector
display_name: Allow and deny using pgvector as a vector database
type: ai-semantic-response-guard
enabled: true
config:
embeddings:
auth:
header_name: Authorization
header_value: Bearer $OPENAI_API_KEY
model:
name: text-embedding-3-small
provider: openai
search:
threshold: 0.7
vectordb:
strategy: pgvector
distance_metric: cosine
threshold: 0.7
dimensions: 1024
pgvector:
host: !env PGVECTOR_HOST
port: 5432
database: kong-pgvector
user: !env PGVECTOR_USER
password: !env PGVECTOR_PASSWORD
ssl: false
ssl_required: false
ssl_verify: false
ssl_version: tlsv1_2
timeout: 5000
rules:
allow_responses:
- Troubleshooting networks and connectivity issues
- Managing cloud platforms (AWS, Azure, GCP)
- Security hardening and incident response strategies
- DevOps pipelines, automation, and observability
- Software engineering concepts and language syntax
- IT governance, compliance, and regulatory guidance
- Continuous integration and deployment practices
- Writing documentation and explaining technical concepts
- Operating system administration and configuration
- Best practices for collaboration and productivity tools
deny_responses:
- Unauthorized penetration testing or hacking tutorials
- Methods for bypassing software licensing or DRM
- Step-by-step instructions for exploiting vulnerabilities
- Techniques to evade or disable security controls
- Collecting or exposing personal or employee data
- Using AI for impersonation, phishing, or fraud
- Manipulative social engineering techniques
- Advice on breaking internal IT or security policies
- Entertainment, dating, or other non-work topics
- Political, religious, or otherwise sensitive discussions unrelated to work
global: falseMake sure to replace the following placeholders with your own values:
-
AI_GATEWAY_ID: Theidof your AI Gateway. -
!env OPENAI_API_KEY: Your OpenAI API key -
!env PGVECTOR_HOST: The host where your pgvector-enabled PostgreSQL instance runs -
!env PGVECTOR_USER: Database user for pgvector -
!env PGVECTOR_PASSWORD: Database password for pgvector
curl -X POST https://{region}.api.konghq.com/v1/ai-gateways/{AIGatewayId}/policies \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"name": "allow-and-deny-responses-pgvector",
"display_name": "Allow and deny using pgvector as a vector database",
"type": "ai-semantic-response-guard",
"enabled": true,
"config": {
"embeddings": {
"auth": {
"header_name": "Authorization",
"header_value": "Bearer '$OPENAI_API_KEY'"
},
"model": {
"name": "text-embedding-3-small",
"provider": "openai"
}
},
"search": {
"threshold": 0.7
},
"vectordb": {
"strategy": "pgvector",
"distance_metric": "cosine",
"threshold": 0.7,
"dimensions": 1024,
"pgvector": {
"host": "'$PGVECTOR_HOST'",
"port": 5432,
"database": "kong-pgvector",
"user": "'$PGVECTOR_USER'",
"password": "'$PGVECTOR_PASSWORD'",
"ssl": false,
"ssl_required": false,
"ssl_verify": false,
"ssl_version": "tlsv1_2",
"timeout": 5000
}
},
"rules": {
"allow_responses": [
"Troubleshooting networks and connectivity issues",
"Managing cloud platforms (AWS, Azure, GCP)",
"Security hardening and incident response strategies",
"DevOps pipelines, automation, and observability",
"Software engineering concepts and language syntax",
"IT governance, compliance, and regulatory guidance",
"Continuous integration and deployment practices",
"Writing documentation and explaining technical concepts",
"Operating system administration and configuration",
"Best practices for collaboration and productivity tools"
],
"deny_responses": [
"Unauthorized penetration testing or hacking tutorials",
"Methods for bypassing software licensing or DRM",
"Step-by-step instructions for exploiting vulnerabilities",
"Techniques to evade or disable security controls",
"Collecting or exposing personal or employee data",
"Using AI for impersonation, phishing, or fraud",
"Manipulative social engineering techniques",
"Advice on breaking internal IT or security policies",
"Entertainment, dating, or other non-work topics",
"Political, religious, or otherwise sensitive discussions unrelated to work"
]
}
},
"global": false
}
'Make sure to replace the following placeholders with your own values:
-
region: Geographic region where your Kong Konnect is hosted and operates. -
KONNECT_TOKEN: Your Personal Access Token (PAT) associated with your Konnect account. -
AIGatewayId: Theidof the AI Gateway.
See the Konnect AI Gateway API reference to learn about region-specific URLs and personal access tokens.
Prerequisite: Configure your Personal Access Token
terraform {
required_providers {
konnect = {
source = "kong/konnect"
}
}
}
provider "konnect" {
personal_access_token = "$KONNECT_TOKEN"
server_url = "https://us.api.konghq.com/"
}Add the following to your Terraform configuration:
resource "konnect_ai_gateway_policy" "my_policy" {
name = "allow-and-deny-responses-pgvector"
display_name = "Allow and deny using pgvector as a vector database"
type = "ai-semantic-response-guard"
enabled = true
global = false
config = jsonencode({
embeddings = {
auth = {
header_name = "Authorization"
header_value = "Bearer var.openai_api_key"
}
model = {
name = "text-embedding-3-small"
provider = "openai"
}
}
search = {
threshold = 0.7
}
vectordb = {
strategy = "pgvector"
distance_metric = "cosine"
threshold = 0.7
dimensions = 1024
pgvector = {
host = var.pgvector_host
port = 5432
database = "kong-pgvector"
user = var.pgvector_user
password = var.pgvector_password
ssl = false
ssl_required = false
ssl_verify = false
ssl_version = "tlsv1_2"
timeout = 5000
}
}
rules = {
allow_responses = ["Troubleshooting networks and connectivity issues", "Managing cloud platforms (AWS, Azure, GCP)", "Security hardening and incident response strategies", "DevOps pipelines, automation, and observability", "Software engineering concepts and language syntax", "IT governance, compliance, and regulatory guidance", "Continuous integration and deployment practices", "Writing documentation and explaining technical concepts", "Operating system administration and configuration", "Best practices for collaboration and productivity tools"]
deny_responses = ["Unauthorized penetration testing or hacking tutorials", "Methods for bypassing software licensing or DRM", "Step-by-step instructions for exploiting vulnerabilities", "Techniques to evade or disable security controls", "Collecting or exposing personal or employee data", "Using AI for impersonation, phishing, or fraud", "Manipulative social engineering techniques", "Advice on breaking internal IT or security policies", "Entertainment, dating, or other non-work topics", "Political, religious, or otherwise sensitive discussions unrelated to work"]
}
})
gateway_id = konnect_ai_gateway.my_ai_gateway.id
}This example requires the following variables to be added to your manifest. You can specify values at runtime by setting TF_VAR_name=value.
variable "openai_api_key" {
type = string
}
variable "pgvector_host" {
type = string
}
variable "pgvector_user" {
type = string
}
variable "pgvector_password" {
type = string
}ai_gateway_policies:
- ref: allow-and-deny-responses-pgvector
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: allow-and-deny-responses-pgvector
display_name: Allow and deny using pgvector as a vector database
type: ai-semantic-response-guard
enabled: true
config:
embeddings:
auth:
header_name: Authorization
header_value: Bearer $OPENAI_API_KEY
model:
name: text-embedding-3-small
provider: openai
search:
threshold: 0.7
vectordb:
strategy: pgvector
distance_metric: cosine
threshold: 0.7
dimensions: 1024
pgvector:
host: !env PGVECTOR_HOST
port: 5432
database: kong-pgvector
user: !env PGVECTOR_USER
password: !env PGVECTOR_PASSWORD
ssl: false
ssl_required: false
ssl_verify: false
ssl_version: tlsv1_2
timeout: 5000
rules:
allow_responses:
- Troubleshooting networks and connectivity issues
- Managing cloud platforms (AWS, Azure, GCP)
- Security hardening and incident response strategies
- DevOps pipelines, automation, and observability
- Software engineering concepts and language syntax
- IT governance, compliance, and regulatory guidance
- Continuous integration and deployment practices
- Writing documentation and explaining technical concepts
- Operating system administration and configuration
- Best practices for collaboration and productivity tools
deny_responses:
- Unauthorized penetration testing or hacking tutorials
- Methods for bypassing software licensing or DRM
- Step-by-step instructions for exploiting vulnerabilities
- Techniques to evade or disable security controls
- Collecting or exposing personal or employee data
- Using AI for impersonation, phishing, or fraud
- Manipulative social engineering techniques
- Advice on breaking internal IT or security policies
- Entertainment, dating, or other non-work topics
- Political, religious, or otherwise sensitive discussions unrelated to work
global: falseMake sure to replace the following placeholders with your own values:
-
AI_GATEWAY_ID: Theidof your AI Gateway. -
!env OPENAI_API_KEY: Your OpenAI API key -
!env PGVECTOR_HOST: The host where your pgvector-enabled PostgreSQL instance runs -
!env PGVECTOR_USER: Database user for pgvector -
!env PGVECTOR_PASSWORD: Database password for pgvector
curl -X POST https://{region}.api.konghq.com/v1/ai-gateways/{AIGatewayId}/policies \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"name": "allow-and-deny-responses-pgvector",
"display_name": "Allow and deny using pgvector as a vector database",
"type": "ai-semantic-response-guard",
"enabled": true,
"config": {
"embeddings": {
"auth": {
"header_name": "Authorization",
"header_value": "Bearer '$OPENAI_API_KEY'"
},
"model": {
"name": "text-embedding-3-small",
"provider": "openai"
}
},
"search": {
"threshold": 0.7
},
"vectordb": {
"strategy": "pgvector",
"distance_metric": "cosine",
"threshold": 0.7,
"dimensions": 1024,
"pgvector": {
"host": "'$PGVECTOR_HOST'",
"port": 5432,
"database": "kong-pgvector",
"user": "'$PGVECTOR_USER'",
"password": "'$PGVECTOR_PASSWORD'",
"ssl": false,
"ssl_required": false,
"ssl_verify": false,
"ssl_version": "tlsv1_2",
"timeout": 5000
}
},
"rules": {
"allow_responses": [
"Troubleshooting networks and connectivity issues",
"Managing cloud platforms (AWS, Azure, GCP)",
"Security hardening and incident response strategies",
"DevOps pipelines, automation, and observability",
"Software engineering concepts and language syntax",
"IT governance, compliance, and regulatory guidance",
"Continuous integration and deployment practices",
"Writing documentation and explaining technical concepts",
"Operating system administration and configuration",
"Best practices for collaboration and productivity tools"
],
"deny_responses": [
"Unauthorized penetration testing or hacking tutorials",
"Methods for bypassing software licensing or DRM",
"Step-by-step instructions for exploiting vulnerabilities",
"Techniques to evade or disable security controls",
"Collecting or exposing personal or employee data",
"Using AI for impersonation, phishing, or fraud",
"Manipulative social engineering techniques",
"Advice on breaking internal IT or security policies",
"Entertainment, dating, or other non-work topics",
"Political, religious, or otherwise sensitive discussions unrelated to work"
]
}
},
"global": false
}
'Make sure to replace the following placeholders with your own values:
-
region: Geographic region where your Kong Konnect is hosted and operates. -
KONNECT_TOKEN: Your Personal Access Token (PAT) associated with your Konnect account. -
AIGatewayId: Theidof the AI Gateway.
See the Konnect AI Gateway API reference to learn about region-specific URLs and personal access tokens.
Prerequisite: Configure your Personal Access Token
terraform {
required_providers {
konnect = {
source = "kong/konnect"
}
}
}
provider "konnect" {
personal_access_token = "$KONNECT_TOKEN"
server_url = "https://us.api.konghq.com/"
}Add the following to your Terraform configuration:
resource "konnect_ai_gateway_policy" "my_policy" {
name = "allow-and-deny-responses-pgvector"
display_name = "Allow and deny using pgvector as a vector database"
type = "ai-semantic-response-guard"
enabled = true
global = false
config = jsonencode({
embeddings = {
auth = {
header_name = "Authorization"
header_value = "Bearer var.openai_api_key"
}
model = {
name = "text-embedding-3-small"
provider = "openai"
}
}
search = {
threshold = 0.7
}
vectordb = {
strategy = "pgvector"
distance_metric = "cosine"
threshold = 0.7
dimensions = 1024
pgvector = {
host = var.pgvector_host
port = 5432
database = "kong-pgvector"
user = var.pgvector_user
password = var.pgvector_password
ssl = false
ssl_required = false
ssl_verify = false
ssl_version = "tlsv1_2"
timeout = 5000
}
}
rules = {
allow_responses = ["Troubleshooting networks and connectivity issues", "Managing cloud platforms (AWS, Azure, GCP)", "Security hardening and incident response strategies", "DevOps pipelines, automation, and observability", "Software engineering concepts and language syntax", "IT governance, compliance, and regulatory guidance", "Continuous integration and deployment practices", "Writing documentation and explaining technical concepts", "Operating system administration and configuration", "Best practices for collaboration and productivity tools"]
deny_responses = ["Unauthorized penetration testing or hacking tutorials", "Methods for bypassing software licensing or DRM", "Step-by-step instructions for exploiting vulnerabilities", "Techniques to evade or disable security controls", "Collecting or exposing personal or employee data", "Using AI for impersonation, phishing, or fraud", "Manipulative social engineering techniques", "Advice on breaking internal IT or security policies", "Entertainment, dating, or other non-work topics", "Political, religious, or otherwise sensitive discussions unrelated to work"]
}
})
gateway_id = konnect_ai_gateway.my_ai_gateway.id
}This example requires the following variables to be added to your manifest. You can specify values at runtime by setting TF_VAR_name=value.
variable "openai_api_key" {
type = string
}
variable "pgvector_host" {
type = string
}
variable "pgvector_user" {
type = string
}
variable "pgvector_password" {
type = string
}ai_gateway_policies:
- ref: allow-and-deny-responses-pgvector
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: allow-and-deny-responses-pgvector
display_name: Allow and deny using pgvector as a vector database
type: ai-semantic-response-guard
enabled: true
config:
embeddings:
auth:
header_name: Authorization
header_value: Bearer $OPENAI_API_KEY
model:
name: text-embedding-3-small
provider: openai
search:
threshold: 0.7
vectordb:
strategy: pgvector
distance_metric: cosine
threshold: 0.7
dimensions: 1024
pgvector:
host: !env PGVECTOR_HOST
port: 5432
database: kong-pgvector
user: !env PGVECTOR_USER
password: !env PGVECTOR_PASSWORD
ssl: false
ssl_required: false
ssl_verify: false
ssl_version: tlsv1_2
timeout: 5000
rules:
allow_responses:
- Troubleshooting networks and connectivity issues
- Managing cloud platforms (AWS, Azure, GCP)
- Security hardening and incident response strategies
- DevOps pipelines, automation, and observability
- Software engineering concepts and language syntax
- IT governance, compliance, and regulatory guidance
- Continuous integration and deployment practices
- Writing documentation and explaining technical concepts
- Operating system administration and configuration
- Best practices for collaboration and productivity tools
deny_responses:
- Unauthorized penetration testing or hacking tutorials
- Methods for bypassing software licensing or DRM
- Step-by-step instructions for exploiting vulnerabilities
- Techniques to evade or disable security controls
- Collecting or exposing personal or employee data
- Using AI for impersonation, phishing, or fraud
- Manipulative social engineering techniques
- Advice on breaking internal IT or security policies
- Entertainment, dating, or other non-work topics
- Political, religious, or otherwise sensitive discussions unrelated to work
global: trueMake sure to replace the following placeholders with your own values:
-
AI_GATEWAY_ID: Theidof your AI Gateway. -
!env OPENAI_API_KEY: Your OpenAI API key -
!env PGVECTOR_HOST: The host where your pgvector-enabled PostgreSQL instance runs -
!env PGVECTOR_USER: Database user for pgvector -
!env PGVECTOR_PASSWORD: Database password for pgvector
curl -X POST https://{region}.api.konghq.com/v1/ai-gateways/{AIGatewayId}/policies \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"name": "allow-and-deny-responses-pgvector",
"display_name": "Allow and deny using pgvector as a vector database",
"type": "ai-semantic-response-guard",
"enabled": true,
"config": {
"embeddings": {
"auth": {
"header_name": "Authorization",
"header_value": "Bearer '$OPENAI_API_KEY'"
},
"model": {
"name": "text-embedding-3-small",
"provider": "openai"
}
},
"search": {
"threshold": 0.7
},
"vectordb": {
"strategy": "pgvector",
"distance_metric": "cosine",
"threshold": 0.7,
"dimensions": 1024,
"pgvector": {
"host": "'$PGVECTOR_HOST'",
"port": 5432,
"database": "kong-pgvector",
"user": "'$PGVECTOR_USER'",
"password": "'$PGVECTOR_PASSWORD'",
"ssl": false,
"ssl_required": false,
"ssl_verify": false,
"ssl_version": "tlsv1_2",
"timeout": 5000
}
},
"rules": {
"allow_responses": [
"Troubleshooting networks and connectivity issues",
"Managing cloud platforms (AWS, Azure, GCP)",
"Security hardening and incident response strategies",
"DevOps pipelines, automation, and observability",
"Software engineering concepts and language syntax",
"IT governance, compliance, and regulatory guidance",
"Continuous integration and deployment practices",
"Writing documentation and explaining technical concepts",
"Operating system administration and configuration",
"Best practices for collaboration and productivity tools"
],
"deny_responses": [
"Unauthorized penetration testing or hacking tutorials",
"Methods for bypassing software licensing or DRM",
"Step-by-step instructions for exploiting vulnerabilities",
"Techniques to evade or disable security controls",
"Collecting or exposing personal or employee data",
"Using AI for impersonation, phishing, or fraud",
"Manipulative social engineering techniques",
"Advice on breaking internal IT or security policies",
"Entertainment, dating, or other non-work topics",
"Political, religious, or otherwise sensitive discussions unrelated to work"
]
}
},
"global": true
}
'Make sure to replace the following placeholders with your own values:
-
region: Geographic region where your Kong Konnect is hosted and operates. -
KONNECT_TOKEN: Your Personal Access Token (PAT) associated with your Konnect account. -
AIGatewayId: Theidof the AI Gateway.
See the Konnect AI Gateway API reference to learn about region-specific URLs and personal access tokens.
Prerequisite: Configure your Personal Access Token
terraform {
required_providers {
konnect = {
source = "kong/konnect"
}
}
}
provider "konnect" {
personal_access_token = "$KONNECT_TOKEN"
server_url = "https://us.api.konghq.com/"
}Add the following to your Terraform configuration:
resource "konnect_ai_gateway_policy" "my_policy" {
name = "allow-and-deny-responses-pgvector"
display_name = "Allow and deny using pgvector as a vector database"
type = "ai-semantic-response-guard"
enabled = true
global = true
config = jsonencode({
embeddings = {
auth = {
header_name = "Authorization"
header_value = "Bearer var.openai_api_key"
}
model = {
name = "text-embedding-3-small"
provider = "openai"
}
}
search = {
threshold = 0.7
}
vectordb = {
strategy = "pgvector"
distance_metric = "cosine"
threshold = 0.7
dimensions = 1024
pgvector = {
host = var.pgvector_host
port = 5432
database = "kong-pgvector"
user = var.pgvector_user
password = var.pgvector_password
ssl = false
ssl_required = false
ssl_verify = false
ssl_version = "tlsv1_2"
timeout = 5000
}
}
rules = {
allow_responses = ["Troubleshooting networks and connectivity issues", "Managing cloud platforms (AWS, Azure, GCP)", "Security hardening and incident response strategies", "DevOps pipelines, automation, and observability", "Software engineering concepts and language syntax", "IT governance, compliance, and regulatory guidance", "Continuous integration and deployment practices", "Writing documentation and explaining technical concepts", "Operating system administration and configuration", "Best practices for collaboration and productivity tools"]
deny_responses = ["Unauthorized penetration testing or hacking tutorials", "Methods for bypassing software licensing or DRM", "Step-by-step instructions for exploiting vulnerabilities", "Techniques to evade or disable security controls", "Collecting or exposing personal or employee data", "Using AI for impersonation, phishing, or fraud", "Manipulative social engineering techniques", "Advice on breaking internal IT or security policies", "Entertainment, dating, or other non-work topics", "Political, religious, or otherwise sensitive discussions unrelated to work"]
}
})
gateway_id = konnect_ai_gateway.my_ai_gateway.id
}This example requires the following variables to be added to your manifest. You can specify values at runtime by setting TF_VAR_name=value.
variable "openai_api_key" {
type = string
}
variable "pgvector_host" {
type = string
}
variable "pgvector_user" {
type = string
}
variable "pgvector_password" {
type = string
}After creating the policy, add allow-and-deny-responses-pgvector to the policies array of your AI Model. The tool examples update an existing AI Model with the policy attached: replace the entity configuration in the example with your own, as the update replaces the entire AI Model.
ai_gateway_models:
- ref: my-gpt-4o
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
display_name: my-gpt-4o
name: my-gpt-4o
type: model
capabilities:
- generate
formats:
- type: openai
targets:
- name: gpt-4o
provider: my-openai-account
config:
type: openai
config:
route:
paths:
- "/v1"
model:
body_param: model
values:
- my-gpt-4o
logging:
payloads: false
policies:
- !ref allow-and-deny-responses-pgvector#nameAfter creating the policy, add allow-and-deny-responses-pgvector to the policies array of your AI Model. The tool examples update an existing AI Model with the policy attached: replace the entity configuration in the example with your own, as the update replaces the entire AI Model.
curl -X PUT https://{region}.api.konghq.com/v1/ai-gateways/{AIGatewayId}/models/my-gpt-4o \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"display_name": "my-gpt-4o",
"name": "my-gpt-4o",
"type": "model",
"capabilities": [
"generate"
],
"formats": [
{
"type": "openai"
}
],
"policies": [
"allow-and-deny-responses-pgvector"
],
"targets": [
{
"name": "gpt-4o",
"provider": "my-openai-account",
"config": {
"type": "openai"
}
}
],
"config": {
"route": {
"paths": [
"/v1"
],
"model": {
"body_param": "model",
"values": [
"my-gpt-4o"
]
}
},
"logging": {
"payloads": false
}
}
}
'After creating the policy, add allow-and-deny-responses-pgvector to the policies array of your AI Model. The tool examples update an existing AI Model with the policy attached: replace the entity configuration in the example with your own, as the update replaces the entire AI Model.
resource "konnect_ai_gateway_model" "my_model" {
display_name = "my-gpt-4o"
name = "my-gpt-4o"
type = "model"
capabilities = ["generate"]
formats = [
{
type = "openai"
} ]
targets = [
{
name = "gpt-4o"
provider = "my-openai-account"
config = {
type = "openai"
}
} ]
config = jsonencode({
route = {
paths = ["/v1"]
model = {
body_param = "model"
values = ["my-gpt-4o"]
}
}
logging = {
payloads = false
}
})
policies = [konnect_ai_gateway_policy.my_policy.name]
gateway_id = konnect_ai_gateway.my_ai_gateway.id
}This example requires the following variables to be added to your manifest. You can specify values at runtime by setting TF_VAR_name=value.
variable "openai_api_key" {
type = string
}
variable "pgvector_host" {
type = string
}
variable "pgvector_user" {
type = string
}
variable "pgvector_password" {
type = string
}After creating the policy, add allow-and-deny-responses-pgvector to the policies array of your AI Consumer. The tool examples update an existing AI Consumer with the policy attached: replace the entity configuration in the example with your own, as the update replaces the entire AI Consumer.
ai_gateway_consumers:
- ref: mobile-app-production
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
display_name: Mobile App - Production
name: mobile-app-production
type: api-key
policies:
- !ref allow-and-deny-responses-pgvector#nameAfter creating the policy, add allow-and-deny-responses-pgvector to the policies array of your AI Consumer. The tool examples update an existing AI Consumer with the policy attached: replace the entity configuration in the example with your own, as the update replaces the entire AI Consumer.
curl -X PUT https://{region}.api.konghq.com/v1/ai-gateways/{AIGatewayId}/consumers/mobile-app-production \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"display_name": "Mobile App - Production",
"name": "mobile-app-production",
"type": "api-key",
"policies": [
"allow-and-deny-responses-pgvector"
]
}
'After creating the policy, add allow-and-deny-responses-pgvector to the policies array of your AI Consumer. The tool examples update an existing AI Consumer with the policy attached: replace the entity configuration in the example with your own, as the update replaces the entire AI Consumer.
resource "konnect_ai_gateway_consumer" "my_consumer" {
display_name = "Mobile App - Production"
name = "mobile-app-production"
type = "api-key"
policies = [konnect_ai_gateway_policy.my_policy.name]
gateway_id = konnect_ai_gateway.my_ai_gateway.id
}This example requires the following variables to be added to your manifest. You can specify values at runtime by setting TF_VAR_name=value.
variable "openai_api_key" {
type = string
}
variable "pgvector_host" {
type = string
}
variable "pgvector_user" {
type = string
}
variable "pgvector_password" {
type = string
}After creating the policy, add allow-and-deny-responses-pgvector to the policies array of your AI Consumer Group. The tool examples update an existing AI Consumer Group with the policy attached: replace the entity configuration in the example with your own, as the update replaces the entire AI Consumer Group.
ai_gateway_consumer_groups:
- ref: internal-teams
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
display_name: Internal Teams
name: internal-teams
policies:
- !ref allow-and-deny-responses-pgvector#nameAfter creating the policy, add allow-and-deny-responses-pgvector to the policies array of your AI Consumer Group. The tool examples update an existing AI Consumer Group with the policy attached: replace the entity configuration in the example with your own, as the update replaces the entire AI Consumer Group.
curl -X PUT https://{region}.api.konghq.com/v1/ai-gateways/{AIGatewayId}/consumer-groups/internal-teams \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"display_name": "Internal Teams",
"name": "internal-teams",
"policies": [
"allow-and-deny-responses-pgvector"
]
}
'After creating the policy, add allow-and-deny-responses-pgvector to the policies array of your AI Consumer Group. The tool examples update an existing AI Consumer Group with the policy attached: replace the entity configuration in the example with your own, as the update replaces the entire AI Consumer Group.
resource "konnect_ai_gateway_consumer_group" "my_consumer_group" {
display_name = "Internal Teams"
name = "internal-teams"
policies = [konnect_ai_gateway_policy.my_policy.name]
gateway_id = konnect_ai_gateway.my_ai_gateway.id
}This example requires the following variables to be added to your manifest. You can specify values at runtime by setting TF_VAR_name=value.
variable "openai_api_key" {
type = string
}
variable "pgvector_host" {
type = string
}
variable "pgvector_user" {
type = string
}
variable "pgvector_password" {
type = string
}