1212
1313package com .tinyengine .it .controller ;
1414
15+ import com .tinyengine .it .common .base .Result ;
16+ import com .tinyengine .it .common .exception .ExceptionEnum ;
1517import com .tinyengine .it .common .log .SystemControllerLog ;
18+ import com .tinyengine .it .model .dto .AiToken ;
1619import com .tinyengine .it .model .dto .ChatRequest ;
1720
1821import com .tinyengine .it .service .app .v1 .AiChatV1Service ;
2427import io .swagger .v3 .oas .annotations .tags .Tag ;
2528
2629import org .springframework .beans .factory .annotation .Autowired ;
27- import org .springframework .http .HttpStatus ;
2830import org .springframework .http .MediaType ;
2931import org .springframework .http .ResponseEntity ;
3032import org .springframework .validation .annotation .Validated ;
@@ -50,6 +52,7 @@ public class AiChatController {
5052 */
5153 @ Autowired
5254 private AiChatV1Service aiChatV1Service ;
55+
5356 /**
5457 * AI api
5558 *
@@ -66,23 +69,29 @@ public class AiChatController {
6669 })
6770 @ SystemControllerLog (description = "AI chat" )
6871 @ PostMapping ("/ai/chat" )
69- public ResponseEntity <?> aiChat (@ RequestBody ChatRequest request ) {
70- try {
71- Object response = aiChatV1Service .chatCompletion (request );
72+ public ResponseEntity <?> aiChat (@ RequestBody ChatRequest request ,
73+ @ RequestHeader (value = "Authorization" , required = false ) String authorization ) throws Exception {
7274
73- if (request .isStream ()) {
74- return ResponseEntity .ok ()
75- .contentType (MediaType .TEXT_EVENT_STREAM )
76- .body ((StreamingResponseBody ) response );
77- } else {
78- return ResponseEntity .ok (response );
79- }
80- } catch (Exception e ) {
81- return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR )
82- .body (e .getMessage ());
75+ if (authorization != null && authorization .startsWith ("Bearer " )) {
76+ String token = authorization .replace ("Bearer " , "" );
77+ request .setApiKey (token );
8378 }
79+
80+ Object response = aiChatV1Service .chatCompletion (request );
81+
82+ if (request .isStream ()) {
83+ return ResponseEntity .ok ()
84+ .contentType (MediaType .TEXT_EVENT_STREAM )
85+ .header ("Cache-Control" , "no-cache" )
86+ .header ("X-Accel-Buffering" , "no" ) // 禁用Nginx缓冲
87+ .body ((StreamingResponseBody ) response );
88+ } else {
89+ return ResponseEntity .ok (response );
90+ }
91+
8492 }
8593
94+
8695 /**
8796 * AI api v1
8897 *
@@ -100,24 +109,46 @@ public ResponseEntity<?> aiChat(@RequestBody ChatRequest request) {
100109 @ SystemControllerLog (description = "AI completions" )
101110 @ PostMapping ("/chat/completions" )
102111 public ResponseEntity <?> completions (@ RequestBody ChatRequest request ,
103- @ RequestHeader ("Authorization" ) String authorization ) {
112+ @ RequestHeader (value = "Authorization" , required = false ) String authorization ) throws Exception {
104113 if (authorization != null && authorization .startsWith ("Bearer " )) {
105114 String token = authorization .replace ("Bearer " , "" );
106115 request .setApiKey (token );
107116 }
108- try {
109- Object response = aiChatV1Service .chatCompletion (request );
110117
111- if (request .isStream ()) {
112- return ResponseEntity .ok ()
113- .contentType (MediaType .TEXT_EVENT_STREAM )
114- .body ((StreamingResponseBody ) response );
115- } else {
116- return ResponseEntity .ok (response );
117- }
118- } catch (Exception e ) {
119- return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR )
120- .body (e .getMessage ());
118+ Object response = aiChatV1Service .chatCompletion (request );
119+
120+ if (request .isStream ()) {
121+ return ResponseEntity .ok ()
122+ .contentType (MediaType .TEXT_EVENT_STREAM )
123+ .header ("Cache-Control" , "no-cache" )
124+ .header ("X-Accel-Buffering" , "no" ) // 禁用Nginx缓冲
125+ .body ((StreamingResponseBody ) response );
126+ } else {
127+ return ResponseEntity .ok (response );
128+ }
129+ }
130+ /**
131+ * get token
132+ *
133+ * @param request the request
134+ * @return ai回答信息 result
135+ */
136+ @ Operation (summary = "获取加密key信息" , description = "获取加密key信息" ,
137+ parameters = {
138+ @ Parameter (name = "request" , description = "入参对象" )
139+ }, responses = {
140+ @ ApiResponse (responseCode = "200" , description = "返回信息" ,
141+ content = @ Content (mediaType = "application/json" , schema = @ Schema ())),
142+ @ ApiResponse (responseCode = "400" , description = "请求失败" )
143+ })
144+ @ SystemControllerLog (description = "get token" )
145+ @ PostMapping ("/encrypt-key" )
146+ public Result <AiToken > getToken (@ RequestBody ChatRequest request ) throws Exception {
147+ String apiKey = request .getApiKey ();
148+ if (apiKey == null || apiKey .isEmpty ()) {
149+ return Result .failed (ExceptionEnum .CM320 );
121150 }
151+ String token = aiChatV1Service .getToken (apiKey );
152+ return Result .success (new AiToken (token ));
122153 }
123154}
0 commit comments