forked from opentiny/tiny-engine-backend-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAiChatController.java
More file actions
183 lines (166 loc) · 7.02 KB
/
AiChatController.java
File metadata and controls
183 lines (166 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
package com.tinyengine.it.controller;
import com.tinyengine.it.common.base.Result;
import com.tinyengine.it.common.exception.ExceptionEnum;
import com.tinyengine.it.common.log.SystemControllerLog;
import com.tinyengine.it.model.dto.AiToken;
import com.tinyengine.it.model.dto.ChatRequest;
import com.tinyengine.it.rag.entity.EmbeddingMatchDto;
import com.tinyengine.it.rag.entity.SearchRequest;
import com.tinyengine.it.rag.service.StorageService;
import com.tinyengine.it.service.app.v1.AiChatV1Service;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
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.StreamingResponseBody;
import java.util.List;
/**
* The type Ai chat controller.
*
* @since 2024-10-20
*/
@Validated
@RestController
@RequestMapping("/app-center/api")
@Tag(name = "AIChat")
public class AiChatController {
/**
* The Ai chat v1 service.
*/
@Autowired
private AiChatV1Service aiChatV1Service;
@Autowired
private StorageService vectorStorageService;
/**
* AI api
*
* @param request the AI param
* @return ai回答信息 result
*/
@Operation(summary = "获取ai回答信息", description = "获取ai回答信息",
parameters = {
@Parameter(name = "ChatRequest", description = "入参对象")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema())),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "AI chat")
@PostMapping("/ai/chat")
public ResponseEntity<?> aiChat(@RequestBody ChatRequest request,
@RequestHeader(value = "Authorization", required = false) String authorization) throws Exception {
if (authorization != null && authorization.startsWith("Bearer ")) {
String token = authorization.replace("Bearer ", "");
request.setApiKey(token);
}
Object response = aiChatV1Service.chatCompletion(request);
if (request.isStream()) {
return ResponseEntity.ok()
.contentType(MediaType.TEXT_EVENT_STREAM)
.header("Cache-Control", "no-cache")
.header("X-Accel-Buffering", "no") // 禁用Nginx缓冲
.body((StreamingResponseBody) response);
} else {
return ResponseEntity.ok(response);
}
}
/**
* AI api v1
*
* @param request the AI param
* @return ai回答信息 result
*/
@Operation(summary = "获取ai回答信息", description = "获取ai回答信息",
parameters = {
@Parameter(name = "ChatRequest", description = "入参对象")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema())),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "AI completions")
@PostMapping("/chat/completions")
public ResponseEntity<?> completions(@RequestBody ChatRequest request,
@RequestHeader(value = "Authorization", required = false) String authorization) throws Exception {
if (authorization != null && authorization.startsWith("Bearer ")) {
String token = authorization.replace("Bearer ", "");
request.setApiKey(token);
}
Object response = aiChatV1Service.chatCompletion(request);
if (request.isStream()) {
return ResponseEntity.ok()
.contentType(MediaType.TEXT_EVENT_STREAM)
.header("Cache-Control", "no-cache")
.header("X-Accel-Buffering", "no") // 禁用Nginx缓冲
.body((StreamingResponseBody) response);
} else {
return ResponseEntity.ok(response);
}
}
/**
* get token
*
* @param request the request
* @return ai回答信息 result
*/
@Operation(summary = "获取加密key信息", description = "获取加密key信息",
parameters = {
@Parameter(name = "request", description = "入参对象")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema())),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "get token")
@PostMapping("/encrypt-key")
public Result<AiToken> getToken(@RequestBody ChatRequest request) throws Exception {
String apiKey = request.getApiKey();
if(apiKey == null || apiKey.isEmpty()) {
return Result.failed(ExceptionEnum.CM320);
}
String token = aiChatV1Service.getToken(apiKey);
return Result.success(new AiToken(token));
}
/**
* search in collection
*
* @param searchDto the searchDto
* @return result
*/
@Operation(summary = "在指定集合中搜索", description = "在指定集合中搜索",
parameters = {
@Parameter(name = "searchDto", description = "搜索请求参数体"),
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema())),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "AI search in collection")
@PostMapping("/ai/search")
public Result<List<EmbeddingMatchDto>> searchInCollection(@RequestBody SearchRequest searchDto) {
List<EmbeddingMatchDto> results = vectorStorageService.search(searchDto);
return Result.success(results);
}
}