-
Notifications
You must be signed in to change notification settings - Fork 52
feat: Add a universal OpenAI API #251
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
Conversation
…va into feat/docker
WalkthroughThis change introduces a new AI chat completion feature with support for streaming responses via server-sent events (SSE). It adds a new REST endpoint, DTO, service interface and implementation, a custom HTTP message converter for streaming, and configuration for OpenAI-compatible APIs. Exception handling and enums are updated to support stream write errors. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant AiChatController
participant AiChatV1Service
participant OpenAI API
participant StreamingResponseBodyConverter
Client->>AiChatController: POST /chat/completions (ChatRequest)
AiChatController->>AiChatV1Service: chatCompletion(request)
alt request.stream == true
AiChatV1Service->>OpenAI API: POST chat/completions (stream=true)
OpenAI API-->>AiChatV1Service: Streaming response (SSE)
AiChatV1Service-->>AiChatController: StreamingResponseBody
AiChatController-->>StreamingResponseBodyConverter: StreamingResponseBody
StreamingResponseBodyConverter-->>Client: SSE data stream
else request.stream == false
AiChatV1Service->>OpenAI API: POST chat/completions (stream=false)
OpenAI API-->>AiChatV1Service: JSON response
AiChatV1Service-->>AiChatController: JsonNode
AiChatController-->>Client: JSON response
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~40 minutes Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 13
🧹 Nitpick comments (3)
app/src/main/java/com/tinyengine/it/config/filter/WebConfig.java (2)
40-41
: Use English for code comments to maintain consistency.The Chinese comment should be in English to match the codebase's language standards.
- // 添加自定义的 StreamingResponseBody 转换器 + // Add custom StreamingResponseBody converter
34-36
: Add defensive null check for the injected converter.While Spring should handle this, adding a null check would make the code more robust.
public WebConfig(StreamingResponseBodyConverter streamingResponseBodyConverter) { + this.streamingResponseBodyConverter = Objects.requireNonNull( + streamingResponseBodyConverter, "StreamingResponseBodyConverter cannot be null"); - this.streamingResponseBodyConverter = streamingResponseBodyConverter; }Add the import:
+import java.util.Objects;
base/src/main/java/com/tinyengine/it/common/converter/StreamingResponseBodyConverter.java (1)
55-55
: Use English for code comments.For consistency and international collaboration, use English for inline comments.
- responseBody.writeTo(outputStream); // 使用 StreamingResponseBody 的 writeTo 方法 + responseBody.writeTo(outputStream); // Use StreamingResponseBody's writeTo method
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (8)
app/src/main/java/com/tinyengine/it/config/filter/WebConfig.java
(2 hunks)base/src/main/java/com/tinyengine/it/common/converter/StreamingResponseBodyConverter.java
(1 hunks)base/src/main/java/com/tinyengine/it/common/exception/ExceptionEnum.java
(1 hunks)base/src/main/java/com/tinyengine/it/config/OpenAIConfig.java
(1 hunks)base/src/main/java/com/tinyengine/it/controller/AiChatController.java
(4 hunks)base/src/main/java/com/tinyengine/it/model/dto/ChatRequest.java
(1 hunks)base/src/main/java/com/tinyengine/it/service/app/impl/v1/AiChatV1ServiceImpl.java
(1 hunks)base/src/main/java/com/tinyengine/it/service/app/v1/AiChatV1Service.java
(1 hunks)
🔇 Additional comments (1)
app/src/main/java/com/tinyengine/it/config/filter/WebConfig.java (1)
34-36
: LGTM! Clean dependency injection implementation.The constructor injection and field assignment follow Spring best practices correctly.
English | 简体中文
PR
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
Background and solution
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit
New Features
Bug Fixes