-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat: ai-prompt-decorator plugin #11515
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
Merged
shreemaan-abhishek
merged 13 commits into
apache:master
from
shreemaan-abhishek:feat/prompt-decorator
Aug 30, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ead0601
feat: ai-prompt-decorator plugin
shreemaan-abhishek f9688ea
fix lint
shreemaan-abhishek 49ea897
add docs
shreemaan-abhishek 0eaaa70
index to json
shreemaan-abhishek c65678f
plugins.t
shreemaan-abhishek e37ccfd
messages fix
shreemaan-abhishek 32e8534
token
shreemaan-abhishek eeedaa9
code review
shreemaan-abhishek c1f480f
add to yaml example
shreemaan-abhishek 564c8bb
from review
shreemaan-abhishek 036af42
remove upstream related misinfo
shreemaan-abhishek 05fda0a
Merge branch 'master' of github.com:apache/apisix into feat/prompt-de…
shreemaan-abhishek 6720411
template should run before decorator
shreemaan-abhishek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
-- | ||
-- Licensed to the Apache Software Foundation (ASF) under one or more | ||
-- contributor license agreements. See the NOTICE file distributed with | ||
-- this work for additional information regarding copyright ownership. | ||
-- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
-- (the "License"); you may not use this file except in compliance with | ||
-- the License. You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
local core = require("apisix.core") | ||
local ngx = ngx | ||
local pairs = pairs | ||
local EMPTY = {} | ||
|
||
local prompt_schema = { | ||
properties = { | ||
role = { | ||
type = "string", | ||
enum = { "system", "user", "assistant" } | ||
}, | ||
content = { | ||
type = "string", | ||
minLength = 1, | ||
} | ||
}, | ||
required = { "role", "content" } | ||
} | ||
|
||
local prompts = { | ||
type = "array", | ||
items = prompt_schema | ||
} | ||
|
||
local schema = { | ||
type = "object", | ||
properties = { | ||
prepend = prompts, | ||
append = prompts, | ||
}, | ||
anyOf = { | ||
{ required = { "prepend" } }, | ||
{ required = { "append" } }, | ||
{ required = { "append", "prepend" } }, | ||
}, | ||
} | ||
|
||
|
||
local _M = { | ||
version = 0.1, | ||
priority = 1070, | ||
name = "ai-prompt-decorator", | ||
schema = schema, | ||
} | ||
|
||
|
||
function _M.check_schema(conf) | ||
return core.schema.check(schema, conf) | ||
end | ||
|
||
|
||
local function get_request_body_table() | ||
local body, err = core.request.get_body() | ||
if not body then | ||
return nil, { message = "could not get body: " .. err } | ||
end | ||
|
||
local body_tab, err = core.json.decode(body) | ||
if not body_tab then | ||
return nil, { message = "could not get parse JSON request body: " .. err } | ||
end | ||
|
||
return body_tab | ||
end | ||
|
||
|
||
local function decorate(conf, body_tab) | ||
local new_messages = conf.prepend or EMPTY | ||
for _, message in pairs(body_tab.messages) do | ||
core.table.insert_tail(new_messages, message) | ||
end | ||
|
||
for _, message in pairs(conf.append or EMPTY) do | ||
core.table.insert_tail(new_messages, message) | ||
end | ||
|
||
body_tab.messages = new_messages | ||
end | ||
|
||
|
||
function _M.rewrite(conf, ctx) | ||
local body_tab, err = get_request_body_table() | ||
if not body_tab then | ||
return 400, err | ||
end | ||
|
||
if not body_tab.messages then | ||
return 400, "messages missing from request body" | ||
end | ||
decorate(conf, body_tab) -- will decorate body_tab in place | ||
|
||
local new_jbody, err = core.json.encode(body_tab) | ||
if not new_jbody then | ||
return 500, { message = "failed to parse modified JSON request body: " .. err } | ||
end | ||
|
||
ngx.req.set_body_data(new_jbody) | ||
end | ||
|
||
|
||
return _M |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
--- | ||
title: ai-prompt-decorator | ||
keywords: | ||
- Apache APISIX | ||
- API Gateway | ||
- Plugin | ||
- ai-prompt-decorator | ||
description: This document contains information about the Apache APISIX ai-prompt-decorator Plugin. | ||
--- | ||
|
||
<!-- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
--> | ||
|
||
## Description | ||
|
||
The `ai-prompt-decorator` plugin simplifies access to LLM providers, such as OpenAI and Anthropic, and their models by appending or prepending prompts into the request. | ||
|
||
## Plugin Attributes | ||
|
||
| **Field** | **Required** | **Type** | **Description** | | ||
| ----------------- | --------------- | -------- | --------------------------------------------------- | | ||
| `prepend` | Conditionally\* | Array | An array of prompt objects to be prepended | | ||
| `prepend.role` | Yes | String | Role of the message (`system`, `user`, `assistant`) | | ||
| `prepend.content` | Yes | String | Content of the message. Minimum length: 1 | | ||
| `append` | Conditionally\* | Array | An array of prompt objects to be appended | | ||
| `append.role` | Yes | String | Role of the message (`system`, `user`, `assistant`) | | ||
| `append.content` | Yes | String | Content of the message. Minimum length: 1 | | ||
|
||
\* **Conditionally Required**: At least one of `prepend` or `append` must be provided. | ||
|
||
## Example usage | ||
|
||
Create a route with the `ai-prompt-decorator` plugin like so: | ||
|
||
```shell | ||
curl "http://127.0.0.1:9180/apisix/admin/routes/1" -X PUT \ | ||
-H "X-API-KEY: ${ADMIN_API_KEY}" \ | ||
-d '{ | ||
"uri": "/v1/chat/completions", | ||
"plugins": { | ||
"ai-prompt-decorator": { | ||
"prepend":[ | ||
{ | ||
"role": "system", | ||
"content": "I have exams tomorrow so explain conceptually and briefly" | ||
} | ||
], | ||
"append":[ | ||
{ | ||
"role": "system", | ||
"content": "End the response with an analogy." | ||
} | ||
] | ||
} | ||
}, | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": { | ||
"api.openai.com:443": 1 | ||
}, | ||
"pass_host": "node", | ||
"scheme": "https" | ||
} | ||
}' | ||
``` | ||
|
||
Now send a request: | ||
|
||
```shell | ||
curl http://127.0.0.1:9080/v1/chat/completions -i -XPOST -H 'Content-Type: application/json' -d '{ | ||
"model": "gpt-4", | ||
"messages": [{ "role": "user", "content": "What is TLS Handshake?" }] | ||
}' -H "Authorization: Bearer <your token here>" | ||
``` | ||
|
||
Then the request body will be modified to something like this: | ||
|
||
```json | ||
{ | ||
"model": "gpt-4", | ||
"messages": [ | ||
{ | ||
"role": "system", | ||
"content": "I have exams tomorrow so explain conceptually and briefly" | ||
}, | ||
{ "role": "user", "content": "What is TLS Handshake?" }, | ||
{ | ||
"role": "system", | ||
"content": "End the response with an analogy." | ||
} | ||
] | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ai-prompt-decorator
plugin should run beforeai-prompt-decorator