-
Notifications
You must be signed in to change notification settings - Fork 3k
refactor: use OpenAiApi directly to return OpenAI Chat Completions format #6341
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
eye-gu
wants to merge
7
commits into
apache:master
Choose a base branch
from
eye-gu:fix-6340
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
03b40be
refactor: use OpenAiApi directly to return OpenAI Chat Completions fo…
eye-gu 69d8ff6
add test
eye-gu b3aa313
use openai cache
eye-gu 225cf0a
fix
eye-gu 37de2d9
fix review
eye-gu c6a6dd6
fix review
eye-gu 298ea96
Merge branch 'master' into fix-6340
eye-gu 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
157 changes: 157 additions & 0 deletions
157
...mmon/src/main/java/org/apache/shenyu/plugin/ai/common/protocol/OpenAiProtocolAdapter.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,157 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.apache.shenyu.plugin.ai.common.protocol; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| import org.apache.shenyu.common.exception.ShenyuException; | ||
| import org.apache.shenyu.plugin.ai.common.config.AiCommonConfig; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionRequest; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Adapts between OpenAI Chat Completions wire format and internal representations. | ||
| * | ||
| * <p>Note: deserialization into Spring AI's {@link ChatCompletionRequest} preserves fields | ||
| * modeled by that class (messages, model, temperature, maxTokens, stream, tools, etc.). | ||
| * Provider-specific extension fields not modeled by {@code ChatCompletionRequest} are dropped | ||
| * during deserialization. For the OpenAI-compatible providers this plugin currently supports, | ||
| * all required fields are covered. | ||
| */ | ||
| public final class OpenAiProtocolAdapter { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(OpenAiProtocolAdapter.class); | ||
|
|
||
| private static final com.fasterxml.jackson.databind.ObjectMapper MAPPER = | ||
| new com.fasterxml.jackson.databind.ObjectMapper(); | ||
|
|
||
| /** | ||
| * OpenAI API field name constants. | ||
| */ | ||
| private static final String FIELD_MODEL = "model"; | ||
|
|
||
| private static final String FIELD_TEMPERATURE = "temperature"; | ||
|
|
||
| private static final String FIELD_STREAM = "stream"; | ||
|
|
||
| private static final String FIELD_MAX_COMPLETION_TOKENS = "max_completion_tokens"; | ||
|
|
||
| private static final String FIELD_MAX_TOKENS = "max_tokens"; | ||
|
|
||
| private OpenAiProtocolAdapter() { | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the stream flag: client request body takes priority, | ||
| * falls back to the provided default value. | ||
| * | ||
| * @param requestBody the raw JSON request body | ||
| * @param fallbackStream the default stream value from admin config | ||
| * @return true if streaming, false otherwise | ||
| */ | ||
| public static boolean resolveStream(final String requestBody, final Boolean fallbackStream) { | ||
| if (Objects.isNull(requestBody) || requestBody.isEmpty()) { | ||
| return Boolean.TRUE.equals(fallbackStream); | ||
| } | ||
| final JsonNode root = parseStrict(requestBody); | ||
| if (Objects.isNull(root)) { | ||
| return Boolean.TRUE.equals(fallbackStream); | ||
| } | ||
| if (root.hasNonNull(FIELD_STREAM)) { | ||
| return root.get(FIELD_STREAM).asBoolean(); | ||
| } | ||
| return Boolean.TRUE.equals(fallbackStream); | ||
| } | ||
|
|
||
| /** | ||
| * Parse raw request body directly into ChatCompletionRequest, preserving fields | ||
| * modeled by Spring AI (including reasoning_content in assistant messages). | ||
| * | ||
| * <p>Spring AI's createRequest() loses reasoning_content, refusal, and annotations | ||
| * when reconstructing ChatCompletionMessage from AssistantMessage. | ||
| * This method avoids that loss by deserializing the raw JSON directly. | ||
| * | ||
| * <p>For max_tokens and max_completion_tokens: client values are preserved as-is; | ||
| * only when the client omits both fields does the fallbackConfig value populate both, | ||
| * ensuring compatibility with providers that require either field. | ||
| * | ||
| * @param requestBody the raw JSON request body in OpenAI Chat Completions format | ||
| * @param stream whether this is a streaming request (sets the stream field) | ||
| * @return a ChatCompletionRequest with modeled fields preserved from the original request | ||
| */ | ||
| public static ChatCompletionRequest toChatCompletionRequest(final String requestBody, final boolean stream) { | ||
| return toChatCompletionRequest(requestBody, stream, null); | ||
| } | ||
|
|
||
| /** | ||
| * Parse raw request body directly into ChatCompletionRequest, preserving modeled fields. | ||
| * When fallbackConfig is provided, its non-null fields (model, temperature, maxTokens) | ||
| * override the client request values, matching the original ChatModel-based fallback behavior | ||
| * where the fallback ChatModel's config takes precedence. | ||
| * | ||
| * @param requestBody the raw JSON request body in OpenAI Chat Completions format | ||
| * @param stream whether this is a streaming request | ||
| * @param fallbackConfig the fallback config whose non-null fields override client values | ||
| * @return a ChatCompletionRequest with modeled fields preserved | ||
| */ | ||
| public static ChatCompletionRequest toChatCompletionRequest(final String requestBody, | ||
| final boolean stream, final AiCommonConfig fallbackConfig) { | ||
| if (Objects.isNull(requestBody) || requestBody.isEmpty()) { | ||
| throw new ShenyuException("Request body must not be empty"); | ||
| } | ||
| final JsonNode root = parseStrict(requestBody); | ||
| if (Objects.isNull(root) || !root.isObject()) { | ||
| throw new ShenyuException("Invalid request body: expected a JSON object"); | ||
| } | ||
| final ObjectNode mutableRoot = (ObjectNode) root; | ||
|
|
||
| if (Objects.nonNull(fallbackConfig)) { | ||
| if (Objects.nonNull(fallbackConfig.getModel()) && !fallbackConfig.getModel().isEmpty()) { | ||
| mutableRoot.put(FIELD_MODEL, fallbackConfig.getModel()); | ||
| } | ||
| if (Objects.nonNull(fallbackConfig.getTemperature())) { | ||
| mutableRoot.put(FIELD_TEMPERATURE, fallbackConfig.getTemperature()); | ||
| } | ||
| if (Objects.nonNull(fallbackConfig.getMaxTokens())) { | ||
| mutableRoot.put(FIELD_MAX_TOKENS, fallbackConfig.getMaxTokens()); | ||
| mutableRoot.put(FIELD_MAX_COMPLETION_TOKENS, fallbackConfig.getMaxTokens()); | ||
| } | ||
|
eye-gu marked this conversation as resolved.
|
||
| } | ||
|
|
||
| mutableRoot.put(FIELD_STREAM, stream); | ||
|
|
||
| try { | ||
| return MAPPER.treeToValue(mutableRoot, ChatCompletionRequest.class); | ||
| } catch (Exception e) { | ||
| LOG.error("[AiProxy] Failed to parse request body into ChatCompletionRequest", e); | ||
| throw new ShenyuException("Failed to parse request body into ChatCompletionRequest", e); | ||
| } | ||
| } | ||
|
|
||
| private static JsonNode parseStrict(final String json) { | ||
| try { | ||
| return MAPPER.readTree(json); | ||
| } catch (JsonProcessingException e) { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
49 changes: 0 additions & 49 deletions
49
...ai-common/src/main/java/org/apache/shenyu/plugin/ai/common/strategy/FallbackStrategy.java
This file was deleted.
Oops, something went wrong.
68 changes: 0 additions & 68 deletions
68
...rc/main/java/org/apache/shenyu/plugin/ai/common/strategy/SimpleModelFallbackStrategy.java
This file was deleted.
Oops, something went wrong.
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.