diff --git a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/po/PlatformPO.java b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/po/PlatformPO.java index df68a3031..99c1071fe 100644 --- a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/po/PlatformPO.java +++ b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/po/PlatformPO.java @@ -43,4 +43,7 @@ public class PlatformPO extends BasePO implements Serializable { @Column(name = "support_models", length = 255) private String supportModels; + + @Column(name = "desc") + private String desc; } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/LLMConfigController.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/LLMConfigController.java index 51f24a002..2b27cbb66 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/LLMConfigController.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/LLMConfigController.java @@ -56,6 +56,12 @@ public ResponseEntity> platforms() { return ResponseEntity.success(llmConfigService.platforms()); } + @Operation(summary = "get platform", description = "Get platform") + @GetMapping("/platforms/{id}") + public ResponseEntity getPlatform(@PathVariable Long id) { + return ResponseEntity.success(llmConfigService.getPlatform(id)); + } + @Operation(summary = "platform credentials", description = "Get platform auth credentials") @GetMapping("/platforms/{platformId}/auth-credentials") public ResponseEntity> platformsAuthCredential(@PathVariable Long platformId) { diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/converter/ChatThreadConverter.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/converter/ChatThreadConverter.java index 59aa3fae9..b28b3700e 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/converter/ChatThreadConverter.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/converter/ChatThreadConverter.java @@ -18,12 +18,15 @@ */ package org.apache.bigtop.manager.server.model.converter; +import org.apache.bigtop.manager.dao.po.AuthPlatformPO; import org.apache.bigtop.manager.dao.po.ChatThreadPO; +import org.apache.bigtop.manager.dao.po.PlatformPO; import org.apache.bigtop.manager.server.config.MapStructSharedConfig; import org.apache.bigtop.manager.server.model.dto.ChatThreadDTO; import org.apache.bigtop.manager.server.model.req.ChatbotThreadReq; import org.apache.bigtop.manager.server.model.vo.ChatThreadVO; +import org.mapstruct.Context; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.factory.Mappers; @@ -35,7 +38,10 @@ public interface ChatThreadConverter { ChatThreadConverter INSTANCE = Mappers.getMapper(ChatThreadConverter.class); @Mapping(source = "id", target = "threadId") - ChatThreadVO fromPO2VO(ChatThreadPO platformAuthorizedPO); + @Mapping(target = "model", expression = "java(authPlatformPO.getModel())") + @Mapping(target = "platformName", expression = "java(platformPO.getName())") + ChatThreadVO fromPO2VO( + ChatThreadPO platformAuthorizedPO, @Context AuthPlatformPO authPlatformPO, @Context PlatformPO platformPO); ChatThreadPO fromDTO2PO(ChatThreadDTO chatThreadDTO); diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/vo/ChatThreadVO.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/vo/ChatThreadVO.java index ce49396b1..846bbc397 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/vo/ChatThreadVO.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/vo/ChatThreadVO.java @@ -30,6 +30,10 @@ public class ChatThreadVO { private String name; + private String platformName; + + private String model; + private String createTime; private String updateTime; diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/vo/PlatformVO.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/vo/PlatformVO.java index 5e127229d..1e6c1c1eb 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/vo/PlatformVO.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/vo/PlatformVO.java @@ -28,9 +28,5 @@ public class PlatformVO { private String supportModels; - public PlatformVO(Long id, String name, String models) { - this.id = id; - this.name = name; - this.supportModels = models; - } + private String desc; } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/vo/TalkVO.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/vo/TalkVO.java new file mode 100644 index 000000000..f52de9798 --- /dev/null +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/model/vo/TalkVO.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.model.vo; + +import lombok.Data; + +@Data +public class TalkVO { + private String content; + + private String finishReason; +} diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/LLMConfigService.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/LLMConfigService.java index c54000023..223b5723c 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/LLMConfigService.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/LLMConfigService.java @@ -46,4 +46,6 @@ public interface LLMConfigService { boolean deactivateAuthorizedPlatform(Long authId); AuthPlatformVO getAuthorizedPlatform(Long authId); + + PlatformVO getPlatform(Long id); } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/ChatbotServiceImpl.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/ChatbotServiceImpl.java index 6bc707764..2ab8acdec 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/ChatbotServiceImpl.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/ChatbotServiceImpl.java @@ -44,6 +44,7 @@ import org.apache.bigtop.manager.server.model.dto.ChatThreadDTO; import org.apache.bigtop.manager.server.model.vo.ChatMessageVO; import org.apache.bigtop.manager.server.model.vo.ChatThreadVO; +import org.apache.bigtop.manager.server.model.vo.TalkVO; import org.apache.bigtop.manager.server.service.ChatbotService; import org.springframework.context.i18n.LocaleContextHolder; @@ -139,7 +140,7 @@ public ChatThreadVO createChatThread(ChatThreadDTO chatThreadDTO) { ChatThreadPO chatThreadPO = ChatThreadConverter.INSTANCE.fromDTO2PO(chatThreadDTO); chatThreadPO.setUserId(userId); chatThreadDao.save(chatThreadPO); - return ChatThreadConverter.INSTANCE.fromPO2VO(chatThreadPO); + return ChatThreadConverter.INSTANCE.fromPO2VO(chatThreadPO, authPlatformPO, platformPO); } @Override @@ -164,6 +165,8 @@ public List getAllChatThreads() { if (authPlatformPO == null) { throw new ApiException(ApiExceptionEnum.NO_PLATFORM_IN_USE); } + PlatformPO platformPO = platformDao.findById(authPlatformPO.getPlatformId()); + Long authId = authPlatformPO.getId(); Long userId = SessionUserHolder.getUserId(); List chatThreadPOS = chatThreadDao.findAllByAuthIdAndUserId(authId, userId); @@ -172,7 +175,8 @@ public List getAllChatThreads() { if (chatThreadPO.getIsDeleted()) { continue; } - ChatThreadVO chatThreadVO = ChatThreadConverter.INSTANCE.fromPO2VO(chatThreadPO); + ChatThreadVO chatThreadVO = + ChatThreadConverter.INSTANCE.fromPO2VO(chatThreadPO, authPlatformPO, platformPO); chatThreads.add(chatThreadVO); } return chatThreads; @@ -207,13 +211,36 @@ public SseEmitter talk(Long threadId, String message) { stringFlux.subscribe( s -> { try { - emitter.send(s); + TalkVO talkVO = new TalkVO(); + talkVO.setContent(s); + talkVO.setFinishReason(null); + emitter.send(talkVO); } catch (Exception e) { emitter.completeWithError(e); } }, - Throwable::printStackTrace, - emitter::complete); + throwable -> { + try { + TalkVO errorVO = new TalkVO(); + errorVO.setContent(null); + errorVO.setFinishReason("Error: " + throwable.getMessage()); + emitter.send(errorVO); + } catch (Exception sendException) { + sendException.printStackTrace(); + } + emitter.completeWithError(throwable); + }, + () -> { + try { + TalkVO finishVO = new TalkVO(); + finishVO.setContent(null); + finishVO.setFinishReason("completed"); + emitter.send(finishVO); + } catch (Exception e) { + e.printStackTrace(); + } + emitter.complete(); + }); emitter.onTimeout(emitter::complete); return emitter; @@ -258,7 +285,9 @@ public ChatThreadVO updateChatThread(ChatThreadDTO chatThreadDTO) { chatThreadPO.setName(chatThreadDTO.getName()); chatThreadDao.partialUpdateById(chatThreadPO); - return ChatThreadConverter.INSTANCE.fromPO2VO(chatThreadPO); + AuthPlatformPO authPlatformPO = authPlatformDao.findById(chatThreadPO.getAuthId()); + return ChatThreadConverter.INSTANCE.fromPO2VO( + chatThreadPO, authPlatformPO, platformDao.findById(authPlatformPO.getPlatformId())); } @Override @@ -271,6 +300,8 @@ public ChatThreadVO getChatThread(Long threadId) { if (!chatThreadPO.getUserId().equals(userId)) { throw new ApiException(ApiExceptionEnum.PERMISSION_DENIED); } - return ChatThreadConverter.INSTANCE.fromPO2VO(chatThreadPO); + AuthPlatformPO authPlatformPO = authPlatformDao.findById(chatThreadPO.getAuthId()); + return ChatThreadConverter.INSTANCE.fromPO2VO( + chatThreadPO, authPlatformPO, platformDao.findById(authPlatformPO.getPlatformId())); } } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/LLMConfigServiceImpl.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/LLMConfigServiceImpl.java index f5cf12c95..e26dfc711 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/LLMConfigServiceImpl.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/LLMConfigServiceImpl.java @@ -276,6 +276,21 @@ public AuthPlatformVO updateAuthorizedPlatform(AuthPlatformDTO authPlatformDTO) throw new ApiException(ApiExceptionEnum.PLATFORM_NOT_AUTHORIZED); } + String newModel = authPlatformDTO.getModel(); + if (newModel != null) { + if (AuthPlatformStatus.isActive(authPlatformPO.getStatus())) { + throw new ApiException(ApiExceptionEnum.PLATFORM_IS_ACTIVE); + } + + authPlatformPO.setModel(newModel); + + if (authPlatformDTO.getTestPassed()) { + authPlatformPO.setStatus(AuthPlatformStatus.AVAILABLE.getCode()); + } else { + authPlatformPO.setStatus(AuthPlatformStatus.UNAVAILABLE.getCode()); + } + } + authPlatformPO.setName(authPlatformDTO.getName()); authPlatformPO.setDesc(authPlatformDTO.getDesc()); @@ -328,4 +343,13 @@ public AuthPlatformVO getAuthorizedPlatform(Long authId) { return AuthPlatformConverter.INSTANCE.fromPO2VO( authPlatformPO, platformDao.findById(authPlatformPO.getPlatformId())); } + + @Override + public PlatformVO getPlatform(Long id) { + PlatformPO platformPO = platformDao.findById(id); + if (platformPO == null) { + throw new ApiException(ApiExceptionEnum.PLATFORM_NOT_FOUND); + } + return PlatformConverter.INSTANCE.fromPO2VO(platformPO); + } } diff --git a/bigtop-manager-server/src/main/resources/ddl/MySQL-DDL-CREATE.sql b/bigtop-manager-server/src/main/resources/ddl/MySQL-DDL-CREATE.sql index 761abea4d..79ea4e516 100644 --- a/bigtop-manager-server/src/main/resources/ddl/MySQL-DDL-CREATE.sql +++ b/bigtop-manager-server/src/main/resources/ddl/MySQL-DDL-CREATE.sql @@ -270,6 +270,7 @@ CREATE TABLE `llm_platform` `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NOT NULL, `credential` TEXT DEFAULT NULL, + `desc` TEXT DEFAULT NULL, `support_models` VARCHAR(255) DEFAULT NULL, `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP, `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, @@ -346,3 +347,15 @@ VALUES ('{"apiKey": "API Key"}', 'OpenAI', 'gpt-3.5-turbo,gpt-4,gpt-4o,gpt-3.5-turbo-16k,gpt-4-turbo-preview,gpt-4-32k,gpt-4o-mini'), ('{"apiKey": "API Key"}', 'DashScope', 'qwen-1.8b-chat,qwen-max,qwen-plus,qwen-turbo'), ('{"apiKey": "API Key", "secretKey": "Secret Key"}', 'QianFan','Yi-34B-Chat,ERNIE-4.0-8K,ERNIE-3.5-128K,ERNIE-Speed-8K,Llama-2-7B-Chat,Fuyu-8B'); + +UPDATE `llm_platform` +SET `desc` = 'Get your API Key in https://platform.openai.com/api-keys' +WHERE `name` = 'OpenAI'; + +UPDATE `llm_platform` +SET `desc` = 'Get your API Key in https://bailian.console.aliyun.com/?apiKey=1#/api-key' +WHERE `name` = 'DashScope'; + +UPDATE `llm_platform` +SET `desc` = 'Get API Key and Secret Key in https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application/v1' +WHERE `name` = 'QianFan'; \ No newline at end of file diff --git a/bigtop-manager-server/src/main/resources/ddl/PostgreSQL-DDL-CREATE.sql b/bigtop-manager-server/src/main/resources/ddl/PostgreSQL-DDL-CREATE.sql index 060893b90..e2b5ca39e 100644 --- a/bigtop-manager-server/src/main/resources/ddl/PostgreSQL-DDL-CREATE.sql +++ b/bigtop-manager-server/src/main/resources/ddl/PostgreSQL-DDL-CREATE.sql @@ -280,6 +280,7 @@ CREATE TABLE llm_platform id BIGINT CHECK (id > 0) NOT NULL GENERATED ALWAYS AS IDENTITY, name VARCHAR(255) NOT NULL, credential TEXT DEFAULT NULL, + "desc" TEXT DEFAULT NULL, support_models VARCHAR(255) DEFAULT NULL, create_time TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP, update_time TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP /* ON UPDATE CURRENT_TIMESTAMP */, @@ -357,3 +358,15 @@ VALUES ('{"apiKey": "API Key"}','OpenAI','gpt-3.5-turbo,gpt-4,gpt-4o,gpt-3.5-turbo-16k,gpt-4-turbo-preview,gpt-4-32k,gpt-4o-mini'), ('{"apiKey": "API Key"}','DashScope','qwen-1.8b-chat,qwen-max,qwen-plus,qwen-turbo'), ('{"apiKey": "API Key", "secretKey": "Secret Key"}','QianFan','Yi-34B-Chat,ERNIE-4.0-8K,ERNIE-3.5-128K,ERNIE-Speed-8K,Llama-2-7B-Chat,Fuyu-8B'); + +UPDATE llm_platform +SET "desc" = 'Get your API Key in https://platform.openai.com/api-keys' +WHERE "name" = 'OpenAI'; + +UPDATE llm_platform +SET "desc" = 'Get your API Key in https://bailian.console.aliyun.com/?apiKey=1#/api-key' +WHERE "name" = 'DashScope'; + +UPDATE llm_platform +SET "desc" = 'Get API Key and Secret Key in https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application/v1' +WHERE "name" = 'QianFan';