Skip to content

refactor: use OpenAiApi directly to return OpenAI Chat Completions format#6341

Open
eye-gu wants to merge 5 commits into
apache:masterfrom
eye-gu:fix-6340
Open

refactor: use OpenAiApi directly to return OpenAI Chat Completions format#6341
eye-gu wants to merge 5 commits into
apache:masterfrom
eye-gu:fix-6340

Conversation

@eye-gu
Copy link
Copy Markdown
Contributor

@eye-gu eye-gu commented May 13, 2026

close #6340
Refactors the ai-proxy plugin to call upstream providers via OpenAiApi directly instead of Spring AI's ChatClient, ensuring responses conform to the standard OpenAI Chat Completions API format.

Changes

  • New: OpenAiProtocolAdapter — parses raw request JSON into ChatCompletionRequest, preserving all fields (including reasoning_content) that Spring AI's createRequest() would lose. Also resolves stream flag from client request with admin config as fallback, and converts max_completion_tokensmax_tokens for compatibility.
  • New: UpstreamErrorLogger — shared utility to extract WebClientResponseException details for upstream error logging.
  • Removed: ChatClientCache — no longer needed since OpenAiApi instances are lightweight and stateless.
  • Removed: FallbackStrategy / SimpleModelFallbackStrategy — fallback is now handled inline with OpenAiApi directly.
  • Removed: AiModelFactoryRegistry dependency from the plugin — OpenAiApi is constructed directly from AiCommonConfig (baseUrl + apiKey).
  • Streaming: now emits ChatCompletionChunk SSE events + data: [DONE] terminator.
  • Non-streaming: returns ChatCompletion JSON directly.
  • Retry: non-streaming calls now use Retry.backoff(3, 1s) instead of Retry.max(1).

Testing

  • OpenAiProtocolAdapterTest — covers stream resolution, request parsing, field preservation, and fallback config merging.
  • Updated AiProxyPluginTest, AiProxyExecutorServiceTest, CommonAiProxyApiKeyDataSubscriberTest to match the new API.

Make sure that:

  • You have read the contribution guidelines.
  • You submit test cases (unit or integration tests) that back your changes.
  • Your local test passed ./mvnw clean install -Dmaven.javadoc.skip=true.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors the AI proxy plugin to call OpenAI-compatible upstream APIs directly through OpenAiApi, returning OpenAI Chat Completions-style responses instead of Spring AI ChatResponse objects.

Changes:

  • Adds request adaptation, direct OpenAI API execution, SSE chunk output, and upstream error logging.
  • Replaces ChatClient-based caching/fallback paths with OpenAiApi-based logic.
  • Updates tests for the new direct-call and protocol-adapter behavior.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
AiProxyPluginConfiguration.java Wires the refactored plugin, handler, executor, and API key subscriber.
CommonAiProxyApiKeyDataSubscriberTest.java Updates subscriber tests for constructor changes.
UpstreamErrorLoggerTest.java Adds tests for upstream error logging.
AiProxyExecutorServiceTest.java Updates executor tests for direct OpenAiApi calls.
AiProxyPluginHandlerTest.java Adds handler cache/removal tests.
AiProxyPluginTest.java Updates plugin tests for direct OpenAiApi execution.
CommonAiProxyApiKeyDataSubscriber.java Clears OpenAiApi cache on API key refresh.
UpstreamErrorLogger.java Adds shared upstream WebClient error logging.
AiProxyExecutorService.java Implements direct streaming/non-streaming OpenAiApi execution with retry/fallback.
AiProxyPluginHandler.java Switches invalidation to OpenAiApiCache and removes selector API keys on delete.
OpenAiApiCache.java Adds OpenAiApi instance cache and invalidation helpers.
AiProxyPlugin.java Builds OpenAiApi clients, adapts requests, and writes OpenAI-format responses.
OpenAiProtocolAdapterTest.java Adds tests for stream resolution and request-field fallback behavior.
SimpleModelFallbackStrategy.java Removes ChatClient fallback strategy.
FallbackStrategy.java Removes fallback strategy interface.
OpenAiProtocolAdapter.java Adds raw OpenAI request parsing and config fallback merging.
AiCommonConfig.java Makes default temperature unset instead of 0.8.
Comments suppressed due to low confidence (1)

shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-proxy/src/main/java/org/apache/shenyu/plugin/ai/proxy/enhanced/cache/OpenAiApiCache.java:102

  • This comment says the cache evicts the oldest entries, but the implementation iterates a ConcurrentHashMap, which has no insertion/access ordering and evicts arbitrary entries. Update the comment or use an ordered cache if oldest-entry eviction is required.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] AI Proxy plugin returns Spring AI ChatResponse instead of OpenAI Chat Completions format

2 participants