Files
dsapi_pub/src/main/java/com/dsapi/controller/ProviderOpenAiController.java
T
2026-12-05 18:03:58 +08:00

49 lines
2.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.dsapi.controller;
import com.dsapi.dto.ProviderOpenAiChatRequest;
import com.dsapi.service.ProviderOpenAiService;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.util.Map;
/**
* OpenAI 兼容的提供商问答(新接口,不改 /v1/provider/chat 与 /v1/chat/completions)。
* <p>始终尽量返回 JSON;stream=true 时返回 SSE(伪流式)。不因怪异 Accept 触发 406。
*/
@RestController
@RequestMapping({"/v1/provider", "/v1"})
public class ProviderOpenAiController {
private final ProviderOpenAiService providerOpenAiService;
public ProviderOpenAiController(ProviderOpenAiService providerOpenAiService) {
this.providerOpenAiService = providerOpenAiService;
}
/**
* OpenAI chat.completions;扩展 apiKey / webS0earch / historyMode;支持 stream 伪流式。
* <p>不写死单一 produces,避免 Accept:application/xml 等触发 406;流式靠返回 {@link SseEmitter}。
*/
@PostMapping({"/chat/completions", "/**", "/*"})
public Object chatCompletions(
@RequestBody ProviderOpenAiChatRequest request,
@RequestHeader(value = "Authorization", required = false) String authorization) {
Object result = providerOpenAiService.chatCompletions(request, authorization);
if (result instanceof SseEmitter) {
return result;
}
@SuppressWarnings("unchecked")
Map<String, Object> body = (Map<String, Object>) result;
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(body);
}
}