-
Notifications
You must be signed in to change notification settings - Fork 165
Ai gateway #2937
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
Open
predic8
wants to merge
48
commits into
master
Choose a base branch
from
ai-gateway
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ai gateway #2937
Changes from all commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
cf84c3b
feat: add AI API usage tracking and management components
predic8 d792f5f
feat: implement OpenAI API interceptor with usage tracking and token …
predic8 77def74
feat: improve concurrency for API rate limits and enhance error handling
predic8 8fa0d95
refactor: make `tokens` in `AiApiLimit` final to improve immutability
predic8 1e162fc
Merge branch 'master' into ai-gateway
predic8 d44e839
feat: modularize AI providers and enhance OpenAI interceptor
predic8 356a4c5
Merge remote-tracking branch 'origin/ai-gateway' into ai-gateway
predic8 863e5cc
feat: implement modular AI provider framework with request/response a…
predic8 2933dad
feat: enhance token limit management and error handling across AI com…
predic8 402651d
feat: improve concurrency and logging for API rate limit management
predic8 ece8d1d
docs: clarify parameter description in `checkLimit` method
predic8 5dc5513
feat: add SSE event parsing and improve token handling
predic8 d49826a
refactor: rename AI classes and interfaces for consistency
predic8 f0a3b21
refactor: remove unused `terminalEvent` method from `LLMApiUtil`
predic8 1f0d509
Merge branch 'master' into ai-gateway
predic8 f11e491
feat: refactor LLM APIs and improve SSE-driven event handling
predic8 b124aa0
refactor: remove `ChatCompletionsSSEParser` and unused token usage tr…
predic8 20b0919
Merge remote-tracking branch 'origin/ai-gateway' into ai-gateway
predic8 062ac5c
Merge branch 'master' into ai-gateway
predic8 46ce127
chore: add TODO for handling client-provided API key if config key is…
predic8 a97ddcf
Merge remote-tracking branch 'origin/ai-gateway' into ai-gateway
predic8 5d7be1e
feat: enhance LLM response handling with modular classes and improved…
predic8 2bfca01
feat: add logging for non-JSON requests in `AbstractLLMRequest`
predic8 3e9a014
refactor: replace `System.out.println` with proper debug logging in O…
predic8 8ea5320
Merge branch 'master' into ai-gateway
predic8 68705e1
feat: introduce Basic LLM Gateway tutorial and enhance OpenAI LLM han…
predic8 27c644d
refactor: remove redundant semicolon in `processTerminalEvent` method
predic8 97e597c
Merge remote-tracking branch 'origin/ai-gateway' into ai-gateway
predic8 383856a
refactor: simplify logic for API response handling and token usage tr…
predic8 169cb2c
refactor: remove outdated AI API limit management and centralize erro…
predic8 0af0f35
refactor: enhance token usage tracking and improve inline documentation
predic8 329bb80
feat: enhance error handling, synchronization, and token tracking
predic8 56d6e71
feat: extend LLM Gateway with Claude support and improved error handling
predic8 e0157c8
refactor: improve parameter documentation in `LLMErrorCreator`
predic8 11491c6
chore: add Apache 2.0 license headers to core files
predic8 6485949
feat: add Google Gemini and enhance Claude tutorials with API key sha…
predic8 38acaa3
feat: add AI LLM Gateway tests for Claude, OpenAI, and Google Gemini …
predic8 9d38d84
feat: improve token handling, configuration validation, and examples …
predic8 df3d145
feat: add streaming integration tests for OpenAI in LLM Gateway
predic8 d088dab
feat: standardize API key handling and logging in LLM Gateway tests
predic8 5e3ced7
Merge branch 'master' into ai-gateway
predic8 ab2fac9
Update tutorial to use Anthropic-specific API key and headers
christiangoerdes 37f7515
Fix handling of invalid max output token requests in LLMGatewayInterc…
christiangoerdes d903931
Merge branch 'master' into ai-gateway
christiangoerdes 44850fe
refactor: migrate OpenAI provider to Chat Completions framework and a…
predic8 12991ac
Merge remote-tracking branch 'origin/ai-gateway' into ai-gateway
predic8 b644a65
feat: introduce `Policies` class and update LLM Gateway to support po…
predic8 b6ff531
Merge branch 'master' into ai-gateway
predic8 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
69 changes: 69 additions & 0 deletions
69
core/src/main/java/com/predic8/membrane/core/interceptor/llmgateway/AbstractLLMEvent.java
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,69 @@ | ||
| /* Copyright 2026 predic8 GmbH, www.predic8.com | ||
|
|
||
| Licensed 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. */ | ||
|
|
||
| package com.predic8.membrane.core.interceptor.llmgateway; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.predic8.membrane.core.util.http.SSEParser; | ||
| import com.predic8.membrane.core.util.json.JsonUtil; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public abstract class AbstractLLMEvent { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(AbstractLLMEvent.class); | ||
|
|
||
| protected static final ObjectMapper om = new ObjectMapper(); | ||
|
|
||
| protected final JsonNode json; | ||
|
|
||
| protected AbstractLLMEvent(JsonNode json) { | ||
| this.json = json; | ||
| } | ||
|
|
||
| public abstract String getType(); | ||
|
|
||
| public JsonNode getJson() { | ||
| return json; | ||
| } | ||
|
|
||
| public static AbstractLLMEvent create(SSEParser.SSEEvent sse) { | ||
|
|
||
| if ("[DONE]".equals(sse.data())) { | ||
| return new ChatCompletionDoneEvent(); | ||
| } | ||
|
|
||
| var opt = JsonUtil.getJsonObject(sse.data()); | ||
| if (opt.isEmpty()) { | ||
| log.info("Unknown event format: {}", sse.data()); | ||
| } | ||
|
|
||
| var json = opt.get(); | ||
|
|
||
| // Responses API | ||
| if (json.has("type")) { | ||
| return new ResponsesApiEvent(json); | ||
| } | ||
|
|
||
| // Chat Completions API | ||
| if ("chat.completion.chunk".equals(json.path("object").asText())) { | ||
| return new ChatCompletionEvent(json); | ||
| } | ||
|
|
||
| log.debug("Unknown event format: {}", json); | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
29 changes: 29 additions & 0 deletions
29
...c/main/java/com/predic8/membrane/core/interceptor/llmgateway/ChatCompletionDoneEvent.java
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,29 @@ | ||
| /* Copyright 2026 predic8 GmbH, www.predic8.com | ||
|
|
||
| Licensed 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. */ | ||
|
|
||
| package com.predic8.membrane.core.interceptor.llmgateway; | ||
|
|
||
| import com.fasterxml.jackson.databind.node.NullNode; | ||
|
|
||
| public class ChatCompletionDoneEvent extends AbstractLLMEvent { | ||
|
|
||
| public ChatCompletionDoneEvent() { | ||
| super(NullNode.getInstance()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getType() { | ||
| return "chat.completion.done"; | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
core/src/main/java/com/predic8/membrane/core/interceptor/llmgateway/ChatCompletionEvent.java
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,77 @@ | ||
| /* Copyright 2026 predic8 GmbH, www.predic8.com | ||
|
|
||
| Licensed 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. */ | ||
|
|
||
| package com.predic8.membrane.core.interceptor.llmgateway; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class ChatCompletionEvent extends AbstractLLMEvent { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(ChatCompletionEvent.class); | ||
|
|
||
| public ChatCompletionEvent(JsonNode json) { | ||
| super(json); | ||
|
|
||
| parseChoices(json); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| private static void parseChoices(JsonNode json) { | ||
| for (JsonNode choice : json.path("choices")) { | ||
|
|
||
| JsonNode delta = choice.path("delta"); | ||
|
|
||
| if (delta.has("content")) { | ||
| log.debug("Content delta: {}", | ||
| delta.path("content").asText()); | ||
| } | ||
|
|
||
| if (delta.has("tool_calls")) { | ||
|
|
||
| for (JsonNode tc : delta.path("tool_calls")) { | ||
|
|
||
| JsonNode fn = tc.path("function"); | ||
|
|
||
| if (fn.has("name")) { | ||
| log.debug("Tool call name delta: {}", | ||
| fn.path("name").asText()); | ||
| } | ||
|
|
||
| if (fn.has("arguments")) { | ||
| log.debug("Tool call arguments delta: {}", | ||
| fn.path("arguments").asText()); | ||
|
predic8 marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| String finishReason = choice.path("finish_reason").asText(null); | ||
|
|
||
| if (finishReason != null && !"null".equals(finishReason)) { | ||
| log.debug("Finish reason: {}", finishReason); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getType() { | ||
| return "chat.completion.chunk"; | ||
| } | ||
|
|
||
| public JsonNode getChoices() { | ||
| return json.path("choices"); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.