Skip to content

Commit bbc21de

Browse files
committed
feat:add AIChat endpoint
1 parent 91ebd34 commit bbc21de

1 file changed

Lines changed: 56 additions & 26 deletions

File tree

base/src/main/java/com/tinyengine/it/controller/AiChatController.java

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/**
22
* Copyright (c) 2023 - present TinyEngine Authors.
33
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4-
*
4+
* <p>
55
* Use of this source code is governed by an MIT-style license.
6-
*
6+
* <p>
77
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
88
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
99
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10-
*
1110
*/
1211

1312
package com.tinyengine.it.controller;
@@ -18,6 +17,9 @@
1817
import com.tinyengine.it.model.dto.AiToken;
1918
import com.tinyengine.it.model.dto.ChatRequest;
2019

20+
import com.tinyengine.it.rag.entity.EmbeddingMatchDto;
21+
import com.tinyengine.it.rag.entity.SearchRequest;
22+
import com.tinyengine.it.rag.service.StorageService;
2123
import com.tinyengine.it.service.app.v1.AiChatV1Service;
2224
import io.swagger.v3.oas.annotations.Operation;
2325
import io.swagger.v3.oas.annotations.Parameter;
@@ -37,6 +39,8 @@
3739
import org.springframework.web.bind.annotation.RestController;
3840
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
3941

42+
import java.util.List;
43+
4044
/**
4145
* The type Ai chat controller.
4246
*
@@ -52,6 +56,11 @@ public class AiChatController {
5256
*/
5357
@Autowired
5458
private AiChatV1Service aiChatV1Service;
59+
/**
60+
* vector storage service
61+
*/
62+
@Autowired
63+
private StorageService vectorStorageService;
5564

5665
/**
5766
* AI api
@@ -60,17 +69,17 @@ public class AiChatController {
6069
* @return ai回答信息 result
6170
*/
6271
@Operation(summary = "获取ai回答信息", description = "获取ai回答信息",
63-
parameters = {
64-
@Parameter(name = "ChatRequest", description = "入参对象")
65-
}, responses = {
72+
parameters = {
73+
@Parameter(name = "ChatRequest", description = "入参对象")
74+
}, responses = {
6675
@ApiResponse(responseCode = "200", description = "返回信息",
67-
content = @Content(mediaType = "application/json", schema = @Schema())),
76+
content = @Content(mediaType = "application/json", schema = @Schema())),
6877
@ApiResponse(responseCode = "400", description = "请求失败")
6978
})
7079
@SystemControllerLog(description = "AI chat")
7180
@PostMapping("/ai/chat")
7281
public ResponseEntity<?> aiChat(@RequestBody ChatRequest request,
73-
@RequestHeader(value = "Authorization", required = false) String authorization) throws Exception {
82+
@RequestHeader(value = "Authorization", required = false) String authorization) throws Exception {
7483

7584
if (authorization != null && authorization.startsWith("Bearer ")) {
7685
String token = authorization.replace("Bearer ", "");
@@ -81,16 +90,36 @@ public ResponseEntity<?> aiChat(@RequestBody ChatRequest request,
8190

8291
if (request.isStream()) {
8392
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);
93+
.contentType(MediaType.TEXT_EVENT_STREAM)
94+
.header("Cache-Control", "no-cache")
95+
.header("X-Accel-Buffering", "no") // 禁用Nginx缓冲
96+
.body((StreamingResponseBody) response);
8897
} else {
8998
return ResponseEntity.ok(response);
9099
}
91100

92101
}
93102

103+
/**
104+
* search in collection
105+
*
106+
* @param searchDto the searchDto
107+
* @return result
108+
*/
109+
@Operation(summary = "在指定集合中搜索", description = "在指定集合中搜索",
110+
parameters = {
111+
@Parameter(name = "searchDto", description = "搜索请求参数体"),
112+
}, responses = {
113+
@ApiResponse(responseCode = "200", description = "返回信息",
114+
content = @Content(mediaType = "application/json", schema = @Schema())),
115+
@ApiResponse(responseCode = "400", description = "请求失败")
116+
})
117+
@SystemControllerLog(description = "AI search in collection")
118+
@PostMapping("/ai/search")
119+
public Result<List<EmbeddingMatchDto>> searchInCollection(@RequestBody SearchRequest searchDto) {
120+
List<EmbeddingMatchDto> results = vectorStorageService.search(searchDto);
121+
return Result.success(results);
122+
}
94123

95124
/**
96125
* AI api v1
@@ -99,17 +128,17 @@ public ResponseEntity<?> aiChat(@RequestBody ChatRequest request,
99128
* @return ai回答信息 result
100129
*/
101130
@Operation(summary = "获取ai回答信息", description = "获取ai回答信息",
102-
parameters = {
103-
@Parameter(name = "ChatRequest", description = "入参对象")
104-
}, responses = {
131+
parameters = {
132+
@Parameter(name = "ChatRequest", description = "入参对象")
133+
}, responses = {
105134
@ApiResponse(responseCode = "200", description = "返回信息",
106-
content = @Content(mediaType = "application/json", schema = @Schema())),
135+
content = @Content(mediaType = "application/json", schema = @Schema())),
107136
@ApiResponse(responseCode = "400", description = "请求失败")
108137
})
109138
@SystemControllerLog(description = "AI completions")
110139
@PostMapping("/chat/completions")
111140
public ResponseEntity<?> completions(@RequestBody ChatRequest request,
112-
@RequestHeader(value = "Authorization", required = false) String authorization) throws Exception {
141+
@RequestHeader(value = "Authorization", required = false) String authorization) throws Exception {
113142
if (authorization != null && authorization.startsWith("Bearer ")) {
114143
String token = authorization.replace("Bearer ", "");
115144
request.setApiKey(token);
@@ -119,33 +148,34 @@ public ResponseEntity<?> completions(@RequestBody ChatRequest request,
119148

120149
if (request.isStream()) {
121150
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);
151+
.contentType(MediaType.TEXT_EVENT_STREAM)
152+
.header("Cache-Control", "no-cache")
153+
.header("X-Accel-Buffering", "no") // 禁用Nginx缓冲
154+
.body((StreamingResponseBody) response);
126155
} else {
127156
return ResponseEntity.ok(response);
128157
}
129158
}
159+
130160
/**
131161
* get token
132162
*
133163
* @param request the request
134164
* @return ai回答信息 result
135165
*/
136166
@Operation(summary = "获取加密key信息", description = "获取加密key信息",
137-
parameters = {
138-
@Parameter(name = "request", description = "入参对象")
139-
}, responses = {
167+
parameters = {
168+
@Parameter(name = "request", description = "入参对象")
169+
}, responses = {
140170
@ApiResponse(responseCode = "200", description = "返回信息",
141-
content = @Content(mediaType = "application/json", schema = @Schema())),
171+
content = @Content(mediaType = "application/json", schema = @Schema())),
142172
@ApiResponse(responseCode = "400", description = "请求失败")
143173
})
144174
@SystemControllerLog(description = "get token")
145175
@PostMapping("/encrypt-key")
146176
public Result<AiToken> getToken(@RequestBody ChatRequest request) throws Exception {
147177
String apiKey = request.getApiKey();
148-
if(apiKey == null || apiKey.isEmpty()) {
178+
if (apiKey == null || apiKey.isEmpty()) {
149179
return Result.failed(ExceptionEnum.CM320);
150180
}
151181
String token = aiChatV1Service.getToken(apiKey);

0 commit comments

Comments
 (0)