Commit a8a9ca3
Ai gateway (#2937)
* feat: add AI API usage tracking and management components
* feat: implement OpenAI API interceptor with usage tracking and token limits
* feat: improve concurrency for API rate limits and enhance error handling
- Introduced thread-safe `AtomicLong` for token management.
- Synchronized reset logic in `AiApiLimit`.
- Improved error handling and null checks in OpenAI API interactions.
- Default-initialized user list in `SimpleAiApiStore`.
- Fixed getter for `AiApiStore` in interceptor.
* refactor: make `tokens` in `AiApiLimit` final to improve immutability
* feat: modularize AI providers and enhance OpenAI interceptor
- Removed `AiUtil` and replaced with modular `AiProvider` interface.
- Added provider implementations: `Claude`, `OpenAI`.
- Updated `OpenAIAPIInterceptor` to use configurable providers and enforce model restrictions.
- Introduced `NoAiApiLimit` for simplified limit management.
- Enhanced error handling with model validation in `OpenAiApiUtil`.
* feat: implement modular AI provider framework with request/response abstraction
- Added `AiApiRequest` and `AiApiResponse` abstractions for request/response handling.
- Introduced `AbstractAiApiRequest` and `AbstractAiApiResponse` as base classes.
- Implemented providers: `Google`, `OpenAI`, and `Claude` with concrete request/response handling.
- Updated `AiProvider` to handle request/response creation.
- Refactored `OpenAIAPIInterceptor` to leverage request/response abstraction and enforce contract restrictions.
- Enhanced `JsonUtil` with helper methods for JSON body parsing and updates.
- Updated `AiApiStore` and related classes for improved usage tracking and user abstraction.
* feat: enhance token limit management and error handling across AI components
- Updated `checkLimit` method to consider input and output tokens.
- Improved token calculation logic in AI request providers.
- Enhanced JSON parsing in `JsonUtil` with Optional for safer operations.
- Added detailed error handling in `OpenAIAPIInterceptor` for invalid requests.
- Refined token estimation logic with safety margins and JSON structure considerations.
* feat: improve concurrency and logging for API rate limit management
- Synchronized token management methods in `AiApiLimit` to ensure thread safety.
- Adjusted log levels for `SimpleAiApiStore` to reduce verbosity.
- Added PostgreSQL dependency to the distribution.
- Updated logging configuration to set debug level for AI interceptors.
* docs: clarify parameter description in `checkLimit` method
* feat: add SSE event parsing and improve token handling
- Introduced `SSEUtil` for parsing Server-Sent Events (SSE) from chunks.
- Enhanced `AbstractAiApiRequest` to handle JSON requests conditionally.
- Deprecated and replaced `max_output_tokens` usage in specific providers.
- Improved stream support in `OpenAiAiRequest` with response usage tracking.
- Refactored token limit logic in `OpenAIAPIInterceptor` for better flow.
* refactor: rename AI classes and interfaces for consistency
- Renamed `AiApiRequest` to `LLMRequest` and `AiApiResponse` to `LLMResponse`.
- Updated providers (`Google`, `OpenAI`, `Claude`) to align with `LLMProvider` interface.
- Refactored `OpenAIAPIInterceptor` to `LLMGatewayInterceptor` and related utilities.
- Removed `SSEUtil` and replaced with `SSEParser`.
- Improved streaming and token usage handling in `AbstractLLMResponse`.
* refactor: remove unused `terminalEvent` method from `LLMApiUtil`
* feat: refactor LLM APIs and improve SSE-driven event handling
- Modularized LLM responses for `Claude` and `OpenAI` providers.
- Replaced `LLMResponse` interface and `AbstractLLMResponse` with updated abstractions.
- Added `ChatCompletionsSSEParser` for advanced SSE chunk handling.
- Introduced specific SSE event classes: `ChatCompletionEvent`, `ChatCompletionDoneEvent`, `ResponsesApiEvent`.
- Renamed and restructured classes for consistency in AI namespace.
- Improved token usage tracking and event-based streaming.
* refactor: remove `ChatCompletionsSSEParser` and unused token usage tracking logic
- Deleted `ChatCompletionsSSEParser` and related classes/methods.
- Simplified `ChatCompletionEvent` by removing token usage parsing.
- Updated tool extraction logic in `AbstractLLMRequest` to handle function-specific tools.
* chore: add TODO for handling client-provided API key if config key is absent
* feat: enhance LLM response handling with modular classes and improved usage tracking
- Added `OpenAiLLMResponsesAPIResponse` for handling OpenAI Responses API.
- Refactored `OpenAiLLMResponse` to `OpenAiChatCompletionsLLMResponse`.
- Improved token usage calculations and SSE event processing.
- Updated `OpenAIProvider` to differentiate between Responses API and Chat Completions.
* feat: add logging for non-JSON requests in `AbstractLLMRequest`
- Introduced SLF4J logger to `AbstractLLMRequest`.
- Added log message for handling non-JSON requests.
- Improved exception handling with informative runtime error.
* refactor: replace `System.out.println` with proper debug logging in OpenAI response handlers
* feat: introduce Basic LLM Gateway tutorial and enhance OpenAI LLM handling
- Added `10-Basic-LLM-Gateway.yaml` tutorial for setting up a basic LLM gateway.
- Introduced new classes `AbstractOpenAiLLMRequest` and `OpenAiLLMChatCompletionsRequest` for modularizing token estimation and API handling.
- Improved token usage tracking with client-requested max output tokens.
- Added detailed inline documentation across AI-related classes for better maintainability.
- Updated `membrane.cmd` and `membrane.sh` for enhanced gateway setup.
* refactor: remove redundant semicolon in `processTerminalEvent` method
* refactor: simplify logic for API response handling and token usage tracking
- Removed redundant `isResponsesAPI` variable in `OpenAIProvider`.
- Optimized tool extraction in `OpenAiLLMResponsesRequest` and `OpenAiLLMChatCompletionsRequest`.
- Updated `AiApiLimit` to support unlimited tokens with `MAX_VALUE`.
- Replaced `token` with `apiKey` in `AiApiUser` along with added `tokens` field.
- Improved JSON parsing logic in `JsonUtil` with better exception handling and logging.
- Adjusted output token parameter naming in multiple request classes for consistency.
* refactor: remove outdated AI API limit management and centralize error handling
- Deleted `AiApiLimit` and `NoAiApiLimit` classes, consolidating token management into `SimpleAiApiStore`.
- Introduced `LLMErrorCreator` and its implementations (`OpenAiErrorCreator`, etc.) for reusable error generation.
- Refactored `LLMGatewayInterceptor` to utilize provider-specific error creators, simplifying token and model validation.
- Enhanced `SimpleAiApiStore` with token reset functionality and user-specific token tracking.
- Updated tutorials and examples to align with this refactored approach.
* refactor: enhance token usage tracking and improve inline documentation
- Added `resetTokensUsedInPeriod` for user-specific token reset in `SimpleAiApiStore`.
- Improved inline documentation for methods across `AiApiUser`, `AiApiStore`, and `LLMGatewayInterceptor`.
- Updated parameter descriptions for clarity and consistency.
* feat: enhance error handling, synchronization, and token tracking
- Added synchronized blocks to `SimpleAiApiStore` for thread-safe access to user data.
- Introduced `invalidRequestError` to `LLMErrorCreator` and implemented it in `OpenAiErrorCreator`.
- Allowed unlimited tokens for users with `MAX_VALUE` in `AiApiUser`.
- Simplified logic in `LLMGatewayInterceptor` for token and model validation.
- Updated tutorial JSON with test input for validation.
* feat: extend LLM Gateway with Claude support and improved error handling
- Added Claude-specific error handling with `ClaudeErrorCreator` and `ClaudeErrorResponse`.
- Introduced `10-Basic-LLM-Gateway.yaml` tutorial for Claude integration.
- Enhanced token usage tracking in `ClaudeLLMResponse`.
- Updated examples and tutorials to support both OpenAI and Claude.
* refactor: improve parameter documentation in `LLMErrorCreator`
* chore: add Apache 2.0 license headers to core files
* feat: add Google Gemini and enhance Claude tutorials with API key sharing and token limit examples
- Added `10-Basic-LLM-Gateway.yaml` and `20-Sharing-API-Keys.yaml` tutorials for Google Gemini.
- Enhanced Claude tutorials with improved key handling and token limit examples.
- Introduced `GoogleErrorCreator` for detailed error handling in Google LLM Gateway.
- Updated `LLMGatewayInterceptor` and token tracking logic to reflect effective max token handling.
- Modified existing OpenAI and Claude examples for consistency and clarity.
* feat: add AI LLM Gateway tests for Claude, OpenAI, and Google Gemini tutorials
- Introduced `AbstractAiTutorialTest` base class and provider-specific extensions for easier test creation.
- Added integration tests for basic gateway setups and API key sharing for Claude, OpenAI, and Google Gemini.
- Simulated upstream mock APIs to enable testing token limits, key handling, and input/output transformations.
* feat: improve token handling, configuration validation, and examples for LLM Gateway
- Ensure thread-safe access to users in `SimpleAiApiStore` with `List.copyOf`.
- Introduce `visibleRemaining` to handle non-negative token values in `GoogleErrorCreator`.
- Add configuration validation in `LLMGatewayInterceptor` to enforce API key substitution.
- Enhance token limit handling to adjust output tokens dynamically in `LLMGatewayInterceptor`.
- Update Google and Claude tutorials with clearer instructions for API key usage and token limits.
* feat: add streaming integration tests for OpenAI in LLM Gateway
- Introduced `StreamingOpenAiLLMGatewayTutorialTest` with SSE mocking and validation.
- Added JSON fixtures (`stream.json`, `max-output-stream.json`) for testing streaming requests.
- Enhanced base test framework to support `text/event-stream` responses.
- Updated `LLMGatewayInterceptor` to handle streaming scenarios with capped tokens.
* feat: standardize API key handling and logging in LLM Gateway tests
- Replaced raw API key placeholders with `TEST_API_KEY` constant in tutorial tests to ensure consistency.
- Added `TEST_API_KEY` to `AbstractAiTutorialTest` for upstream key substitution verification.
- Updated `log4j2.xml` to limit logging to `com.predic8.membrane.core.interceptor.ai`.
- Introduced PostgreSQL dependency in `pom.xml` for future enhancements.
* Update tutorial to use Anthropic-specific API key and headers
* Fix handling of invalid max output token requests in LLMGatewayInterceptor
* refactor: migrate OpenAI provider to Chat Completions framework and add policies support
- Unified OpenAI and Chat Completions error handling under `ChatCompletionsErrorCreator`.
- Deprecated older OpenAI-specific classes in favor of `ChatCompletions` equivalents.
- Introduced detailed usage policies handling in `LLMGatewayInterceptor`.
- Updated YAML tutorials to reflect the new `policies` configuration model.
* feat: introduce `Policies` class and update LLM Gateway to support policy-based token and model restrictions
- Added `Policies` class for defining restrictions on models, input tokens, and output tokens in the LLM Gateway.
- Replaced `maxInputTokens` and `maxOutputTokens` fields in `LLMGatewayInterceptor` with `Policies`.
- Updated YAML tutorials (OpenAI, Claude, Google) to use the new `policies` configuration.
* feat: refactor policies and introduce system prompt support
- Replaced `Policies` class implementation with `DefaultPolicies` and `NullPolicies` for enhanced flexibility.
- Added `SystemPrompt` class to support dynamic system prompt management in LLM Gateway.
- Updated `LLMGatewayInterceptor` to delegate policy enforcement and system prompt handling to respective components.
- Extended providers (OpenAI, Claude, Google Gemini) with standardized system prompt methods (`getSystemPrompt`, `setSystemPrompt`, `removeSystemPrompt`).
- Enhanced test coverage with `AbstractLLMRequestTest` for API key handling and bearer token case insensitivity.
* feat: extend `SystemPrompt` with new actions and update tests
- Added `setSystemPrompt`, `removeSystemPrompt`, and `isChatCompletion` methods for enhanced prompt management.
- Refactored `SystemPrompt.Action` to remove unused `REJECT` action.
- Updated `AbstractLLMRequestTest` to validate new `SystemPrompt` behaviors.
---------
Co-authored-by: Christian Gördes <christian.goerdes@outlook.de>
Co-authored-by: Christian Gördes <118011644+christiangoerdes@users.noreply.github.com>1 parent 2f20108 commit a8a9ca3
115 files changed
Lines changed: 6149 additions & 48 deletions
File tree
- annot/src/main/java/com/predic8/membrane/annot/yaml/parsing/binding
- core/src
- main/java/com/predic8/membrane/core
- interceptor
- llmgateway
- provider
- chatcompletions
- claude
- google
- openai
- store
- mcp
- jsonrpc
- mcp
- security
- util
- http
- jdbc
- json
- test/java/com/predic8/membrane/core
- interceptor
- llmgateway/provider
- mcp
- jsonrpc
- mcp
- util/http
- distribution
- src/test/java/com/predic8/membrane/tutorials/ai/llmgateway
- claude
- google
- openai
- tutorials/ai/llm-gateway
- claude
- google
- openai
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 11 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
| 36 | + | |
41 | 37 | | |
42 | 38 | | |
43 | 39 | | |
| |||
49 | 45 | | |
50 | 46 | | |
51 | 47 | | |
52 | | - | |
| 48 | + | |
| 49 | + | |
53 | 50 | | |
54 | 51 | | |
55 | 52 | | |
| |||
102 | 99 | | |
103 | 100 | | |
104 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
105 | 110 | | |
106 | 111 | | |
107 | 112 | | |
| |||
117 | 122 | | |
118 | 123 | | |
119 | 124 | | |
120 | | - | |
121 | 125 | | |
122 | 126 | | |
123 | 127 | | |
| |||
Lines changed: 69 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
Lines changed: 29 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
Lines changed: 77 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
Lines changed: 126 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
0 commit comments