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(plugins): ai-proxy-plugin #12323

Merged
merged 4 commits into from
Jan 19, 2024
Merged

feat(plugins): ai-proxy-plugin #12323

merged 4 commits into from
Jan 19, 2024

Conversation

tysoekong
Copy link
Contributor

Summary

This commit transforms and proxies requests to a number of AI / LLM providers and models.

It adds a plugin that takes requests in one of a few defined and standardised formats, translates them to the configured target format, and then transforms the response back into a standard format.

The request/response formats are based on OpenAI, and conform to the JSON schema in the fixtures

There is a sample API specification to describe the suported formats also

The plugin controls:

  1. The backend URL (unless it's a self-hosted model, in which case set config.model.options.upstream_url)
  2. The API key insertion
  3. The request/response body transformation
  4. Capturing and storing metrics from the upstream responses, into normalised kong.log entries, which will then output via any configured logging platform e.g. file-log, kafka-log...

The reasoning for trying to flatten all provider formats, is that it allows standardised manipulation of the data before and after transmission.

It currently supports v1/chat and v1/completion style requests for all available providers. Each provider implementation class is in its own module under kong.plugins.ai-proxy.drivers.

You can also set mode preserve to pass through, and the plugin will perform a best-effort to conform to the chosen config.model.provider, based on the configured header patterns and values. This may need re-thinking, please provide input on this.

This implementation only supports REST-based full text responses, but streaming support could be added based upon adoption, as with Kong >3.0 we can now process WebSocket frames.

Ideal documentation (docs.konghq.com) is being finalised internally, will be ready soon.

Checklist

  • The Pull Request has tests
  • A changelog file has been created under changelog/unreleased/kong or skip-changelog label added on PR if changelog is unnecessary. README.md
  • There is a user-facing docs PR against https://github.com/Kong/docs.konghq.com - PUT DOCS PR HERE

@tysoekong tysoekong requested review from fffonion and flrgh January 9, 2024 22:32
@github-actions github-actions bot added chore Not part of the core functionality of kong, but still needed schema-change-noteworthy labels Jan 9, 2024
@tysoekong tysoekong force-pushed the feat/ai_proxy_plugin branch 2 times, most recently from a074d1c to d9b303e Compare January 9, 2024 22:50
@flrgh flrgh mentioned this pull request Jan 9, 2024
3 tasks
@tysoekong tysoekong force-pushed the feat/ai_proxy_plugin branch from 2fc22cd to 3b1216c Compare January 10, 2024 22:54
@tysoekong
Copy link
Contributor Author

@fffonion Anything else for me to do here?

@tysoekong tysoekong force-pushed the feat/ai_proxy_plugin branch 3 times, most recently from c634c5c to 7d6b50a Compare January 16, 2024 21:24
@RobSerafini RobSerafini requested a review from a team January 17, 2024 16:28
@tysoekong tysoekong force-pushed the feat/ai_proxy_plugin branch 2 times, most recently from 4b3f822 to 949a161 Compare January 17, 2024 17:36
Copy link
Contributor

@locao locao left a comment

Choose a reason for hiding this comment

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

This is great! 🚀

None of my comments are real blockers to merge this PR, just some suggestions.

kong/llm/drivers/anthropic.lua Outdated Show resolved Hide resolved
kong/llm/drivers/azure.lua Outdated Show resolved Hide resolved

if type(body) == "table" then
body_string, err = cjson.encode(body)
if err then return nil, nil, "failed to parse body to json: " .. err end
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if err then return nil, nil, "failed to parse body to json: " .. err end
if err then
return nil, nil, "failed to parse body to json: " .. err
end

kong/llm/drivers/anthropic.lua Outdated Show resolved Hide resolved
kong/llm/drivers/anthropic.lua Outdated Show resolved Hide resolved
kong/llm/drivers/openai.lua Outdated Show resolved Hide resolved
kong/llm/init.lua Outdated Show resolved Hide resolved

local _M = {}

local auth_schema = {
Copy link
Contributor

@locao locao Jan 17, 2024

Choose a reason for hiding this comment

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

The schemas could go to their own file to keep this file cleaner.

kong/llm/init.lua Outdated Show resolved Hide resolved
kong/llm/init.lua Outdated Show resolved Hide resolved
@tysoekong tysoekong force-pushed the feat/ai_proxy_plugin branch from 77b23dd to f5be8e6 Compare January 18, 2024 13:41
@tysoekong tysoekong removed the request for review from fffonion January 18, 2024 15:55
@tysoekong tysoekong force-pushed the feat/ai_proxy_plugin branch from f83b2e9 to a25535c Compare January 18, 2024 20:03
@flrgh flrgh added the cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee label Jan 19, 2024
@locao locao merged commit 58fe2dd into master Jan 19, 2024
23 checks passed
@locao locao deleted the feat/ai_proxy_plugin branch January 19, 2024 19:16
@team-gateway-bot
Copy link
Collaborator

Successfully created cherry-pick PR for master:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee chore Not part of the core functionality of kong, but still needed schema-change-noteworthy size/XXL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants