Skip to content

Commit

Permalink
BIGTOP-4293: Adjust some LLM APIs (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhpqaq authored Dec 4, 2024
1 parent 7c1b852 commit 42a1721
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public ResponseEntity<List<PlatformVO>> platforms() {
return ResponseEntity.success(llmConfigService.platforms());
}

@Operation(summary = "get platform", description = "Get platform")
@GetMapping("/platforms/{id}")
public ResponseEntity<PlatformVO> 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<List<PlatformAuthCredentialVO>> platformsAuthCredential(@PathVariable Long platformId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public class ChatThreadVO {

private String name;

private String platformName;

private String model;

private String createTime;

private String updateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ public interface LLMConfigService {
boolean deactivateAuthorizedPlatform(Long authId);

AuthPlatformVO getAuthorizedPlatform(Long authId);

PlatformVO getPlatform(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -164,6 +165,8 @@ public List<ChatThreadVO> 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<ChatThreadPO> chatThreadPOS = chatThreadDao.findAllByAuthIdAndUserId(authId, userId);
Expand All @@ -172,7 +175,8 @@ public List<ChatThreadVO> getAllChatThreads() {
if (chatThreadPO.getIsDeleted()) {
continue;
}
ChatThreadVO chatThreadVO = ChatThreadConverter.INSTANCE.fromPO2VO(chatThreadPO);
ChatThreadVO chatThreadVO =
ChatThreadConverter.INSTANCE.fromPO2VO(chatThreadPO, authPlatformPO, platformPO);
chatThreads.add(chatThreadVO);
}
return chatThreads;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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);
}
}
13 changes: 13 additions & 0 deletions bigtop-manager-server/src/main/resources/ddl/MySQL-DDL-CREATE.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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 */,
Expand Down Expand Up @@ -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';

0 comments on commit 42a1721

Please sign in to comment.