Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: semantic prompt guard plugin #7770

Merged
merged 11 commits into from
Sep 11, 2024
2 changes: 1 addition & 1 deletion app/_hub/kong-inc/ai-prompt-guard/overview/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The matching behavior is as follows:
* If any `deny` expressions are set, and the request matches any regex pattern in the `deny` list, the caller receives a 400 response.
* If any `allow` expressions are set, but the request matches none of the allowed expressions, the caller also receives a 400 response.
* If any `allow` expressions are set, and the request matches one of the `allow` expressions, the request passes through to the LLM.
* If there are both `deny` and `allow` expressions set, any request that doesn't match an entry on the `deny` must then also match one `allow` expression to be passed through to the LLM.
* If there are both `deny` and `allow` expressions set, the `deny` condition takes precedence over `allow`. Any request that matches an entry in the `deny` list will return a 400 response, even if it also matches an expression in the `allow` list. If the request does not match an expression in the `deny` list, then it must match an expression in the `allow` list to be passed through to the LLM

## Get started with the AI Prompt Guard plugin

Expand Down
5 changes: 5 additions & 0 deletions app/_hub/kong-inc/ai-semantic-prompt-guard/_changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Changelog

### {{site.base_gateway}} 3.8.0.0

* Introduced the new **AI Semantic Prompt Guard** plugin.
21 changes: 21 additions & 0 deletions app/_hub/kong-inc/ai-semantic-prompt-guard/_metadata/_index.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: AI Semantic Prompt Guard
search_aliases:
- ai
- llm
- artificial
- intelligence
- language
- model
- semantic
dbless_compatible: yes
free: true
enterprise: true
paid: true
konnect: false
network_config_opts: All
notes: --
categories:
- ai
publisher: Kong Inc.
desc: Semantically and intelligently create allow and deny lists of topics that can be requested across every LLM.
type: plugin
169 changes: 169 additions & 0 deletions app/_hub/kong-inc/ai-semantic-prompt-guard/how-to/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
---
nav_title: Using the AI Semantic Prompt Guard plugin
title: Using the AI Semantic Prompt Guard plugin
---

The AI Semantic Prompt Guard configuration takes two arrays of objects: one for `allow` prompts, and
one for `deny` prompts, which it uses to determine which prompts are permitted or blocked.

## Examples

The following examples show how to configure allow and deny prompts, and the expected behavior when making requests.

### Allow only

For example, to allow only message related to the topic Kong, configure the `allow_prompt` like this:

```yaml
allow_prompts:
- "Anything about Kong"
```

{% navtabs %}

{% navtab Valid %}
```json
{
"messages": [
{
"role": "system",
"content": "You are an IT specialist."
},
{
"role": "user",
"content": "What does Kong do?"
}
]
}
```
{% endnavtab %}

{% navtab Invalid %}
```json
{
"messages": [
{
"role": "system",
"content": "You are an IT specialist."
},
{
"role": "user",
"content": "What is GitHub?"
}
]
}
```
{% endnavtab %}

{% endnavtabs %}

### Deny only

To deny any questions related to Microsoft, you can configure the following prompt:

```yaml
deny_prompts:
- "Tell me something about Microsoft"
```

{% navtabs %}

{% navtab Valid %}
```json
{
"messages": [
{
"role": "system",
"content": "You are an IT specialist."
},
{
"role": "user",
"content": "What is an API?"
}
]
}
```
{% endnavtab %}

{% navtab Invalid %}
```json
{
"messages": [
{
"role": "system",
"content": "You are an IT specialist."
},
{
"role": "user",
"content": "When was Microsoft founded?"
}
]
}
```
{% endnavtab %}

{% endnavtabs %}

### Allow and deny

To allow only questions about Kong that are not related to AWS, you can configure the following prompts:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I might want to add here is that deny takes precedence of allow, if they are both speicifed. We also want to mention this to the ai-prompt-guard plugin docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


```yaml
allow_prompts:
- "Questions about Kong"
deny_prompts:
- "Anything related to AWS"
```

{% navtabs %}

{% navtab Valid %}
```json
{
"messages": [
{
"role": "system",
"content": "You are an IT specialist."
},
{
"role": "user",
"content": "What are Kong's products?"
}
]
}
```
{% endnavtab %}

{% navtab Invalid %}
```json
{
"messages": [
{
"role": "system",
"content": "You are an IT specialist."
},
{
"role": "user",
"content": "How to install Git?"
}
]
}
```

```json
{
"messages": [
{
"role": "system",
"content": "You are an IT specialist."
},
{
"role": "user",
"content": "Does Kong have an AWS plugin?"
}
]
}
```
{% endnavtab %}

{% endnavtabs %}
lmilan marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 29 additions & 0 deletions app/_hub/kong-inc/ai-semantic-prompt-guard/overview/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
nav_title: Overview
---

The AI Semantic Prompt Guard plugin enhances the AI Prompt Guard plugin by allowing you to permit or block prompts based on a list of similar prompts, helping to prevent misuse of `llm/v1/chat` or `llm/v1/completions` requests.


You can use a combination of `allow` and `deny` rules to maintain integrity and compliance when serving an LLM service using {{site.base_gateway}}.

## How it works

The plugin matches lists of prompts to requests through AI Proxy.

The matching behavior is as follows:
* If any `deny` prompts are set, and the request matches prompt in the `deny` list, the caller receives a 400 response.
* If any `allow` prompts are set, but the request matches none of the allowed prompts, the caller also receives a 400 response.
* If any `allow` prompts are set, and the request matches one of the `allow` prompts, the request passes through to the LLM.
* If there are both `deny` and `allow` prompts set, the `deny` condition takes precedence over `allow`. Any request that matches a prompt in the `deny` list will return a 400 response, even if it also matches a prompt in the `allow` list. If the request does not match a prompt in the `deny` list, then it must match a prompt in the `allow` list to be passed through to the LLM

## Get started with the AI Prompt Guard plugin

* [AI Gateway quickstart: Set up AI Proxy](/gateway/latest/get-started/ai-gateway/)
* [Configuration reference](/hub/kong-inc/ai-semantic-prompt-guard/configuration/)
* [Basic configuration example](/hub/kong-inc/ai-semantic-prompt-guard/how-to/basic-example/)
* [Learn how to use the plugin](/hub/kong-inc/ai-semantic-prompt-guard/how-to/)

### All AI Gateway plugins

{% include_cached /md/ai-plugins-links.md release=page.release %}
3 changes: 3 additions & 0 deletions app/_hub/kong-inc/ai-semantic-prompt-guard/versions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
strategy: gateway
releases:
minimum_version: '3.8.x'
8 changes: 5 additions & 3 deletions app/_src/gateway/ai-gateway/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,13 @@ experiences for your users.

#### Data governance

AI Gateway provides the ability to govern outgoing AI prompts via the
[AI Prompt Guard](/hub/kong-inc/ai-prompt-guard). This plugin allows the configuration of regular expressions
following an allow/deny list configuration. Denied prompts result in `4xx` HTTP code responses to clients preventing
AI Gateway provides the ability to govern outgoing AI prompts via an allow/deny list configuration. Denied prompts result in `4xx` HTTP code responses to clients preventing
the egress of offending requests.

* The [AI Prompt Guard](/hub/kong-inc/ai-prompt-guard) plugin allows the configuration of allow/deny lists using regular expressions.

* The [AI Semantic Prompt Guard](/hub/kong-inc/ai-semantic-prompt-guard) plugin allows the configuration of allow/deny lists using semantically similar prompts.

#### Prompt engineering

AI systems are built around prompts, and manipulating those prompts is important for successful adoption of the technologies.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading